blob: 92a4bcc45bc3d0bdfff1dd608b69d87cafa73090 [file] [log] [blame]
Allen George8b96bfb2016-11-02 08:01:08 -04001// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements. See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership. The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License. You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied. See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
Allen George55c3e4c2021-03-01 23:19:52 -050018use clap::{clap_app, value_t};
Allen George7ddbcc02020-11-08 09:51:19 -050019use log::*;
Allen Georgebc1344d2017-04-28 10:22:03 -040020
Allen George8b96bfb2016-11-02 08:01:08 -040021use std::collections::{BTreeMap, BTreeSet};
22use std::thread;
23use std::time::Duration;
24
Allen George55c3e4c2021-03-01 23:19:52 -050025use thrift::protocol::{
26 TBinaryInputProtocolFactory, TBinaryOutputProtocolFactory, TCompactInputProtocolFactory,
27 TCompactOutputProtocolFactory, TInputProtocolFactory, TOutputProtocolFactory,
28};
Allen Georgebc1344d2017-04-28 10:22:03 -040029use thrift::server::{TMultiplexedProcessor, TServer};
Allen George55c3e4c2021-03-01 23:19:52 -050030use thrift::transport::{
31 TBufferedReadTransportFactory, TBufferedWriteTransportFactory, TFramedReadTransportFactory,
32 TFramedWriteTransportFactory, TReadTransportFactory, TWriteTransportFactory,
33};
34use thrift::OrderedFloat;
Allen George8b96bfb2016-11-02 08:01:08 -040035use thrift_test::*;
36
37fn main() {
Allen George7ddbcc02020-11-08 09:51:19 -050038 env_logger::init();
Allen Georgebc1344d2017-04-28 10:22:03 -040039
40 debug!("initialized logger - running cross-test server");
41
Allen George8b96bfb2016-11-02 08:01:08 -040042 match run() {
Allen Georgebc1344d2017-04-28 10:22:03 -040043 Ok(()) => info!("cross-test server succeeded"),
Allen George8b96bfb2016-11-02 08:01:08 -040044 Err(e) => {
Allen Georgebc1344d2017-04-28 10:22:03 -040045 info!("cross-test server failed with error {:?}", e);
Allen George8b96bfb2016-11-02 08:01:08 -040046 std::process::exit(1);
47 }
48 }
49}
50
51fn run() -> thrift::Result<()> {
Allen George8b96bfb2016-11-02 08:01:08 -040052 // unsupported options:
Jens Geyer4a33b182020-03-22 13:46:34 +010053 // --pipe
Allen George8b96bfb2016-11-02 08:01:08 -040054 // --ssl
Allen George8b96bfb2016-11-02 08:01:08 -040055 let matches = clap_app!(rust_test_client =>
56 (version: "1.0")
57 (author: "Apache Thrift Developers <dev@thrift.apache.org>")
58 (about: "Rust Thrift test server")
59 (@arg port: --port +takes_value "port on which the test server listens")
tokcumf0336412022-03-30 11:39:08 +020060 (@arg domain_socket: --("domain-socket") +takes_value "Unix Domain Socket on which the test server listens")
Allen George8b96bfb2016-11-02 08:01:08 -040061 (@arg transport: --transport +takes_value "transport implementation to use (\"buffered\", \"framed\")")
62 (@arg protocol: --protocol +takes_value "protocol implementation to use (\"binary\", \"compact\")")
tokcumf0336412022-03-30 11:39:08 +020063 (@arg server_type: --("server-type") +takes_value "type of server instantiated (\"simple\", \"thread-pool\")")
Allen George0e22c362017-01-30 07:15:00 -050064 (@arg workers: -n --workers +takes_value "number of thread-pool workers (\"4\")")
65 )
tokcumf0336412022-03-30 11:39:08 +020066 .get_matches();
Allen George8b96bfb2016-11-02 08:01:08 -040067
68 let port = value_t!(matches, "port", u16).unwrap_or(9090);
tokcumf0336412022-03-30 11:39:08 +020069 let domain_socket = matches.value_of("domain_socket");
Allen George8b96bfb2016-11-02 08:01:08 -040070 let transport = matches.value_of("transport").unwrap_or("buffered");
71 let protocol = matches.value_of("protocol").unwrap_or("binary");
Allen George0e22c362017-01-30 07:15:00 -050072 let server_type = matches.value_of("server_type").unwrap_or("thread-pool");
73 let workers = value_t!(matches, "workers", usize).unwrap_or(4);
Allen George8b96bfb2016-11-02 08:01:08 -040074 let listen_address = format!("127.0.0.1:{}", port);
75
tokcumf0336412022-03-30 11:39:08 +020076 match domain_socket {
77 None => info!("Server is binding to {}", listen_address),
78 Some(domain_socket) => info!("Server is binding to {} (UDS)", domain_socket),
79 }
Allen George8b96bfb2016-11-02 08:01:08 -040080
Allen George55c3e4c2021-03-01 23:19:52 -050081 let (i_transport_factory, o_transport_factory): (
82 Box<dyn TReadTransportFactory>,
83 Box<dyn TWriteTransportFactory>,
84 ) = match &*transport {
85 "buffered" => (
86 Box::new(TBufferedReadTransportFactory::new()),
87 Box::new(TBufferedWriteTransportFactory::new()),
88 ),
89 "framed" => (
90 Box::new(TFramedReadTransportFactory::new()),
91 Box::new(TFramedWriteTransportFactory::new()),
92 ),
93 unknown => {
94 return Err(format!("unsupported transport type {}", unknown).into());
95 }
96 };
Allen George8b96bfb2016-11-02 08:01:08 -040097
Allen George55c3e4c2021-03-01 23:19:52 -050098 let (i_protocol_factory, o_protocol_factory): (
99 Box<dyn TInputProtocolFactory>,
100 Box<dyn TOutputProtocolFactory>,
101 ) = match &*protocol {
102 "binary" | "multi" | "multi:binary" => (
103 Box::new(TBinaryInputProtocolFactory::new()),
104 Box::new(TBinaryOutputProtocolFactory::new()),
105 ),
106 "compact" | "multic" | "multi:compact" => (
107 Box::new(TCompactInputProtocolFactory::new()),
108 Box::new(TCompactOutputProtocolFactory::new()),
109 ),
110 unknown => {
111 return Err(format!("unsupported transport type {}", unknown).into());
112 }
113 };
Allen George8b96bfb2016-11-02 08:01:08 -0400114
Allen Georgebc1344d2017-04-28 10:22:03 -0400115 let test_processor = ThriftTestSyncProcessor::new(ThriftTestSyncHandlerImpl {});
Allen George8b96bfb2016-11-02 08:01:08 -0400116
Allen Georgebc1344d2017-04-28 10:22:03 -0400117 match &*server_type {
118 "simple" | "thread-pool" => {
119 if protocol == "multi" || protocol == "multic" {
Allen George55c3e4c2021-03-01 23:19:52 -0500120 let second_service_processor =
121 SecondServiceSyncProcessor::new(SecondServiceSyncHandlerImpl {});
Allen George8b96bfb2016-11-02 08:01:08 -0400122
Allen Georgebc1344d2017-04-28 10:22:03 -0400123 let mut multiplexed_processor = TMultiplexedProcessor::new();
Allen George55c3e4c2021-03-01 23:19:52 -0500124 multiplexed_processor.register("ThriftTest", Box::new(test_processor), true)?;
125 multiplexed_processor.register(
126 "SecondService",
127 Box::new(second_service_processor),
128 false,
129 )?;
Allen Georgebc1344d2017-04-28 10:22:03 -0400130
131 let mut server = TServer::new(
132 i_transport_factory,
133 i_protocol_factory,
134 o_transport_factory,
135 o_protocol_factory,
136 multiplexed_processor,
137 workers,
138 );
139
tokcumf0336412022-03-30 11:39:08 +0200140 match domain_socket {
141 None => server.listen(&listen_address),
142 Some(domain_socket) => server.listen_uds(domain_socket),
143 }
Allen Georgebc1344d2017-04-28 10:22:03 -0400144 } else {
145 let mut server = TServer::new(
146 i_transport_factory,
147 i_protocol_factory,
148 o_transport_factory,
149 o_protocol_factory,
150 test_processor,
151 workers,
152 );
153
tokcumf0336412022-03-30 11:39:08 +0200154 match domain_socket {
155 None => server.listen(&listen_address),
156 Some(domain_socket) => server.listen_uds(domain_socket),
157 }
Allen Georgebc1344d2017-04-28 10:22:03 -0400158 }
159 }
tokcumf0336412022-03-30 11:39:08 +0200160
Allen Georgebc1344d2017-04-28 10:22:03 -0400161 unknown => Err(format!("unsupported server type {}", unknown).into()),
162 }
Allen George8b96bfb2016-11-02 08:01:08 -0400163}
164
165struct ThriftTestSyncHandlerImpl;
166impl ThriftTestSyncHandler for ThriftTestSyncHandlerImpl {
Allen George0e22c362017-01-30 07:15:00 -0500167 fn handle_test_void(&self) -> thrift::Result<()> {
Allen Georgebc1344d2017-04-28 10:22:03 -0400168 info!("testVoid()");
Allen George8b96bfb2016-11-02 08:01:08 -0400169 Ok(())
170 }
171
Allen George0e22c362017-01-30 07:15:00 -0500172 fn handle_test_string(&self, thing: String) -> thrift::Result<String> {
Allen Georgebc1344d2017-04-28 10:22:03 -0400173 info!("testString({})", &thing);
Allen George8b96bfb2016-11-02 08:01:08 -0400174 Ok(thing)
175 }
176
Allen George0e22c362017-01-30 07:15:00 -0500177 fn handle_test_bool(&self, thing: bool) -> thrift::Result<bool> {
Allen Georgebc1344d2017-04-28 10:22:03 -0400178 info!("testBool({})", thing);
Allen George8b96bfb2016-11-02 08:01:08 -0400179 Ok(thing)
180 }
181
Allen George0e22c362017-01-30 07:15:00 -0500182 fn handle_test_byte(&self, thing: i8) -> thrift::Result<i8> {
Allen Georgebc1344d2017-04-28 10:22:03 -0400183 info!("testByte({})", thing);
Allen George8b96bfb2016-11-02 08:01:08 -0400184 Ok(thing)
185 }
186
Allen George0e22c362017-01-30 07:15:00 -0500187 fn handle_test_i32(&self, thing: i32) -> thrift::Result<i32> {
Allen Georgebc1344d2017-04-28 10:22:03 -0400188 info!("testi32({})", thing);
Allen George8b96bfb2016-11-02 08:01:08 -0400189 Ok(thing)
190 }
191
Allen George0e22c362017-01-30 07:15:00 -0500192 fn handle_test_i64(&self, thing: i64) -> thrift::Result<i64> {
Allen Georgebc1344d2017-04-28 10:22:03 -0400193 info!("testi64({})", thing);
Allen George8b96bfb2016-11-02 08:01:08 -0400194 Ok(thing)
195 }
196
Allen George0e22c362017-01-30 07:15:00 -0500197 fn handle_test_double(&self, thing: OrderedFloat<f64>) -> thrift::Result<OrderedFloat<f64>> {
Allen Georgebc1344d2017-04-28 10:22:03 -0400198 info!("testDouble({})", thing);
Allen George8b96bfb2016-11-02 08:01:08 -0400199 Ok(thing)
200 }
201
Allen George0e22c362017-01-30 07:15:00 -0500202 fn handle_test_binary(&self, thing: Vec<u8>) -> thrift::Result<Vec<u8>> {
Allen Georgebc1344d2017-04-28 10:22:03 -0400203 info!("testBinary({:?})", thing);
Allen George8b96bfb2016-11-02 08:01:08 -0400204 Ok(thing)
205 }
206
Allen George0e22c362017-01-30 07:15:00 -0500207 fn handle_test_struct(&self, thing: Xtruct) -> thrift::Result<Xtruct> {
Allen Georgebc1344d2017-04-28 10:22:03 -0400208 info!("testStruct({:?})", thing);
Allen George8b96bfb2016-11-02 08:01:08 -0400209 Ok(thing)
210 }
211
Allen George0e22c362017-01-30 07:15:00 -0500212 fn handle_test_nest(&self, thing: Xtruct2) -> thrift::Result<Xtruct2> {
Allen Georgebc1344d2017-04-28 10:22:03 -0400213 info!("testNest({:?})", thing);
Allen George8b96bfb2016-11-02 08:01:08 -0400214 Ok(thing)
215 }
216
Allen George0e22c362017-01-30 07:15:00 -0500217 fn handle_test_map(&self, thing: BTreeMap<i32, i32>) -> thrift::Result<BTreeMap<i32, i32>> {
Allen Georgebc1344d2017-04-28 10:22:03 -0400218 info!("testMap({:?})", thing);
Allen George8b96bfb2016-11-02 08:01:08 -0400219 Ok(thing)
220 }
221
Allen George0e22c362017-01-30 07:15:00 -0500222 fn handle_test_string_map(
223 &self,
224 thing: BTreeMap<String, String>,
225 ) -> thrift::Result<BTreeMap<String, String>> {
Allen Georgebc1344d2017-04-28 10:22:03 -0400226 info!("testStringMap({:?})", thing);
Allen George8b96bfb2016-11-02 08:01:08 -0400227 Ok(thing)
228 }
229
Allen George0e22c362017-01-30 07:15:00 -0500230 fn handle_test_set(&self, thing: BTreeSet<i32>) -> thrift::Result<BTreeSet<i32>> {
Allen Georgebc1344d2017-04-28 10:22:03 -0400231 info!("testSet({:?})", thing);
Allen George8b96bfb2016-11-02 08:01:08 -0400232 Ok(thing)
233 }
234
Allen George0e22c362017-01-30 07:15:00 -0500235 fn handle_test_list(&self, thing: Vec<i32>) -> thrift::Result<Vec<i32>> {
Allen Georgebc1344d2017-04-28 10:22:03 -0400236 info!("testList({:?})", thing);
Allen George8b96bfb2016-11-02 08:01:08 -0400237 Ok(thing)
238 }
239
Allen George0e22c362017-01-30 07:15:00 -0500240 fn handle_test_enum(&self, thing: Numberz) -> thrift::Result<Numberz> {
Allen Georgebc1344d2017-04-28 10:22:03 -0400241 info!("testEnum({:?})", thing);
Allen George8b96bfb2016-11-02 08:01:08 -0400242 Ok(thing)
243 }
244
Allen George0e22c362017-01-30 07:15:00 -0500245 fn handle_test_typedef(&self, thing: UserId) -> thrift::Result<UserId> {
Allen Georgebc1344d2017-04-28 10:22:03 -0400246 info!("testTypedef({})", thing);
Allen George8b96bfb2016-11-02 08:01:08 -0400247 Ok(thing)
248 }
249
250 /// @return map<i32,map<i32,i32>> - returns a dictionary with these values:
Allen George0e22c362017-01-30 07:15:00 -0500251 /// {-4 => {-4 => -4, -3 => -3, -2 => -2, -1 => -1, }, 4 => {1 => 1, 2 =>
252 /// 2, 3 => 3, 4 => 4, }, }
253 fn handle_test_map_map(&self, hello: i32) -> thrift::Result<BTreeMap<i32, BTreeMap<i32, i32>>> {
Allen Georgebc1344d2017-04-28 10:22:03 -0400254 info!("testMapMap({})", hello);
Allen George8b96bfb2016-11-02 08:01:08 -0400255
256 let mut inner_map_0: BTreeMap<i32, i32> = BTreeMap::new();
Jiayu Liuaa855932022-06-26 05:00:25 +0200257 for i in -4..0_i32 {
Allen George8b96bfb2016-11-02 08:01:08 -0400258 inner_map_0.insert(i, i);
259 }
260
261 let mut inner_map_1: BTreeMap<i32, i32> = BTreeMap::new();
262 for i in 1..5 {
263 inner_map_1.insert(i, i);
264 }
265
266 let mut ret_map: BTreeMap<i32, BTreeMap<i32, i32>> = BTreeMap::new();
267 ret_map.insert(-4, inner_map_0);
268 ret_map.insert(4, inner_map_1);
269
270 Ok(ret_map)
271 }
272
273 /// Creates a the returned map with these values and prints it out:
274 /// { 1 => { 2 => argument,
275 /// 3 => argument,
276 /// },
277 /// 2 => { 6 => <empty Insanity struct>, },
278 /// }
279 /// return map<UserId, map<Numberz,Insanity>> - a map with the above values
Allen George0e22c362017-01-30 07:15:00 -0500280 fn handle_test_insanity(
281 &self,
282 argument: Insanity,
283 ) -> thrift::Result<BTreeMap<UserId, BTreeMap<Numberz, Insanity>>> {
Allen Georgebc1344d2017-04-28 10:22:03 -0400284 info!("testInsanity({:?})", argument);
Allen George8b96bfb2016-11-02 08:01:08 -0400285 let mut map_0: BTreeMap<Numberz, Insanity> = BTreeMap::new();
Allen George2e90ef52021-03-01 14:47:04 -0500286 map_0.insert(Numberz::TWO, argument.clone());
287 map_0.insert(Numberz::THREE, argument);
Allen George8b96bfb2016-11-02 08:01:08 -0400288
289 let mut map_1: BTreeMap<Numberz, Insanity> = BTreeMap::new();
290 let insanity = Insanity {
291 user_map: None,
292 xtructs: None,
293 };
Allen George2e90ef52021-03-01 14:47:04 -0500294 map_1.insert(Numberz::SIX, insanity);
Allen George8b96bfb2016-11-02 08:01:08 -0400295
296 let mut ret: BTreeMap<UserId, BTreeMap<Numberz, Insanity>> = BTreeMap::new();
297 ret.insert(1, map_0);
298 ret.insert(2, map_1);
299
300 Ok(ret)
301 }
302
Allen George0e22c362017-01-30 07:15:00 -0500303 /// returns an Xtruct with:
304 /// string_thing = "Hello2", byte_thing = arg0, i32_thing = arg1 and
305 /// i64_thing = arg2
306 fn handle_test_multi(
307 &self,
308 arg0: i8,
309 arg1: i32,
310 arg2: i64,
311 _: BTreeMap<i16, String>,
312 _: Numberz,
313 _: UserId,
314 ) -> thrift::Result<Xtruct> {
Allen George8b96bfb2016-11-02 08:01:08 -0400315 let x_ret = Xtruct {
316 string_thing: Some("Hello2".to_owned()),
317 byte_thing: Some(arg0),
318 i32_thing: Some(arg1),
319 i64_thing: Some(arg2),
320 };
321
322 Ok(x_ret)
323 }
324
Allen George0e22c362017-01-30 07:15:00 -0500325 /// if arg == "Xception" throw Xception with errorCode = 1001 and message =
326 /// arg
Allen George8b96bfb2016-11-02 08:01:08 -0400327 /// else if arg == "TException" throw TException
328 /// else do not throw anything
Allen George0e22c362017-01-30 07:15:00 -0500329 fn handle_test_exception(&self, arg: String) -> thrift::Result<()> {
Allen Georgebc1344d2017-04-28 10:22:03 -0400330 info!("testException({})", arg);
Allen George8b96bfb2016-11-02 08:01:08 -0400331
332 match &*arg {
Allen George55c3e4c2021-03-01 23:19:52 -0500333 "Xception" => Err((Xception {
334 error_code: Some(1001),
335 message: Some(arg),
336 })
337 .into()),
Allen George8b96bfb2016-11-02 08:01:08 -0400338 "TException" => Err("this is a random error".into()),
339 _ => Ok(()),
340 }
341 }
342
Allen George0e22c362017-01-30 07:15:00 -0500343 /// if arg0 == "Xception":
344 /// throw Xception with errorCode = 1001 and message = "This is an
345 /// Xception"
346 /// else if arg0 == "Xception2":
347 /// throw Xception2 with errorCode = 2002 and struct_thing.string_thing =
348 /// "This is an Xception2"
349 // else:
350 // do not throw anything and return Xtruct with string_thing = arg1
351 fn handle_test_multi_exception(&self, arg0: String, arg1: String) -> thrift::Result<Xtruct> {
Allen George8b96bfb2016-11-02 08:01:08 -0400352 match &*arg0 {
Allen George55c3e4c2021-03-01 23:19:52 -0500353 "Xception" => Err((Xception {
354 error_code: Some(1001),
355 message: Some("This is an Xception".to_owned()),
356 })
357 .into()),
358 "Xception2" => Err((Xception2 {
359 error_code: Some(2002),
360 struct_thing: Some(Xtruct {
361 string_thing: Some("This is an Xception2".to_owned()),
362 byte_thing: None,
363 i32_thing: None,
364 i64_thing: None,
365 }),
366 })
367 .into()),
368 _ => Ok(Xtruct {
369 string_thing: Some(arg1),
370 byte_thing: None,
371 i32_thing: None,
372 i64_thing: None,
373 }),
Allen George8b96bfb2016-11-02 08:01:08 -0400374 }
375 }
376
Allen George0e22c362017-01-30 07:15:00 -0500377 fn handle_test_oneway(&self, seconds_to_sleep: i32) -> thrift::Result<()> {
Allen George8b96bfb2016-11-02 08:01:08 -0400378 thread::sleep(Duration::from_secs(seconds_to_sleep as u64));
379 Ok(())
380 }
381}
Allen Georgebc1344d2017-04-28 10:22:03 -0400382
383struct SecondServiceSyncHandlerImpl;
384impl SecondServiceSyncHandler for SecondServiceSyncHandlerImpl {
Allen Georgebc1344d2017-04-28 10:22:03 -0400385 fn handle_secondtest_string(&self, thing: String) -> thrift::Result<String> {
James E. King, III20e16bc2017-11-18 22:37:54 -0500386 info!("(second)testString({})", &thing);
Allen Georgebc1344d2017-04-28 10:22:03 -0400387 let ret = format!("testString(\"{}\")", &thing);
388 Ok(ret)
389 }
390}