blob: 3659ccb74647b130c8fa7d341b28b47e0b7ee2d7 [file] [log] [blame]
Roger Meier32f39822014-06-18 22:43:17 +02001#!/usr/bin/env ruby
2
David Reissea2cba82009-03-30 21:35:00 +00003#
4# Licensed to the Apache Software Foundation (ASF) under one
5# or more contributor license agreements. See the NOTICE file
6# distributed with this work for additional information
7# regarding copyright ownership. The ASF licenses this file
8# to you under the Apache License, Version 2.0 (the
9# "License"); you may not use this file except in compliance
10# with the License. You may obtain a copy of the License at
11#
12# http://www.apache.org/licenses/LICENSE-2.0
13#
14# Unless required by applicable law or agreed to in writing,
15# software distributed under the License is distributed on an
16# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17# KIND, either express or implied. See the License for the
18# specific language governing permissions and limitations
19# under the License.
20#
21
Roger Meierc95d5df2014-01-19 21:53:02 +010022$:.push File.dirname(__FILE__) + '/..'
Kevin Clark4bd89162008-07-08 00:47:49 +000023
Roger Meierc95d5df2014-01-19 21:53:02 +010024require 'test_helper'
Kevin Clark4bd89162008-07-08 00:47:49 +000025require 'thrift'
Roger Meierc95d5df2014-01-19 21:53:02 +010026require 'thrift_test'
Kevin Clark4bd89162008-07-08 00:47:49 +000027
Roger Meiera3570ac2014-06-10 22:16:14 +020028$protocolType = "binary"
29$host = "localhost"
30$port = 9090
31$transport = "buffered"
32ARGV.each do|a|
33 if a == "--help"
34 puts "Allowed options:"
35 puts "\t -h [ --help ] \t produce help message"
36 puts "\t--host arg (=localhost) \t Host to connect"
37 puts "\t--port arg (=9090) \t Port number to listen"
38 puts "\t--protocol arg (=binary) \t protocol: binary, accel"
39 puts "\t--transport arg (=buffered) transport: buffered, framed, http"
40 exit
41 elsif a.start_with?("--host")
42 $host = a.split("=")[1]
43 elsif a.start_with?("--protocol")
44 $protocolType = a.split("=")[1]
45 elsif a.start_with?("--transport")
46 $transport = a.split("=")[1]
47 elsif a.start_with?("--port")
48 $port = a.split("=")[1].to_i
49 end
50end
51ARGV=[]
52
Kevin Clark4bd89162008-07-08 00:47:49 +000053class SimpleClientTest < Test::Unit::TestCase
Roger Meiera3570ac2014-06-10 22:16:14 +020054 def setup
Kevin Clark4bd89162008-07-08 00:47:49 +000055 unless @socket
Roger Meiera3570ac2014-06-10 22:16:14 +020056 @socket = Thrift::Socket.new($host, $port)
57 transportFactory = Thrift::BufferedTransport.new(@socket)
58 if $transport == "buffered"
59 transportFactory = Thrift::BufferedTransport.new(@socket)
60 elsif $transport == ""
61 transportFactory = Thrift::BufferedTransport.new(@socket)
62 elsif $transport == "framed"
63 transportFactory = Thrift::FramedTransport.new(@socket)
64 else
65 raise 'Unknown transport type'
66 end
67
68 if $protocolType == "binary"
69 @protocol = Thrift::BinaryProtocol.new(transportFactory)
70 elsif $protocolType == ""
71 @protocol = Thrift::BinaryProtocol.new(transportFactory)
72 elsif $protocolType == "compact"
73 @protocol = Thrift::CompactProtocol.new(transportFactory)
74 elsif $protocolType == "json"
75 @protocol = Thrift::JsonProtocol.new(transportFactory)
76 elsif $protocolType == "accel"
77 @protocol = Thrift::BinaryProtocolAccelerated.new(transportFactory)
78 else
79 raise 'Unknown protocol type'
80 end
Kevin Clark4bd89162008-07-08 00:47:49 +000081 @client = Thrift::Test::ThriftTest::Client.new(@protocol)
82 @socket.open
83 end
84 end
85
Roger Meiera3570ac2014-06-10 22:16:14 +020086 def test_void
87 @client.testVoid()
88 end
89
Kevin Clark4bd89162008-07-08 00:47:49 +000090 def test_string
91 assert_equal(@client.testString('string'), 'string')
92 end
93
94 def test_byte
95 val = 8
96 assert_equal(@client.testByte(val), val)
97 assert_equal(@client.testByte(-val), -val)
98 end
99
100 def test_i32
101 val = 32
102 assert_equal(@client.testI32(val), val)
103 assert_equal(@client.testI32(-val), -val)
104 end
105
106 def test_i64
107 val = 64
108 assert_equal(@client.testI64(val), val)
109 assert_equal(@client.testI64(-val), -val)
110 end
111
112 def test_double
113 val = 3.14
114 assert_equal(@client.testDouble(val), val)
115 assert_equal(@client.testDouble(-val), -val)
116 assert_kind_of(Float, @client.testDouble(val))
117 end
118
Jens Geyer8bcfdd92014-12-14 03:14:26 +0100119 # TODO: testBinary
120
Kevin Clark4bd89162008-07-08 00:47:49 +0000121 def test_map
122 val = {1 => 1, 2 => 2, 3 => 3}
123 assert_equal(@client.testMap(val), val)
124 assert_kind_of(Hash, @client.testMap(val))
125 end
126
127 def test_list
128 val = [1,2,3,4,5]
129 assert_equal(@client.testList(val), val)
130 assert_kind_of(Array, @client.testList(val))
131 end
132
133 def test_enum
134 val = Thrift::Test::Numberz::SIX
135 ret = @client.testEnum(val)
136
137 assert_equal(ret, 6)
138 assert_kind_of(Fixnum, ret)
139 end
140
141 def test_typedef
142 #UserId testTypedef(1: UserId thing),
Roger Meiera3570ac2014-06-10 22:16:14 +0200143 assert_equal(@client.testTypedef(309858235082523), 309858235082523)
144 assert_kind_of(Fixnum, @client.testTypedef(309858235082523))
Kevin Clark4bd89162008-07-08 00:47:49 +0000145 true
146 end
147
148 def test_set
149 val = Set.new([1,2,3])
150 assert_equal(@client.testSet(val), val)
151 assert_kind_of(Set, @client.testSet(val))
152 end
153
154 def get_struct
155 Thrift::Test::Xtruct.new({'string_thing' => 'hi!', 'i32_thing' => 4 })
156 end
157
158 def test_struct
159 ret = @client.testStruct(get_struct)
160
161 assert_nil(ret.byte_thing, nil)
162 assert_nil(ret.i64_thing, nil)
163 assert_equal(ret.string_thing, 'hi!')
164 assert_equal(ret.i32_thing, 4)
165 assert_kind_of(Thrift::Test::Xtruct, ret)
166 end
167
168 def test_nest
169 struct2 = Thrift::Test::Xtruct2.new({'struct_thing' => get_struct, 'i32_thing' => 10})
170
171 ret = @client.testNest(struct2)
172
173 assert_nil(ret.struct_thing.byte_thing, nil)
174 assert_nil(ret.struct_thing.i64_thing, nil)
175 assert_equal(ret.struct_thing.string_thing, 'hi!')
176 assert_equal(ret.struct_thing.i32_thing, 4)
177 assert_equal(ret.i32_thing, 10)
178
179 assert_kind_of(Thrift::Test::Xtruct, ret.struct_thing)
180 assert_kind_of(Thrift::Test::Xtruct2, ret)
181 end
182
Roger Meiera3570ac2014-06-10 22:16:14 +0200183 def test_insanity
Kevin Clark4bd89162008-07-08 00:47:49 +0000184 insane = Thrift::Test::Insanity.new({
185 'userMap' => { Thrift::Test::Numberz::ONE => 44 },
186 'xtructs' => [get_struct,
187 Thrift::Test::Xtruct.new({
188 'string_thing' => 'hi again',
189 'i32_thing' => 12
190 })
191 ]
192 })
193
194 ret = @client.testInsanity(insane)
195
196 assert_not_nil(ret[44])
197 assert_not_nil(ret[44][1])
198
199 struct = ret[44][1]
200
201 assert_equal(struct.userMap[Thrift::Test::Numberz::ONE], 44)
202 assert_equal(struct.xtructs[1].string_thing, 'hi again')
203 assert_equal(struct.xtructs[1].i32_thing, 12)
204
205 assert_kind_of(Hash, struct.userMap)
206 assert_kind_of(Array, struct.xtructs)
207 assert_kind_of(Thrift::Test::Insanity, struct)
208 end
209
210 def test_map_map
211 ret = @client.testMapMap(4)
212 assert_kind_of(Hash, ret)
213 assert_equal(ret, { 4 => { 4 => 4}})
214 end
215
216 def test_exception
217 assert_raise Thrift::Test::Xception do
Roger Meiera3570ac2014-06-10 22:16:14 +0200218 @client.testException('Xception')
Kevin Clark4bd89162008-07-08 00:47:49 +0000219 end
Roger Meiera3570ac2014-06-10 22:16:14 +0200220# assert_raise Thrift::TException do
221# @client.testException('TException')
222# end
223 assert_equal( @client.testException('test'), "test")
Kevin Clark4bd89162008-07-08 00:47:49 +0000224 end
Roger Meiera3570ac2014-06-10 22:16:14 +0200225
226 def test_multi_exception
227 assert_raise Thrift::Test::Xception do
228 @client.testMultiException("Xception", "test 1")
229 end
230 assert_raise Thrift::Test::Xception2 do
231 @client.testMultiException("Xception2", "test 2")
232 end
233 assert_equal( @client.testMultiException("Success", "test 3").string_thing, "test 3")
234 end
235
236 def test_oneway
237 time1 = Time.now.to_f
238 @client.testOneway(3)
239 time2 = Time.now.to_f
240 assert_equal((time2-time1)*1000000<400, true)
241 end
242
Kevin Clark4bd89162008-07-08 00:47:49 +0000243end
244