blob: d04f4750706c842d8c0fd64dcaef0438b8b04b57 [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")
Randy Abernethy983bf7d2015-10-09 12:28:57 -070048 $port = a.split("=")[1].to_i
Roger Meiera3570ac2014-06-10 22:16:14 +020049 end
50end
51ARGV=[]
52
Kevin Clark4bd89162008-07-08 00:47:49 +000053class SimpleClientTest < Test::Unit::TestCase
Randy Abernethy983bf7d2015-10-09 12:28:57 -070054 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
Randy Abernethy983bf7d2015-10-09 12:28:57 -070080
81 def teardown
82 @socket.close
83 end
84
Roger Meiera3570ac2014-06-10 22:16:14 +020085 def test_void
Nobuaki Sukegawa68238292015-09-21 23:28:22 +090086 p 'test_void'
Roger Meiera3570ac2014-06-10 22:16:14 +020087 @client.testVoid()
88 end
89
Kevin Clark4bd89162008-07-08 00:47:49 +000090 def test_string
Nobuaki Sukegawa68238292015-09-21 23:28:22 +090091 p 'test_string'
Kevin Clark4bd89162008-07-08 00:47:49 +000092 assert_equal(@client.testString('string'), 'string')
93 end
94
Nobuaki Sukegawa68238292015-09-21 23:28:22 +090095 def test_bool
96 p 'test_bool'
97 assert_equal(@client.testBool(true), true)
98 assert_equal(@client.testBool(false), false)
99 end
100
Kevin Clark4bd89162008-07-08 00:47:49 +0000101 def test_byte
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900102 p 'test_byte'
103 val = 120
Kevin Clark4bd89162008-07-08 00:47:49 +0000104 assert_equal(@client.testByte(val), val)
105 assert_equal(@client.testByte(-val), -val)
106 end
107
108 def test_i32
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900109 p 'test_i32'
110 val = 2000000032
Kevin Clark4bd89162008-07-08 00:47:49 +0000111 assert_equal(@client.testI32(val), val)
112 assert_equal(@client.testI32(-val), -val)
113 end
114
115 def test_i64
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900116 p 'test_i64'
117 val = 9000000000000000064
Kevin Clark4bd89162008-07-08 00:47:49 +0000118 assert_equal(@client.testI64(val), val)
119 assert_equal(@client.testI64(-val), -val)
120 end
121
122 def test_double
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900123 p 'test_double'
Kevin Clark4bd89162008-07-08 00:47:49 +0000124 val = 3.14
125 assert_equal(@client.testDouble(val), val)
126 assert_equal(@client.testDouble(-val), -val)
127 assert_kind_of(Float, @client.testDouble(val))
128 end
129
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900130 def test_binary
131 p 'test_binary'
Jens Geyer123258b2015-10-02 00:38:17 +0200132 val = (0...256).reverse_each.to_a
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900133 ret = @client.testBinary(val.pack('C*'))
134 assert_equal(val, ret.bytes.to_a)
135 end
Randy Abernethy983bf7d2015-10-09 12:28:57 -0700136
Kevin Clark4bd89162008-07-08 00:47:49 +0000137 def test_map
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900138 p 'test_map'
Kevin Clark4bd89162008-07-08 00:47:49 +0000139 val = {1 => 1, 2 => 2, 3 => 3}
140 assert_equal(@client.testMap(val), val)
141 assert_kind_of(Hash, @client.testMap(val))
142 end
143
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900144 def test_string_map
145 p 'test_string_map'
146 val = {'a' => '2', 'b' => 'blah', 'some' => 'thing'}
147 ret = @client.testStringMap(val)
148 assert_equal(val, ret)
149 assert_kind_of(Hash, ret)
150 end
151
Kevin Clark4bd89162008-07-08 00:47:49 +0000152 def test_list
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900153 p 'test_list'
Kevin Clark4bd89162008-07-08 00:47:49 +0000154 val = [1,2,3,4,5]
155 assert_equal(@client.testList(val), val)
156 assert_kind_of(Array, @client.testList(val))
157 end
158
159 def test_enum
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900160 p 'test_enum'
Kevin Clark4bd89162008-07-08 00:47:49 +0000161 val = Thrift::Test::Numberz::SIX
162 ret = @client.testEnum(val)
163
164 assert_equal(ret, 6)
165 assert_kind_of(Fixnum, ret)
166 end
167
168 def test_typedef
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900169 p 'test_typedef'
Kevin Clark4bd89162008-07-08 00:47:49 +0000170 #UserId testTypedef(1: UserId thing),
Roger Meiera3570ac2014-06-10 22:16:14 +0200171 assert_equal(@client.testTypedef(309858235082523), 309858235082523)
172 assert_kind_of(Fixnum, @client.testTypedef(309858235082523))
Kevin Clark4bd89162008-07-08 00:47:49 +0000173 true
174 end
175
176 def test_set
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900177 p 'test_set'
Kevin Clark4bd89162008-07-08 00:47:49 +0000178 val = Set.new([1,2,3])
179 assert_equal(@client.testSet(val), val)
180 assert_kind_of(Set, @client.testSet(val))
181 end
182
183 def get_struct
184 Thrift::Test::Xtruct.new({'string_thing' => 'hi!', 'i32_thing' => 4 })
185 end
186
187 def test_struct
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900188 p 'test_struct'
Kevin Clark4bd89162008-07-08 00:47:49 +0000189 ret = @client.testStruct(get_struct)
190
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900191 # TODO: not sure what unspecified "default" requiredness values should be
192 assert(ret.byte_thing == nil || ret.byte_thing == 0)
193 assert(ret.i64_thing == nil || ret.i64_thing == 0)
194
Kevin Clark4bd89162008-07-08 00:47:49 +0000195 assert_equal(ret.string_thing, 'hi!')
196 assert_equal(ret.i32_thing, 4)
197 assert_kind_of(Thrift::Test::Xtruct, ret)
198 end
199
200 def test_nest
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900201 p 'test_nest'
Kevin Clark4bd89162008-07-08 00:47:49 +0000202 struct2 = Thrift::Test::Xtruct2.new({'struct_thing' => get_struct, 'i32_thing' => 10})
203
204 ret = @client.testNest(struct2)
205
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900206 # TODO: not sure what unspecified "default" requiredness values should be
207 assert(ret.struct_thing.byte_thing == nil || ret.struct_thing.byte_thing == 0)
208 assert(ret.struct_thing.i64_thing == nil || ret.struct_thing.i64_thing == 0)
209
Kevin Clark4bd89162008-07-08 00:47:49 +0000210 assert_equal(ret.struct_thing.string_thing, 'hi!')
211 assert_equal(ret.struct_thing.i32_thing, 4)
212 assert_equal(ret.i32_thing, 10)
213
214 assert_kind_of(Thrift::Test::Xtruct, ret.struct_thing)
215 assert_kind_of(Thrift::Test::Xtruct2, ret)
216 end
217
Roger Meiera3570ac2014-06-10 22:16:14 +0200218 def test_insanity
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900219 p 'test_insanity'
Kevin Clark4bd89162008-07-08 00:47:49 +0000220 insane = Thrift::Test::Insanity.new({
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900221 'userMap' => {
222 Thrift::Test::Numberz::FIVE => 5,
223 Thrift::Test::Numberz::EIGHT => 8,
224 },
225 'xtructs' => [
Kevin Clark4bd89162008-07-08 00:47:49 +0000226 Thrift::Test::Xtruct.new({
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900227 'string_thing' => 'Goodbye4',
228 'byte_thing' => 4,
229 'i32_thing' => 4,
230 'i64_thing' => 4,
231 }),
232 Thrift::Test::Xtruct.new({
233 'string_thing' => 'Hello2',
234 'byte_thing' => 2,
235 'i32_thing' => 2,
236 'i64_thing' => 2,
Kevin Clark4bd89162008-07-08 00:47:49 +0000237 })
238 ]
239 })
240
241 ret = @client.testInsanity(insane)
242
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900243 assert_equal(insane, ret[1][2])
244 assert_equal(insane, ret[1][3])
Kevin Clark4bd89162008-07-08 00:47:49 +0000245
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900246 assert(ret[2][6].userMap == nil || ret[2][6].userMap.length == 0)
247 assert(ret[2][6].xtructs == nil || ret[2][6].xtructs.length == 0)
Kevin Clark4bd89162008-07-08 00:47:49 +0000248 end
249
250 def test_map_map
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900251 p 'test_map_map'
Kevin Clark4bd89162008-07-08 00:47:49 +0000252 ret = @client.testMapMap(4)
253 assert_kind_of(Hash, ret)
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900254 expected = {
255 -4 => {
256 -4 => -4,
257 -3 => -3,
258 -2 => -2,
259 -1 => -1,
260 },
261 4 => {
262 4 => 4,
263 3 => 3,
264 2 => 2,
265 1 => 1,
266 }
267 }
268 assert_equal(expected, ret)
269 end
270
271 def test_multi
272 p 'test_multi'
273 ret = @client.testMulti(42, 4242, 424242, {1 => 'blah', 2 => 'thing'}, Thrift::Test::Numberz::EIGHT, 24)
274 expected = Thrift::Test::Xtruct.new({
275 :string_thing => 'Hello2',
276 :byte_thing => 42,
277 :i32_thing => 4242,
278 :i64_thing => 424242
279 })
280 assert_equal(expected, ret)
Kevin Clark4bd89162008-07-08 00:47:49 +0000281 end
282
283 def test_exception
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900284 p 'test_exception'
Kevin Clark4bd89162008-07-08 00:47:49 +0000285 assert_raise Thrift::Test::Xception do
Roger Meiera3570ac2014-06-10 22:16:14 +0200286 @client.testException('Xception')
Kevin Clark4bd89162008-07-08 00:47:49 +0000287 end
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900288 begin
289 @client.testException('TException')
290 rescue => e
291 assert e.class.ancestors.include?(Thrift::Exception)
292 end
293 assert_nothing_raised do
294 @client.testException('test')
295 end
Kevin Clark4bd89162008-07-08 00:47:49 +0000296 end
Roger Meiera3570ac2014-06-10 22:16:14 +0200297
298 def test_multi_exception
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900299 p 'test_multi_exception'
Roger Meiera3570ac2014-06-10 22:16:14 +0200300 assert_raise Thrift::Test::Xception do
301 @client.testMultiException("Xception", "test 1")
302 end
303 assert_raise Thrift::Test::Xception2 do
304 @client.testMultiException("Xception2", "test 2")
305 end
306 assert_equal( @client.testMultiException("Success", "test 3").string_thing, "test 3")
307 end
308
309 def test_oneway
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900310 p 'test_oneway'
Roger Meiera3570ac2014-06-10 22:16:14 +0200311 time1 = Time.now.to_f
312 @client.testOneway(3)
313 time2 = Time.now.to_f
314 assert_equal((time2-time1)*1000000<400, true)
315 end
316
Kevin Clark4bd89162008-07-08 00:47:49 +0000317end
318