blob: c1f31758a58e219acf07b683042448cec56a627b [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 George7ddbcc02020-11-08 09:51:19 -050018use env_logger;
19use log::*;
20use clap::{clap_app, value_t};
Allen Georgebc1344d2017-04-28 10:22:03 -040021
Allen George8b96bfb2016-11-02 08:01:08 -040022use std::collections::{BTreeMap, BTreeSet};
23use std::thread;
24use std::time::Duration;
25
Allen George7ddbcc02020-11-08 09:51:19 -050026use thrift;
27use thrift::OrderedFloat;
Allen George8b96bfb2016-11-02 08:01:08 -040028use thrift::protocol::{TBinaryInputProtocolFactory, TBinaryOutputProtocolFactory,
29 TCompactInputProtocolFactory, TCompactOutputProtocolFactory,
30 TInputProtocolFactory, TOutputProtocolFactory};
Allen Georgebc1344d2017-04-28 10:22:03 -040031use thrift::server::{TMultiplexedProcessor, TServer};
Allen George0e22c362017-01-30 07:15:00 -050032use thrift::transport::{TBufferedReadTransportFactory, TBufferedWriteTransportFactory,
33 TFramedReadTransportFactory, TFramedWriteTransportFactory,
34 TReadTransportFactory, TWriteTransportFactory};
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<()> {
52
53 // unsupported options:
54 // --domain-socket
Jens Geyer4a33b182020-03-22 13:46:34 +010055 // --pipe
Allen George8b96bfb2016-11-02 08:01:08 -040056 // --ssl
Allen George8b96bfb2016-11-02 08:01:08 -040057 let matches = clap_app!(rust_test_client =>
58 (version: "1.0")
59 (author: "Apache Thrift Developers <dev@thrift.apache.org>")
60 (about: "Rust Thrift test server")
61 (@arg port: --port +takes_value "port on which the test server listens")
62 (@arg transport: --transport +takes_value "transport implementation to use (\"buffered\", \"framed\")")
63 (@arg protocol: --protocol +takes_value "protocol implementation to use (\"binary\", \"compact\")")
Allen George0e22c362017-01-30 07:15:00 -050064 (@arg server_type: --server_type +takes_value "type of server instantiated (\"simple\", \"thread-pool\")")
65 (@arg workers: -n --workers +takes_value "number of thread-pool workers (\"4\")")
66 )
67 .get_matches();
Allen George8b96bfb2016-11-02 08:01:08 -040068
69 let port = value_t!(matches, "port", u16).unwrap_or(9090);
70 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
Allen Georgebc1344d2017-04-28 10:22:03 -040076 info!("binding to {}", listen_address);
Allen George8b96bfb2016-11-02 08:01:08 -040077
Allen Georgeb0d14132020-03-29 11:48:55 -040078 let (i_transport_factory, o_transport_factory): (Box<dyn TReadTransportFactory>,
79 Box<dyn TWriteTransportFactory>) =
Allen George0e22c362017-01-30 07:15:00 -050080 match &*transport {
81 "buffered" => {
82 (Box::new(TBufferedReadTransportFactory::new()),
83 Box::new(TBufferedWriteTransportFactory::new()))
84 }
85 "framed" => {
86 (Box::new(TFramedReadTransportFactory::new()),
87 Box::new(TFramedWriteTransportFactory::new()))
88 }
89 unknown => {
90 return Err(format!("unsupported transport type {}", unknown).into());
91 }
92 };
Allen George8b96bfb2016-11-02 08:01:08 -040093
Allen Georgeb0d14132020-03-29 11:48:55 -040094 let (i_protocol_factory, o_protocol_factory): (Box<dyn TInputProtocolFactory>,
95 Box<dyn TOutputProtocolFactory>) =
Allen George8b96bfb2016-11-02 08:01:08 -040096 match &*protocol {
Allen Georgebc1344d2017-04-28 10:22:03 -040097 "binary" | "multi" | "multi:binary" => {
Allen George8b96bfb2016-11-02 08:01:08 -040098 (Box::new(TBinaryInputProtocolFactory::new()),
99 Box::new(TBinaryOutputProtocolFactory::new()))
100 }
Allen Georgebc1344d2017-04-28 10:22:03 -0400101 "compact" | "multic" | "multi:compact" => {
Allen George8b96bfb2016-11-02 08:01:08 -0400102 (Box::new(TCompactInputProtocolFactory::new()),
103 Box::new(TCompactOutputProtocolFactory::new()))
104 }
105 unknown => {
106 return Err(format!("unsupported transport type {}", unknown).into());
107 }
108 };
109
Allen Georgebc1344d2017-04-28 10:22:03 -0400110 let test_processor = ThriftTestSyncProcessor::new(ThriftTestSyncHandlerImpl {});
Allen George8b96bfb2016-11-02 08:01:08 -0400111
Allen Georgebc1344d2017-04-28 10:22:03 -0400112 match &*server_type {
113 "simple" | "thread-pool" => {
114 if protocol == "multi" || protocol == "multic" {
115 let second_service_processor = SecondServiceSyncProcessor::new(SecondServiceSyncHandlerImpl {},);
Allen George8b96bfb2016-11-02 08:01:08 -0400116
Allen Georgebc1344d2017-04-28 10:22:03 -0400117 let mut multiplexed_processor = TMultiplexedProcessor::new();
118 multiplexed_processor
119 .register("ThriftTest", Box::new(test_processor), true)?;
120 multiplexed_processor
121 .register("SecondService", Box::new(second_service_processor), false)?;
122
123 let mut server = TServer::new(
124 i_transport_factory,
125 i_protocol_factory,
126 o_transport_factory,
127 o_protocol_factory,
128 multiplexed_processor,
129 workers,
130 );
131
132 server.listen(&listen_address)
133 } else {
134 let mut server = TServer::new(
135 i_transport_factory,
136 i_protocol_factory,
137 o_transport_factory,
138 o_protocol_factory,
139 test_processor,
140 workers,
141 );
142
143 server.listen(&listen_address)
144 }
145 }
146 unknown => Err(format!("unsupported server type {}", unknown).into()),
147 }
Allen George8b96bfb2016-11-02 08:01:08 -0400148}
149
150struct ThriftTestSyncHandlerImpl;
151impl ThriftTestSyncHandler for ThriftTestSyncHandlerImpl {
Allen George0e22c362017-01-30 07:15:00 -0500152 fn handle_test_void(&self) -> thrift::Result<()> {
Allen Georgebc1344d2017-04-28 10:22:03 -0400153 info!("testVoid()");
Allen George8b96bfb2016-11-02 08:01:08 -0400154 Ok(())
155 }
156
Allen George0e22c362017-01-30 07:15:00 -0500157 fn handle_test_string(&self, thing: String) -> thrift::Result<String> {
Allen Georgebc1344d2017-04-28 10:22:03 -0400158 info!("testString({})", &thing);
Allen George8b96bfb2016-11-02 08:01:08 -0400159 Ok(thing)
160 }
161
Allen George0e22c362017-01-30 07:15:00 -0500162 fn handle_test_bool(&self, thing: bool) -> thrift::Result<bool> {
Allen Georgebc1344d2017-04-28 10:22:03 -0400163 info!("testBool({})", thing);
Allen George8b96bfb2016-11-02 08:01:08 -0400164 Ok(thing)
165 }
166
Allen George0e22c362017-01-30 07:15:00 -0500167 fn handle_test_byte(&self, thing: i8) -> thrift::Result<i8> {
Allen Georgebc1344d2017-04-28 10:22:03 -0400168 info!("testByte({})", thing);
Allen George8b96bfb2016-11-02 08:01:08 -0400169 Ok(thing)
170 }
171
Allen George0e22c362017-01-30 07:15:00 -0500172 fn handle_test_i32(&self, thing: i32) -> thrift::Result<i32> {
Allen Georgebc1344d2017-04-28 10:22:03 -0400173 info!("testi32({})", thing);
Allen George8b96bfb2016-11-02 08:01:08 -0400174 Ok(thing)
175 }
176
Allen George0e22c362017-01-30 07:15:00 -0500177 fn handle_test_i64(&self, thing: i64) -> thrift::Result<i64> {
Allen Georgebc1344d2017-04-28 10:22:03 -0400178 info!("testi64({})", thing);
Allen George8b96bfb2016-11-02 08:01:08 -0400179 Ok(thing)
180 }
181
Allen George0e22c362017-01-30 07:15:00 -0500182 fn handle_test_double(&self, thing: OrderedFloat<f64>) -> thrift::Result<OrderedFloat<f64>> {
Allen Georgebc1344d2017-04-28 10:22:03 -0400183 info!("testDouble({})", thing);
Allen George8b96bfb2016-11-02 08:01:08 -0400184 Ok(thing)
185 }
186
Allen George0e22c362017-01-30 07:15:00 -0500187 fn handle_test_binary(&self, thing: Vec<u8>) -> thrift::Result<Vec<u8>> {
Allen Georgebc1344d2017-04-28 10:22:03 -0400188 info!("testBinary({:?})", thing);
Allen George8b96bfb2016-11-02 08:01:08 -0400189 Ok(thing)
190 }
191
Allen George0e22c362017-01-30 07:15:00 -0500192 fn handle_test_struct(&self, thing: Xtruct) -> thrift::Result<Xtruct> {
Allen Georgebc1344d2017-04-28 10:22:03 -0400193 info!("testStruct({:?})", thing);
Allen George8b96bfb2016-11-02 08:01:08 -0400194 Ok(thing)
195 }
196
Allen George0e22c362017-01-30 07:15:00 -0500197 fn handle_test_nest(&self, thing: Xtruct2) -> thrift::Result<Xtruct2> {
Allen Georgebc1344d2017-04-28 10:22:03 -0400198 info!("testNest({:?})", thing);
Allen George8b96bfb2016-11-02 08:01:08 -0400199 Ok(thing)
200 }
201
Allen George0e22c362017-01-30 07:15:00 -0500202 fn handle_test_map(&self, thing: BTreeMap<i32, i32>) -> thrift::Result<BTreeMap<i32, i32>> {
Allen Georgebc1344d2017-04-28 10:22:03 -0400203 info!("testMap({:?})", thing);
Allen George8b96bfb2016-11-02 08:01:08 -0400204 Ok(thing)
205 }
206
Allen George0e22c362017-01-30 07:15:00 -0500207 fn handle_test_string_map(
208 &self,
209 thing: BTreeMap<String, String>,
210 ) -> thrift::Result<BTreeMap<String, String>> {
Allen Georgebc1344d2017-04-28 10:22:03 -0400211 info!("testStringMap({:?})", thing);
Allen George8b96bfb2016-11-02 08:01:08 -0400212 Ok(thing)
213 }
214
Allen George0e22c362017-01-30 07:15:00 -0500215 fn handle_test_set(&self, thing: BTreeSet<i32>) -> thrift::Result<BTreeSet<i32>> {
Allen Georgebc1344d2017-04-28 10:22:03 -0400216 info!("testSet({:?})", thing);
Allen George8b96bfb2016-11-02 08:01:08 -0400217 Ok(thing)
218 }
219
Allen George0e22c362017-01-30 07:15:00 -0500220 fn handle_test_list(&self, thing: Vec<i32>) -> thrift::Result<Vec<i32>> {
Allen Georgebc1344d2017-04-28 10:22:03 -0400221 info!("testList({:?})", thing);
Allen George8b96bfb2016-11-02 08:01:08 -0400222 Ok(thing)
223 }
224
Allen George0e22c362017-01-30 07:15:00 -0500225 fn handle_test_enum(&self, thing: Numberz) -> thrift::Result<Numberz> {
Allen Georgebc1344d2017-04-28 10:22:03 -0400226 info!("testEnum({:?})", thing);
Allen George8b96bfb2016-11-02 08:01:08 -0400227 Ok(thing)
228 }
229
Allen George0e22c362017-01-30 07:15:00 -0500230 fn handle_test_typedef(&self, thing: UserId) -> thrift::Result<UserId> {
Allen Georgebc1344d2017-04-28 10:22:03 -0400231 info!("testTypedef({})", thing);
Allen George8b96bfb2016-11-02 08:01:08 -0400232 Ok(thing)
233 }
234
235 /// @return map<i32,map<i32,i32>> - returns a dictionary with these values:
Allen George0e22c362017-01-30 07:15:00 -0500236 /// {-4 => {-4 => -4, -3 => -3, -2 => -2, -1 => -1, }, 4 => {1 => 1, 2 =>
237 /// 2, 3 => 3, 4 => 4, }, }
238 fn handle_test_map_map(&self, hello: i32) -> thrift::Result<BTreeMap<i32, BTreeMap<i32, i32>>> {
Allen Georgebc1344d2017-04-28 10:22:03 -0400239 info!("testMapMap({})", hello);
Allen George8b96bfb2016-11-02 08:01:08 -0400240
241 let mut inner_map_0: BTreeMap<i32, i32> = BTreeMap::new();
242 for i in -4..(0 as i32) {
243 inner_map_0.insert(i, i);
244 }
245
246 let mut inner_map_1: BTreeMap<i32, i32> = BTreeMap::new();
247 for i in 1..5 {
248 inner_map_1.insert(i, i);
249 }
250
251 let mut ret_map: BTreeMap<i32, BTreeMap<i32, i32>> = BTreeMap::new();
252 ret_map.insert(-4, inner_map_0);
253 ret_map.insert(4, inner_map_1);
254
255 Ok(ret_map)
256 }
257
258 /// Creates a the returned map with these values and prints it out:
259 /// { 1 => { 2 => argument,
260 /// 3 => argument,
261 /// },
262 /// 2 => { 6 => <empty Insanity struct>, },
263 /// }
264 /// return map<UserId, map<Numberz,Insanity>> - a map with the above values
Allen George0e22c362017-01-30 07:15:00 -0500265 fn handle_test_insanity(
266 &self,
267 argument: Insanity,
268 ) -> thrift::Result<BTreeMap<UserId, BTreeMap<Numberz, Insanity>>> {
Allen Georgebc1344d2017-04-28 10:22:03 -0400269 info!("testInsanity({:?})", argument);
Allen George8b96bfb2016-11-02 08:01:08 -0400270 let mut map_0: BTreeMap<Numberz, Insanity> = BTreeMap::new();
Allen George2e90ef52021-03-01 14:47:04 -0500271 map_0.insert(Numberz::TWO, argument.clone());
272 map_0.insert(Numberz::THREE, argument);
Allen George8b96bfb2016-11-02 08:01:08 -0400273
274 let mut map_1: BTreeMap<Numberz, Insanity> = BTreeMap::new();
275 let insanity = Insanity {
276 user_map: None,
277 xtructs: None,
278 };
Allen George2e90ef52021-03-01 14:47:04 -0500279 map_1.insert(Numberz::SIX, insanity);
Allen George8b96bfb2016-11-02 08:01:08 -0400280
281 let mut ret: BTreeMap<UserId, BTreeMap<Numberz, Insanity>> = BTreeMap::new();
282 ret.insert(1, map_0);
283 ret.insert(2, map_1);
284
285 Ok(ret)
286 }
287
Allen George0e22c362017-01-30 07:15:00 -0500288 /// returns an Xtruct with:
289 /// string_thing = "Hello2", byte_thing = arg0, i32_thing = arg1 and
290 /// i64_thing = arg2
291 fn handle_test_multi(
292 &self,
293 arg0: i8,
294 arg1: i32,
295 arg2: i64,
296 _: BTreeMap<i16, String>,
297 _: Numberz,
298 _: UserId,
299 ) -> thrift::Result<Xtruct> {
Allen George8b96bfb2016-11-02 08:01:08 -0400300 let x_ret = Xtruct {
301 string_thing: Some("Hello2".to_owned()),
302 byte_thing: Some(arg0),
303 i32_thing: Some(arg1),
304 i64_thing: Some(arg2),
305 };
306
307 Ok(x_ret)
308 }
309
Allen George0e22c362017-01-30 07:15:00 -0500310 /// if arg == "Xception" throw Xception with errorCode = 1001 and message =
311 /// arg
Allen George8b96bfb2016-11-02 08:01:08 -0400312 /// else if arg == "TException" throw TException
313 /// else do not throw anything
Allen George0e22c362017-01-30 07:15:00 -0500314 fn handle_test_exception(&self, arg: String) -> thrift::Result<()> {
Allen Georgebc1344d2017-04-28 10:22:03 -0400315 info!("testException({})", arg);
Allen George8b96bfb2016-11-02 08:01:08 -0400316
317 match &*arg {
318 "Xception" => {
Allen George0e22c362017-01-30 07:15:00 -0500319 Err(
320 (Xception {
321 error_code: Some(1001),
322 message: Some(arg),
323 })
324 .into(),
325 )
Allen George8b96bfb2016-11-02 08:01:08 -0400326 }
327 "TException" => Err("this is a random error".into()),
328 _ => Ok(()),
329 }
330 }
331
Allen George0e22c362017-01-30 07:15:00 -0500332 /// if arg0 == "Xception":
333 /// throw Xception with errorCode = 1001 and message = "This is an
334 /// Xception"
335 /// else if arg0 == "Xception2":
336 /// throw Xception2 with errorCode = 2002 and struct_thing.string_thing =
337 /// "This is an Xception2"
338 // else:
339 // do not throw anything and return Xtruct with string_thing = arg1
340 fn handle_test_multi_exception(&self, arg0: String, arg1: String) -> thrift::Result<Xtruct> {
Allen George8b96bfb2016-11-02 08:01:08 -0400341 match &*arg0 {
342 "Xception" => {
Allen George0e22c362017-01-30 07:15:00 -0500343 Err(
344 (Xception {
345 error_code: Some(1001),
346 message: Some("This is an Xception".to_owned()),
347 })
348 .into(),
349 )
Allen George8b96bfb2016-11-02 08:01:08 -0400350 }
351 "Xception2" => {
Allen George0e22c362017-01-30 07:15:00 -0500352 Err(
353 (Xception2 {
354 error_code: Some(2002),
355 struct_thing: Some(
356 Xtruct {
357 string_thing: Some("This is an Xception2".to_owned()),
358 byte_thing: None,
359 i32_thing: None,
360 i64_thing: None,
361 },
362 ),
363 })
364 .into(),
365 )
Allen George8b96bfb2016-11-02 08:01:08 -0400366 }
367 _ => {
Allen George0e22c362017-01-30 07:15:00 -0500368 Ok(
369 Xtruct {
370 string_thing: Some(arg1),
371 byte_thing: None,
372 i32_thing: None,
373 i64_thing: None,
374 },
375 )
Allen George8b96bfb2016-11-02 08:01:08 -0400376 }
377 }
378 }
379
Allen George0e22c362017-01-30 07:15:00 -0500380 fn handle_test_oneway(&self, seconds_to_sleep: i32) -> thrift::Result<()> {
Allen George8b96bfb2016-11-02 08:01:08 -0400381 thread::sleep(Duration::from_secs(seconds_to_sleep as u64));
382 Ok(())
383 }
384}
Allen Georgebc1344d2017-04-28 10:22:03 -0400385
386struct SecondServiceSyncHandlerImpl;
387impl SecondServiceSyncHandler for SecondServiceSyncHandlerImpl {
Allen Georgebc1344d2017-04-28 10:22:03 -0400388 fn handle_secondtest_string(&self, thing: String) -> thrift::Result<String> {
James E. King, III20e16bc2017-11-18 22:37:54 -0500389 info!("(second)testString({})", &thing);
Allen Georgebc1344d2017-04-28 10:22:03 -0400390 let ret = format!("testString(\"{}\")", &thing);
391 Ok(ret)
392 }
393}