blob: 54f3504ea176745ca70445f7d8682403211c908e [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 define_method(meth) do |thing|
Jens Geyer123258b2015-10-02 00:38:17 +020036 p meth
37 p thing
Roger Meiera3570ac2014-06-10 22:16:14 +020038 thing
39 end
Roger Meiera3570ac2014-06-10 22:16:14 +020040 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
Dmytro Shteflyuk96d62dd2026-02-13 15:16:16 -0500109options = {
110 domain_socket: nil,
111 port: 9090,
112 protocol: 'binary',
113 ssl: false,
114 transport: 'buffered',
115 server_type: 'threaded',
116 workers: nil,
117}
James E. King III9aaf2952018-03-20 15:06:08 -0400118
Dmytro Shteflyuk96d62dd2026-02-13 15:16:16 -0500119server_type_map = {
120 'simple' => 'simple',
121 'threaded' => 'threaded',
122 'thread-pool' => 'thread-pool',
123 'thread_pool' => 'thread-pool',
124 'threadpool' => 'thread-pool',
125 'nonblocking' => 'nonblocking',
126 'tsimpleserver' => 'simple',
127 'tthreadedserver' => 'threaded',
128 'tthreadpoolserver' => 'thread-pool',
129 'tnonblockingserver' => 'nonblocking',
130}
131
132parser = OptionParser.new do |opts|
133 opts.banner = "Allowed options:"
134 opts.on('-h', '--help', 'produce help message') do
135 puts opts
136 exit 0
Roger Meiera3570ac2014-06-10 22:16:14 +0200137 end
Dmytro Shteflyuk96d62dd2026-02-13 15:16:16 -0500138 opts.on('--domain-socket=PATH', String, 'Unix domain socket path') { |v| options[:domain_socket] = v }
139 opts.on('--port=PORT', Integer, 'Port number to listen (not valid with domain-socket)') { |v| options[:port] = v }
140 opts.on('--protocol=PROTO', String, 'protocol: accel, binary, compact, json, header') { |v| options[:protocol] = v }
141 opts.on('--ssl', 'use ssl (not valid with domain-socket)') { options[:ssl] = true }
142 opts.on('--transport=TRANSPORT', String, 'transport: buffered, framed, header') { |v| options[:transport] = v }
143 opts.on('--server-type=TYPE', String, 'type of server: simple, thread-pool, threaded, nonblocking') { |v| options[:server_type] = v }
144 opts.on('-n', '--workers=N', Integer, 'Number of workers (thread-pool/nonblocking)') { |v| options[:workers] = v }
Roger Meiera3570ac2014-06-10 22:16:14 +0200145end
146
Dmytro Shteflyuk96d62dd2026-02-13 15:16:16 -0500147begin
148 parser.parse!(ARGV)
149 if ARGV.length > 1
150 raise OptionParser::InvalidOption, "Only one positional server type may be specified"
151 end
152rescue OptionParser::ParseError => e
153 warn e.message
154 warn parser
155 exit 1
156end
157
158# Accept Python-style server type positional arg for compatibility.
159options[:server_type] = ARGV.first unless ARGV.empty?
160normalized_server_type = server_type_map[options[:server_type].to_s.downcase]
161if normalized_server_type.nil?
162 warn "Unknown server type '#{options[:server_type]}'"
163 warn parser
164 exit 1
165end
166
167if options[:ssl] && !options[:domain_socket].to_s.strip.empty?
168 warn '--ssl is not valid with --domain-socket'
169 exit 1
170end
171
172protocol_factory = case options[:protocol].to_s.strip
173when '', 'binary'
174 Thrift::BinaryProtocolFactory.new
175when 'compact'
176 Thrift::CompactProtocolFactory.new
177when 'json'
178 Thrift::JsonProtocolFactory.new
179when 'accel'
180 Thrift::BinaryProtocolAcceleratedFactory.new
181when 'header'
182 Thrift::HeaderProtocolFactory.new
Roger Meiera3570ac2014-06-10 22:16:14 +0200183else
Dmytro Shteflyuk96d62dd2026-02-13 15:16:16 -0500184 raise "Unknown protocol type '#{options[:protocol]}'"
Roger Meiera3570ac2014-06-10 22:16:14 +0200185end
186
Dmytro Shteflyuk96d62dd2026-02-13 15:16:16 -0500187transport_factory = case options[:transport].to_s.strip
188when '', 'buffered'
189 Thrift::BufferedTransportFactory.new
190when 'framed'
191 Thrift::FramedTransportFactory.new
192when 'header'
193 Thrift::HeaderTransportFactory.new
Roger Meiera3570ac2014-06-10 22:16:14 +0200194else
Dmytro Shteflyuk96d62dd2026-02-13 15:16:16 -0500195 raise "Unknown transport type '#{options[:transport]}'"
Roger Meiera3570ac2014-06-10 22:16:14 +0200196end
197
Dmytro Shteflyuk96d62dd2026-02-13 15:16:16 -0500198if normalized_server_type == 'nonblocking' && options[:transport] != 'framed'
199 raise 'server-type nonblocking requires transport of framed'
200end
201
202handler = SimpleHandler.new
203processor = Thrift::Test::ThriftTest::Processor.new(handler)
204
205transport = nil
206if options[:domain_socket].to_s.strip.empty?
207 if options[:ssl]
James E. King III714c77c2018-03-20 19:58:38 -0400208 # the working directory for ruby crosstest is test/rb/gen-rb
209 keysDir = File.join(File.dirname(File.dirname(Dir.pwd)), "keys")
210 ctx = OpenSSL::SSL::SSLContext.new
211 ctx.ca_file = File.join(keysDir, "CA.pem")
Dmytro Shteflyuk84554fa2025-11-19 19:41:05 -0500212 ctx.cert = OpenSSL::X509::Certificate.new(File.binread(File.join(keysDir, "server.crt")))
James E. King III714c77c2018-03-20 19:58:38 -0400213 ctx.cert_store = OpenSSL::X509::Store.new
214 ctx.cert_store.add_file(File.join(keysDir, 'client.pem'))
Dmytro Shteflyuk84554fa2025-11-19 19:41:05 -0500215 ctx.key = OpenSSL::PKey::RSA.new(File.binread(File.join(keysDir, "server.key")))
216 ctx.min_version = :TLS1_2
James E. King III714c77c2018-03-20 19:58:38 -0400217 ctx.verify_mode = OpenSSL::SSL::VERIFY_PEER
Dmytro Shteflyuk96d62dd2026-02-13 15:16:16 -0500218 transport = Thrift::SSLServerSocket.new(nil, options[:port], ctx)
James E. King III714c77c2018-03-20 19:58:38 -0400219 else
Dmytro Shteflyuk96d62dd2026-02-13 15:16:16 -0500220 transport = Thrift::ServerSocket.new(options[:port])
James E. King III714c77c2018-03-20 19:58:38 -0400221 end
James E. King III9aaf2952018-03-20 15:06:08 -0400222else
Dmytro Shteflyuk96d62dd2026-02-13 15:16:16 -0500223 transport = Thrift::UNIXServerSocket.new(options[:domain_socket])
James E. King III9aaf2952018-03-20 15:06:08 -0400224end
225
Dmytro Shteflyuk96d62dd2026-02-13 15:16:16 -0500226workers = options[:workers] || 20
227server = case normalized_server_type
228when 'simple'
229 Thrift::SimpleServer.new(processor, transport, transport_factory, protocol_factory)
230when 'threaded'
231 Thrift::ThreadedServer.new(processor, transport, transport_factory, protocol_factory)
232when 'thread-pool'
233 Thrift::ThreadPoolServer.new(processor, transport, transport_factory, protocol_factory, workers)
234when 'nonblocking'
235 logger = Logger.new(STDERR)
236 logger.level = Logger::WARN
237 Thrift::NonblockingServer.new(processor, transport, transport_factory, protocol_factory, workers, logger)
238end
James E. King III9aaf2952018-03-20 15:06:08 -0400239
Dmytro Shteflyuk96d62dd2026-02-13 15:16:16 -0500240puts "Starting TestServer #{server.to_s}"
241server.serve
James E. King III9aaf2952018-03-20 15:06:08 -0400242puts "done."