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