blob: b31a024122658c539c2c9aac7b081e2049c74e66 [file] [log] [blame]
Roger Meier32f39822014-06-18 22:43:17 +02001#!/usr/bin/env ruby
2
David Reissea2cba82009-03-30 21:35:00 +00003#
4# Licensed to the Apache Software Foundation (ASF) under one
5# or more contributor license agreements. See the NOTICE file
6# distributed with this work for additional information
7# regarding copyright ownership. The ASF licenses this file
8# to you under the Apache License, Version 2.0 (the
9# "License"); you may not use this file except in compliance
10# with the License. You may obtain a copy of the License at
11#
12# http://www.apache.org/licenses/LICENSE-2.0
13#
14# Unless required by applicable law or agreed to in writing,
15# software distributed under the License is distributed on an
16# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17# KIND, either express or implied. See the License for the
18# specific language governing permissions and limitations
19# under the License.
20#
21
Roger Meierc95d5df2014-01-19 21:53:02 +010022$:.push File.dirname(__FILE__) + '/..'
Kevin Clark4bd89162008-07-08 00:47:49 +000023
Roger Meierc95d5df2014-01-19 21:53:02 +010024require 'test_helper'
Kevin Clark4bd89162008-07-08 00:47:49 +000025require 'thrift'
Roger Meierc95d5df2014-01-19 21:53:02 +010026require 'thrift_test'
Kevin Clark4bd89162008-07-08 00:47:49 +000027
Roger Meiera3570ac2014-06-10 22:16:14 +020028$protocolType = "binary"
29$host = "localhost"
30$port = 9090
31$transport = "buffered"
32ARGV.each do|a|
33 if a == "--help"
34 puts "Allowed options:"
35 puts "\t -h [ --help ] \t produce help message"
36 puts "\t--host arg (=localhost) \t Host to connect"
37 puts "\t--port arg (=9090) \t Port number to listen"
38 puts "\t--protocol arg (=binary) \t protocol: binary, accel"
39 puts "\t--transport arg (=buffered) transport: buffered, framed, http"
40 exit
41 elsif a.start_with?("--host")
42 $host = a.split("=")[1]
43 elsif a.start_with?("--protocol")
44 $protocolType = a.split("=")[1]
45 elsif a.start_with?("--transport")
46 $transport = a.split("=")[1]
47 elsif a.start_with?("--port")
48 $port = a.split("=")[1].to_i
49 end
50end
51ARGV=[]
52
Kevin Clark4bd89162008-07-08 00:47:49 +000053class SimpleClientTest < Test::Unit::TestCase
Roger Meiera3570ac2014-06-10 22:16:14 +020054 def setup
Kevin Clark4bd89162008-07-08 00:47:49 +000055 unless @socket
Roger Meiera3570ac2014-06-10 22:16:14 +020056 @socket = Thrift::Socket.new($host, $port)
Roger Meiera3570ac2014-06-10 22:16:14 +020057 if $transport == "buffered"
58 transportFactory = Thrift::BufferedTransport.new(@socket)
Roger Meiera3570ac2014-06-10 22:16:14 +020059 elsif $transport == "framed"
60 transportFactory = Thrift::FramedTransport.new(@socket)
61 else
62 raise 'Unknown transport type'
63 end
64
65 if $protocolType == "binary"
66 @protocol = Thrift::BinaryProtocol.new(transportFactory)
Roger Meiera3570ac2014-06-10 22:16:14 +020067 elsif $protocolType == "compact"
68 @protocol = Thrift::CompactProtocol.new(transportFactory)
69 elsif $protocolType == "json"
70 @protocol = Thrift::JsonProtocol.new(transportFactory)
71 elsif $protocolType == "accel"
72 @protocol = Thrift::BinaryProtocolAccelerated.new(transportFactory)
73 else
74 raise 'Unknown protocol type'
75 end
Kevin Clark4bd89162008-07-08 00:47:49 +000076 @client = Thrift::Test::ThriftTest::Client.new(@protocol)
77 @socket.open
78 end
79 end
80
Roger Meiera3570ac2014-06-10 22:16:14 +020081 def test_void
Nobuaki Sukegawa68238292015-09-21 23:28:22 +090082 p 'test_void'
Roger Meiera3570ac2014-06-10 22:16:14 +020083 @client.testVoid()
84 end
85
Kevin Clark4bd89162008-07-08 00:47:49 +000086 def test_string
Nobuaki Sukegawa68238292015-09-21 23:28:22 +090087 p 'test_string'
Kevin Clark4bd89162008-07-08 00:47:49 +000088 assert_equal(@client.testString('string'), 'string')
89 end
90
Nobuaki Sukegawa68238292015-09-21 23:28:22 +090091 def test_bool
92 p 'test_bool'
93 assert_equal(@client.testBool(true), true)
94 assert_equal(@client.testBool(false), false)
95 end
96
Kevin Clark4bd89162008-07-08 00:47:49 +000097 def test_byte
Nobuaki Sukegawa68238292015-09-21 23:28:22 +090098 p 'test_byte'
99 val = 120
Kevin Clark4bd89162008-07-08 00:47:49 +0000100 assert_equal(@client.testByte(val), val)
101 assert_equal(@client.testByte(-val), -val)
102 end
103
104 def test_i32
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900105 p 'test_i32'
106 val = 2000000032
Kevin Clark4bd89162008-07-08 00:47:49 +0000107 assert_equal(@client.testI32(val), val)
108 assert_equal(@client.testI32(-val), -val)
109 end
110
111 def test_i64
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900112 p 'test_i64'
113 val = 9000000000000000064
Kevin Clark4bd89162008-07-08 00:47:49 +0000114 assert_equal(@client.testI64(val), val)
115 assert_equal(@client.testI64(-val), -val)
116 end
117
118 def test_double
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900119 p 'test_double'
Kevin Clark4bd89162008-07-08 00:47:49 +0000120 val = 3.14
121 assert_equal(@client.testDouble(val), val)
122 assert_equal(@client.testDouble(-val), -val)
123 assert_kind_of(Float, @client.testDouble(val))
124 end
125
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900126 def test_binary
127 p 'test_binary'
128 val = [42, 0, 142, 242]
129 ret = @client.testBinary(val.pack('C*'))
130 assert_equal(val, ret.bytes.to_a)
131 end
Jens Geyer8bcfdd92014-12-14 03:14:26 +0100132
Kevin Clark4bd89162008-07-08 00:47:49 +0000133 def test_map
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900134 p 'test_map'
Kevin Clark4bd89162008-07-08 00:47:49 +0000135 val = {1 => 1, 2 => 2, 3 => 3}
136 assert_equal(@client.testMap(val), val)
137 assert_kind_of(Hash, @client.testMap(val))
138 end
139
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900140 def test_string_map
141 p 'test_string_map'
142 val = {'a' => '2', 'b' => 'blah', 'some' => 'thing'}
143 ret = @client.testStringMap(val)
144 assert_equal(val, ret)
145 assert_kind_of(Hash, ret)
146 end
147
Kevin Clark4bd89162008-07-08 00:47:49 +0000148 def test_list
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900149 p 'test_list'
Kevin Clark4bd89162008-07-08 00:47:49 +0000150 val = [1,2,3,4,5]
151 assert_equal(@client.testList(val), val)
152 assert_kind_of(Array, @client.testList(val))
153 end
154
155 def test_enum
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900156 p 'test_enum'
Kevin Clark4bd89162008-07-08 00:47:49 +0000157 val = Thrift::Test::Numberz::SIX
158 ret = @client.testEnum(val)
159
160 assert_equal(ret, 6)
161 assert_kind_of(Fixnum, ret)
162 end
163
164 def test_typedef
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900165 p 'test_typedef'
Kevin Clark4bd89162008-07-08 00:47:49 +0000166 #UserId testTypedef(1: UserId thing),
Roger Meiera3570ac2014-06-10 22:16:14 +0200167 assert_equal(@client.testTypedef(309858235082523), 309858235082523)
168 assert_kind_of(Fixnum, @client.testTypedef(309858235082523))
Kevin Clark4bd89162008-07-08 00:47:49 +0000169 true
170 end
171
172 def test_set
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900173 p 'test_set'
Kevin Clark4bd89162008-07-08 00:47:49 +0000174 val = Set.new([1,2,3])
175 assert_equal(@client.testSet(val), val)
176 assert_kind_of(Set, @client.testSet(val))
177 end
178
179 def get_struct
180 Thrift::Test::Xtruct.new({'string_thing' => 'hi!', 'i32_thing' => 4 })
181 end
182
183 def test_struct
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900184 p 'test_struct'
Kevin Clark4bd89162008-07-08 00:47:49 +0000185 ret = @client.testStruct(get_struct)
186
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900187 # TODO: not sure what unspecified "default" requiredness values should be
188 assert(ret.byte_thing == nil || ret.byte_thing == 0)
189 assert(ret.i64_thing == nil || ret.i64_thing == 0)
190
Kevin Clark4bd89162008-07-08 00:47:49 +0000191 assert_equal(ret.string_thing, 'hi!')
192 assert_equal(ret.i32_thing, 4)
193 assert_kind_of(Thrift::Test::Xtruct, ret)
194 end
195
196 def test_nest
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900197 p 'test_nest'
Kevin Clark4bd89162008-07-08 00:47:49 +0000198 struct2 = Thrift::Test::Xtruct2.new({'struct_thing' => get_struct, 'i32_thing' => 10})
199
200 ret = @client.testNest(struct2)
201
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900202 # TODO: not sure what unspecified "default" requiredness values should be
203 assert(ret.struct_thing.byte_thing == nil || ret.struct_thing.byte_thing == 0)
204 assert(ret.struct_thing.i64_thing == nil || ret.struct_thing.i64_thing == 0)
205
Kevin Clark4bd89162008-07-08 00:47:49 +0000206 assert_equal(ret.struct_thing.string_thing, 'hi!')
207 assert_equal(ret.struct_thing.i32_thing, 4)
208 assert_equal(ret.i32_thing, 10)
209
210 assert_kind_of(Thrift::Test::Xtruct, ret.struct_thing)
211 assert_kind_of(Thrift::Test::Xtruct2, ret)
212 end
213
Roger Meiera3570ac2014-06-10 22:16:14 +0200214 def test_insanity
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900215 p 'test_insanity'
Kevin Clark4bd89162008-07-08 00:47:49 +0000216 insane = Thrift::Test::Insanity.new({
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900217 'userMap' => {
218 Thrift::Test::Numberz::FIVE => 5,
219 Thrift::Test::Numberz::EIGHT => 8,
220 },
221 'xtructs' => [
Kevin Clark4bd89162008-07-08 00:47:49 +0000222 Thrift::Test::Xtruct.new({
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900223 'string_thing' => 'Goodbye4',
224 'byte_thing' => 4,
225 'i32_thing' => 4,
226 'i64_thing' => 4,
227 }),
228 Thrift::Test::Xtruct.new({
229 'string_thing' => 'Hello2',
230 'byte_thing' => 2,
231 'i32_thing' => 2,
232 'i64_thing' => 2,
Kevin Clark4bd89162008-07-08 00:47:49 +0000233 })
234 ]
235 })
236
237 ret = @client.testInsanity(insane)
238
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900239 assert_equal(insane, ret[1][2])
240 assert_equal(insane, ret[1][3])
Kevin Clark4bd89162008-07-08 00:47:49 +0000241
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900242 assert(ret[2][6].userMap == nil || ret[2][6].userMap.length == 0)
243 assert(ret[2][6].xtructs == nil || ret[2][6].xtructs.length == 0)
Kevin Clark4bd89162008-07-08 00:47:49 +0000244 end
245
246 def test_map_map
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900247 p 'test_map_map'
Kevin Clark4bd89162008-07-08 00:47:49 +0000248 ret = @client.testMapMap(4)
249 assert_kind_of(Hash, ret)
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900250 expected = {
251 -4 => {
252 -4 => -4,
253 -3 => -3,
254 -2 => -2,
255 -1 => -1,
256 },
257 4 => {
258 4 => 4,
259 3 => 3,
260 2 => 2,
261 1 => 1,
262 }
263 }
264 assert_equal(expected, ret)
265 end
266
267 def test_multi
268 p 'test_multi'
269 ret = @client.testMulti(42, 4242, 424242, {1 => 'blah', 2 => 'thing'}, Thrift::Test::Numberz::EIGHT, 24)
270 expected = Thrift::Test::Xtruct.new({
271 :string_thing => 'Hello2',
272 :byte_thing => 42,
273 :i32_thing => 4242,
274 :i64_thing => 424242
275 })
276 assert_equal(expected, ret)
Kevin Clark4bd89162008-07-08 00:47:49 +0000277 end
278
279 def test_exception
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900280 p 'test_exception'
Kevin Clark4bd89162008-07-08 00:47:49 +0000281 assert_raise Thrift::Test::Xception do
Roger Meiera3570ac2014-06-10 22:16:14 +0200282 @client.testException('Xception')
Kevin Clark4bd89162008-07-08 00:47:49 +0000283 end
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900284 begin
285 @client.testException('TException')
286 rescue => e
287 assert e.class.ancestors.include?(Thrift::Exception)
288 end
289 assert_nothing_raised do
290 @client.testException('test')
291 end
Kevin Clark4bd89162008-07-08 00:47:49 +0000292 end
Roger Meiera3570ac2014-06-10 22:16:14 +0200293
294 def test_multi_exception
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900295 p 'test_multi_exception'
Roger Meiera3570ac2014-06-10 22:16:14 +0200296 assert_raise Thrift::Test::Xception do
297 @client.testMultiException("Xception", "test 1")
298 end
299 assert_raise Thrift::Test::Xception2 do
300 @client.testMultiException("Xception2", "test 2")
301 end
302 assert_equal( @client.testMultiException("Success", "test 3").string_thing, "test 3")
303 end
304
305 def test_oneway
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900306 p 'test_oneway'
Roger Meiera3570ac2014-06-10 22:16:14 +0200307 time1 = Time.now.to_f
308 @client.testOneway(3)
309 time2 = Time.now.to_f
310 assert_equal((time2-time1)*1000000<400, true)
311 end
312
Kevin Clark4bd89162008-07-08 00:47:49 +0000313end
314