blob: f3450dbf69223b510e1f13846135a19401d8addc [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__) + '/..'
Kevin Clark4bd89162008-07-08 00:47:49 +000021
Roger Meierc95d5df2014-01-19 21:53:02 +010022require 'test_helper'
Kevin Clark4bd89162008-07-08 00:47:49 +000023require 'thrift'
Roger Meierc95d5df2014-01-19 21:53:02 +010024require 'thrift_test'
Kevin Clark4bd89162008-07-08 00:47:49 +000025
Roger Meiera3570ac2014-06-10 22:16:14 +020026$protocolType = "binary"
27$host = "localhost"
28$port = 9090
29$transport = "buffered"
30ARGV.each do|a|
31 if a == "--help"
32 puts "Allowed options:"
33 puts "\t -h [ --help ] \t produce help message"
34 puts "\t--host arg (=localhost) \t Host to connect"
35 puts "\t--port arg (=9090) \t Port number to listen"
36 puts "\t--protocol arg (=binary) \t protocol: binary, accel"
37 puts "\t--transport arg (=buffered) transport: buffered, framed, http"
38 exit
39 elsif a.start_with?("--host")
40 $host = a.split("=")[1]
41 elsif a.start_with?("--protocol")
42 $protocolType = a.split("=")[1]
43 elsif a.start_with?("--transport")
44 $transport = a.split("=")[1]
45 elsif a.start_with?("--port")
46 $port = a.split("=")[1].to_i
47 end
48end
49ARGV=[]
50
Kevin Clark4bd89162008-07-08 00:47:49 +000051class SimpleClientTest < Test::Unit::TestCase
Roger Meiera3570ac2014-06-10 22:16:14 +020052 def setup
Kevin Clark4bd89162008-07-08 00:47:49 +000053 unless @socket
Roger Meiera3570ac2014-06-10 22:16:14 +020054 @socket = Thrift::Socket.new($host, $port)
55 transportFactory = Thrift::BufferedTransport.new(@socket)
56 if $transport == "buffered"
57 transportFactory = Thrift::BufferedTransport.new(@socket)
58 elsif $transport == ""
59 transportFactory = Thrift::BufferedTransport.new(@socket)
60 elsif $transport == "framed"
61 transportFactory = Thrift::FramedTransport.new(@socket)
62 else
63 raise 'Unknown transport type'
64 end
65
66 if $protocolType == "binary"
67 @protocol = Thrift::BinaryProtocol.new(transportFactory)
68 elsif $protocolType == ""
69 @protocol = Thrift::BinaryProtocol.new(transportFactory)
70 elsif $protocolType == "compact"
71 @protocol = Thrift::CompactProtocol.new(transportFactory)
72 elsif $protocolType == "json"
73 @protocol = Thrift::JsonProtocol.new(transportFactory)
74 elsif $protocolType == "accel"
75 @protocol = Thrift::BinaryProtocolAccelerated.new(transportFactory)
76 else
77 raise 'Unknown protocol type'
78 end
Kevin Clark4bd89162008-07-08 00:47:49 +000079 @client = Thrift::Test::ThriftTest::Client.new(@protocol)
80 @socket.open
81 end
82 end
83
Roger Meiera3570ac2014-06-10 22:16:14 +020084 def test_void
85 @client.testVoid()
86 end
87
Kevin Clark4bd89162008-07-08 00:47:49 +000088 def test_string
89 assert_equal(@client.testString('string'), 'string')
90 end
91
92 def test_byte
93 val = 8
94 assert_equal(@client.testByte(val), val)
95 assert_equal(@client.testByte(-val), -val)
96 end
97
98 def test_i32
99 val = 32
100 assert_equal(@client.testI32(val), val)
101 assert_equal(@client.testI32(-val), -val)
102 end
103
104 def test_i64
105 val = 64
106 assert_equal(@client.testI64(val), val)
107 assert_equal(@client.testI64(-val), -val)
108 end
109
110 def test_double
111 val = 3.14
112 assert_equal(@client.testDouble(val), val)
113 assert_equal(@client.testDouble(-val), -val)
114 assert_kind_of(Float, @client.testDouble(val))
115 end
116
117 def test_map
118 val = {1 => 1, 2 => 2, 3 => 3}
119 assert_equal(@client.testMap(val), val)
120 assert_kind_of(Hash, @client.testMap(val))
121 end
122
123 def test_list
124 val = [1,2,3,4,5]
125 assert_equal(@client.testList(val), val)
126 assert_kind_of(Array, @client.testList(val))
127 end
128
129 def test_enum
130 val = Thrift::Test::Numberz::SIX
131 ret = @client.testEnum(val)
132
133 assert_equal(ret, 6)
134 assert_kind_of(Fixnum, ret)
135 end
136
137 def test_typedef
138 #UserId testTypedef(1: UserId thing),
Roger Meiera3570ac2014-06-10 22:16:14 +0200139 assert_equal(@client.testTypedef(309858235082523), 309858235082523)
140 assert_kind_of(Fixnum, @client.testTypedef(309858235082523))
Kevin Clark4bd89162008-07-08 00:47:49 +0000141 true
142 end
143
144 def test_set
145 val = Set.new([1,2,3])
146 assert_equal(@client.testSet(val), val)
147 assert_kind_of(Set, @client.testSet(val))
148 end
149
150 def get_struct
151 Thrift::Test::Xtruct.new({'string_thing' => 'hi!', 'i32_thing' => 4 })
152 end
153
154 def test_struct
155 ret = @client.testStruct(get_struct)
156
157 assert_nil(ret.byte_thing, nil)
158 assert_nil(ret.i64_thing, nil)
159 assert_equal(ret.string_thing, 'hi!')
160 assert_equal(ret.i32_thing, 4)
161 assert_kind_of(Thrift::Test::Xtruct, ret)
162 end
163
164 def test_nest
165 struct2 = Thrift::Test::Xtruct2.new({'struct_thing' => get_struct, 'i32_thing' => 10})
166
167 ret = @client.testNest(struct2)
168
169 assert_nil(ret.struct_thing.byte_thing, nil)
170 assert_nil(ret.struct_thing.i64_thing, nil)
171 assert_equal(ret.struct_thing.string_thing, 'hi!')
172 assert_equal(ret.struct_thing.i32_thing, 4)
173 assert_equal(ret.i32_thing, 10)
174
175 assert_kind_of(Thrift::Test::Xtruct, ret.struct_thing)
176 assert_kind_of(Thrift::Test::Xtruct2, ret)
177 end
178
Roger Meiera3570ac2014-06-10 22:16:14 +0200179 def test_insanity
Kevin Clark4bd89162008-07-08 00:47:49 +0000180 insane = Thrift::Test::Insanity.new({
181 'userMap' => { Thrift::Test::Numberz::ONE => 44 },
182 'xtructs' => [get_struct,
183 Thrift::Test::Xtruct.new({
184 'string_thing' => 'hi again',
185 'i32_thing' => 12
186 })
187 ]
188 })
189
190 ret = @client.testInsanity(insane)
191
192 assert_not_nil(ret[44])
193 assert_not_nil(ret[44][1])
194
195 struct = ret[44][1]
196
197 assert_equal(struct.userMap[Thrift::Test::Numberz::ONE], 44)
198 assert_equal(struct.xtructs[1].string_thing, 'hi again')
199 assert_equal(struct.xtructs[1].i32_thing, 12)
200
201 assert_kind_of(Hash, struct.userMap)
202 assert_kind_of(Array, struct.xtructs)
203 assert_kind_of(Thrift::Test::Insanity, struct)
204 end
205
206 def test_map_map
207 ret = @client.testMapMap(4)
208 assert_kind_of(Hash, ret)
209 assert_equal(ret, { 4 => { 4 => 4}})
210 end
211
212 def test_exception
213 assert_raise Thrift::Test::Xception do
Roger Meiera3570ac2014-06-10 22:16:14 +0200214 @client.testException('Xception')
Kevin Clark4bd89162008-07-08 00:47:49 +0000215 end
Roger Meiera3570ac2014-06-10 22:16:14 +0200216# assert_raise Thrift::TException do
217# @client.testException('TException')
218# end
219 assert_equal( @client.testException('test'), "test")
Kevin Clark4bd89162008-07-08 00:47:49 +0000220 end
Roger Meiera3570ac2014-06-10 22:16:14 +0200221
222 def test_multi_exception
223 assert_raise Thrift::Test::Xception do
224 @client.testMultiException("Xception", "test 1")
225 end
226 assert_raise Thrift::Test::Xception2 do
227 @client.testMultiException("Xception2", "test 2")
228 end
229 assert_equal( @client.testMultiException("Success", "test 3").string_thing, "test 3")
230 end
231
232 def test_oneway
233 time1 = Time.now.to_f
234 @client.testOneway(3)
235 time2 = Time.now.to_f
236 assert_equal((time2-time1)*1000000<400, true)
237 end
238
Kevin Clark4bd89162008-07-08 00:47:49 +0000239end
240