Kevin Clark | 4bd8916 | 2008-07-08 00:47:49 +0000 | [diff] [blame] | 1 | require File.join(File.dirname(__FILE__), '../test_helper') |
| 2 | |
| 3 | require 'thrift' |
| 4 | require 'thrift/protocol/binaryprotocolaccelerated' |
| 5 | require 'ThriftTest' |
| 6 | |
| 7 | class AcceleratedBufferedClientTest < Test::Unit::TestCase |
| 8 | def setup |
| 9 | unless @socket |
| 10 | @socket = Thrift::Socket.new('localhost', 9090) |
| 11 | @protocol = Thrift::BinaryProtocolAccelerated.new(Thrift::BufferedTransport.new(@socket)) |
| 12 | @client = Thrift::Test::ThriftTest::Client.new(@protocol) |
| 13 | @socket.open |
| 14 | end |
| 15 | end |
| 16 | |
| 17 | def test_string |
| 18 | assert_equal(@client.testString('string'), 'string') |
| 19 | end |
| 20 | |
| 21 | def test_byte |
| 22 | val = 8 |
| 23 | assert_equal(@client.testByte(val), val) |
| 24 | assert_equal(@client.testByte(-val), -val) |
| 25 | end |
| 26 | |
| 27 | def test_i32 |
| 28 | val = 32 |
| 29 | assert_equal(@client.testI32(val), val) |
| 30 | assert_equal(@client.testI32(-val), -val) |
| 31 | end |
| 32 | |
| 33 | def test_i64 |
| 34 | val = 64 |
| 35 | assert_equal(@client.testI64(val), val) |
| 36 | assert_equal(@client.testI64(-val), -val) |
| 37 | end |
| 38 | |
| 39 | def test_double |
| 40 | val = 3.14 |
| 41 | assert_equal(@client.testDouble(val), val) |
| 42 | assert_equal(@client.testDouble(-val), -val) |
| 43 | assert_kind_of(Float, @client.testDouble(val)) |
| 44 | end |
| 45 | |
| 46 | def test_map |
| 47 | val = {1 => 1, 2 => 2, 3 => 3} |
| 48 | assert_equal(@client.testMap(val), val) |
| 49 | assert_kind_of(Hash, @client.testMap(val)) |
| 50 | end |
| 51 | |
| 52 | def test_list |
| 53 | val = [1,2,3,4,5] |
| 54 | assert_equal(@client.testList(val), val) |
| 55 | assert_kind_of(Array, @client.testList(val)) |
| 56 | end |
| 57 | |
| 58 | def test_enum |
| 59 | val = Thrift::Test::Numberz::SIX |
| 60 | ret = @client.testEnum(val) |
| 61 | |
| 62 | assert_equal(ret, 6) |
| 63 | assert_kind_of(Fixnum, ret) |
| 64 | end |
| 65 | |
| 66 | def test_typedef |
| 67 | #UserId testTypedef(1: UserId thing), |
| 68 | true |
| 69 | end |
| 70 | |
| 71 | def test_set |
| 72 | val = Set.new([1,2,3]) |
| 73 | assert_equal(@client.testSet(val), val) |
| 74 | assert_kind_of(Set, @client.testSet(val)) |
| 75 | end |
| 76 | |
| 77 | def get_struct |
| 78 | Thrift::Test::Xtruct.new({'string_thing' => 'hi!', 'i32_thing' => 4 }) |
| 79 | end |
| 80 | |
| 81 | def test_struct |
| 82 | ret = @client.testStruct(get_struct) |
| 83 | |
| 84 | assert_nil(ret.byte_thing, nil) |
| 85 | assert_nil(ret.i64_thing, nil) |
| 86 | assert_equal(ret.string_thing, 'hi!') |
| 87 | assert_equal(ret.i32_thing, 4) |
| 88 | assert_kind_of(Thrift::Test::Xtruct, ret) |
| 89 | end |
| 90 | |
| 91 | def test_nest |
| 92 | struct2 = Thrift::Test::Xtruct2.new({'struct_thing' => get_struct, 'i32_thing' => 10}) |
| 93 | |
| 94 | ret = @client.testNest(struct2) |
| 95 | |
| 96 | assert_nil(ret.struct_thing.byte_thing, nil) |
| 97 | assert_nil(ret.struct_thing.i64_thing, nil) |
| 98 | assert_equal(ret.struct_thing.string_thing, 'hi!') |
| 99 | assert_equal(ret.struct_thing.i32_thing, 4) |
| 100 | assert_equal(ret.i32_thing, 10) |
| 101 | |
| 102 | assert_kind_of(Thrift::Test::Xtruct, ret.struct_thing) |
| 103 | assert_kind_of(Thrift::Test::Xtruct2, ret) |
| 104 | end |
| 105 | |
| 106 | def test_insane |
| 107 | insane = Thrift::Test::Insanity.new({ |
| 108 | 'userMap' => { Thrift::Test::Numberz::ONE => 44 }, |
| 109 | 'xtructs' => [get_struct, |
| 110 | Thrift::Test::Xtruct.new({ |
| 111 | 'string_thing' => 'hi again', |
| 112 | 'i32_thing' => 12 |
| 113 | }) |
| 114 | ] |
| 115 | }) |
| 116 | |
| 117 | ret = @client.testInsanity(insane) |
| 118 | |
| 119 | assert_not_nil(ret[44]) |
| 120 | assert_not_nil(ret[44][1]) |
| 121 | |
| 122 | struct = ret[44][1] |
| 123 | |
| 124 | assert_equal(struct.userMap[Thrift::Test::Numberz::ONE], 44) |
| 125 | assert_equal(struct.xtructs[1].string_thing, 'hi again') |
| 126 | assert_equal(struct.xtructs[1].i32_thing, 12) |
| 127 | |
| 128 | assert_kind_of(Hash, struct.userMap) |
| 129 | assert_kind_of(Array, struct.xtructs) |
| 130 | assert_kind_of(Thrift::Test::Insanity, struct) |
| 131 | end |
| 132 | |
| 133 | def test_map_map |
| 134 | ret = @client.testMapMap(4) |
| 135 | assert_kind_of(Hash, ret) |
| 136 | assert_equal(ret, { 4 => { 4 => 4}}) |
| 137 | end |
| 138 | |
| 139 | def test_exception |
| 140 | assert_raise Thrift::Test::Xception do |
| 141 | @client.testException('foo') |
| 142 | end |
| 143 | end |
| 144 | end |
| 145 | |