blob: 509f452fa58f70d4a5a86bd730a958fef25a9145 [file] [log] [blame]
Kevin Clark28580f42008-06-18 00:49:17 +00001require File.join(File.dirname(__FILE__), '../test_helper')
Mark Slee89f57162008-01-10 00:53:08 +00002
3require 'thrift/transport/tsocket'
4require 'thrift/protocol/tbinaryprotocol'
5require 'thrift/server/tserver'
6require 'ThriftTest'
Mark Slee89f57162008-01-10 00:53:08 +00007
Kevin Clark28580f42008-06-18 00:49:17 +00008class TestHandler
9 [:testString, :testByte, :testI32, :testI64, :testDouble,
10 :testStruct, :testMap, :testSet, :testList, :testNest,
11 :testEnum, :testTypedef].each do |meth|
Mark Slee89f57162008-01-10 00:53:08 +000012
Kevin Clark28580f42008-06-18 00:49:17 +000013 define_method(meth) do |thing|
14 thing
15 end
16
17 end
18
19 def testInsanity(thing)
20 num, uid = thing.userMap.find { true }
21 return {uid => {num => thing}}
22 end
23
24 def testMapMap(thing)
25 return {thing => {thing => thing}}
26 end
27
28 def testEnum(thing)
29 return thing
30 end
31
32 def testTypedef(thing)
33 return thing
34 end
35
36 def testException(thing)
37 raise Thrift::Test::Xception, 'error'
38 end
39
40end
Mark Slee89f57162008-01-10 00:53:08 +000041class TestThrift < Test::Unit::TestCase
42
43 @@INIT = nil
44
45 def setup
46 if @@INIT.nil?
47 # Initialize the server
48 @handler = TestHandler.new()
49 @processor = Thrift::Test::ThriftTest::Processor.new(@handler)
50 @transport = TServerSocket.new(9090)
51 @server = TThreadedServer.new(@processor, @transport)
52
53 @thread = Thread.new { @server.serve }
54
55 # And the Client
56 @socket = TSocket.new('localhost', 9090)
57 @protocol = TBinaryProtocol.new(@socket)
58 @client = Thrift::Test::ThriftTest::Client.new(@protocol)
59 @socket.open
60 end
61 end
62
63 def test_string
64 assert_equal(@client.testString('string'), 'string')
65 end
66
67 def test_byte
68 val = 8
69 assert_equal(@client.testByte(val), val)
70 assert_equal(@client.testByte(-val), -val)
71 end
72
73 def test_i32
74 val = 32
75 assert_equal(@client.testI32(val), val)
76 assert_equal(@client.testI32(-val), -val)
77 end
78
79 def test_i64
80 val = 64
81 assert_equal(@client.testI64(val), val)
82 assert_equal(@client.testI64(-val), -val)
83 end
84
85 def test_double
86 val = 3.14
87 assert_equal(@client.testDouble(val), val)
88 assert_equal(@client.testDouble(-val), -val)
89 assert_kind_of(Float, @client.testDouble(val))
90 end
91
92 def test_map
93 val = {1 => 1, 2 => 2, 3 => 3}
94 assert_equal(@client.testMap(val), val)
95 assert_kind_of(Hash, @client.testMap(val))
96 end
97
98 def test_list
99 val = [1,2,3,4,5]
100 assert_equal(@client.testList(val), val)
101 assert_kind_of(Array, @client.testList(val))
102 end
103
104 def test_enum
105 val = Thrift::Test::Numberz::SIX
106 ret = @client.testEnum(val)
107
108 assert_equal(ret, 6)
109 assert_kind_of(Fixnum, ret)
110 end
111
112 def test_typedef
113 #UserId testTypedef(1: UserId thing),
114 true
115 end
116
117 def test_set
118 val = {1 => true, 2 => true, 3 => true}
119 assert_equal(@client.testSet(val), val)
120 assert_kind_of(Hash, @client.testSet(val))
121 end
122
123 def get_struct
124 Thrift::Test::Xtruct.new({'string_thing' => 'hi!', 'i32_thing' => 4 })
125 end
126
127 def test_struct
128 ret = @client.testStruct(get_struct)
129
130 assert_nil(ret.byte_thing, nil)
131 assert_nil(ret.i64_thing, nil)
132 assert_equal(ret.string_thing, 'hi!')
133 assert_equal(ret.i32_thing, 4)
134 assert_kind_of(Thrift::Test::Xtruct, ret)
135 end
136
137 def test_nest
138 struct2 = Thrift::Test::Xtruct2.new({'struct_thing' => get_struct, 'i32_thing' => 10})
139
140 ret = @client.testNest(struct2)
141
142 assert_nil(ret.struct_thing.byte_thing, nil)
143 assert_nil(ret.struct_thing.i64_thing, nil)
144 assert_equal(ret.struct_thing.string_thing, 'hi!')
145 assert_equal(ret.struct_thing.i32_thing, 4)
146 assert_equal(ret.i32_thing, 10)
147
148 assert_kind_of(Thrift::Test::Xtruct, ret.struct_thing)
149 assert_kind_of(Thrift::Test::Xtruct2, ret)
150 end
151
152 def test_insane
153 insane = Thrift::Test::Insanity.new({
154 'userMap' => { Thrift::Test::Numberz::ONE => 44 },
David Reiss0c90f6f2008-02-06 22:18:40 +0000155 'xtructs' => [get_struct,
Mark Slee89f57162008-01-10 00:53:08 +0000156 Thrift::Test::Xtruct.new({
157 'string_thing' => 'hi again',
158 'i32_thing' => 12
159 })
160 ]
161 })
162
163 ret = @client.testInsanity(insane)
164
165 assert_not_nil(ret[44])
166 assert_not_nil(ret[44][1])
167
168 struct = ret[44][1]
169
170 assert_equal(struct.userMap[Thrift::Test::Numberz::ONE], 44)
171 assert_equal(struct.xtructs[1].string_thing, 'hi again')
172 assert_equal(struct.xtructs[1].i32_thing, 12)
173
174 assert_kind_of(Hash, struct.userMap)
175 assert_kind_of(Array, struct.xtructs)
176 assert_kind_of(Thrift::Test::Insanity, struct)
177 end
178
179 def test_map_map
180 ret = @client.testMapMap(4)
181 assert_kind_of(Hash, ret)
182 assert_equal(ret, { 4 => { 4 => 4}})
183 end
184
185 def test_exception
186 assert_raise Thrift::Test::Xception do
187 @client.testException('foo')
188 end
189 end
190
191 def teardown
192 end
193
194end