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) |
| 57 | transportFactory = Thrift::BufferedTransport.new(@socket) |
| 58 | if $transport == "buffered" |
| 59 | transportFactory = Thrift::BufferedTransport.new(@socket) |
| 60 | elsif $transport == "" |
| 61 | transportFactory = Thrift::BufferedTransport.new(@socket) |
| 62 | elsif $transport == "framed" |
| 63 | transportFactory = Thrift::FramedTransport.new(@socket) |
| 64 | else |
| 65 | raise 'Unknown transport type' |
| 66 | end |
| 67 | |
| 68 | if $protocolType == "binary" |
| 69 | @protocol = Thrift::BinaryProtocol.new(transportFactory) |
| 70 | elsif $protocolType == "" |
| 71 | @protocol = Thrift::BinaryProtocol.new(transportFactory) |
| 72 | elsif $protocolType == "compact" |
| 73 | @protocol = Thrift::CompactProtocol.new(transportFactory) |
| 74 | elsif $protocolType == "json" |
| 75 | @protocol = Thrift::JsonProtocol.new(transportFactory) |
| 76 | elsif $protocolType == "accel" |
| 77 | @protocol = Thrift::BinaryProtocolAccelerated.new(transportFactory) |
| 78 | else |
| 79 | raise 'Unknown protocol type' |
| 80 | end |
Kevin Clark | 4bd8916 | 2008-07-08 00:47:49 +0000 | [diff] [blame] | 81 | @client = Thrift::Test::ThriftTest::Client.new(@protocol) |
| 82 | @socket.open |
| 83 | end |
| 84 | end |
| 85 | |
Roger Meier | a3570ac | 2014-06-10 22:16:14 +0200 | [diff] [blame] | 86 | def test_void |
| 87 | @client.testVoid() |
| 88 | end |
| 89 | |
Kevin Clark | 4bd8916 | 2008-07-08 00:47:49 +0000 | [diff] [blame] | 90 | def test_string |
| 91 | assert_equal(@client.testString('string'), 'string') |
| 92 | end |
| 93 | |
| 94 | def test_byte |
| 95 | val = 8 |
| 96 | assert_equal(@client.testByte(val), val) |
| 97 | assert_equal(@client.testByte(-val), -val) |
| 98 | end |
| 99 | |
| 100 | def test_i32 |
| 101 | val = 32 |
| 102 | assert_equal(@client.testI32(val), val) |
| 103 | assert_equal(@client.testI32(-val), -val) |
| 104 | end |
| 105 | |
| 106 | def test_i64 |
| 107 | val = 64 |
| 108 | assert_equal(@client.testI64(val), val) |
| 109 | assert_equal(@client.testI64(-val), -val) |
| 110 | end |
| 111 | |
| 112 | def test_double |
| 113 | val = 3.14 |
| 114 | assert_equal(@client.testDouble(val), val) |
| 115 | assert_equal(@client.testDouble(-val), -val) |
| 116 | assert_kind_of(Float, @client.testDouble(val)) |
| 117 | end |
| 118 | |
| 119 | def test_map |
| 120 | val = {1 => 1, 2 => 2, 3 => 3} |
| 121 | assert_equal(@client.testMap(val), val) |
| 122 | assert_kind_of(Hash, @client.testMap(val)) |
| 123 | end |
| 124 | |
| 125 | def test_list |
| 126 | val = [1,2,3,4,5] |
| 127 | assert_equal(@client.testList(val), val) |
| 128 | assert_kind_of(Array, @client.testList(val)) |
| 129 | end |
| 130 | |
| 131 | def test_enum |
| 132 | val = Thrift::Test::Numberz::SIX |
| 133 | ret = @client.testEnum(val) |
| 134 | |
| 135 | assert_equal(ret, 6) |
| 136 | assert_kind_of(Fixnum, ret) |
| 137 | end |
| 138 | |
| 139 | def test_typedef |
| 140 | #UserId testTypedef(1: UserId thing), |
Roger Meier | a3570ac | 2014-06-10 22:16:14 +0200 | [diff] [blame] | 141 | assert_equal(@client.testTypedef(309858235082523), 309858235082523) |
| 142 | assert_kind_of(Fixnum, @client.testTypedef(309858235082523)) |
Kevin Clark | 4bd8916 | 2008-07-08 00:47:49 +0000 | [diff] [blame] | 143 | true |
| 144 | end |
| 145 | |
| 146 | def test_set |
| 147 | val = Set.new([1,2,3]) |
| 148 | assert_equal(@client.testSet(val), val) |
| 149 | assert_kind_of(Set, @client.testSet(val)) |
| 150 | end |
| 151 | |
| 152 | def get_struct |
| 153 | Thrift::Test::Xtruct.new({'string_thing' => 'hi!', 'i32_thing' => 4 }) |
| 154 | end |
| 155 | |
| 156 | def test_struct |
| 157 | ret = @client.testStruct(get_struct) |
| 158 | |
| 159 | assert_nil(ret.byte_thing, nil) |
| 160 | assert_nil(ret.i64_thing, nil) |
| 161 | assert_equal(ret.string_thing, 'hi!') |
| 162 | assert_equal(ret.i32_thing, 4) |
| 163 | assert_kind_of(Thrift::Test::Xtruct, ret) |
| 164 | end |
| 165 | |
| 166 | def test_nest |
| 167 | struct2 = Thrift::Test::Xtruct2.new({'struct_thing' => get_struct, 'i32_thing' => 10}) |
| 168 | |
| 169 | ret = @client.testNest(struct2) |
| 170 | |
| 171 | assert_nil(ret.struct_thing.byte_thing, nil) |
| 172 | assert_nil(ret.struct_thing.i64_thing, nil) |
| 173 | assert_equal(ret.struct_thing.string_thing, 'hi!') |
| 174 | assert_equal(ret.struct_thing.i32_thing, 4) |
| 175 | assert_equal(ret.i32_thing, 10) |
| 176 | |
| 177 | assert_kind_of(Thrift::Test::Xtruct, ret.struct_thing) |
| 178 | assert_kind_of(Thrift::Test::Xtruct2, ret) |
| 179 | end |
| 180 | |
Roger Meier | a3570ac | 2014-06-10 22:16:14 +0200 | [diff] [blame] | 181 | def test_insanity |
Kevin Clark | 4bd8916 | 2008-07-08 00:47:49 +0000 | [diff] [blame] | 182 | insane = Thrift::Test::Insanity.new({ |
| 183 | 'userMap' => { Thrift::Test::Numberz::ONE => 44 }, |
| 184 | 'xtructs' => [get_struct, |
| 185 | Thrift::Test::Xtruct.new({ |
| 186 | 'string_thing' => 'hi again', |
| 187 | 'i32_thing' => 12 |
| 188 | }) |
| 189 | ] |
| 190 | }) |
| 191 | |
| 192 | ret = @client.testInsanity(insane) |
| 193 | |
| 194 | assert_not_nil(ret[44]) |
| 195 | assert_not_nil(ret[44][1]) |
| 196 | |
| 197 | struct = ret[44][1] |
| 198 | |
| 199 | assert_equal(struct.userMap[Thrift::Test::Numberz::ONE], 44) |
| 200 | assert_equal(struct.xtructs[1].string_thing, 'hi again') |
| 201 | assert_equal(struct.xtructs[1].i32_thing, 12) |
| 202 | |
| 203 | assert_kind_of(Hash, struct.userMap) |
| 204 | assert_kind_of(Array, struct.xtructs) |
| 205 | assert_kind_of(Thrift::Test::Insanity, struct) |
| 206 | end |
| 207 | |
| 208 | def test_map_map |
| 209 | ret = @client.testMapMap(4) |
| 210 | assert_kind_of(Hash, ret) |
| 211 | assert_equal(ret, { 4 => { 4 => 4}}) |
| 212 | end |
| 213 | |
| 214 | def test_exception |
| 215 | assert_raise Thrift::Test::Xception do |
Roger Meier | a3570ac | 2014-06-10 22:16:14 +0200 | [diff] [blame] | 216 | @client.testException('Xception') |
Kevin Clark | 4bd8916 | 2008-07-08 00:47:49 +0000 | [diff] [blame] | 217 | end |
Roger Meier | a3570ac | 2014-06-10 22:16:14 +0200 | [diff] [blame] | 218 | # assert_raise Thrift::TException do |
| 219 | # @client.testException('TException') |
| 220 | # end |
| 221 | assert_equal( @client.testException('test'), "test") |
Kevin Clark | 4bd8916 | 2008-07-08 00:47:49 +0000 | [diff] [blame] | 222 | end |
Roger Meier | a3570ac | 2014-06-10 22:16:14 +0200 | [diff] [blame] | 223 | |
| 224 | def test_multi_exception |
| 225 | assert_raise Thrift::Test::Xception do |
| 226 | @client.testMultiException("Xception", "test 1") |
| 227 | end |
| 228 | assert_raise Thrift::Test::Xception2 do |
| 229 | @client.testMultiException("Xception2", "test 2") |
| 230 | end |
| 231 | assert_equal( @client.testMultiException("Success", "test 3").string_thing, "test 3") |
| 232 | end |
| 233 | |
| 234 | def test_oneway |
| 235 | time1 = Time.now.to_f |
| 236 | @client.testOneway(3) |
| 237 | time2 = Time.now.to_f |
| 238 | assert_equal((time2-time1)*1000000<400, true) |
| 239 | end |
| 240 | |
Kevin Clark | 4bd8916 | 2008-07-08 00:47:49 +0000 | [diff] [blame] | 241 | end |
| 242 | |