blob: 0021e2ad322fab5a6bb94a3ef03996098e49b809 [file] [log] [blame]
Roger Meier32f39822014-06-18 22:43:17 +02001#!/usr/bin/env ruby
2
Roger Meiera3570ac2014-06-10 22:16:14 +02003#
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
22$:.push File.dirname(__FILE__) + '/..'
23
24require 'test_helper'
25require 'thrift'
26require 'thrift_test'
Nobuaki Sukegawa68238292015-09-21 23:28:22 +090027require 'thrift_test_types'
Roger Meiera3570ac2014-06-10 22:16:14 +020028
29class SimpleHandler
Nobuaki Sukegawa68238292015-09-21 23:28:22 +090030 [:testVoid, :testString, :testBool, :testByte, :testI32, :testI64, :testDouble, :testBinary,
31 :testStruct, :testMap, :testStringMap, :testSet, :testList, :testNest, :testEnum, :testTypedef,
Roger Meiera3570ac2014-06-10 22:16:14 +020032 :testEnum, :testTypedef, :testMultiException].each do |meth|
33
34 define_method(meth) do |thing|
35 thing
36 end
37
38 end
39
40 def testVoid()
41 end
42
43 def testInsanity(thing)
Nobuaki Sukegawa68238292015-09-21 23:28:22 +090044 return {
45 1 => {
46 2 => thing,
47 3 => thing
48 },
49 2 => {
50 6 => Thrift::Test::Insanity::new()
51 }
52 }
Roger Meiera3570ac2014-06-10 22:16:14 +020053 end
54
55 def testMapMap(thing)
Nobuaki Sukegawa68238292015-09-21 23:28:22 +090056 return {
57 -4 => {
58 -4 => -4,
59 -3 => -3,
60 -2 => -2,
61 -1 => -1,
62 },
63 4 => {
64 4 => 4,
65 3 => 3,
66 2 => 2,
67 1 => 1,
68 }
69 }
Roger Meiera3570ac2014-06-10 22:16:14 +020070 end
71
Nobuaki Sukegawa68238292015-09-21 23:28:22 +090072 def testMulti(arg0, arg1, arg2, arg3, arg4, arg5)
73 return Thrift::Test::Xtruct.new({
74 'string_thing' => 'Hello2',
75 'byte_thing' => arg0,
76 'i32_thing' => arg1,
77 'i64_thing' => arg2,
78 })
Roger Meiera3570ac2014-06-10 22:16:14 +020079 end
80
81 def testException(thing)
82 if thing == "Xception"
Nobuaki Sukegawa68238292015-09-21 23:28:22 +090083 raise Thrift::Test::Xception, :errorCode => 1001, :message => thing
Roger Meiera3570ac2014-06-10 22:16:14 +020084 elsif thing == "TException"
Nobuaki Sukegawa68238292015-09-21 23:28:22 +090085 raise Thrift::Exception, :message => thing
Roger Meiera3570ac2014-06-10 22:16:14 +020086 else
Nobuaki Sukegawa68238292015-09-21 23:28:22 +090087 # no-op
Roger Meiera3570ac2014-06-10 22:16:14 +020088 end
89 end
90
91 def testMultiException(arg0, arg1)
92 if arg0 == "Xception2"
Nobuaki Sukegawa68238292015-09-21 23:28:22 +090093 raise Thrift::Test::Xception2, :errorCode => 2002, :struct_thing => ::Thrift::Test::Xtruct.new({ :string_thing => 'This is an Xception2' })
Roger Meiera3570ac2014-06-10 22:16:14 +020094 elsif arg0 == "Xception"
Nobuaki Sukegawa68238292015-09-21 23:28:22 +090095 raise Thrift::Test::Xception, :errorCode => 1001, :message => 'This is an Xception'
Roger Meiera3570ac2014-06-10 22:16:14 +020096 else
Nobuaki Sukegawa68238292015-09-21 23:28:22 +090097 return ::Thrift::Test::Xtruct.new({'string_thing' => arg1})
Roger Meiera3570ac2014-06-10 22:16:14 +020098 end
99 end
100
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900101 def testOneway(arg0)
102 sleep(arg0)
103 end
104
Roger Meiera3570ac2014-06-10 22:16:14 +0200105end
106
107protocol = "binary"
108port = 9090
109transport = "buffered"
110@transportFactory = Thrift::BufferedTransportFactory.new
111@protocolFactory = Thrift::BinaryProtocolFactory.new
112ARGV.each do|a|
113 if a == "--help"
114 puts "Allowed options:"
115 puts "\t -h [ --help ] \t produce help message"
116 puts "\t--port arg (=9090) \t Port number to listen"
117 puts "\t--protocol arg (=binary) \t protocol: binary, accel"
118 puts "\t--transport arg (=buffered) transport: buffered, framed, http"
119 exit
120 elsif a.start_with?("--protocol")
121 protocol = a.split("=")[1]
122 elsif a.start_with?("--transport")
123 transport = a.split("=")[1]
124 elsif a.start_with?("--port")
125 port = a.split("=")[1].to_i
126 end
127end
128
129if protocol == "binary"
130 @protocolFactory = Thrift::BinaryProtocolFactory.new
131elsif protocol == ""
132 @protocolFactory = Thrift::BinaryProtocolFactory.new
133elsif protocol == "compact"
134 @protocolFactory = Thrift::CompactProtocolFactory.new
135elsif protocol == "json"
136 @protocolFactory = Thrift::JsonProtocolFactory.new
137elsif protocol == "accel"
138 @protocolFactory = Thrift::BinaryProtocolAcceleratedFactory.new
139else
140 raise 'Unknown protocol type'
141end
142
143if transport == "buffered"
144 @transportFactory = Thrift::BufferedTransportFactory.new
145elsif transport == ""
146 @transportFactory = Thrift::BufferedTransportFactory.new
147elsif transport == "framed"
148 @transportFactory = Thrift::FramedTransportFactory.new
149else
150 raise 'Unknown transport type'
151end
152
153@handler = SimpleHandler.new
154@processor = Thrift::Test::ThriftTest::Processor.new(@handler)
155@transport = Thrift::ServerSocket.new(port)
156@server = Thrift::ThreadedServer.new(@processor, @transport, @transportFactory, @protocolFactory)
157@server.serve