blob: d0ba1fed63794b0b449289ecee260adda33c622e [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'
Dmytro Shteflyuk96d62dd2026-02-13 15:16:16 -050028require 'logger'
29require 'optparse'
Roger Meiera3570ac2014-06-10 22:16:14 +020030
31class SimpleHandler
Nobuaki Sukegawa68238292015-09-21 23:28:22 +090032 [:testVoid, :testString, :testBool, :testByte, :testI32, :testI64, :testDouble, :testBinary,
33 :testStruct, :testMap, :testStringMap, :testSet, :testList, :testNest, :testEnum, :testTypedef,
Dmytro Shteflyuke9ac8e32025-11-19 23:33:23 -050034 :testEnum, :testTypedef, :testMultiException, :testUuid].each do |meth|
Roger Meiera3570ac2014-06-10 22:16:14 +020035
36 define_method(meth) do |thing|
Jens Geyer123258b2015-10-02 00:38:17 +020037 p meth
38 p thing
Roger Meiera3570ac2014-06-10 22:16:14 +020039 thing
40 end
41
42 end
43
44 def testVoid()
45 end
46
47 def testInsanity(thing)
Nobuaki Sukegawa68238292015-09-21 23:28:22 +090048 return {
49 1 => {
50 2 => thing,
51 3 => thing
52 },
53 2 => {
54 6 => Thrift::Test::Insanity::new()
55 }
56 }
Roger Meiera3570ac2014-06-10 22:16:14 +020057 end
58
59 def testMapMap(thing)
Nobuaki Sukegawa68238292015-09-21 23:28:22 +090060 return {
61 -4 => {
62 -4 => -4,
63 -3 => -3,
64 -2 => -2,
65 -1 => -1,
66 },
67 4 => {
68 4 => 4,
69 3 => 3,
70 2 => 2,
71 1 => 1,
72 }
73 }
Roger Meiera3570ac2014-06-10 22:16:14 +020074 end
75
Nobuaki Sukegawa68238292015-09-21 23:28:22 +090076 def testMulti(arg0, arg1, arg2, arg3, arg4, arg5)
77 return Thrift::Test::Xtruct.new({
78 'string_thing' => 'Hello2',
79 'byte_thing' => arg0,
80 'i32_thing' => arg1,
81 'i64_thing' => arg2,
82 })
Roger Meiera3570ac2014-06-10 22:16:14 +020083 end
84
85 def testException(thing)
86 if thing == "Xception"
Nobuaki Sukegawa68238292015-09-21 23:28:22 +090087 raise Thrift::Test::Xception, :errorCode => 1001, :message => thing
Roger Meiera3570ac2014-06-10 22:16:14 +020088 elsif thing == "TException"
Nobuaki Sukegawa68238292015-09-21 23:28:22 +090089 raise Thrift::Exception, :message => thing
Roger Meiera3570ac2014-06-10 22:16:14 +020090 else
Nobuaki Sukegawa68238292015-09-21 23:28:22 +090091 # no-op
Roger Meiera3570ac2014-06-10 22:16:14 +020092 end
93 end
94
95 def testMultiException(arg0, arg1)
96 if arg0 == "Xception2"
Nobuaki Sukegawa68238292015-09-21 23:28:22 +090097 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 +020098 elsif arg0 == "Xception"
Nobuaki Sukegawa68238292015-09-21 23:28:22 +090099 raise Thrift::Test::Xception, :errorCode => 1001, :message => 'This is an Xception'
Roger Meiera3570ac2014-06-10 22:16:14 +0200100 else
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900101 return ::Thrift::Test::Xtruct.new({'string_thing' => arg1})
Roger Meiera3570ac2014-06-10 22:16:14 +0200102 end
103 end
104
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900105 def testOneway(arg0)
106 sleep(arg0)
107 end
108
Roger Meiera3570ac2014-06-10 22:16:14 +0200109end
110
Dmytro Shteflyuk96d62dd2026-02-13 15:16:16 -0500111options = {
112 domain_socket: nil,
113 port: 9090,
114 protocol: 'binary',
115 ssl: false,
116 transport: 'buffered',
117 server_type: 'threaded',
118 workers: nil,
119}
James E. King III9aaf2952018-03-20 15:06:08 -0400120
Dmytro Shteflyuk96d62dd2026-02-13 15:16:16 -0500121server_type_map = {
122 'simple' => 'simple',
123 'threaded' => 'threaded',
124 'thread-pool' => 'thread-pool',
125 'thread_pool' => 'thread-pool',
126 'threadpool' => 'thread-pool',
127 'nonblocking' => 'nonblocking',
128 'tsimpleserver' => 'simple',
129 'tthreadedserver' => 'threaded',
130 'tthreadpoolserver' => 'thread-pool',
131 'tnonblockingserver' => 'nonblocking',
132}
133
134parser = OptionParser.new do |opts|
135 opts.banner = "Allowed options:"
136 opts.on('-h', '--help', 'produce help message') do
137 puts opts
138 exit 0
Roger Meiera3570ac2014-06-10 22:16:14 +0200139 end
Dmytro Shteflyuk96d62dd2026-02-13 15:16:16 -0500140 opts.on('--domain-socket=PATH', String, 'Unix domain socket path') { |v| options[:domain_socket] = v }
141 opts.on('--port=PORT', Integer, 'Port number to listen (not valid with domain-socket)') { |v| options[:port] = v }
142 opts.on('--protocol=PROTO', String, 'protocol: accel, binary, compact, json, header') { |v| options[:protocol] = v }
143 opts.on('--ssl', 'use ssl (not valid with domain-socket)') { options[:ssl] = true }
144 opts.on('--transport=TRANSPORT', String, 'transport: buffered, framed, header') { |v| options[:transport] = v }
145 opts.on('--server-type=TYPE', String, 'type of server: simple, thread-pool, threaded, nonblocking') { |v| options[:server_type] = v }
146 opts.on('-n', '--workers=N', Integer, 'Number of workers (thread-pool/nonblocking)') { |v| options[:workers] = v }
Roger Meiera3570ac2014-06-10 22:16:14 +0200147end
148
Dmytro Shteflyuk96d62dd2026-02-13 15:16:16 -0500149begin
150 parser.parse!(ARGV)
151 if ARGV.length > 1
152 raise OptionParser::InvalidOption, "Only one positional server type may be specified"
153 end
154rescue OptionParser::ParseError => e
155 warn e.message
156 warn parser
157 exit 1
158end
159
160# Accept Python-style server type positional arg for compatibility.
161options[:server_type] = ARGV.first unless ARGV.empty?
162normalized_server_type = server_type_map[options[:server_type].to_s.downcase]
163if normalized_server_type.nil?
164 warn "Unknown server type '#{options[:server_type]}'"
165 warn parser
166 exit 1
167end
168
169if options[:ssl] && !options[:domain_socket].to_s.strip.empty?
170 warn '--ssl is not valid with --domain-socket'
171 exit 1
172end
173
174protocol_factory = case options[:protocol].to_s.strip
175when '', 'binary'
176 Thrift::BinaryProtocolFactory.new
177when 'compact'
178 Thrift::CompactProtocolFactory.new
179when 'json'
180 Thrift::JsonProtocolFactory.new
181when 'accel'
182 Thrift::BinaryProtocolAcceleratedFactory.new
183when 'header'
184 Thrift::HeaderProtocolFactory.new
Roger Meiera3570ac2014-06-10 22:16:14 +0200185else
Dmytro Shteflyuk96d62dd2026-02-13 15:16:16 -0500186 raise "Unknown protocol type '#{options[:protocol]}'"
Roger Meiera3570ac2014-06-10 22:16:14 +0200187end
188
Dmytro Shteflyuk96d62dd2026-02-13 15:16:16 -0500189transport_factory = case options[:transport].to_s.strip
190when '', 'buffered'
191 Thrift::BufferedTransportFactory.new
192when 'framed'
193 Thrift::FramedTransportFactory.new
194when 'header'
195 Thrift::HeaderTransportFactory.new
Roger Meiera3570ac2014-06-10 22:16:14 +0200196else
Dmytro Shteflyuk96d62dd2026-02-13 15:16:16 -0500197 raise "Unknown transport type '#{options[:transport]}'"
Roger Meiera3570ac2014-06-10 22:16:14 +0200198end
199
Dmytro Shteflyuk96d62dd2026-02-13 15:16:16 -0500200if normalized_server_type == 'nonblocking' && options[:transport] != 'framed'
201 raise 'server-type nonblocking requires transport of framed'
202end
203
204handler = SimpleHandler.new
205processor = Thrift::Test::ThriftTest::Processor.new(handler)
206
207transport = nil
208if options[:domain_socket].to_s.strip.empty?
209 if options[:ssl]
James E. King III714c77c2018-03-20 19:58:38 -0400210 # the working directory for ruby crosstest is test/rb/gen-rb
211 keysDir = File.join(File.dirname(File.dirname(Dir.pwd)), "keys")
212 ctx = OpenSSL::SSL::SSLContext.new
213 ctx.ca_file = File.join(keysDir, "CA.pem")
Dmytro Shteflyuk84554fa2025-11-19 19:41:05 -0500214 ctx.cert = OpenSSL::X509::Certificate.new(File.binread(File.join(keysDir, "server.crt")))
James E. King III714c77c2018-03-20 19:58:38 -0400215 ctx.cert_store = OpenSSL::X509::Store.new
216 ctx.cert_store.add_file(File.join(keysDir, 'client.pem'))
Dmytro Shteflyuk84554fa2025-11-19 19:41:05 -0500217 ctx.key = OpenSSL::PKey::RSA.new(File.binread(File.join(keysDir, "server.key")))
218 ctx.min_version = :TLS1_2
James E. King III714c77c2018-03-20 19:58:38 -0400219 ctx.verify_mode = OpenSSL::SSL::VERIFY_PEER
Dmytro Shteflyuk96d62dd2026-02-13 15:16:16 -0500220 transport = Thrift::SSLServerSocket.new(nil, options[:port], ctx)
James E. King III714c77c2018-03-20 19:58:38 -0400221 else
Dmytro Shteflyuk96d62dd2026-02-13 15:16:16 -0500222 transport = Thrift::ServerSocket.new(options[:port])
James E. King III714c77c2018-03-20 19:58:38 -0400223 end
James E. King III9aaf2952018-03-20 15:06:08 -0400224else
Dmytro Shteflyuk96d62dd2026-02-13 15:16:16 -0500225 transport = Thrift::UNIXServerSocket.new(options[:domain_socket])
James E. King III9aaf2952018-03-20 15:06:08 -0400226end
227
Dmytro Shteflyuk96d62dd2026-02-13 15:16:16 -0500228workers = options[:workers] || 20
229server = case normalized_server_type
230when 'simple'
231 Thrift::SimpleServer.new(processor, transport, transport_factory, protocol_factory)
232when 'threaded'
233 Thrift::ThreadedServer.new(processor, transport, transport_factory, protocol_factory)
234when 'thread-pool'
235 Thrift::ThreadPoolServer.new(processor, transport, transport_factory, protocol_factory, workers)
236when 'nonblocking'
237 logger = Logger.new(STDERR)
238 logger.level = Logger::WARN
239 Thrift::NonblockingServer.new(processor, transport, transport_factory, protocol_factory, workers, logger)
240end
James E. King III9aaf2952018-03-20 15:06:08 -0400241
Dmytro Shteflyuk96d62dd2026-02-13 15:16:16 -0500242puts "Starting TestServer #{server.to_s}"
243server.serve
James E. King III9aaf2952018-03-20 15:06:08 -0400244puts "done."