blob: 6aec596ce8c367ff3b7d36e4bfddce02df344e65 [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'
Nobuaki Sukegawa8cd519f2015-10-10 01:52:13 +090092 test_string =
93 'quote: \" backslash:' +
94 ' forwardslash-escaped: \/ ' +
95 ' backspace: \b formfeed: \f newline: \n return: \r tab: ' +
96 ' now-all-of-them-together: "\\\/\b\n\r\t' +
97 ' now-a-bunch-of-junk: !@#$%&()(&%$#{}{}<><><' +
98 ' char-to-test-json-parsing: ]] \"]] \\" }}}{ [[[ '
99
100 result_string = @client.testString(test_string)
101 assert_equal(test_string, result_string.force_encoding(Encoding::UTF_8))
Kevin Clark4bd89162008-07-08 00:47:49 +0000102 end
103
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900104 def test_bool
105 p 'test_bool'
106 assert_equal(@client.testBool(true), true)
107 assert_equal(@client.testBool(false), false)
108 end
109
Kevin Clark4bd89162008-07-08 00:47:49 +0000110 def test_byte
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900111 p 'test_byte'
112 val = 120
Kevin Clark4bd89162008-07-08 00:47:49 +0000113 assert_equal(@client.testByte(val), val)
114 assert_equal(@client.testByte(-val), -val)
115 end
116
117 def test_i32
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900118 p 'test_i32'
119 val = 2000000032
Kevin Clark4bd89162008-07-08 00:47:49 +0000120 assert_equal(@client.testI32(val), val)
121 assert_equal(@client.testI32(-val), -val)
122 end
123
124 def test_i64
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900125 p 'test_i64'
126 val = 9000000000000000064
Kevin Clark4bd89162008-07-08 00:47:49 +0000127 assert_equal(@client.testI64(val), val)
128 assert_equal(@client.testI64(-val), -val)
129 end
130
131 def test_double
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900132 p 'test_double'
Nobuaki Sukegawa228b3282015-10-10 03:11:49 +0900133 val = 3.14159265358979323846
Kevin Clark4bd89162008-07-08 00:47:49 +0000134 assert_equal(@client.testDouble(val), val)
135 assert_equal(@client.testDouble(-val), -val)
136 assert_kind_of(Float, @client.testDouble(val))
137 end
138
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900139 def test_binary
140 p 'test_binary'
Jens Geyer123258b2015-10-02 00:38:17 +0200141 val = (0...256).reverse_each.to_a
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900142 ret = @client.testBinary(val.pack('C*'))
143 assert_equal(val, ret.bytes.to_a)
144 end
Randy Abernethy983bf7d2015-10-09 12:28:57 -0700145
Kevin Clark4bd89162008-07-08 00:47:49 +0000146 def test_map
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900147 p 'test_map'
Kevin Clark4bd89162008-07-08 00:47:49 +0000148 val = {1 => 1, 2 => 2, 3 => 3}
149 assert_equal(@client.testMap(val), val)
150 assert_kind_of(Hash, @client.testMap(val))
151 end
152
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900153 def test_string_map
154 p 'test_string_map'
155 val = {'a' => '2', 'b' => 'blah', 'some' => 'thing'}
156 ret = @client.testStringMap(val)
157 assert_equal(val, ret)
158 assert_kind_of(Hash, ret)
159 end
160
Kevin Clark4bd89162008-07-08 00:47:49 +0000161 def test_list
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900162 p 'test_list'
Kevin Clark4bd89162008-07-08 00:47:49 +0000163 val = [1,2,3,4,5]
164 assert_equal(@client.testList(val), val)
165 assert_kind_of(Array, @client.testList(val))
166 end
167
168 def test_enum
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900169 p 'test_enum'
Kevin Clark4bd89162008-07-08 00:47:49 +0000170 val = Thrift::Test::Numberz::SIX
171 ret = @client.testEnum(val)
172
173 assert_equal(ret, 6)
174 assert_kind_of(Fixnum, ret)
175 end
176
177 def test_typedef
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900178 p 'test_typedef'
Kevin Clark4bd89162008-07-08 00:47:49 +0000179 #UserId testTypedef(1: UserId thing),
Roger Meiera3570ac2014-06-10 22:16:14 +0200180 assert_equal(@client.testTypedef(309858235082523), 309858235082523)
181 assert_kind_of(Fixnum, @client.testTypedef(309858235082523))
Kevin Clark4bd89162008-07-08 00:47:49 +0000182 true
183 end
184
185 def test_set
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900186 p 'test_set'
Kevin Clark4bd89162008-07-08 00:47:49 +0000187 val = Set.new([1,2,3])
188 assert_equal(@client.testSet(val), val)
189 assert_kind_of(Set, @client.testSet(val))
190 end
191
192 def get_struct
193 Thrift::Test::Xtruct.new({'string_thing' => 'hi!', 'i32_thing' => 4 })
194 end
195
196 def test_struct
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900197 p 'test_struct'
Kevin Clark4bd89162008-07-08 00:47:49 +0000198 ret = @client.testStruct(get_struct)
199
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900200 # TODO: not sure what unspecified "default" requiredness values should be
201 assert(ret.byte_thing == nil || ret.byte_thing == 0)
202 assert(ret.i64_thing == nil || ret.i64_thing == 0)
203
Kevin Clark4bd89162008-07-08 00:47:49 +0000204 assert_equal(ret.string_thing, 'hi!')
205 assert_equal(ret.i32_thing, 4)
206 assert_kind_of(Thrift::Test::Xtruct, ret)
207 end
208
209 def test_nest
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900210 p 'test_nest'
Kevin Clark4bd89162008-07-08 00:47:49 +0000211 struct2 = Thrift::Test::Xtruct2.new({'struct_thing' => get_struct, 'i32_thing' => 10})
212
213 ret = @client.testNest(struct2)
214
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900215 # TODO: not sure what unspecified "default" requiredness values should be
216 assert(ret.struct_thing.byte_thing == nil || ret.struct_thing.byte_thing == 0)
217 assert(ret.struct_thing.i64_thing == nil || ret.struct_thing.i64_thing == 0)
218
Kevin Clark4bd89162008-07-08 00:47:49 +0000219 assert_equal(ret.struct_thing.string_thing, 'hi!')
220 assert_equal(ret.struct_thing.i32_thing, 4)
221 assert_equal(ret.i32_thing, 10)
222
223 assert_kind_of(Thrift::Test::Xtruct, ret.struct_thing)
224 assert_kind_of(Thrift::Test::Xtruct2, ret)
225 end
226
Roger Meiera3570ac2014-06-10 22:16:14 +0200227 def test_insanity
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900228 p 'test_insanity'
Kevin Clark4bd89162008-07-08 00:47:49 +0000229 insane = Thrift::Test::Insanity.new({
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900230 'userMap' => {
231 Thrift::Test::Numberz::FIVE => 5,
232 Thrift::Test::Numberz::EIGHT => 8,
233 },
234 'xtructs' => [
Kevin Clark4bd89162008-07-08 00:47:49 +0000235 Thrift::Test::Xtruct.new({
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900236 'string_thing' => 'Goodbye4',
237 'byte_thing' => 4,
238 'i32_thing' => 4,
239 'i64_thing' => 4,
240 }),
241 Thrift::Test::Xtruct.new({
242 'string_thing' => 'Hello2',
243 'byte_thing' => 2,
244 'i32_thing' => 2,
245 'i64_thing' => 2,
Kevin Clark4bd89162008-07-08 00:47:49 +0000246 })
247 ]
248 })
249
250 ret = @client.testInsanity(insane)
251
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900252 assert_equal(insane, ret[1][2])
253 assert_equal(insane, ret[1][3])
Kevin Clark4bd89162008-07-08 00:47:49 +0000254
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900255 assert(ret[2][6].userMap == nil || ret[2][6].userMap.length == 0)
256 assert(ret[2][6].xtructs == nil || ret[2][6].xtructs.length == 0)
Kevin Clark4bd89162008-07-08 00:47:49 +0000257 end
258
259 def test_map_map
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900260 p 'test_map_map'
Kevin Clark4bd89162008-07-08 00:47:49 +0000261 ret = @client.testMapMap(4)
262 assert_kind_of(Hash, ret)
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900263 expected = {
264 -4 => {
265 -4 => -4,
266 -3 => -3,
267 -2 => -2,
268 -1 => -1,
269 },
270 4 => {
271 4 => 4,
272 3 => 3,
273 2 => 2,
274 1 => 1,
275 }
276 }
277 assert_equal(expected, ret)
278 end
279
280 def test_multi
281 p 'test_multi'
282 ret = @client.testMulti(42, 4242, 424242, {1 => 'blah', 2 => 'thing'}, Thrift::Test::Numberz::EIGHT, 24)
283 expected = Thrift::Test::Xtruct.new({
284 :string_thing => 'Hello2',
285 :byte_thing => 42,
286 :i32_thing => 4242,
287 :i64_thing => 424242
288 })
289 assert_equal(expected, ret)
Kevin Clark4bd89162008-07-08 00:47:49 +0000290 end
291
292 def test_exception
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900293 p 'test_exception'
Kevin Clark4bd89162008-07-08 00:47:49 +0000294 assert_raise Thrift::Test::Xception do
Roger Meiera3570ac2014-06-10 22:16:14 +0200295 @client.testException('Xception')
Kevin Clark4bd89162008-07-08 00:47:49 +0000296 end
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900297 begin
298 @client.testException('TException')
299 rescue => e
300 assert e.class.ancestors.include?(Thrift::Exception)
301 end
302 assert_nothing_raised do
303 @client.testException('test')
304 end
Kevin Clark4bd89162008-07-08 00:47:49 +0000305 end
Roger Meiera3570ac2014-06-10 22:16:14 +0200306
307 def test_multi_exception
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900308 p 'test_multi_exception'
Roger Meiera3570ac2014-06-10 22:16:14 +0200309 assert_raise Thrift::Test::Xception do
310 @client.testMultiException("Xception", "test 1")
311 end
312 assert_raise Thrift::Test::Xception2 do
313 @client.testMultiException("Xception2", "test 2")
314 end
315 assert_equal( @client.testMultiException("Success", "test 3").string_thing, "test 3")
316 end
317
318 def test_oneway
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900319 p 'test_oneway'
Roger Meiera3570ac2014-06-10 22:16:14 +0200320 time1 = Time.now.to_f
321 @client.testOneway(3)
322 time2 = Time.now.to_f
323 assert_equal((time2-time1)*1000000<400, true)
324 end
325
Kevin Clark4bd89162008-07-08 00:47:49 +0000326end
327