blob: 079298d0641951346ea2a540dc25cceda49d69cf [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|
Jens Geyer123258b2015-10-02 00:38:17 +020035 p meth
36 p thing
Roger Meiera3570ac2014-06-10 22:16:14 +020037 thing
38 end
39
40 end
41
42 def testVoid()
43 end
44
45 def testInsanity(thing)
Nobuaki Sukegawa68238292015-09-21 23:28:22 +090046 return {
47 1 => {
48 2 => thing,
49 3 => thing
50 },
51 2 => {
52 6 => Thrift::Test::Insanity::new()
53 }
54 }
Roger Meiera3570ac2014-06-10 22:16:14 +020055 end
56
57 def testMapMap(thing)
Nobuaki Sukegawa68238292015-09-21 23:28:22 +090058 return {
59 -4 => {
60 -4 => -4,
61 -3 => -3,
62 -2 => -2,
63 -1 => -1,
64 },
65 4 => {
66 4 => 4,
67 3 => 3,
68 2 => 2,
69 1 => 1,
70 }
71 }
Roger Meiera3570ac2014-06-10 22:16:14 +020072 end
73
Nobuaki Sukegawa68238292015-09-21 23:28:22 +090074 def testMulti(arg0, arg1, arg2, arg3, arg4, arg5)
75 return Thrift::Test::Xtruct.new({
76 'string_thing' => 'Hello2',
77 'byte_thing' => arg0,
78 'i32_thing' => arg1,
79 'i64_thing' => arg2,
80 })
Roger Meiera3570ac2014-06-10 22:16:14 +020081 end
82
83 def testException(thing)
84 if thing == "Xception"
Nobuaki Sukegawa68238292015-09-21 23:28:22 +090085 raise Thrift::Test::Xception, :errorCode => 1001, :message => thing
Roger Meiera3570ac2014-06-10 22:16:14 +020086 elsif thing == "TException"
Nobuaki Sukegawa68238292015-09-21 23:28:22 +090087 raise Thrift::Exception, :message => thing
Roger Meiera3570ac2014-06-10 22:16:14 +020088 else
Nobuaki Sukegawa68238292015-09-21 23:28:22 +090089 # no-op
Roger Meiera3570ac2014-06-10 22:16:14 +020090 end
91 end
92
93 def testMultiException(arg0, arg1)
94 if arg0 == "Xception2"
Nobuaki Sukegawa68238292015-09-21 23:28:22 +090095 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 +020096 elsif arg0 == "Xception"
Nobuaki Sukegawa68238292015-09-21 23:28:22 +090097 raise Thrift::Test::Xception, :errorCode => 1001, :message => 'This is an Xception'
Roger Meiera3570ac2014-06-10 22:16:14 +020098 else
Nobuaki Sukegawa68238292015-09-21 23:28:22 +090099 return ::Thrift::Test::Xtruct.new({'string_thing' => arg1})
Roger Meiera3570ac2014-06-10 22:16:14 +0200100 end
101 end
102
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900103 def testOneway(arg0)
104 sleep(arg0)
105 end
106
Roger Meiera3570ac2014-06-10 22:16:14 +0200107end
108
James E. King III9aaf2952018-03-20 15:06:08 -0400109domain_socket = nil
Roger Meiera3570ac2014-06-10 22:16:14 +0200110port = 9090
James E. King III9aaf2952018-03-20 15:06:08 -0400111protocol = "binary"
112@protocolFactory = nil
Roger Meiera3570ac2014-06-10 22:16:14 +0200113transport = "buffered"
James E. King III9aaf2952018-03-20 15:06:08 -0400114@transportFactory = nil
115
Roger Meiera3570ac2014-06-10 22:16:14 +0200116ARGV.each do|a|
117 if a == "--help"
118 puts "Allowed options:"
119 puts "\t -h [ --help ] \t produce help message"
James E. King III9aaf2952018-03-20 15:06:08 -0400120 puts "\t--domain-socket arg (=) \t Unix domain socket path - if not empty, port is ignored"
Roger Meiera3570ac2014-06-10 22:16:14 +0200121 puts "\t--port arg (=9090) \t Port number to listen"
James E. King III9aaf2952018-03-20 15:06:08 -0400122 puts "\t--protocol arg (=binary) \t protocol: accel, binary, compact, json"
Roger Meiera3570ac2014-06-10 22:16:14 +0200123 puts "\t--transport arg (=buffered) transport: buffered, framed, http"
124 exit
James E. King III9aaf2952018-03-20 15:06:08 -0400125 elsif a.start_with?("--domain-socket")
126 domain_socket = a.split("=")[1]
Roger Meiera3570ac2014-06-10 22:16:14 +0200127 elsif a.start_with?("--protocol")
128 protocol = a.split("=")[1]
129 elsif a.start_with?("--transport")
130 transport = a.split("=")[1]
131 elsif a.start_with?("--port")
132 port = a.split("=")[1].to_i
133 end
134end
135
James E. King III9aaf2952018-03-20 15:06:08 -0400136if protocol == "binary" || protocol.to_s.strip.empty?
Roger Meiera3570ac2014-06-10 22:16:14 +0200137 @protocolFactory = Thrift::BinaryProtocolFactory.new
138elsif protocol == "compact"
139 @protocolFactory = Thrift::CompactProtocolFactory.new
140elsif protocol == "json"
141 @protocolFactory = Thrift::JsonProtocolFactory.new
142elsif protocol == "accel"
143 @protocolFactory = Thrift::BinaryProtocolAcceleratedFactory.new
144else
145 raise 'Unknown protocol type'
146end
147
James E. King III9aaf2952018-03-20 15:06:08 -0400148if transport == "buffered" || transport.to_s.strip.empty?
Roger Meiera3570ac2014-06-10 22:16:14 +0200149 @transportFactory = Thrift::BufferedTransportFactory.new
150elsif transport == "framed"
151 @transportFactory = Thrift::FramedTransportFactory.new
152else
153 raise 'Unknown transport type'
154end
155
James E. King III9aaf2952018-03-20 15:06:08 -0400156@handler = SimpleHandler.new
Roger Meiera3570ac2014-06-10 22:16:14 +0200157@processor = Thrift::Test::ThriftTest::Processor.new(@handler)
James E. King III9aaf2952018-03-20 15:06:08 -0400158@transport = nil
159if domain_socket.to_s.strip.empty?
160 @transport = Thrift::ServerSocket.new(port)
161else
162 @transport = Thrift::UNIXServerSocket.new(domain_socket)
163end
164
165@server = Thrift::ThreadedServer.new(@processor, @transport, @transportFactory, @protocolFactory)
166
167puts "Starting TestServer #{@server.to_s}"
Roger Meiera3570ac2014-06-10 22:16:14 +0200168@server.serve
James E. King III9aaf2952018-03-20 15:06:08 -0400169puts "done."