blob: beebe44e522e8767ade36ea7ef58ff566b1dfa51 [file] [log] [blame]
Roger Meier32f39822014-06-18 22:43:17 +02001#!/usr/bin/env ruby
Nobuaki Sukegawa9b35a7c2015-11-17 11:01:41 +09002# encoding: utf-8
Roger Meier32f39822014-06-18 22:43:17 +02003
David Reissea2cba82009-03-30 21:35:00 +00004#
5# Licensed to the Apache Software Foundation (ASF) under one
6# or more contributor license agreements. See the NOTICE file
7# distributed with this work for additional information
8# regarding copyright ownership. The ASF licenses this file
9# to you under the Apache License, Version 2.0 (the
10# "License"); you may not use this file except in compliance
11# with the License. You may obtain a copy of the License at
12#
13# http://www.apache.org/licenses/LICENSE-2.0
14#
15# Unless required by applicable law or agreed to in writing,
16# software distributed under the License is distributed on an
17# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18# KIND, either express or implied. See the License for the
19# specific language governing permissions and limitations
20# under the License.
21#
22
Roger Meierc95d5df2014-01-19 21:53:02 +010023$:.push File.dirname(__FILE__) + '/..'
Kevin Clark4bd89162008-07-08 00:47:49 +000024
Roger Meierc95d5df2014-01-19 21:53:02 +010025require 'test_helper'
Kevin Clark4bd89162008-07-08 00:47:49 +000026require 'thrift'
Roger Meierc95d5df2014-01-19 21:53:02 +010027require 'thrift_test'
Kevin Clark4bd89162008-07-08 00:47:49 +000028
Roger Meiera3570ac2014-06-10 22:16:14 +020029$protocolType = "binary"
30$host = "localhost"
31$port = 9090
32$transport = "buffered"
33ARGV.each do|a|
34 if a == "--help"
35 puts "Allowed options:"
36 puts "\t -h [ --help ] \t produce help message"
37 puts "\t--host arg (=localhost) \t Host to connect"
38 puts "\t--port arg (=9090) \t Port number to listen"
39 puts "\t--protocol arg (=binary) \t protocol: binary, accel"
40 puts "\t--transport arg (=buffered) transport: buffered, framed, http"
41 exit
42 elsif a.start_with?("--host")
43 $host = a.split("=")[1]
44 elsif a.start_with?("--protocol")
45 $protocolType = a.split("=")[1]
46 elsif a.start_with?("--transport")
47 $transport = a.split("=")[1]
48 elsif a.start_with?("--port")
Randy Abernethy983bf7d2015-10-09 12:28:57 -070049 $port = a.split("=")[1].to_i
Roger Meiera3570ac2014-06-10 22:16:14 +020050 end
51end
52ARGV=[]
53
Kevin Clark4bd89162008-07-08 00:47:49 +000054class SimpleClientTest < Test::Unit::TestCase
Randy Abernethy983bf7d2015-10-09 12:28:57 -070055 def setup
Kevin Clark4bd89162008-07-08 00:47:49 +000056 unless @socket
Roger Meiera3570ac2014-06-10 22:16:14 +020057 @socket = Thrift::Socket.new($host, $port)
Roger Meiera3570ac2014-06-10 22:16:14 +020058 if $transport == "buffered"
59 transportFactory = Thrift::BufferedTransport.new(@socket)
Roger Meiera3570ac2014-06-10 22:16:14 +020060 elsif $transport == "framed"
61 transportFactory = Thrift::FramedTransport.new(@socket)
62 else
63 raise 'Unknown transport type'
64 end
65
66 if $protocolType == "binary"
67 @protocol = Thrift::BinaryProtocol.new(transportFactory)
Roger Meiera3570ac2014-06-10 22:16:14 +020068 elsif $protocolType == "compact"
69 @protocol = Thrift::CompactProtocol.new(transportFactory)
70 elsif $protocolType == "json"
71 @protocol = Thrift::JsonProtocol.new(transportFactory)
72 elsif $protocolType == "accel"
73 @protocol = Thrift::BinaryProtocolAccelerated.new(transportFactory)
74 else
75 raise 'Unknown protocol type'
76 end
Kevin Clark4bd89162008-07-08 00:47:49 +000077 @client = Thrift::Test::ThriftTest::Client.new(@protocol)
78 @socket.open
79 end
80 end
Randy Abernethy983bf7d2015-10-09 12:28:57 -070081
82 def teardown
83 @socket.close
84 end
85
Roger Meiera3570ac2014-06-10 22:16:14 +020086 def test_void
Nobuaki Sukegawa68238292015-09-21 23:28:22 +090087 p 'test_void'
Roger Meiera3570ac2014-06-10 22:16:14 +020088 @client.testVoid()
89 end
90
Kevin Clark4bd89162008-07-08 00:47:49 +000091 def test_string
Nobuaki Sukegawa68238292015-09-21 23:28:22 +090092 p 'test_string'
Nobuaki Sukegawa8cd519f2015-10-10 01:52:13 +090093 test_string =
94 'quote: \" backslash:' +
95 ' forwardslash-escaped: \/ ' +
96 ' backspace: \b formfeed: \f newline: \n return: \r tab: ' +
97 ' now-all-of-them-together: "\\\/\b\n\r\t' +
98 ' now-a-bunch-of-junk: !@#$%&()(&%$#{}{}<><><' +
99 ' char-to-test-json-parsing: ]] \"]] \\" }}}{ [[[ '
Nobuaki Sukegawa9b35a7c2015-11-17 11:01:41 +0900100 test_string = "Afrikaans, Alemannisch, Aragonés, العربية, مصرى, " +
101 "Asturianu, Aymar aru, Azərbaycan, Башҡорт, Boarisch, Žemaitėška, " +
102 "Беларуская, Беларуская (тарашкевіца), Български, Bamanankan, " +
103 "বাংলা, Brezhoneg, Bosanski, Català, Mìng-dĕ̤ng-ngṳ̄, Нохчийн, " +
104 "Cebuano, ᏣᎳᎩ, Česky, Словѣ́ньскъ / ⰔⰎⰑⰂⰡⰐⰠⰔⰍⰟ, Чӑвашла, Cymraeg, " +
105 "Dansk, Zazaki, ދިވެހިބަސް, Ελληνικά, Emiliàn e rumagnòl, English, " +
106 "Esperanto, Español, Eesti, Euskara, فارسی, Suomi, Võro, Føroyskt, " +
107 "Français, Arpetan, Furlan, Frysk, Gaeilge, 贛語, Gàidhlig, Galego, " +
108 "Avañe'ẽ, ગુજરાતી, Gaelg, עברית, हिन्दी, Fiji Hindi, Hrvatski, " +
109 "Kreyòl ayisyen, Magyar, Հայերեն, Interlingua, Bahasa Indonesia, " +
110 "Ilokano, Ido, Íslenska, Italiano, 日本語, Lojban, Basa Jawa, " +
111 "ქართული, Kongo, Kalaallisut, ಕನ್ನಡ, 한국어, Къарачай-Малкъар, " +
112 "Ripoarisch, Kurdî, Коми, Kernewek, Кыргызча, Latina, Ladino, " +
113 "Lëtzebuergesch, Limburgs, Lingála, ລາວ, Lietuvių, Latviešu, Basa " +
114 "Banyumasan, Malagasy, Македонски, മലയാളം, मराठी, مازِرونی, Bahasa " +
115 "Melayu, Nnapulitano, Nedersaksisch, नेपाल भाषा, Nederlands, ‪" +
116 "Norsk (nynorsk)‬, ‪Norsk (bokmål)‬, Nouormand, Diné bizaad, " +
117 "Occitan, Иронау, Papiamentu, Deitsch, Polski, پنجابی, پښتو, " +
118 "Norfuk / Pitkern, Português, Runa Simi, Rumantsch, Romani, Română, " +
119 "Русский, Саха тыла, Sardu, Sicilianu, Scots, Sámegiella, Simple " +
120 "English, Slovenčina, Slovenščina, Српски / Srpski, Seeltersk, " +
121 "Svenska, Kiswahili, தமிழ், తెలుగు, Тоҷикӣ, ไทย, Türkmençe, Tagalog, " +
122 "Türkçe, Татарча/Tatarça, Українська, اردو, Tiếng Việt, Volapük, " +
123 "Walon, Winaray, 吴语, isiXhosa, ייִדיש, Yorùbá, Zeêuws, 中文, " +
124 "Bân-lâm-gú, 粵語"
Nobuaki Sukegawa8cd519f2015-10-10 01:52:13 +0900125
126 result_string = @client.testString(test_string)
127 assert_equal(test_string, result_string.force_encoding(Encoding::UTF_8))
Kevin Clark4bd89162008-07-08 00:47:49 +0000128 end
129
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900130 def test_bool
131 p 'test_bool'
132 assert_equal(@client.testBool(true), true)
133 assert_equal(@client.testBool(false), false)
134 end
135
Kevin Clark4bd89162008-07-08 00:47:49 +0000136 def test_byte
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900137 p 'test_byte'
138 val = 120
Kevin Clark4bd89162008-07-08 00:47:49 +0000139 assert_equal(@client.testByte(val), val)
140 assert_equal(@client.testByte(-val), -val)
141 end
142
143 def test_i32
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900144 p 'test_i32'
145 val = 2000000032
Kevin Clark4bd89162008-07-08 00:47:49 +0000146 assert_equal(@client.testI32(val), val)
147 assert_equal(@client.testI32(-val), -val)
148 end
149
150 def test_i64
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900151 p 'test_i64'
152 val = 9000000000000000064
Kevin Clark4bd89162008-07-08 00:47:49 +0000153 assert_equal(@client.testI64(val), val)
154 assert_equal(@client.testI64(-val), -val)
155 end
156
157 def test_double
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900158 p 'test_double'
Nobuaki Sukegawa228b3282015-10-10 03:11:49 +0900159 val = 3.14159265358979323846
Kevin Clark4bd89162008-07-08 00:47:49 +0000160 assert_equal(@client.testDouble(val), val)
161 assert_equal(@client.testDouble(-val), -val)
162 assert_kind_of(Float, @client.testDouble(val))
163 end
164
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900165 def test_binary
166 p 'test_binary'
Jens Geyer123258b2015-10-02 00:38:17 +0200167 val = (0...256).reverse_each.to_a
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900168 ret = @client.testBinary(val.pack('C*'))
169 assert_equal(val, ret.bytes.to_a)
170 end
Randy Abernethy983bf7d2015-10-09 12:28:57 -0700171
Kevin Clark4bd89162008-07-08 00:47:49 +0000172 def test_map
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900173 p 'test_map'
Kevin Clark4bd89162008-07-08 00:47:49 +0000174 val = {1 => 1, 2 => 2, 3 => 3}
175 assert_equal(@client.testMap(val), val)
176 assert_kind_of(Hash, @client.testMap(val))
177 end
178
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900179 def test_string_map
180 p 'test_string_map'
181 val = {'a' => '2', 'b' => 'blah', 'some' => 'thing'}
182 ret = @client.testStringMap(val)
183 assert_equal(val, ret)
184 assert_kind_of(Hash, ret)
185 end
186
Kevin Clark4bd89162008-07-08 00:47:49 +0000187 def test_list
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900188 p 'test_list'
Kevin Clark4bd89162008-07-08 00:47:49 +0000189 val = [1,2,3,4,5]
190 assert_equal(@client.testList(val), val)
191 assert_kind_of(Array, @client.testList(val))
192 end
193
194 def test_enum
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900195 p 'test_enum'
Kevin Clark4bd89162008-07-08 00:47:49 +0000196 val = Thrift::Test::Numberz::SIX
197 ret = @client.testEnum(val)
198
199 assert_equal(ret, 6)
200 assert_kind_of(Fixnum, ret)
201 end
202
203 def test_typedef
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900204 p 'test_typedef'
Kevin Clark4bd89162008-07-08 00:47:49 +0000205 #UserId testTypedef(1: UserId thing),
Roger Meiera3570ac2014-06-10 22:16:14 +0200206 assert_equal(@client.testTypedef(309858235082523), 309858235082523)
207 assert_kind_of(Fixnum, @client.testTypedef(309858235082523))
Kevin Clark4bd89162008-07-08 00:47:49 +0000208 true
209 end
210
211 def test_set
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900212 p 'test_set'
Kevin Clark4bd89162008-07-08 00:47:49 +0000213 val = Set.new([1,2,3])
214 assert_equal(@client.testSet(val), val)
215 assert_kind_of(Set, @client.testSet(val))
216 end
217
218 def get_struct
219 Thrift::Test::Xtruct.new({'string_thing' => 'hi!', 'i32_thing' => 4 })
220 end
221
222 def test_struct
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900223 p 'test_struct'
Kevin Clark4bd89162008-07-08 00:47:49 +0000224 ret = @client.testStruct(get_struct)
225
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900226 # TODO: not sure what unspecified "default" requiredness values should be
227 assert(ret.byte_thing == nil || ret.byte_thing == 0)
228 assert(ret.i64_thing == nil || ret.i64_thing == 0)
229
Kevin Clark4bd89162008-07-08 00:47:49 +0000230 assert_equal(ret.string_thing, 'hi!')
231 assert_equal(ret.i32_thing, 4)
232 assert_kind_of(Thrift::Test::Xtruct, ret)
233 end
234
235 def test_nest
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900236 p 'test_nest'
Kevin Clark4bd89162008-07-08 00:47:49 +0000237 struct2 = Thrift::Test::Xtruct2.new({'struct_thing' => get_struct, 'i32_thing' => 10})
238
239 ret = @client.testNest(struct2)
240
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900241 # TODO: not sure what unspecified "default" requiredness values should be
242 assert(ret.struct_thing.byte_thing == nil || ret.struct_thing.byte_thing == 0)
243 assert(ret.struct_thing.i64_thing == nil || ret.struct_thing.i64_thing == 0)
244
Kevin Clark4bd89162008-07-08 00:47:49 +0000245 assert_equal(ret.struct_thing.string_thing, 'hi!')
246 assert_equal(ret.struct_thing.i32_thing, 4)
247 assert_equal(ret.i32_thing, 10)
248
249 assert_kind_of(Thrift::Test::Xtruct, ret.struct_thing)
250 assert_kind_of(Thrift::Test::Xtruct2, ret)
251 end
252
Roger Meiera3570ac2014-06-10 22:16:14 +0200253 def test_insanity
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900254 p 'test_insanity'
Kevin Clark4bd89162008-07-08 00:47:49 +0000255 insane = Thrift::Test::Insanity.new({
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900256 'userMap' => {
257 Thrift::Test::Numberz::FIVE => 5,
258 Thrift::Test::Numberz::EIGHT => 8,
259 },
260 'xtructs' => [
Kevin Clark4bd89162008-07-08 00:47:49 +0000261 Thrift::Test::Xtruct.new({
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900262 'string_thing' => 'Goodbye4',
263 'byte_thing' => 4,
264 'i32_thing' => 4,
265 'i64_thing' => 4,
266 }),
267 Thrift::Test::Xtruct.new({
268 'string_thing' => 'Hello2',
269 'byte_thing' => 2,
270 'i32_thing' => 2,
271 'i64_thing' => 2,
Kevin Clark4bd89162008-07-08 00:47:49 +0000272 })
273 ]
274 })
275
276 ret = @client.testInsanity(insane)
277
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900278 assert_equal(insane, ret[1][2])
279 assert_equal(insane, ret[1][3])
Kevin Clark4bd89162008-07-08 00:47:49 +0000280
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900281 assert(ret[2][6].userMap == nil || ret[2][6].userMap.length == 0)
282 assert(ret[2][6].xtructs == nil || ret[2][6].xtructs.length == 0)
Kevin Clark4bd89162008-07-08 00:47:49 +0000283 end
284
285 def test_map_map
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900286 p 'test_map_map'
Kevin Clark4bd89162008-07-08 00:47:49 +0000287 ret = @client.testMapMap(4)
288 assert_kind_of(Hash, ret)
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900289 expected = {
290 -4 => {
291 -4 => -4,
292 -3 => -3,
293 -2 => -2,
294 -1 => -1,
295 },
296 4 => {
297 4 => 4,
298 3 => 3,
299 2 => 2,
300 1 => 1,
301 }
302 }
303 assert_equal(expected, ret)
304 end
305
306 def test_multi
307 p 'test_multi'
308 ret = @client.testMulti(42, 4242, 424242, {1 => 'blah', 2 => 'thing'}, Thrift::Test::Numberz::EIGHT, 24)
309 expected = Thrift::Test::Xtruct.new({
310 :string_thing => 'Hello2',
311 :byte_thing => 42,
312 :i32_thing => 4242,
313 :i64_thing => 424242
314 })
315 assert_equal(expected, ret)
Kevin Clark4bd89162008-07-08 00:47:49 +0000316 end
317
318 def test_exception
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900319 p 'test_exception'
Kevin Clark4bd89162008-07-08 00:47:49 +0000320 assert_raise Thrift::Test::Xception do
Roger Meiera3570ac2014-06-10 22:16:14 +0200321 @client.testException('Xception')
Kevin Clark4bd89162008-07-08 00:47:49 +0000322 end
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900323 begin
324 @client.testException('TException')
325 rescue => e
326 assert e.class.ancestors.include?(Thrift::Exception)
327 end
328 assert_nothing_raised do
329 @client.testException('test')
330 end
Kevin Clark4bd89162008-07-08 00:47:49 +0000331 end
Roger Meiera3570ac2014-06-10 22:16:14 +0200332
333 def test_multi_exception
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900334 p 'test_multi_exception'
Roger Meiera3570ac2014-06-10 22:16:14 +0200335 assert_raise Thrift::Test::Xception do
336 @client.testMultiException("Xception", "test 1")
337 end
338 assert_raise Thrift::Test::Xception2 do
339 @client.testMultiException("Xception2", "test 2")
340 end
341 assert_equal( @client.testMultiException("Success", "test 3").string_thing, "test 3")
342 end
343
344 def test_oneway
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900345 p 'test_oneway'
Roger Meiera3570ac2014-06-10 22:16:14 +0200346 time1 = Time.now.to_f
Nobuaki Sukegawaa6ab1f52015-11-28 15:04:39 +0900347 @client.testOneway(1)
Roger Meiera3570ac2014-06-10 22:16:14 +0200348 time2 = Time.now.to_f
Nobuaki Sukegawaa6ab1f52015-11-28 15:04:39 +0900349 assert_operator (time2-time1), :<, 0.1
Roger Meiera3570ac2014-06-10 22:16:14 +0200350 end
351
Kevin Clark4bd89162008-07-08 00:47:49 +0000352end
353