Roger Meier | 32f3982 | 2014-06-18 22:43:17 +0200 | [diff] [blame] | 1 | #!/usr/bin/env ruby |
| 2 | |
David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 3 | # |
| 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 Meier | c95d5df | 2014-01-19 21:53:02 +0100 | [diff] [blame] | 22 | $:.push File.dirname(__FILE__) + '/..' |
Kevin Clark | 4bd8916 | 2008-07-08 00:47:49 +0000 | [diff] [blame] | 23 | |
Roger Meier | c95d5df | 2014-01-19 21:53:02 +0100 | [diff] [blame] | 24 | require 'test_helper' |
Kevin Clark | 4bd8916 | 2008-07-08 00:47:49 +0000 | [diff] [blame] | 25 | require 'thrift' |
Roger Meier | c95d5df | 2014-01-19 21:53:02 +0100 | [diff] [blame] | 26 | require 'thrift_test' |
Kevin Clark | 4bd8916 | 2008-07-08 00:47:49 +0000 | [diff] [blame] | 27 | |
Roger Meier | a3570ac | 2014-06-10 22:16:14 +0200 | [diff] [blame] | 28 | $protocolType = "binary" |
| 29 | $host = "localhost" |
| 30 | $port = 9090 |
| 31 | $transport = "buffered" |
| 32 | ARGV.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 Abernethy | 983bf7d | 2015-10-09 12:28:57 -0700 | [diff] [blame] | 48 | $port = a.split("=")[1].to_i |
Roger Meier | a3570ac | 2014-06-10 22:16:14 +0200 | [diff] [blame] | 49 | end |
| 50 | end |
| 51 | ARGV=[] |
| 52 | |
Kevin Clark | 4bd8916 | 2008-07-08 00:47:49 +0000 | [diff] [blame] | 53 | class SimpleClientTest < Test::Unit::TestCase |
Randy Abernethy | 983bf7d | 2015-10-09 12:28:57 -0700 | [diff] [blame] | 54 | def setup |
Kevin Clark | 4bd8916 | 2008-07-08 00:47:49 +0000 | [diff] [blame] | 55 | unless @socket |
Roger Meier | a3570ac | 2014-06-10 22:16:14 +0200 | [diff] [blame] | 56 | @socket = Thrift::Socket.new($host, $port) |
Roger Meier | a3570ac | 2014-06-10 22:16:14 +0200 | [diff] [blame] | 57 | if $transport == "buffered" |
| 58 | transportFactory = Thrift::BufferedTransport.new(@socket) |
Roger Meier | a3570ac | 2014-06-10 22:16:14 +0200 | [diff] [blame] | 59 | 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 Meier | a3570ac | 2014-06-10 22:16:14 +0200 | [diff] [blame] | 67 | 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 Clark | 4bd8916 | 2008-07-08 00:47:49 +0000 | [diff] [blame] | 76 | @client = Thrift::Test::ThriftTest::Client.new(@protocol) |
| 77 | @socket.open |
| 78 | end |
| 79 | end |
Randy Abernethy | 983bf7d | 2015-10-09 12:28:57 -0700 | [diff] [blame] | 80 | |
| 81 | def teardown |
| 82 | @socket.close |
| 83 | end |
| 84 | |
Roger Meier | a3570ac | 2014-06-10 22:16:14 +0200 | [diff] [blame] | 85 | def test_void |
Nobuaki Sukegawa | 6823829 | 2015-09-21 23:28:22 +0900 | [diff] [blame] | 86 | p 'test_void' |
Roger Meier | a3570ac | 2014-06-10 22:16:14 +0200 | [diff] [blame] | 87 | @client.testVoid() |
| 88 | end |
| 89 | |
Kevin Clark | 4bd8916 | 2008-07-08 00:47:49 +0000 | [diff] [blame] | 90 | def test_string |
Nobuaki Sukegawa | 6823829 | 2015-09-21 23:28:22 +0900 | [diff] [blame] | 91 | p 'test_string' |
Nobuaki Sukegawa | 8cd519f | 2015-10-10 01:52:13 +0900 | [diff] [blame] | 92 | 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 Clark | 4bd8916 | 2008-07-08 00:47:49 +0000 | [diff] [blame] | 102 | end |
| 103 | |
Nobuaki Sukegawa | 6823829 | 2015-09-21 23:28:22 +0900 | [diff] [blame] | 104 | 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 Clark | 4bd8916 | 2008-07-08 00:47:49 +0000 | [diff] [blame] | 110 | def test_byte |
Nobuaki Sukegawa | 6823829 | 2015-09-21 23:28:22 +0900 | [diff] [blame] | 111 | p 'test_byte' |
| 112 | val = 120 |
Kevin Clark | 4bd8916 | 2008-07-08 00:47:49 +0000 | [diff] [blame] | 113 | assert_equal(@client.testByte(val), val) |
| 114 | assert_equal(@client.testByte(-val), -val) |
| 115 | end |
| 116 | |
| 117 | def test_i32 |
Nobuaki Sukegawa | 6823829 | 2015-09-21 23:28:22 +0900 | [diff] [blame] | 118 | p 'test_i32' |
| 119 | val = 2000000032 |
Kevin Clark | 4bd8916 | 2008-07-08 00:47:49 +0000 | [diff] [blame] | 120 | assert_equal(@client.testI32(val), val) |
| 121 | assert_equal(@client.testI32(-val), -val) |
| 122 | end |
| 123 | |
| 124 | def test_i64 |
Nobuaki Sukegawa | 6823829 | 2015-09-21 23:28:22 +0900 | [diff] [blame] | 125 | p 'test_i64' |
| 126 | val = 9000000000000000064 |
Kevin Clark | 4bd8916 | 2008-07-08 00:47:49 +0000 | [diff] [blame] | 127 | assert_equal(@client.testI64(val), val) |
| 128 | assert_equal(@client.testI64(-val), -val) |
| 129 | end |
| 130 | |
| 131 | def test_double |
Nobuaki Sukegawa | 6823829 | 2015-09-21 23:28:22 +0900 | [diff] [blame] | 132 | p 'test_double' |
Nobuaki Sukegawa | 228b328 | 2015-10-10 03:11:49 +0900 | [diff] [blame^] | 133 | val = 3.14159265358979323846 |
Kevin Clark | 4bd8916 | 2008-07-08 00:47:49 +0000 | [diff] [blame] | 134 | 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 Sukegawa | 6823829 | 2015-09-21 23:28:22 +0900 | [diff] [blame] | 139 | def test_binary |
| 140 | p 'test_binary' |
Jens Geyer | 123258b | 2015-10-02 00:38:17 +0200 | [diff] [blame] | 141 | val = (0...256).reverse_each.to_a |
Nobuaki Sukegawa | 6823829 | 2015-09-21 23:28:22 +0900 | [diff] [blame] | 142 | ret = @client.testBinary(val.pack('C*')) |
| 143 | assert_equal(val, ret.bytes.to_a) |
| 144 | end |
Randy Abernethy | 983bf7d | 2015-10-09 12:28:57 -0700 | [diff] [blame] | 145 | |
Kevin Clark | 4bd8916 | 2008-07-08 00:47:49 +0000 | [diff] [blame] | 146 | def test_map |
Nobuaki Sukegawa | 6823829 | 2015-09-21 23:28:22 +0900 | [diff] [blame] | 147 | p 'test_map' |
Kevin Clark | 4bd8916 | 2008-07-08 00:47:49 +0000 | [diff] [blame] | 148 | 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 Sukegawa | 6823829 | 2015-09-21 23:28:22 +0900 | [diff] [blame] | 153 | 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 Clark | 4bd8916 | 2008-07-08 00:47:49 +0000 | [diff] [blame] | 161 | def test_list |
Nobuaki Sukegawa | 6823829 | 2015-09-21 23:28:22 +0900 | [diff] [blame] | 162 | p 'test_list' |
Kevin Clark | 4bd8916 | 2008-07-08 00:47:49 +0000 | [diff] [blame] | 163 | 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 Sukegawa | 6823829 | 2015-09-21 23:28:22 +0900 | [diff] [blame] | 169 | p 'test_enum' |
Kevin Clark | 4bd8916 | 2008-07-08 00:47:49 +0000 | [diff] [blame] | 170 | 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 Sukegawa | 6823829 | 2015-09-21 23:28:22 +0900 | [diff] [blame] | 178 | p 'test_typedef' |
Kevin Clark | 4bd8916 | 2008-07-08 00:47:49 +0000 | [diff] [blame] | 179 | #UserId testTypedef(1: UserId thing), |
Roger Meier | a3570ac | 2014-06-10 22:16:14 +0200 | [diff] [blame] | 180 | assert_equal(@client.testTypedef(309858235082523), 309858235082523) |
| 181 | assert_kind_of(Fixnum, @client.testTypedef(309858235082523)) |
Kevin Clark | 4bd8916 | 2008-07-08 00:47:49 +0000 | [diff] [blame] | 182 | true |
| 183 | end |
| 184 | |
| 185 | def test_set |
Nobuaki Sukegawa | 6823829 | 2015-09-21 23:28:22 +0900 | [diff] [blame] | 186 | p 'test_set' |
Kevin Clark | 4bd8916 | 2008-07-08 00:47:49 +0000 | [diff] [blame] | 187 | 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 Sukegawa | 6823829 | 2015-09-21 23:28:22 +0900 | [diff] [blame] | 197 | p 'test_struct' |
Kevin Clark | 4bd8916 | 2008-07-08 00:47:49 +0000 | [diff] [blame] | 198 | ret = @client.testStruct(get_struct) |
| 199 | |
Nobuaki Sukegawa | 6823829 | 2015-09-21 23:28:22 +0900 | [diff] [blame] | 200 | # 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 Clark | 4bd8916 | 2008-07-08 00:47:49 +0000 | [diff] [blame] | 204 | 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 Sukegawa | 6823829 | 2015-09-21 23:28:22 +0900 | [diff] [blame] | 210 | p 'test_nest' |
Kevin Clark | 4bd8916 | 2008-07-08 00:47:49 +0000 | [diff] [blame] | 211 | struct2 = Thrift::Test::Xtruct2.new({'struct_thing' => get_struct, 'i32_thing' => 10}) |
| 212 | |
| 213 | ret = @client.testNest(struct2) |
| 214 | |
Nobuaki Sukegawa | 6823829 | 2015-09-21 23:28:22 +0900 | [diff] [blame] | 215 | # 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 Clark | 4bd8916 | 2008-07-08 00:47:49 +0000 | [diff] [blame] | 219 | 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 Meier | a3570ac | 2014-06-10 22:16:14 +0200 | [diff] [blame] | 227 | def test_insanity |
Nobuaki Sukegawa | 6823829 | 2015-09-21 23:28:22 +0900 | [diff] [blame] | 228 | p 'test_insanity' |
Kevin Clark | 4bd8916 | 2008-07-08 00:47:49 +0000 | [diff] [blame] | 229 | insane = Thrift::Test::Insanity.new({ |
Nobuaki Sukegawa | 6823829 | 2015-09-21 23:28:22 +0900 | [diff] [blame] | 230 | 'userMap' => { |
| 231 | Thrift::Test::Numberz::FIVE => 5, |
| 232 | Thrift::Test::Numberz::EIGHT => 8, |
| 233 | }, |
| 234 | 'xtructs' => [ |
Kevin Clark | 4bd8916 | 2008-07-08 00:47:49 +0000 | [diff] [blame] | 235 | Thrift::Test::Xtruct.new({ |
Nobuaki Sukegawa | 6823829 | 2015-09-21 23:28:22 +0900 | [diff] [blame] | 236 | '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 Clark | 4bd8916 | 2008-07-08 00:47:49 +0000 | [diff] [blame] | 246 | }) |
| 247 | ] |
| 248 | }) |
| 249 | |
| 250 | ret = @client.testInsanity(insane) |
| 251 | |
Nobuaki Sukegawa | 6823829 | 2015-09-21 23:28:22 +0900 | [diff] [blame] | 252 | assert_equal(insane, ret[1][2]) |
| 253 | assert_equal(insane, ret[1][3]) |
Kevin Clark | 4bd8916 | 2008-07-08 00:47:49 +0000 | [diff] [blame] | 254 | |
Nobuaki Sukegawa | 6823829 | 2015-09-21 23:28:22 +0900 | [diff] [blame] | 255 | 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 Clark | 4bd8916 | 2008-07-08 00:47:49 +0000 | [diff] [blame] | 257 | end |
| 258 | |
| 259 | def test_map_map |
Nobuaki Sukegawa | 6823829 | 2015-09-21 23:28:22 +0900 | [diff] [blame] | 260 | p 'test_map_map' |
Kevin Clark | 4bd8916 | 2008-07-08 00:47:49 +0000 | [diff] [blame] | 261 | ret = @client.testMapMap(4) |
| 262 | assert_kind_of(Hash, ret) |
Nobuaki Sukegawa | 6823829 | 2015-09-21 23:28:22 +0900 | [diff] [blame] | 263 | 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 Clark | 4bd8916 | 2008-07-08 00:47:49 +0000 | [diff] [blame] | 290 | end |
| 291 | |
| 292 | def test_exception |
Nobuaki Sukegawa | 6823829 | 2015-09-21 23:28:22 +0900 | [diff] [blame] | 293 | p 'test_exception' |
Kevin Clark | 4bd8916 | 2008-07-08 00:47:49 +0000 | [diff] [blame] | 294 | assert_raise Thrift::Test::Xception do |
Roger Meier | a3570ac | 2014-06-10 22:16:14 +0200 | [diff] [blame] | 295 | @client.testException('Xception') |
Kevin Clark | 4bd8916 | 2008-07-08 00:47:49 +0000 | [diff] [blame] | 296 | end |
Nobuaki Sukegawa | 6823829 | 2015-09-21 23:28:22 +0900 | [diff] [blame] | 297 | 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 Clark | 4bd8916 | 2008-07-08 00:47:49 +0000 | [diff] [blame] | 305 | end |
Roger Meier | a3570ac | 2014-06-10 22:16:14 +0200 | [diff] [blame] | 306 | |
| 307 | def test_multi_exception |
Nobuaki Sukegawa | 6823829 | 2015-09-21 23:28:22 +0900 | [diff] [blame] | 308 | p 'test_multi_exception' |
Roger Meier | a3570ac | 2014-06-10 22:16:14 +0200 | [diff] [blame] | 309 | 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 Sukegawa | 6823829 | 2015-09-21 23:28:22 +0900 | [diff] [blame] | 319 | p 'test_oneway' |
Roger Meier | a3570ac | 2014-06-10 22:16:14 +0200 | [diff] [blame] | 320 | 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 Clark | 4bd8916 | 2008-07-08 00:47:49 +0000 | [diff] [blame] | 326 | end |
| 327 | |