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") |
| 48 | $port = a.split("=")[1].to_i |
| 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 |
Roger Meier | a3570ac | 2014-06-10 22:16:14 +0200 | [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 |
| 80 | |
Roger Meier | a3570ac | 2014-06-10 22:16:14 +0200 | [diff] [blame] | 81 | def test_void |
Nobuaki Sukegawa | 6823829 | 2015-09-21 23:28:22 +0900 | [diff] [blame] | 82 | p 'test_void' |
Roger Meier | a3570ac | 2014-06-10 22:16:14 +0200 | [diff] [blame] | 83 | @client.testVoid() |
| 84 | end |
| 85 | |
Kevin Clark | 4bd8916 | 2008-07-08 00:47:49 +0000 | [diff] [blame] | 86 | def test_string |
Nobuaki Sukegawa | 6823829 | 2015-09-21 23:28:22 +0900 | [diff] [blame] | 87 | p 'test_string' |
Kevin Clark | 4bd8916 | 2008-07-08 00:47:49 +0000 | [diff] [blame] | 88 | assert_equal(@client.testString('string'), 'string') |
| 89 | end |
| 90 | |
Nobuaki Sukegawa | 6823829 | 2015-09-21 23:28:22 +0900 | [diff] [blame] | 91 | def test_bool |
| 92 | p 'test_bool' |
| 93 | assert_equal(@client.testBool(true), true) |
| 94 | assert_equal(@client.testBool(false), false) |
| 95 | end |
| 96 | |
Kevin Clark | 4bd8916 | 2008-07-08 00:47:49 +0000 | [diff] [blame] | 97 | def test_byte |
Nobuaki Sukegawa | 6823829 | 2015-09-21 23:28:22 +0900 | [diff] [blame] | 98 | p 'test_byte' |
| 99 | val = 120 |
Kevin Clark | 4bd8916 | 2008-07-08 00:47:49 +0000 | [diff] [blame] | 100 | assert_equal(@client.testByte(val), val) |
| 101 | assert_equal(@client.testByte(-val), -val) |
| 102 | end |
| 103 | |
| 104 | def test_i32 |
Nobuaki Sukegawa | 6823829 | 2015-09-21 23:28:22 +0900 | [diff] [blame] | 105 | p 'test_i32' |
| 106 | val = 2000000032 |
Kevin Clark | 4bd8916 | 2008-07-08 00:47:49 +0000 | [diff] [blame] | 107 | assert_equal(@client.testI32(val), val) |
| 108 | assert_equal(@client.testI32(-val), -val) |
| 109 | end |
| 110 | |
| 111 | def test_i64 |
Nobuaki Sukegawa | 6823829 | 2015-09-21 23:28:22 +0900 | [diff] [blame] | 112 | p 'test_i64' |
| 113 | val = 9000000000000000064 |
Kevin Clark | 4bd8916 | 2008-07-08 00:47:49 +0000 | [diff] [blame] | 114 | assert_equal(@client.testI64(val), val) |
| 115 | assert_equal(@client.testI64(-val), -val) |
| 116 | end |
| 117 | |
| 118 | def test_double |
Nobuaki Sukegawa | 6823829 | 2015-09-21 23:28:22 +0900 | [diff] [blame] | 119 | p 'test_double' |
Kevin Clark | 4bd8916 | 2008-07-08 00:47:49 +0000 | [diff] [blame] | 120 | val = 3.14 |
| 121 | assert_equal(@client.testDouble(val), val) |
| 122 | assert_equal(@client.testDouble(-val), -val) |
| 123 | assert_kind_of(Float, @client.testDouble(val)) |
| 124 | end |
| 125 | |
Nobuaki Sukegawa | 6823829 | 2015-09-21 23:28:22 +0900 | [diff] [blame] | 126 | def test_binary |
| 127 | p 'test_binary' |
Jens Geyer | 123258b | 2015-10-02 00:38:17 +0200 | [diff] [blame] | 128 | val = (0...256).reverse_each.to_a |
Nobuaki Sukegawa | 6823829 | 2015-09-21 23:28:22 +0900 | [diff] [blame] | 129 | ret = @client.testBinary(val.pack('C*')) |
| 130 | assert_equal(val, ret.bytes.to_a) |
| 131 | end |
Jens Geyer | 8bcfdd9 | 2014-12-14 03:14:26 +0100 | [diff] [blame] | 132 | |
Kevin Clark | 4bd8916 | 2008-07-08 00:47:49 +0000 | [diff] [blame] | 133 | def test_map |
Nobuaki Sukegawa | 6823829 | 2015-09-21 23:28:22 +0900 | [diff] [blame] | 134 | p 'test_map' |
Kevin Clark | 4bd8916 | 2008-07-08 00:47:49 +0000 | [diff] [blame] | 135 | val = {1 => 1, 2 => 2, 3 => 3} |
| 136 | assert_equal(@client.testMap(val), val) |
| 137 | assert_kind_of(Hash, @client.testMap(val)) |
| 138 | end |
| 139 | |
Nobuaki Sukegawa | 6823829 | 2015-09-21 23:28:22 +0900 | [diff] [blame] | 140 | def test_string_map |
| 141 | p 'test_string_map' |
| 142 | val = {'a' => '2', 'b' => 'blah', 'some' => 'thing'} |
| 143 | ret = @client.testStringMap(val) |
| 144 | assert_equal(val, ret) |
| 145 | assert_kind_of(Hash, ret) |
| 146 | end |
| 147 | |
Kevin Clark | 4bd8916 | 2008-07-08 00:47:49 +0000 | [diff] [blame] | 148 | def test_list |
Nobuaki Sukegawa | 6823829 | 2015-09-21 23:28:22 +0900 | [diff] [blame] | 149 | p 'test_list' |
Kevin Clark | 4bd8916 | 2008-07-08 00:47:49 +0000 | [diff] [blame] | 150 | val = [1,2,3,4,5] |
| 151 | assert_equal(@client.testList(val), val) |
| 152 | assert_kind_of(Array, @client.testList(val)) |
| 153 | end |
| 154 | |
| 155 | def test_enum |
Nobuaki Sukegawa | 6823829 | 2015-09-21 23:28:22 +0900 | [diff] [blame] | 156 | p 'test_enum' |
Kevin Clark | 4bd8916 | 2008-07-08 00:47:49 +0000 | [diff] [blame] | 157 | val = Thrift::Test::Numberz::SIX |
| 158 | ret = @client.testEnum(val) |
| 159 | |
| 160 | assert_equal(ret, 6) |
| 161 | assert_kind_of(Fixnum, ret) |
| 162 | end |
| 163 | |
| 164 | def test_typedef |
Nobuaki Sukegawa | 6823829 | 2015-09-21 23:28:22 +0900 | [diff] [blame] | 165 | p 'test_typedef' |
Kevin Clark | 4bd8916 | 2008-07-08 00:47:49 +0000 | [diff] [blame] | 166 | #UserId testTypedef(1: UserId thing), |
Roger Meier | a3570ac | 2014-06-10 22:16:14 +0200 | [diff] [blame] | 167 | assert_equal(@client.testTypedef(309858235082523), 309858235082523) |
| 168 | assert_kind_of(Fixnum, @client.testTypedef(309858235082523)) |
Kevin Clark | 4bd8916 | 2008-07-08 00:47:49 +0000 | [diff] [blame] | 169 | true |
| 170 | end |
| 171 | |
| 172 | def test_set |
Nobuaki Sukegawa | 6823829 | 2015-09-21 23:28:22 +0900 | [diff] [blame] | 173 | p 'test_set' |
Kevin Clark | 4bd8916 | 2008-07-08 00:47:49 +0000 | [diff] [blame] | 174 | val = Set.new([1,2,3]) |
| 175 | assert_equal(@client.testSet(val), val) |
| 176 | assert_kind_of(Set, @client.testSet(val)) |
| 177 | end |
| 178 | |
| 179 | def get_struct |
| 180 | Thrift::Test::Xtruct.new({'string_thing' => 'hi!', 'i32_thing' => 4 }) |
| 181 | end |
| 182 | |
| 183 | def test_struct |
Nobuaki Sukegawa | 6823829 | 2015-09-21 23:28:22 +0900 | [diff] [blame] | 184 | p 'test_struct' |
Kevin Clark | 4bd8916 | 2008-07-08 00:47:49 +0000 | [diff] [blame] | 185 | ret = @client.testStruct(get_struct) |
| 186 | |
Nobuaki Sukegawa | 6823829 | 2015-09-21 23:28:22 +0900 | [diff] [blame] | 187 | # TODO: not sure what unspecified "default" requiredness values should be |
| 188 | assert(ret.byte_thing == nil || ret.byte_thing == 0) |
| 189 | assert(ret.i64_thing == nil || ret.i64_thing == 0) |
| 190 | |
Kevin Clark | 4bd8916 | 2008-07-08 00:47:49 +0000 | [diff] [blame] | 191 | assert_equal(ret.string_thing, 'hi!') |
| 192 | assert_equal(ret.i32_thing, 4) |
| 193 | assert_kind_of(Thrift::Test::Xtruct, ret) |
| 194 | end |
| 195 | |
| 196 | def test_nest |
Nobuaki Sukegawa | 6823829 | 2015-09-21 23:28:22 +0900 | [diff] [blame] | 197 | p 'test_nest' |
Kevin Clark | 4bd8916 | 2008-07-08 00:47:49 +0000 | [diff] [blame] | 198 | struct2 = Thrift::Test::Xtruct2.new({'struct_thing' => get_struct, 'i32_thing' => 10}) |
| 199 | |
| 200 | ret = @client.testNest(struct2) |
| 201 | |
Nobuaki Sukegawa | 6823829 | 2015-09-21 23:28:22 +0900 | [diff] [blame] | 202 | # TODO: not sure what unspecified "default" requiredness values should be |
| 203 | assert(ret.struct_thing.byte_thing == nil || ret.struct_thing.byte_thing == 0) |
| 204 | assert(ret.struct_thing.i64_thing == nil || ret.struct_thing.i64_thing == 0) |
| 205 | |
Kevin Clark | 4bd8916 | 2008-07-08 00:47:49 +0000 | [diff] [blame] | 206 | assert_equal(ret.struct_thing.string_thing, 'hi!') |
| 207 | assert_equal(ret.struct_thing.i32_thing, 4) |
| 208 | assert_equal(ret.i32_thing, 10) |
| 209 | |
| 210 | assert_kind_of(Thrift::Test::Xtruct, ret.struct_thing) |
| 211 | assert_kind_of(Thrift::Test::Xtruct2, ret) |
| 212 | end |
| 213 | |
Roger Meier | a3570ac | 2014-06-10 22:16:14 +0200 | [diff] [blame] | 214 | def test_insanity |
Nobuaki Sukegawa | 6823829 | 2015-09-21 23:28:22 +0900 | [diff] [blame] | 215 | p 'test_insanity' |
Kevin Clark | 4bd8916 | 2008-07-08 00:47:49 +0000 | [diff] [blame] | 216 | insane = Thrift::Test::Insanity.new({ |
Nobuaki Sukegawa | 6823829 | 2015-09-21 23:28:22 +0900 | [diff] [blame] | 217 | 'userMap' => { |
| 218 | Thrift::Test::Numberz::FIVE => 5, |
| 219 | Thrift::Test::Numberz::EIGHT => 8, |
| 220 | }, |
| 221 | 'xtructs' => [ |
Kevin Clark | 4bd8916 | 2008-07-08 00:47:49 +0000 | [diff] [blame] | 222 | Thrift::Test::Xtruct.new({ |
Nobuaki Sukegawa | 6823829 | 2015-09-21 23:28:22 +0900 | [diff] [blame] | 223 | 'string_thing' => 'Goodbye4', |
| 224 | 'byte_thing' => 4, |
| 225 | 'i32_thing' => 4, |
| 226 | 'i64_thing' => 4, |
| 227 | }), |
| 228 | Thrift::Test::Xtruct.new({ |
| 229 | 'string_thing' => 'Hello2', |
| 230 | 'byte_thing' => 2, |
| 231 | 'i32_thing' => 2, |
| 232 | 'i64_thing' => 2, |
Kevin Clark | 4bd8916 | 2008-07-08 00:47:49 +0000 | [diff] [blame] | 233 | }) |
| 234 | ] |
| 235 | }) |
| 236 | |
| 237 | ret = @client.testInsanity(insane) |
| 238 | |
Nobuaki Sukegawa | 6823829 | 2015-09-21 23:28:22 +0900 | [diff] [blame] | 239 | assert_equal(insane, ret[1][2]) |
| 240 | assert_equal(insane, ret[1][3]) |
Kevin Clark | 4bd8916 | 2008-07-08 00:47:49 +0000 | [diff] [blame] | 241 | |
Nobuaki Sukegawa | 6823829 | 2015-09-21 23:28:22 +0900 | [diff] [blame] | 242 | assert(ret[2][6].userMap == nil || ret[2][6].userMap.length == 0) |
| 243 | assert(ret[2][6].xtructs == nil || ret[2][6].xtructs.length == 0) |
Kevin Clark | 4bd8916 | 2008-07-08 00:47:49 +0000 | [diff] [blame] | 244 | end |
| 245 | |
| 246 | def test_map_map |
Nobuaki Sukegawa | 6823829 | 2015-09-21 23:28:22 +0900 | [diff] [blame] | 247 | p 'test_map_map' |
Kevin Clark | 4bd8916 | 2008-07-08 00:47:49 +0000 | [diff] [blame] | 248 | ret = @client.testMapMap(4) |
| 249 | assert_kind_of(Hash, ret) |
Nobuaki Sukegawa | 6823829 | 2015-09-21 23:28:22 +0900 | [diff] [blame] | 250 | expected = { |
| 251 | -4 => { |
| 252 | -4 => -4, |
| 253 | -3 => -3, |
| 254 | -2 => -2, |
| 255 | -1 => -1, |
| 256 | }, |
| 257 | 4 => { |
| 258 | 4 => 4, |
| 259 | 3 => 3, |
| 260 | 2 => 2, |
| 261 | 1 => 1, |
| 262 | } |
| 263 | } |
| 264 | assert_equal(expected, ret) |
| 265 | end |
| 266 | |
| 267 | def test_multi |
| 268 | p 'test_multi' |
| 269 | ret = @client.testMulti(42, 4242, 424242, {1 => 'blah', 2 => 'thing'}, Thrift::Test::Numberz::EIGHT, 24) |
| 270 | expected = Thrift::Test::Xtruct.new({ |
| 271 | :string_thing => 'Hello2', |
| 272 | :byte_thing => 42, |
| 273 | :i32_thing => 4242, |
| 274 | :i64_thing => 424242 |
| 275 | }) |
| 276 | assert_equal(expected, ret) |
Kevin Clark | 4bd8916 | 2008-07-08 00:47:49 +0000 | [diff] [blame] | 277 | end |
| 278 | |
| 279 | def test_exception |
Nobuaki Sukegawa | 6823829 | 2015-09-21 23:28:22 +0900 | [diff] [blame] | 280 | p 'test_exception' |
Kevin Clark | 4bd8916 | 2008-07-08 00:47:49 +0000 | [diff] [blame] | 281 | assert_raise Thrift::Test::Xception do |
Roger Meier | a3570ac | 2014-06-10 22:16:14 +0200 | [diff] [blame] | 282 | @client.testException('Xception') |
Kevin Clark | 4bd8916 | 2008-07-08 00:47:49 +0000 | [diff] [blame] | 283 | end |
Nobuaki Sukegawa | 6823829 | 2015-09-21 23:28:22 +0900 | [diff] [blame] | 284 | begin |
| 285 | @client.testException('TException') |
| 286 | rescue => e |
| 287 | assert e.class.ancestors.include?(Thrift::Exception) |
| 288 | end |
| 289 | assert_nothing_raised do |
| 290 | @client.testException('test') |
| 291 | end |
Kevin Clark | 4bd8916 | 2008-07-08 00:47:49 +0000 | [diff] [blame] | 292 | end |
Roger Meier | a3570ac | 2014-06-10 22:16:14 +0200 | [diff] [blame] | 293 | |
| 294 | def test_multi_exception |
Nobuaki Sukegawa | 6823829 | 2015-09-21 23:28:22 +0900 | [diff] [blame] | 295 | p 'test_multi_exception' |
Roger Meier | a3570ac | 2014-06-10 22:16:14 +0200 | [diff] [blame] | 296 | assert_raise Thrift::Test::Xception do |
| 297 | @client.testMultiException("Xception", "test 1") |
| 298 | end |
| 299 | assert_raise Thrift::Test::Xception2 do |
| 300 | @client.testMultiException("Xception2", "test 2") |
| 301 | end |
| 302 | assert_equal( @client.testMultiException("Success", "test 3").string_thing, "test 3") |
| 303 | end |
| 304 | |
| 305 | def test_oneway |
Nobuaki Sukegawa | 6823829 | 2015-09-21 23:28:22 +0900 | [diff] [blame] | 306 | p 'test_oneway' |
Roger Meier | a3570ac | 2014-06-10 22:16:14 +0200 | [diff] [blame] | 307 | time1 = Time.now.to_f |
| 308 | @client.testOneway(3) |
| 309 | time2 = Time.now.to_f |
| 310 | assert_equal((time2-time1)*1000000<400, true) |
| 311 | end |
| 312 | |
Kevin Clark | 4bd8916 | 2008-07-08 00:47:49 +0000 | [diff] [blame] | 313 | end |
| 314 | |