blob: c0561391d1e835d812d4d95149aa127fae7b4ec4 [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001#
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 Meierc95d5df2014-01-19 21:53:02 +010020$:.push File.dirname(__FILE__) + '/..'
Mark Slee89f57162008-01-10 00:53:08 +000021
Roger Meierc95d5df2014-01-19 21:53:02 +010022require 'test_helper'
Kevin Clarkd639ac12008-06-18 01:04:18 +000023require 'thrift'
Roger Meierc95d5df2014-01-19 21:53:02 +010024require 'thrift_test'
Mark Slee89f57162008-01-10 00:53:08 +000025
Kevin Clark28580f42008-06-18 00:49:17 +000026class TestHandler
27 [:testString, :testByte, :testI32, :testI64, :testDouble,
28 :testStruct, :testMap, :testSet, :testList, :testNest,
29 :testEnum, :testTypedef].each do |meth|
Mark Slee89f57162008-01-10 00:53:08 +000030
Kevin Clark28580f42008-06-18 00:49:17 +000031 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 Clark45671682008-06-18 01:14:17 +000055 raise Thrift::Test::Xception, :message => 'error'
Kevin Clark28580f42008-06-18 00:49:17 +000056 end
57
58end
Mark Slee89f57162008-01-10 00:53:08 +000059class 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 Clarkd639ac12008-06-18 01:04:18 +000068 @transport = Thrift::ServerSocket.new(9090)
69 @server = Thrift::ThreadedServer.new(@processor, @transport)
Mark Slee89f57162008-01-10 00:53:08 +000070
71 @thread = Thread.new { @server.serve }
72
73 # And the Client
Kevin Clarkd639ac12008-06-18 01:04:18 +000074 @socket = Thrift::Socket.new('localhost', 9090)
75 @protocol = Thrift::BinaryProtocol.new(@socket)
Mark Slee89f57162008-01-10 00:53:08 +000076 @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 Clark41c0a022008-06-18 01:09:28 +0000136 val = Set.new([1, 2, 3])
137 assert_equal(val, @client.testSet(val))
138 assert_kind_of(Set, @client.testSet(val))
Mark Slee89f57162008-01-10 00:53:08 +0000139 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 Reiss0c90f6f2008-02-06 22:18:40 +0000173 'xtructs' => [get_struct,
Mark Slee89f57162008-01-10 00:53:08 +0000174 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
212end