| 30 |
ashish |
1 |
#
|
|
|
2 |
# Licensed to the Apache Software Foundation (ASF) under one
|
|
|
3 |
# or more contributor license agreements. See the NOTICE file
|
|
|
4 |
# distributed with this work for additional information
|
|
|
5 |
# regarding copyright ownership. The ASF licenses this file
|
|
|
6 |
# to you under the Apache License, Version 2.0 (the
|
|
|
7 |
# "License"); you may not use this file except in compliance
|
|
|
8 |
# with the License. You may obtain a copy of the License at
|
|
|
9 |
#
|
|
|
10 |
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
11 |
#
|
|
|
12 |
# Unless required by applicable law or agreed to in writing,
|
|
|
13 |
# software distributed under the License is distributed on an
|
|
|
14 |
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
15 |
# KIND, either express or implied. See the License for the
|
|
|
16 |
# specific language governing permissions and limitations
|
|
|
17 |
# under the License.
|
|
|
18 |
#
|
|
|
19 |
|
|
|
20 |
require File.join(File.dirname(__FILE__), '../test_helper')
|
|
|
21 |
|
|
|
22 |
require 'thrift'
|
|
|
23 |
require 'ThriftTest'
|
|
|
24 |
|
|
|
25 |
class TestHandler
|
|
|
26 |
[:testString, :testByte, :testI32, :testI64, :testDouble,
|
|
|
27 |
:testStruct, :testMap, :testSet, :testList, :testNest,
|
|
|
28 |
:testEnum, :testTypedef].each do |meth|
|
|
|
29 |
|
|
|
30 |
define_method(meth) do |thing|
|
|
|
31 |
thing
|
|
|
32 |
end
|
|
|
33 |
|
|
|
34 |
end
|
|
|
35 |
|
|
|
36 |
def testInsanity(thing)
|
|
|
37 |
num, uid = thing.userMap.find { true }
|
|
|
38 |
return {uid => {num => thing}}
|
|
|
39 |
end
|
|
|
40 |
|
|
|
41 |
def testMapMap(thing)
|
|
|
42 |
return {thing => {thing => thing}}
|
|
|
43 |
end
|
|
|
44 |
|
|
|
45 |
def testEnum(thing)
|
|
|
46 |
return thing
|
|
|
47 |
end
|
|
|
48 |
|
|
|
49 |
def testTypedef(thing)
|
|
|
50 |
return thing
|
|
|
51 |
end
|
|
|
52 |
|
|
|
53 |
def testException(thing)
|
|
|
54 |
raise Thrift::Test::Xception, :message => 'error'
|
|
|
55 |
end
|
|
|
56 |
|
|
|
57 |
end
|
|
|
58 |
class TestThrift < Test::Unit::TestCase
|
|
|
59 |
|
|
|
60 |
@@INIT = nil
|
|
|
61 |
|
|
|
62 |
def setup
|
|
|
63 |
if @@INIT.nil?
|
|
|
64 |
# Initialize the server
|
|
|
65 |
@handler = TestHandler.new()
|
|
|
66 |
@processor = Thrift::Test::ThriftTest::Processor.new(@handler)
|
|
|
67 |
@transport = Thrift::ServerSocket.new(9090)
|
|
|
68 |
@server = Thrift::ThreadedServer.new(@processor, @transport)
|
|
|
69 |
|
|
|
70 |
@thread = Thread.new { @server.serve }
|
|
|
71 |
|
|
|
72 |
# And the Client
|
|
|
73 |
@socket = Thrift::Socket.new('localhost', 9090)
|
|
|
74 |
@protocol = Thrift::BinaryProtocol.new(@socket)
|
|
|
75 |
@client = Thrift::Test::ThriftTest::Client.new(@protocol)
|
|
|
76 |
@socket.open
|
|
|
77 |
end
|
|
|
78 |
end
|
|
|
79 |
|
|
|
80 |
def test_string
|
|
|
81 |
assert_equal(@client.testString('string'), 'string')
|
|
|
82 |
end
|
|
|
83 |
|
|
|
84 |
def test_byte
|
|
|
85 |
val = 8
|
|
|
86 |
assert_equal(@client.testByte(val), val)
|
|
|
87 |
assert_equal(@client.testByte(-val), -val)
|
|
|
88 |
end
|
|
|
89 |
|
|
|
90 |
def test_i32
|
|
|
91 |
val = 32
|
|
|
92 |
assert_equal(@client.testI32(val), val)
|
|
|
93 |
assert_equal(@client.testI32(-val), -val)
|
|
|
94 |
end
|
|
|
95 |
|
|
|
96 |
def test_i64
|
|
|
97 |
val = 64
|
|
|
98 |
assert_equal(@client.testI64(val), val)
|
|
|
99 |
assert_equal(@client.testI64(-val), -val)
|
|
|
100 |
end
|
|
|
101 |
|
|
|
102 |
def test_double
|
|
|
103 |
val = 3.14
|
|
|
104 |
assert_equal(@client.testDouble(val), val)
|
|
|
105 |
assert_equal(@client.testDouble(-val), -val)
|
|
|
106 |
assert_kind_of(Float, @client.testDouble(val))
|
|
|
107 |
end
|
|
|
108 |
|
|
|
109 |
def test_map
|
|
|
110 |
val = {1 => 1, 2 => 2, 3 => 3}
|
|
|
111 |
assert_equal(@client.testMap(val), val)
|
|
|
112 |
assert_kind_of(Hash, @client.testMap(val))
|
|
|
113 |
end
|
|
|
114 |
|
|
|
115 |
def test_list
|
|
|
116 |
val = [1,2,3,4,5]
|
|
|
117 |
assert_equal(@client.testList(val), val)
|
|
|
118 |
assert_kind_of(Array, @client.testList(val))
|
|
|
119 |
end
|
|
|
120 |
|
|
|
121 |
def test_enum
|
|
|
122 |
val = Thrift::Test::Numberz::SIX
|
|
|
123 |
ret = @client.testEnum(val)
|
|
|
124 |
|
|
|
125 |
assert_equal(ret, 6)
|
|
|
126 |
assert_kind_of(Fixnum, ret)
|
|
|
127 |
end
|
|
|
128 |
|
|
|
129 |
def test_typedef
|
|
|
130 |
#UserId testTypedef(1: UserId thing),
|
|
|
131 |
true
|
|
|
132 |
end
|
|
|
133 |
|
|
|
134 |
def test_set
|
|
|
135 |
val = Set.new([1, 2, 3])
|
|
|
136 |
assert_equal(val, @client.testSet(val))
|
|
|
137 |
assert_kind_of(Set, @client.testSet(val))
|
|
|
138 |
end
|
|
|
139 |
|
|
|
140 |
def get_struct
|
|
|
141 |
Thrift::Test::Xtruct.new({'string_thing' => 'hi!', 'i32_thing' => 4 })
|
|
|
142 |
end
|
|
|
143 |
|
|
|
144 |
def test_struct
|
|
|
145 |
ret = @client.testStruct(get_struct)
|
|
|
146 |
|
|
|
147 |
assert_nil(ret.byte_thing, nil)
|
|
|
148 |
assert_nil(ret.i64_thing, nil)
|
|
|
149 |
assert_equal(ret.string_thing, 'hi!')
|
|
|
150 |
assert_equal(ret.i32_thing, 4)
|
|
|
151 |
assert_kind_of(Thrift::Test::Xtruct, ret)
|
|
|
152 |
end
|
|
|
153 |
|
|
|
154 |
def test_nest
|
|
|
155 |
struct2 = Thrift::Test::Xtruct2.new({'struct_thing' => get_struct, 'i32_thing' => 10})
|
|
|
156 |
|
|
|
157 |
ret = @client.testNest(struct2)
|
|
|
158 |
|
|
|
159 |
assert_nil(ret.struct_thing.byte_thing, nil)
|
|
|
160 |
assert_nil(ret.struct_thing.i64_thing, nil)
|
|
|
161 |
assert_equal(ret.struct_thing.string_thing, 'hi!')
|
|
|
162 |
assert_equal(ret.struct_thing.i32_thing, 4)
|
|
|
163 |
assert_equal(ret.i32_thing, 10)
|
|
|
164 |
|
|
|
165 |
assert_kind_of(Thrift::Test::Xtruct, ret.struct_thing)
|
|
|
166 |
assert_kind_of(Thrift::Test::Xtruct2, ret)
|
|
|
167 |
end
|
|
|
168 |
|
|
|
169 |
def test_insane
|
|
|
170 |
insane = Thrift::Test::Insanity.new({
|
|
|
171 |
'userMap' => { Thrift::Test::Numberz::ONE => 44 },
|
|
|
172 |
'xtructs' => [get_struct,
|
|
|
173 |
Thrift::Test::Xtruct.new({
|
|
|
174 |
'string_thing' => 'hi again',
|
|
|
175 |
'i32_thing' => 12
|
|
|
176 |
})
|
|
|
177 |
]
|
|
|
178 |
})
|
|
|
179 |
|
|
|
180 |
ret = @client.testInsanity(insane)
|
|
|
181 |
|
|
|
182 |
assert_not_nil(ret[44])
|
|
|
183 |
assert_not_nil(ret[44][1])
|
|
|
184 |
|
|
|
185 |
struct = ret[44][1]
|
|
|
186 |
|
|
|
187 |
assert_equal(struct.userMap[Thrift::Test::Numberz::ONE], 44)
|
|
|
188 |
assert_equal(struct.xtructs[1].string_thing, 'hi again')
|
|
|
189 |
assert_equal(struct.xtructs[1].i32_thing, 12)
|
|
|
190 |
|
|
|
191 |
assert_kind_of(Hash, struct.userMap)
|
|
|
192 |
assert_kind_of(Array, struct.xtructs)
|
|
|
193 |
assert_kind_of(Thrift::Test::Insanity, struct)
|
|
|
194 |
end
|
|
|
195 |
|
|
|
196 |
def test_map_map
|
|
|
197 |
ret = @client.testMapMap(4)
|
|
|
198 |
assert_kind_of(Hash, ret)
|
|
|
199 |
assert_equal(ret, { 4 => { 4 => 4}})
|
|
|
200 |
end
|
|
|
201 |
|
|
|
202 |
def test_exception
|
|
|
203 |
assert_raise Thrift::Test::Xception do
|
|
|
204 |
@client.testException('foo')
|
|
|
205 |
end
|
|
|
206 |
end
|
|
|
207 |
|
|
|
208 |
def teardown
|
|
|
209 |
end
|
|
|
210 |
|
|
|
211 |
end
|