blob: 9c738ab01e3754615eecc386d2e58b34de08d6e6 [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
18#[macro_use]
19extern crate clap;
20extern crate ordered_float;
21extern crate thrift;
22extern crate thrift_test;
23
24use ordered_float::OrderedFloat;
25use std::collections::{BTreeMap, BTreeSet};
26use std::thread;
27use std::time::Duration;
28
29use thrift::protocol::{TBinaryInputProtocolFactory, TBinaryOutputProtocolFactory,
30 TCompactInputProtocolFactory, TCompactOutputProtocolFactory,
31 TInputProtocolFactory, TOutputProtocolFactory};
Allen George0e22c362017-01-30 07:15:00 -050032use thrift::server::TServer;
33use thrift::transport::{TBufferedReadTransportFactory, TBufferedWriteTransportFactory,
34 TFramedReadTransportFactory, TFramedWriteTransportFactory,
35 TReadTransportFactory, TWriteTransportFactory};
Allen George8b96bfb2016-11-02 08:01:08 -040036use thrift_test::*;
37
38fn main() {
39 match run() {
40 Ok(()) => println!("cross-test server succeeded"),
41 Err(e) => {
42 println!("cross-test server failed with error {:?}", e);
43 std::process::exit(1);
44 }
45 }
46}
47
48fn run() -> thrift::Result<()> {
49
50 // unsupported options:
51 // --domain-socket
52 // --named-pipe
53 // --ssl
Allen George8b96bfb2016-11-02 08:01:08 -040054 let matches = clap_app!(rust_test_client =>
55 (version: "1.0")
56 (author: "Apache Thrift Developers <dev@thrift.apache.org>")
57 (about: "Rust Thrift test server")
58 (@arg port: --port +takes_value "port on which the test server listens")
59 (@arg transport: --transport +takes_value "transport implementation to use (\"buffered\", \"framed\")")
60 (@arg protocol: --protocol +takes_value "protocol implementation to use (\"binary\", \"compact\")")
Allen George0e22c362017-01-30 07:15:00 -050061 (@arg server_type: --server_type +takes_value "type of server instantiated (\"simple\", \"thread-pool\")")
62 (@arg workers: -n --workers +takes_value "number of thread-pool workers (\"4\")")
63 )
64 .get_matches();
Allen George8b96bfb2016-11-02 08:01:08 -040065
66 let port = value_t!(matches, "port", u16).unwrap_or(9090);
67 let transport = matches.value_of("transport").unwrap_or("buffered");
68 let protocol = matches.value_of("protocol").unwrap_or("binary");
Allen George0e22c362017-01-30 07:15:00 -050069 let server_type = matches.value_of("server_type").unwrap_or("thread-pool");
70 let workers = value_t!(matches, "workers", usize).unwrap_or(4);
Allen George8b96bfb2016-11-02 08:01:08 -040071 let listen_address = format!("127.0.0.1:{}", port);
72
73 println!("binding to {}", listen_address);
74
Allen George0e22c362017-01-30 07:15:00 -050075 let (i_transport_factory, o_transport_factory): (Box<TReadTransportFactory>,
76 Box<TWriteTransportFactory>) =
77 match &*transport {
78 "buffered" => {
79 (Box::new(TBufferedReadTransportFactory::new()),
80 Box::new(TBufferedWriteTransportFactory::new()))
81 }
82 "framed" => {
83 (Box::new(TFramedReadTransportFactory::new()),
84 Box::new(TFramedWriteTransportFactory::new()))
85 }
86 unknown => {
87 return Err(format!("unsupported transport type {}", unknown).into());
88 }
89 };
Allen George8b96bfb2016-11-02 08:01:08 -040090
91 let (i_protocol_factory, o_protocol_factory): (Box<TInputProtocolFactory>,
92 Box<TOutputProtocolFactory>) =
93 match &*protocol {
94 "binary" => {
95 (Box::new(TBinaryInputProtocolFactory::new()),
96 Box::new(TBinaryOutputProtocolFactory::new()))
97 }
98 "compact" => {
99 (Box::new(TCompactInputProtocolFactory::new()),
100 Box::new(TCompactOutputProtocolFactory::new()))
101 }
102 unknown => {
103 return Err(format!("unsupported transport type {}", unknown).into());
104 }
105 };
106
107 let processor = ThriftTestSyncProcessor::new(ThriftTestSyncHandlerImpl {});
108
109 let mut server = match &*server_type {
110 "simple" => {
Allen George0e22c362017-01-30 07:15:00 -0500111 TServer::new(
112 i_transport_factory,
113 i_protocol_factory,
114 o_transport_factory,
115 o_protocol_factory,
116 processor,
117 1,
118 )
119 }
120 "thread-pool" => {
121 TServer::new(
122 i_transport_factory,
123 i_protocol_factory,
124 o_transport_factory,
125 o_protocol_factory,
126 processor,
127 workers,
128 )
Allen George8b96bfb2016-11-02 08:01:08 -0400129 }
130 unknown => {
131 return Err(format!("unsupported server type {}", unknown).into());
132 }
133 };
134
135 server.listen(&listen_address)
136}
137
138struct ThriftTestSyncHandlerImpl;
139impl ThriftTestSyncHandler for ThriftTestSyncHandlerImpl {
Allen George0e22c362017-01-30 07:15:00 -0500140 fn handle_test_void(&self) -> thrift::Result<()> {
Allen George8b96bfb2016-11-02 08:01:08 -0400141 println!("testVoid()");
142 Ok(())
143 }
144
Allen George0e22c362017-01-30 07:15:00 -0500145 fn handle_test_string(&self, thing: String) -> thrift::Result<String> {
Allen George8b96bfb2016-11-02 08:01:08 -0400146 println!("testString({})", &thing);
147 Ok(thing)
148 }
149
Allen George0e22c362017-01-30 07:15:00 -0500150 fn handle_test_bool(&self, thing: bool) -> thrift::Result<bool> {
Allen George8b96bfb2016-11-02 08:01:08 -0400151 println!("testBool({})", thing);
152 Ok(thing)
153 }
154
Allen George0e22c362017-01-30 07:15:00 -0500155 fn handle_test_byte(&self, thing: i8) -> thrift::Result<i8> {
Allen George8b96bfb2016-11-02 08:01:08 -0400156 println!("testByte({})", thing);
157 Ok(thing)
158 }
159
Allen George0e22c362017-01-30 07:15:00 -0500160 fn handle_test_i32(&self, thing: i32) -> thrift::Result<i32> {
Allen George8b96bfb2016-11-02 08:01:08 -0400161 println!("testi32({})", thing);
162 Ok(thing)
163 }
164
Allen George0e22c362017-01-30 07:15:00 -0500165 fn handle_test_i64(&self, thing: i64) -> thrift::Result<i64> {
Allen George8b96bfb2016-11-02 08:01:08 -0400166 println!("testi64({})", thing);
167 Ok(thing)
168 }
169
Allen George0e22c362017-01-30 07:15:00 -0500170 fn handle_test_double(&self, thing: OrderedFloat<f64>) -> thrift::Result<OrderedFloat<f64>> {
Allen George8b96bfb2016-11-02 08:01:08 -0400171 println!("testDouble({})", thing);
172 Ok(thing)
173 }
174
Allen George0e22c362017-01-30 07:15:00 -0500175 fn handle_test_binary(&self, thing: Vec<u8>) -> thrift::Result<Vec<u8>> {
Allen George8b96bfb2016-11-02 08:01:08 -0400176 println!("testBinary({:?})", thing);
177 Ok(thing)
178 }
179
Allen George0e22c362017-01-30 07:15:00 -0500180 fn handle_test_struct(&self, thing: Xtruct) -> thrift::Result<Xtruct> {
Allen George8b96bfb2016-11-02 08:01:08 -0400181 println!("testStruct({:?})", thing);
182 Ok(thing)
183 }
184
Allen George0e22c362017-01-30 07:15:00 -0500185 fn handle_test_nest(&self, thing: Xtruct2) -> thrift::Result<Xtruct2> {
Allen George8b96bfb2016-11-02 08:01:08 -0400186 println!("testNest({:?})", thing);
187 Ok(thing)
188 }
189
Allen George0e22c362017-01-30 07:15:00 -0500190 fn handle_test_map(&self, thing: BTreeMap<i32, i32>) -> thrift::Result<BTreeMap<i32, i32>> {
Allen George8b96bfb2016-11-02 08:01:08 -0400191 println!("testMap({:?})", thing);
192 Ok(thing)
193 }
194
Allen George0e22c362017-01-30 07:15:00 -0500195 fn handle_test_string_map(
196 &self,
197 thing: BTreeMap<String, String>,
198 ) -> thrift::Result<BTreeMap<String, String>> {
Allen George8b96bfb2016-11-02 08:01:08 -0400199 println!("testStringMap({:?})", thing);
200 Ok(thing)
201 }
202
Allen George0e22c362017-01-30 07:15:00 -0500203 fn handle_test_set(&self, thing: BTreeSet<i32>) -> thrift::Result<BTreeSet<i32>> {
Allen George8b96bfb2016-11-02 08:01:08 -0400204 println!("testSet({:?})", thing);
205 Ok(thing)
206 }
207
Allen George0e22c362017-01-30 07:15:00 -0500208 fn handle_test_list(&self, thing: Vec<i32>) -> thrift::Result<Vec<i32>> {
Allen George8b96bfb2016-11-02 08:01:08 -0400209 println!("testList({:?})", thing);
210 Ok(thing)
211 }
212
Allen George0e22c362017-01-30 07:15:00 -0500213 fn handle_test_enum(&self, thing: Numberz) -> thrift::Result<Numberz> {
Allen George8b96bfb2016-11-02 08:01:08 -0400214 println!("testEnum({:?})", thing);
215 Ok(thing)
216 }
217
Allen George0e22c362017-01-30 07:15:00 -0500218 fn handle_test_typedef(&self, thing: UserId) -> thrift::Result<UserId> {
Allen George8b96bfb2016-11-02 08:01:08 -0400219 println!("testTypedef({})", thing);
220 Ok(thing)
221 }
222
223 /// @return map<i32,map<i32,i32>> - returns a dictionary with these values:
Allen George0e22c362017-01-30 07:15:00 -0500224 /// {-4 => {-4 => -4, -3 => -3, -2 => -2, -1 => -1, }, 4 => {1 => 1, 2 =>
225 /// 2, 3 => 3, 4 => 4, }, }
226 fn handle_test_map_map(&self, hello: i32) -> thrift::Result<BTreeMap<i32, BTreeMap<i32, i32>>> {
Allen George8b96bfb2016-11-02 08:01:08 -0400227 println!("testMapMap({})", hello);
228
229 let mut inner_map_0: BTreeMap<i32, i32> = BTreeMap::new();
230 for i in -4..(0 as i32) {
231 inner_map_0.insert(i, i);
232 }
233
234 let mut inner_map_1: BTreeMap<i32, i32> = BTreeMap::new();
235 for i in 1..5 {
236 inner_map_1.insert(i, i);
237 }
238
239 let mut ret_map: BTreeMap<i32, BTreeMap<i32, i32>> = BTreeMap::new();
240 ret_map.insert(-4, inner_map_0);
241 ret_map.insert(4, inner_map_1);
242
243 Ok(ret_map)
244 }
245
246 /// Creates a the returned map with these values and prints it out:
247 /// { 1 => { 2 => argument,
248 /// 3 => argument,
249 /// },
250 /// 2 => { 6 => <empty Insanity struct>, },
251 /// }
252 /// return map<UserId, map<Numberz,Insanity>> - a map with the above values
Allen George0e22c362017-01-30 07:15:00 -0500253 fn handle_test_insanity(
254 &self,
255 argument: Insanity,
256 ) -> thrift::Result<BTreeMap<UserId, BTreeMap<Numberz, Insanity>>> {
Allen George8b96bfb2016-11-02 08:01:08 -0400257 println!("testInsanity({:?})", argument);
258 let mut map_0: BTreeMap<Numberz, Insanity> = BTreeMap::new();
259 map_0.insert(Numberz::TWO, argument.clone());
260 map_0.insert(Numberz::THREE, argument.clone());
261
262 let mut map_1: BTreeMap<Numberz, Insanity> = BTreeMap::new();
263 let insanity = Insanity {
264 user_map: None,
265 xtructs: None,
266 };
267 map_1.insert(Numberz::SIX, insanity);
268
269 let mut ret: BTreeMap<UserId, BTreeMap<Numberz, Insanity>> = BTreeMap::new();
270 ret.insert(1, map_0);
271 ret.insert(2, map_1);
272
273 Ok(ret)
274 }
275
Allen George0e22c362017-01-30 07:15:00 -0500276 /// returns an Xtruct with:
277 /// string_thing = "Hello2", byte_thing = arg0, i32_thing = arg1 and
278 /// i64_thing = arg2
279 fn handle_test_multi(
280 &self,
281 arg0: i8,
282 arg1: i32,
283 arg2: i64,
284 _: BTreeMap<i16, String>,
285 _: Numberz,
286 _: UserId,
287 ) -> thrift::Result<Xtruct> {
Allen George8b96bfb2016-11-02 08:01:08 -0400288 let x_ret = Xtruct {
289 string_thing: Some("Hello2".to_owned()),
290 byte_thing: Some(arg0),
291 i32_thing: Some(arg1),
292 i64_thing: Some(arg2),
293 };
294
295 Ok(x_ret)
296 }
297
Allen George0e22c362017-01-30 07:15:00 -0500298 /// if arg == "Xception" throw Xception with errorCode = 1001 and message =
299 /// arg
Allen George8b96bfb2016-11-02 08:01:08 -0400300 /// else if arg == "TException" throw TException
301 /// else do not throw anything
Allen George0e22c362017-01-30 07:15:00 -0500302 fn handle_test_exception(&self, arg: String) -> thrift::Result<()> {
Allen George8b96bfb2016-11-02 08:01:08 -0400303 println!("testException({})", arg);
304
305 match &*arg {
306 "Xception" => {
Allen George0e22c362017-01-30 07:15:00 -0500307 Err(
308 (Xception {
309 error_code: Some(1001),
310 message: Some(arg),
311 })
312 .into(),
313 )
Allen George8b96bfb2016-11-02 08:01:08 -0400314 }
315 "TException" => Err("this is a random error".into()),
316 _ => Ok(()),
317 }
318 }
319
Allen George0e22c362017-01-30 07:15:00 -0500320 /// if arg0 == "Xception":
321 /// throw Xception with errorCode = 1001 and message = "This is an
322 /// Xception"
323 /// else if arg0 == "Xception2":
324 /// throw Xception2 with errorCode = 2002 and struct_thing.string_thing =
325 /// "This is an Xception2"
326 // else:
327 // do not throw anything and return Xtruct with string_thing = arg1
328 fn handle_test_multi_exception(&self, arg0: String, arg1: String) -> thrift::Result<Xtruct> {
Allen George8b96bfb2016-11-02 08:01:08 -0400329 match &*arg0 {
330 "Xception" => {
Allen George0e22c362017-01-30 07:15:00 -0500331 Err(
332 (Xception {
333 error_code: Some(1001),
334 message: Some("This is an Xception".to_owned()),
335 })
336 .into(),
337 )
Allen George8b96bfb2016-11-02 08:01:08 -0400338 }
339 "Xception2" => {
Allen George0e22c362017-01-30 07:15:00 -0500340 Err(
341 (Xception2 {
342 error_code: Some(2002),
343 struct_thing: Some(
344 Xtruct {
345 string_thing: Some("This is an Xception2".to_owned()),
346 byte_thing: None,
347 i32_thing: None,
348 i64_thing: None,
349 },
350 ),
351 })
352 .into(),
353 )
Allen George8b96bfb2016-11-02 08:01:08 -0400354 }
355 _ => {
Allen George0e22c362017-01-30 07:15:00 -0500356 Ok(
357 Xtruct {
358 string_thing: Some(arg1),
359 byte_thing: None,
360 i32_thing: None,
361 i64_thing: None,
362 },
363 )
Allen George8b96bfb2016-11-02 08:01:08 -0400364 }
365 }
366 }
367
Allen George0e22c362017-01-30 07:15:00 -0500368 fn handle_test_oneway(&self, seconds_to_sleep: i32) -> thrift::Result<()> {
Allen George8b96bfb2016-11-02 08:01:08 -0400369 thread::sleep(Duration::from_secs(seconds_to_sleep as u64));
370 Ok(())
371 }
372}