David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 1 | # |
| 2 | # Licensed to the Apache Software Foundation (ASF) under one |
| 3 | # or more contributor license agreements. See the NOTICE file |
| 4 | # distributed with this work for additional information |
| 5 | # regarding copyright ownership. The ASF licenses this file |
| 6 | # to you under the Apache License, Version 2.0 (the |
| 7 | # "License"); you may not use this file except in compliance |
| 8 | # with the License. You may obtain a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, |
| 13 | # software distributed under the License is distributed on an |
| 14 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | # KIND, either express or implied. See the License for the |
| 16 | # specific language governing permissions and limitations |
| 17 | # under the License. |
| 18 | # |
| 19 | |
Roger Meier | c95d5df | 2014-01-19 21:53:02 +0100 | [diff] [blame] | 20 | $:.push File.dirname(__FILE__) + '/..' |
Mark Slee | 89f5716 | 2008-01-10 00:53:08 +0000 | [diff] [blame] | 21 | |
Roger Meier | c95d5df | 2014-01-19 21:53:02 +0100 | [diff] [blame] | 22 | require 'test_helper' |
Kevin Clark | d639ac1 | 2008-06-18 01:04:18 +0000 | [diff] [blame] | 23 | require 'thrift' |
Roger Meier | c95d5df | 2014-01-19 21:53:02 +0100 | [diff] [blame] | 24 | require 'thrift_test' |
Mark Slee | 89f5716 | 2008-01-10 00:53:08 +0000 | [diff] [blame] | 25 | |
Kevin Clark | 28580f4 | 2008-06-18 00:49:17 +0000 | [diff] [blame] | 26 | class TestHandler |
| 27 | [:testString, :testByte, :testI32, :testI64, :testDouble, |
| 28 | :testStruct, :testMap, :testSet, :testList, :testNest, |
| 29 | :testEnum, :testTypedef].each do |meth| |
Mark Slee | 89f5716 | 2008-01-10 00:53:08 +0000 | [diff] [blame] | 30 | |
Kevin Clark | 28580f4 | 2008-06-18 00:49:17 +0000 | [diff] [blame] | 31 | define_method(meth) do |thing| |
| 32 | thing |
| 33 | end |
| 34 | |
| 35 | end |
| 36 | |
| 37 | def testInsanity(thing) |
| 38 | num, uid = thing.userMap.find { true } |
| 39 | return {uid => {num => thing}} |
| 40 | end |
| 41 | |
| 42 | def testMapMap(thing) |
| 43 | return {thing => {thing => thing}} |
| 44 | end |
| 45 | |
| 46 | def testEnum(thing) |
| 47 | return thing |
| 48 | end |
| 49 | |
| 50 | def testTypedef(thing) |
| 51 | return thing |
| 52 | end |
| 53 | |
| 54 | def testException(thing) |
Kevin Clark | 4567168 | 2008-06-18 01:14:17 +0000 | [diff] [blame] | 55 | raise Thrift::Test::Xception, :message => 'error' |
Kevin Clark | 28580f4 | 2008-06-18 00:49:17 +0000 | [diff] [blame] | 56 | end |
| 57 | |
| 58 | end |
Mark Slee | 89f5716 | 2008-01-10 00:53:08 +0000 | [diff] [blame] | 59 | class TestThrift < Test::Unit::TestCase |
| 60 | |
| 61 | @@INIT = nil |
| 62 | |
| 63 | def setup |
| 64 | if @@INIT.nil? |
| 65 | # Initialize the server |
| 66 | @handler = TestHandler.new() |
| 67 | @processor = Thrift::Test::ThriftTest::Processor.new(@handler) |
Kevin Clark | d639ac1 | 2008-06-18 01:04:18 +0000 | [diff] [blame] | 68 | @transport = Thrift::ServerSocket.new(9090) |
| 69 | @server = Thrift::ThreadedServer.new(@processor, @transport) |
Mark Slee | 89f5716 | 2008-01-10 00:53:08 +0000 | [diff] [blame] | 70 | |
| 71 | @thread = Thread.new { @server.serve } |
| 72 | |
| 73 | # And the Client |
Kevin Clark | d639ac1 | 2008-06-18 01:04:18 +0000 | [diff] [blame] | 74 | @socket = Thrift::Socket.new('localhost', 9090) |
| 75 | @protocol = Thrift::BinaryProtocol.new(@socket) |
Mark Slee | 89f5716 | 2008-01-10 00:53:08 +0000 | [diff] [blame] | 76 | @client = Thrift::Test::ThriftTest::Client.new(@protocol) |
| 77 | @socket.open |
| 78 | end |
| 79 | end |
| 80 | |
| 81 | def test_string |
| 82 | assert_equal(@client.testString('string'), 'string') |
| 83 | end |
| 84 | |
| 85 | def test_byte |
| 86 | val = 8 |
| 87 | assert_equal(@client.testByte(val), val) |
| 88 | assert_equal(@client.testByte(-val), -val) |
| 89 | end |
| 90 | |
| 91 | def test_i32 |
| 92 | val = 32 |
| 93 | assert_equal(@client.testI32(val), val) |
| 94 | assert_equal(@client.testI32(-val), -val) |
| 95 | end |
| 96 | |
| 97 | def test_i64 |
| 98 | val = 64 |
| 99 | assert_equal(@client.testI64(val), val) |
| 100 | assert_equal(@client.testI64(-val), -val) |
| 101 | end |
| 102 | |
| 103 | def test_double |
| 104 | val = 3.14 |
| 105 | assert_equal(@client.testDouble(val), val) |
| 106 | assert_equal(@client.testDouble(-val), -val) |
| 107 | assert_kind_of(Float, @client.testDouble(val)) |
| 108 | end |
| 109 | |
| 110 | def test_map |
| 111 | val = {1 => 1, 2 => 2, 3 => 3} |
| 112 | assert_equal(@client.testMap(val), val) |
| 113 | assert_kind_of(Hash, @client.testMap(val)) |
| 114 | end |
| 115 | |
| 116 | def test_list |
| 117 | val = [1,2,3,4,5] |
| 118 | assert_equal(@client.testList(val), val) |
| 119 | assert_kind_of(Array, @client.testList(val)) |
| 120 | end |
| 121 | |
| 122 | def test_enum |
| 123 | val = Thrift::Test::Numberz::SIX |
| 124 | ret = @client.testEnum(val) |
| 125 | |
| 126 | assert_equal(ret, 6) |
| 127 | assert_kind_of(Fixnum, ret) |
| 128 | end |
| 129 | |
| 130 | def test_typedef |
| 131 | #UserId testTypedef(1: UserId thing), |
| 132 | true |
| 133 | end |
| 134 | |
| 135 | def test_set |
Kevin Clark | 41c0a02 | 2008-06-18 01:09:28 +0000 | [diff] [blame] | 136 | val = Set.new([1, 2, 3]) |
| 137 | assert_equal(val, @client.testSet(val)) |
| 138 | assert_kind_of(Set, @client.testSet(val)) |
Mark Slee | 89f5716 | 2008-01-10 00:53:08 +0000 | [diff] [blame] | 139 | end |
| 140 | |
| 141 | def get_struct |
| 142 | Thrift::Test::Xtruct.new({'string_thing' => 'hi!', 'i32_thing' => 4 }) |
| 143 | end |
| 144 | |
| 145 | def test_struct |
| 146 | ret = @client.testStruct(get_struct) |
| 147 | |
| 148 | assert_nil(ret.byte_thing, nil) |
| 149 | assert_nil(ret.i64_thing, nil) |
| 150 | assert_equal(ret.string_thing, 'hi!') |
| 151 | assert_equal(ret.i32_thing, 4) |
| 152 | assert_kind_of(Thrift::Test::Xtruct, ret) |
| 153 | end |
| 154 | |
| 155 | def test_nest |
| 156 | struct2 = Thrift::Test::Xtruct2.new({'struct_thing' => get_struct, 'i32_thing' => 10}) |
| 157 | |
| 158 | ret = @client.testNest(struct2) |
| 159 | |
| 160 | assert_nil(ret.struct_thing.byte_thing, nil) |
| 161 | assert_nil(ret.struct_thing.i64_thing, nil) |
| 162 | assert_equal(ret.struct_thing.string_thing, 'hi!') |
| 163 | assert_equal(ret.struct_thing.i32_thing, 4) |
| 164 | assert_equal(ret.i32_thing, 10) |
| 165 | |
| 166 | assert_kind_of(Thrift::Test::Xtruct, ret.struct_thing) |
| 167 | assert_kind_of(Thrift::Test::Xtruct2, ret) |
| 168 | end |
| 169 | |
| 170 | def test_insane |
| 171 | insane = Thrift::Test::Insanity.new({ |
| 172 | 'userMap' => { Thrift::Test::Numberz::ONE => 44 }, |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 173 | 'xtructs' => [get_struct, |
Mark Slee | 89f5716 | 2008-01-10 00:53:08 +0000 | [diff] [blame] | 174 | Thrift::Test::Xtruct.new({ |
| 175 | 'string_thing' => 'hi again', |
| 176 | 'i32_thing' => 12 |
| 177 | }) |
| 178 | ] |
| 179 | }) |
| 180 | |
| 181 | ret = @client.testInsanity(insane) |
| 182 | |
| 183 | assert_not_nil(ret[44]) |
| 184 | assert_not_nil(ret[44][1]) |
| 185 | |
| 186 | struct = ret[44][1] |
| 187 | |
| 188 | assert_equal(struct.userMap[Thrift::Test::Numberz::ONE], 44) |
| 189 | assert_equal(struct.xtructs[1].string_thing, 'hi again') |
| 190 | assert_equal(struct.xtructs[1].i32_thing, 12) |
| 191 | |
| 192 | assert_kind_of(Hash, struct.userMap) |
| 193 | assert_kind_of(Array, struct.xtructs) |
| 194 | assert_kind_of(Thrift::Test::Insanity, struct) |
| 195 | end |
| 196 | |
| 197 | def test_map_map |
| 198 | ret = @client.testMapMap(4) |
| 199 | assert_kind_of(Hash, ret) |
| 200 | assert_equal(ret, { 4 => { 4 => 4}}) |
| 201 | end |
| 202 | |
| 203 | def test_exception |
| 204 | assert_raise Thrift::Test::Xception do |
| 205 | @client.testException('foo') |
| 206 | end |
| 207 | end |
| 208 | |
| 209 | def teardown |
| 210 | end |
| 211 | |
| 212 | end |