blob: 5259680178a2718da6783dc8324e1895c51d6ed3 [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
James E. King III9aaf2952018-03-20 15:06:08 -040029$domain_socket = nil
Roger Meiera3570ac2014-06-10 22:16:14 +020030$protocolType = "binary"
31$host = "localhost"
32$port = 9090
33$transport = "buffered"
James E. King III9aaf2952018-03-20 15:06:08 -040034
Roger Meiera3570ac2014-06-10 22:16:14 +020035ARGV.each do|a|
36 if a == "--help"
37 puts "Allowed options:"
38 puts "\t -h [ --help ] \t produce help message"
James E. King III9aaf2952018-03-20 15:06:08 -040039 puts "\t--domain-socket arg (=) \t Unix domain socket path - if not empty, host and port are ignored"
Roger Meiera3570ac2014-06-10 22:16:14 +020040 puts "\t--host arg (=localhost) \t Host to connect"
41 puts "\t--port arg (=9090) \t Port number to listen"
James E. King III9aaf2952018-03-20 15:06:08 -040042 puts "\t--protocol arg (=binary) \t protocol: accel, binary, compact, json"
Roger Meiera3570ac2014-06-10 22:16:14 +020043 puts "\t--transport arg (=buffered) transport: buffered, framed, http"
44 exit
James E. King III9aaf2952018-03-20 15:06:08 -040045 elsif a.start_with?("--domain-socket")
46 $domain_socket = a.split("=")[1]
Roger Meiera3570ac2014-06-10 22:16:14 +020047 elsif a.start_with?("--host")
48 $host = a.split("=")[1]
49 elsif a.start_with?("--protocol")
50 $protocolType = a.split("=")[1]
51 elsif a.start_with?("--transport")
52 $transport = a.split("=")[1]
53 elsif a.start_with?("--port")
Randy Abernethy983bf7d2015-10-09 12:28:57 -070054 $port = a.split("=")[1].to_i
Roger Meiera3570ac2014-06-10 22:16:14 +020055 end
56end
57ARGV=[]
58
Kevin Clark4bd89162008-07-08 00:47:49 +000059class SimpleClientTest < Test::Unit::TestCase
Randy Abernethy983bf7d2015-10-09 12:28:57 -070060 def setup
Kevin Clark4bd89162008-07-08 00:47:49 +000061 unless @socket
James E. King III9aaf2952018-03-20 15:06:08 -040062 if $domain_socket.to_s.strip.empty?
63 @socket = Thrift::Socket.new($host, $port)
64 else
65 @socket = Thrift::UNIXSocket.new($domain_socket)
66 end
67
Roger Meiera3570ac2014-06-10 22:16:14 +020068 if $transport == "buffered"
69 transportFactory = Thrift::BufferedTransport.new(@socket)
Roger Meiera3570ac2014-06-10 22:16:14 +020070 elsif $transport == "framed"
71 transportFactory = Thrift::FramedTransport.new(@socket)
72 else
73 raise 'Unknown transport type'
74 end
75
76 if $protocolType == "binary"
77 @protocol = Thrift::BinaryProtocol.new(transportFactory)
Roger Meiera3570ac2014-06-10 22:16:14 +020078 elsif $protocolType == "compact"
79 @protocol = Thrift::CompactProtocol.new(transportFactory)
80 elsif $protocolType == "json"
81 @protocol = Thrift::JsonProtocol.new(transportFactory)
82 elsif $protocolType == "accel"
83 @protocol = Thrift::BinaryProtocolAccelerated.new(transportFactory)
84 else
85 raise 'Unknown protocol type'
86 end
Kevin Clark4bd89162008-07-08 00:47:49 +000087 @client = Thrift::Test::ThriftTest::Client.new(@protocol)
88 @socket.open
89 end
90 end
Randy Abernethy983bf7d2015-10-09 12:28:57 -070091
92 def teardown
93 @socket.close
94 end
95
Roger Meiera3570ac2014-06-10 22:16:14 +020096 def test_void
Nobuaki Sukegawa68238292015-09-21 23:28:22 +090097 p 'test_void'
Roger Meiera3570ac2014-06-10 22:16:14 +020098 @client.testVoid()
99 end
100
Kevin Clark4bd89162008-07-08 00:47:49 +0000101 def test_string
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900102 p 'test_string'
Nobuaki Sukegawa8cd519f2015-10-10 01:52:13 +0900103 test_string =
104 'quote: \" backslash:' +
105 ' forwardslash-escaped: \/ ' +
106 ' backspace: \b formfeed: \f newline: \n return: \r tab: ' +
107 ' now-all-of-them-together: "\\\/\b\n\r\t' +
108 ' now-a-bunch-of-junk: !@#$%&()(&%$#{}{}<><><' +
109 ' char-to-test-json-parsing: ]] \"]] \\" }}}{ [[[ '
Nobuaki Sukegawa9b35a7c2015-11-17 11:01:41 +0900110 test_string = "Afrikaans, Alemannisch, Aragonés, العربية, مصرى, " +
111 "Asturianu, Aymar aru, Azərbaycan, Башҡорт, Boarisch, Žemaitėška, " +
112 "Беларуская, Беларуская (тарашкевіца), Български, Bamanankan, " +
113 "বাংলা, Brezhoneg, Bosanski, Català, Mìng-dĕ̤ng-ngṳ̄, Нохчийн, " +
114 "Cebuano, ᏣᎳᎩ, Česky, Словѣ́ньскъ / ⰔⰎⰑⰂⰡⰐⰠⰔⰍⰟ, Чӑвашла, Cymraeg, " +
115 "Dansk, Zazaki, ދިވެހިބަސް, Ελληνικά, Emiliàn e rumagnòl, English, " +
116 "Esperanto, Español, Eesti, Euskara, فارسی, Suomi, Võro, Føroyskt, " +
117 "Français, Arpetan, Furlan, Frysk, Gaeilge, 贛語, Gàidhlig, Galego, " +
118 "Avañe'ẽ, ગુજરાતી, Gaelg, עברית, हिन्दी, Fiji Hindi, Hrvatski, " +
119 "Kreyòl ayisyen, Magyar, Հայերեն, Interlingua, Bahasa Indonesia, " +
120 "Ilokano, Ido, Íslenska, Italiano, 日本語, Lojban, Basa Jawa, " +
121 "ქართული, Kongo, Kalaallisut, ಕನ್ನಡ, 한국어, Къарачай-Малкъар, " +
122 "Ripoarisch, Kurdî, Коми, Kernewek, Кыргызча, Latina, Ladino, " +
123 "Lëtzebuergesch, Limburgs, Lingála, ລາວ, Lietuvių, Latviešu, Basa " +
124 "Banyumasan, Malagasy, Македонски, മലയാളം, मराठी, مازِرونی, Bahasa " +
125 "Melayu, Nnapulitano, Nedersaksisch, नेपाल भाषा, Nederlands, ‪" +
126 "Norsk (nynorsk)‬, ‪Norsk (bokmål)‬, Nouormand, Diné bizaad, " +
127 "Occitan, Иронау, Papiamentu, Deitsch, Polski, پنجابی, پښتو, " +
128 "Norfuk / Pitkern, Português, Runa Simi, Rumantsch, Romani, Română, " +
129 "Русский, Саха тыла, Sardu, Sicilianu, Scots, Sámegiella, Simple " +
130 "English, Slovenčina, Slovenščina, Српски / Srpski, Seeltersk, " +
131 "Svenska, Kiswahili, தமிழ், తెలుగు, Тоҷикӣ, ไทย, Türkmençe, Tagalog, " +
132 "Türkçe, Татарча/Tatarça, Українська, اردو, Tiếng Việt, Volapük, " +
133 "Walon, Winaray, 吴语, isiXhosa, ייִדיש, Yorùbá, Zeêuws, 中文, " +
134 "Bân-lâm-gú, 粵語"
Nobuaki Sukegawa8cd519f2015-10-10 01:52:13 +0900135
136 result_string = @client.testString(test_string)
137 assert_equal(test_string, result_string.force_encoding(Encoding::UTF_8))
Kevin Clark4bd89162008-07-08 00:47:49 +0000138 end
139
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900140 def test_bool
141 p 'test_bool'
142 assert_equal(@client.testBool(true), true)
143 assert_equal(@client.testBool(false), false)
144 end
145
Kevin Clark4bd89162008-07-08 00:47:49 +0000146 def test_byte
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900147 p 'test_byte'
148 val = 120
Kevin Clark4bd89162008-07-08 00:47:49 +0000149 assert_equal(@client.testByte(val), val)
150 assert_equal(@client.testByte(-val), -val)
151 end
152
153 def test_i32
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900154 p 'test_i32'
155 val = 2000000032
Kevin Clark4bd89162008-07-08 00:47:49 +0000156 assert_equal(@client.testI32(val), val)
157 assert_equal(@client.testI32(-val), -val)
158 end
159
160 def test_i64
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900161 p 'test_i64'
162 val = 9000000000000000064
Kevin Clark4bd89162008-07-08 00:47:49 +0000163 assert_equal(@client.testI64(val), val)
164 assert_equal(@client.testI64(-val), -val)
165 end
166
167 def test_double
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900168 p 'test_double'
Nobuaki Sukegawa228b3282015-10-10 03:11:49 +0900169 val = 3.14159265358979323846
Kevin Clark4bd89162008-07-08 00:47:49 +0000170 assert_equal(@client.testDouble(val), val)
171 assert_equal(@client.testDouble(-val), -val)
172 assert_kind_of(Float, @client.testDouble(val))
173 end
174
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900175 def test_binary
176 p 'test_binary'
Jens Geyer123258b2015-10-02 00:38:17 +0200177 val = (0...256).reverse_each.to_a
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900178 ret = @client.testBinary(val.pack('C*'))
179 assert_equal(val, ret.bytes.to_a)
180 end
Randy Abernethy983bf7d2015-10-09 12:28:57 -0700181
Kevin Clark4bd89162008-07-08 00:47:49 +0000182 def test_map
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900183 p 'test_map'
Kevin Clark4bd89162008-07-08 00:47:49 +0000184 val = {1 => 1, 2 => 2, 3 => 3}
185 assert_equal(@client.testMap(val), val)
186 assert_kind_of(Hash, @client.testMap(val))
187 end
188
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900189 def test_string_map
190 p 'test_string_map'
191 val = {'a' => '2', 'b' => 'blah', 'some' => 'thing'}
192 ret = @client.testStringMap(val)
193 assert_equal(val, ret)
194 assert_kind_of(Hash, ret)
195 end
196
Kevin Clark4bd89162008-07-08 00:47:49 +0000197 def test_list
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900198 p 'test_list'
Kevin Clark4bd89162008-07-08 00:47:49 +0000199 val = [1,2,3,4,5]
200 assert_equal(@client.testList(val), val)
201 assert_kind_of(Array, @client.testList(val))
202 end
203
204 def test_enum
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900205 p 'test_enum'
Kevin Clark4bd89162008-07-08 00:47:49 +0000206 val = Thrift::Test::Numberz::SIX
207 ret = @client.testEnum(val)
208
209 assert_equal(ret, 6)
210 assert_kind_of(Fixnum, ret)
211 end
212
213 def test_typedef
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900214 p 'test_typedef'
Kevin Clark4bd89162008-07-08 00:47:49 +0000215 #UserId testTypedef(1: UserId thing),
Roger Meiera3570ac2014-06-10 22:16:14 +0200216 assert_equal(@client.testTypedef(309858235082523), 309858235082523)
217 assert_kind_of(Fixnum, @client.testTypedef(309858235082523))
Kevin Clark4bd89162008-07-08 00:47:49 +0000218 true
219 end
220
221 def test_set
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900222 p 'test_set'
Kevin Clark4bd89162008-07-08 00:47:49 +0000223 val = Set.new([1,2,3])
224 assert_equal(@client.testSet(val), val)
225 assert_kind_of(Set, @client.testSet(val))
226 end
227
228 def get_struct
229 Thrift::Test::Xtruct.new({'string_thing' => 'hi!', 'i32_thing' => 4 })
230 end
231
232 def test_struct
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900233 p 'test_struct'
Kevin Clark4bd89162008-07-08 00:47:49 +0000234 ret = @client.testStruct(get_struct)
235
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900236 # TODO: not sure what unspecified "default" requiredness values should be
237 assert(ret.byte_thing == nil || ret.byte_thing == 0)
238 assert(ret.i64_thing == nil || ret.i64_thing == 0)
239
Kevin Clark4bd89162008-07-08 00:47:49 +0000240 assert_equal(ret.string_thing, 'hi!')
241 assert_equal(ret.i32_thing, 4)
242 assert_kind_of(Thrift::Test::Xtruct, ret)
243 end
244
245 def test_nest
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900246 p 'test_nest'
Kevin Clark4bd89162008-07-08 00:47:49 +0000247 struct2 = Thrift::Test::Xtruct2.new({'struct_thing' => get_struct, 'i32_thing' => 10})
248
249 ret = @client.testNest(struct2)
250
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900251 # TODO: not sure what unspecified "default" requiredness values should be
252 assert(ret.struct_thing.byte_thing == nil || ret.struct_thing.byte_thing == 0)
253 assert(ret.struct_thing.i64_thing == nil || ret.struct_thing.i64_thing == 0)
254
Kevin Clark4bd89162008-07-08 00:47:49 +0000255 assert_equal(ret.struct_thing.string_thing, 'hi!')
256 assert_equal(ret.struct_thing.i32_thing, 4)
257 assert_equal(ret.i32_thing, 10)
258
259 assert_kind_of(Thrift::Test::Xtruct, ret.struct_thing)
260 assert_kind_of(Thrift::Test::Xtruct2, ret)
261 end
262
Roger Meiera3570ac2014-06-10 22:16:14 +0200263 def test_insanity
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900264 p 'test_insanity'
Kevin Clark4bd89162008-07-08 00:47:49 +0000265 insane = Thrift::Test::Insanity.new({
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900266 'userMap' => {
267 Thrift::Test::Numberz::FIVE => 5,
268 Thrift::Test::Numberz::EIGHT => 8,
269 },
270 'xtructs' => [
Kevin Clark4bd89162008-07-08 00:47:49 +0000271 Thrift::Test::Xtruct.new({
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900272 'string_thing' => 'Goodbye4',
273 'byte_thing' => 4,
274 'i32_thing' => 4,
275 'i64_thing' => 4,
276 }),
277 Thrift::Test::Xtruct.new({
278 'string_thing' => 'Hello2',
279 'byte_thing' => 2,
280 'i32_thing' => 2,
281 'i64_thing' => 2,
Kevin Clark4bd89162008-07-08 00:47:49 +0000282 })
283 ]
284 })
285
286 ret = @client.testInsanity(insane)
287
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900288 assert_equal(insane, ret[1][2])
289 assert_equal(insane, ret[1][3])
Kevin Clark4bd89162008-07-08 00:47:49 +0000290
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900291 assert(ret[2][6].userMap == nil || ret[2][6].userMap.length == 0)
292 assert(ret[2][6].xtructs == nil || ret[2][6].xtructs.length == 0)
Kevin Clark4bd89162008-07-08 00:47:49 +0000293 end
294
295 def test_map_map
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900296 p 'test_map_map'
Kevin Clark4bd89162008-07-08 00:47:49 +0000297 ret = @client.testMapMap(4)
298 assert_kind_of(Hash, ret)
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900299 expected = {
300 -4 => {
301 -4 => -4,
302 -3 => -3,
303 -2 => -2,
304 -1 => -1,
305 },
306 4 => {
307 4 => 4,
308 3 => 3,
309 2 => 2,
310 1 => 1,
311 }
312 }
313 assert_equal(expected, ret)
314 end
315
316 def test_multi
317 p 'test_multi'
318 ret = @client.testMulti(42, 4242, 424242, {1 => 'blah', 2 => 'thing'}, Thrift::Test::Numberz::EIGHT, 24)
319 expected = Thrift::Test::Xtruct.new({
320 :string_thing => 'Hello2',
321 :byte_thing => 42,
322 :i32_thing => 4242,
323 :i64_thing => 424242
324 })
325 assert_equal(expected, ret)
Kevin Clark4bd89162008-07-08 00:47:49 +0000326 end
327
328 def test_exception
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900329 p 'test_exception'
Kevin Clark4bd89162008-07-08 00:47:49 +0000330 assert_raise Thrift::Test::Xception do
Roger Meiera3570ac2014-06-10 22:16:14 +0200331 @client.testException('Xception')
Kevin Clark4bd89162008-07-08 00:47:49 +0000332 end
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900333 begin
334 @client.testException('TException')
335 rescue => e
336 assert e.class.ancestors.include?(Thrift::Exception)
337 end
338 assert_nothing_raised do
339 @client.testException('test')
340 end
Kevin Clark4bd89162008-07-08 00:47:49 +0000341 end
Roger Meiera3570ac2014-06-10 22:16:14 +0200342
343 def test_multi_exception
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900344 p 'test_multi_exception'
Roger Meiera3570ac2014-06-10 22:16:14 +0200345 assert_raise Thrift::Test::Xception do
346 @client.testMultiException("Xception", "test 1")
347 end
348 assert_raise Thrift::Test::Xception2 do
349 @client.testMultiException("Xception2", "test 2")
350 end
351 assert_equal( @client.testMultiException("Success", "test 3").string_thing, "test 3")
352 end
353
354 def test_oneway
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900355 p 'test_oneway'
Roger Meiera3570ac2014-06-10 22:16:14 +0200356 time1 = Time.now.to_f
Nobuaki Sukegawaa6ab1f52015-11-28 15:04:39 +0900357 @client.testOneway(1)
Roger Meiera3570ac2014-06-10 22:16:14 +0200358 time2 = Time.now.to_f
Nobuaki Sukegawaa6ab1f52015-11-28 15:04:39 +0900359 assert_operator (time2-time1), :<, 0.1
Roger Meiera3570ac2014-06-10 22:16:14 +0200360 end
361
Kevin Clark4bd89162008-07-08 00:47:49 +0000362end
363