Kevin Clark | 28580f4 | 2008-06-18 00:49:17 +0000 | [diff] [blame] | 1 | require File.join(File.dirname(__FILE__), '../test_helper') |
Mark Slee | 89f5716 | 2008-01-10 00:53:08 +0000 | [diff] [blame] | 2 | |
Kevin Clark | d639ac1 | 2008-06-18 01:04:18 +0000 | [diff] [blame] | 3 | require 'thrift' |
| 4 | require 'thrift/protocol/binaryprotocol' |
Mark Slee | 89f5716 | 2008-01-10 00:53:08 +0000 | [diff] [blame] | 5 | require 'ThriftTest' |
Mark Slee | 89f5716 | 2008-01-10 00:53:08 +0000 | [diff] [blame] | 6 | |
Kevin Clark | 28580f4 | 2008-06-18 00:49:17 +0000 | [diff] [blame] | 7 | class TestHandler |
| 8 | [:testString, :testByte, :testI32, :testI64, :testDouble, |
| 9 | :testStruct, :testMap, :testSet, :testList, :testNest, |
| 10 | :testEnum, :testTypedef].each do |meth| |
Mark Slee | 89f5716 | 2008-01-10 00:53:08 +0000 | [diff] [blame] | 11 | |
Kevin Clark | 28580f4 | 2008-06-18 00:49:17 +0000 | [diff] [blame] | 12 | define_method(meth) do |thing| |
| 13 | thing |
| 14 | end |
| 15 | |
| 16 | end |
| 17 | |
| 18 | def testInsanity(thing) |
| 19 | num, uid = thing.userMap.find { true } |
| 20 | return {uid => {num => thing}} |
| 21 | end |
| 22 | |
| 23 | def testMapMap(thing) |
| 24 | return {thing => {thing => thing}} |
| 25 | end |
| 26 | |
| 27 | def testEnum(thing) |
| 28 | return thing |
| 29 | end |
| 30 | |
| 31 | def testTypedef(thing) |
| 32 | return thing |
| 33 | end |
| 34 | |
| 35 | def testException(thing) |
| 36 | raise Thrift::Test::Xception, 'error' |
| 37 | end |
| 38 | |
| 39 | end |
Mark Slee | 89f5716 | 2008-01-10 00:53:08 +0000 | [diff] [blame] | 40 | class TestThrift < Test::Unit::TestCase |
| 41 | |
| 42 | @@INIT = nil |
| 43 | |
| 44 | def setup |
| 45 | if @@INIT.nil? |
| 46 | # Initialize the server |
| 47 | @handler = TestHandler.new() |
| 48 | @processor = Thrift::Test::ThriftTest::Processor.new(@handler) |
Kevin Clark | d639ac1 | 2008-06-18 01:04:18 +0000 | [diff] [blame] | 49 | @transport = Thrift::ServerSocket.new(9090) |
| 50 | @server = Thrift::ThreadedServer.new(@processor, @transport) |
Mark Slee | 89f5716 | 2008-01-10 00:53:08 +0000 | [diff] [blame] | 51 | |
| 52 | @thread = Thread.new { @server.serve } |
| 53 | |
| 54 | # And the Client |
Kevin Clark | d639ac1 | 2008-06-18 01:04:18 +0000 | [diff] [blame] | 55 | @socket = Thrift::Socket.new('localhost', 9090) |
| 56 | @protocol = Thrift::BinaryProtocol.new(@socket) |
Mark Slee | 89f5716 | 2008-01-10 00:53:08 +0000 | [diff] [blame] | 57 | @client = Thrift::Test::ThriftTest::Client.new(@protocol) |
| 58 | @socket.open |
| 59 | end |
| 60 | end |
| 61 | |
| 62 | def test_string |
| 63 | assert_equal(@client.testString('string'), 'string') |
| 64 | end |
| 65 | |
| 66 | def test_byte |
| 67 | val = 8 |
| 68 | assert_equal(@client.testByte(val), val) |
| 69 | assert_equal(@client.testByte(-val), -val) |
| 70 | end |
| 71 | |
| 72 | def test_i32 |
| 73 | val = 32 |
| 74 | assert_equal(@client.testI32(val), val) |
| 75 | assert_equal(@client.testI32(-val), -val) |
| 76 | end |
| 77 | |
| 78 | def test_i64 |
| 79 | val = 64 |
| 80 | assert_equal(@client.testI64(val), val) |
| 81 | assert_equal(@client.testI64(-val), -val) |
| 82 | end |
| 83 | |
| 84 | def test_double |
| 85 | val = 3.14 |
| 86 | assert_equal(@client.testDouble(val), val) |
| 87 | assert_equal(@client.testDouble(-val), -val) |
| 88 | assert_kind_of(Float, @client.testDouble(val)) |
| 89 | end |
| 90 | |
| 91 | def test_map |
| 92 | val = {1 => 1, 2 => 2, 3 => 3} |
| 93 | assert_equal(@client.testMap(val), val) |
| 94 | assert_kind_of(Hash, @client.testMap(val)) |
| 95 | end |
| 96 | |
| 97 | def test_list |
| 98 | val = [1,2,3,4,5] |
| 99 | assert_equal(@client.testList(val), val) |
| 100 | assert_kind_of(Array, @client.testList(val)) |
| 101 | end |
| 102 | |
| 103 | def test_enum |
| 104 | val = Thrift::Test::Numberz::SIX |
| 105 | ret = @client.testEnum(val) |
| 106 | |
| 107 | assert_equal(ret, 6) |
| 108 | assert_kind_of(Fixnum, ret) |
| 109 | end |
| 110 | |
| 111 | def test_typedef |
| 112 | #UserId testTypedef(1: UserId thing), |
| 113 | true |
| 114 | end |
| 115 | |
| 116 | def test_set |
Kevin Clark | 41c0a02 | 2008-06-18 01:09:28 +0000 | [diff] [blame] | 117 | val = Set.new([1, 2, 3]) |
| 118 | assert_equal(val, @client.testSet(val)) |
| 119 | assert_kind_of(Set, @client.testSet(val)) |
Mark Slee | 89f5716 | 2008-01-10 00:53:08 +0000 | [diff] [blame] | 120 | end |
| 121 | |
| 122 | def get_struct |
| 123 | Thrift::Test::Xtruct.new({'string_thing' => 'hi!', 'i32_thing' => 4 }) |
| 124 | end |
| 125 | |
| 126 | def test_struct |
| 127 | ret = @client.testStruct(get_struct) |
| 128 | |
| 129 | assert_nil(ret.byte_thing, nil) |
| 130 | assert_nil(ret.i64_thing, nil) |
| 131 | assert_equal(ret.string_thing, 'hi!') |
| 132 | assert_equal(ret.i32_thing, 4) |
| 133 | assert_kind_of(Thrift::Test::Xtruct, ret) |
| 134 | end |
| 135 | |
| 136 | def test_nest |
| 137 | struct2 = Thrift::Test::Xtruct2.new({'struct_thing' => get_struct, 'i32_thing' => 10}) |
| 138 | |
| 139 | ret = @client.testNest(struct2) |
| 140 | |
| 141 | assert_nil(ret.struct_thing.byte_thing, nil) |
| 142 | assert_nil(ret.struct_thing.i64_thing, nil) |
| 143 | assert_equal(ret.struct_thing.string_thing, 'hi!') |
| 144 | assert_equal(ret.struct_thing.i32_thing, 4) |
| 145 | assert_equal(ret.i32_thing, 10) |
| 146 | |
| 147 | assert_kind_of(Thrift::Test::Xtruct, ret.struct_thing) |
| 148 | assert_kind_of(Thrift::Test::Xtruct2, ret) |
| 149 | end |
| 150 | |
| 151 | def test_insane |
| 152 | insane = Thrift::Test::Insanity.new({ |
| 153 | 'userMap' => { Thrift::Test::Numberz::ONE => 44 }, |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 154 | 'xtructs' => [get_struct, |
Mark Slee | 89f5716 | 2008-01-10 00:53:08 +0000 | [diff] [blame] | 155 | Thrift::Test::Xtruct.new({ |
| 156 | 'string_thing' => 'hi again', |
| 157 | 'i32_thing' => 12 |
| 158 | }) |
| 159 | ] |
| 160 | }) |
| 161 | |
| 162 | ret = @client.testInsanity(insane) |
| 163 | |
| 164 | assert_not_nil(ret[44]) |
| 165 | assert_not_nil(ret[44][1]) |
| 166 | |
| 167 | struct = ret[44][1] |
| 168 | |
| 169 | assert_equal(struct.userMap[Thrift::Test::Numberz::ONE], 44) |
| 170 | assert_equal(struct.xtructs[1].string_thing, 'hi again') |
| 171 | assert_equal(struct.xtructs[1].i32_thing, 12) |
| 172 | |
| 173 | assert_kind_of(Hash, struct.userMap) |
| 174 | assert_kind_of(Array, struct.xtructs) |
| 175 | assert_kind_of(Thrift::Test::Insanity, struct) |
| 176 | end |
| 177 | |
| 178 | def test_map_map |
| 179 | ret = @client.testMapMap(4) |
| 180 | assert_kind_of(Hash, ret) |
| 181 | assert_equal(ret, { 4 => { 4 => 4}}) |
| 182 | end |
| 183 | |
| 184 | def test_exception |
| 185 | assert_raise Thrift::Test::Xception do |
| 186 | @client.testException('foo') |
| 187 | end |
| 188 | end |
| 189 | |
| 190 | def teardown |
| 191 | end |
| 192 | |
| 193 | end |