Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 1 | // 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] |
| 19 | extern crate clap; |
| 20 | extern crate ordered_float; |
| 21 | extern crate thrift; |
| 22 | extern crate thrift_test; |
| 23 | |
| 24 | use ordered_float::OrderedFloat; |
| 25 | use std::collections::{BTreeMap, BTreeSet}; |
| 26 | use std::thread; |
| 27 | use std::time::Duration; |
| 28 | |
| 29 | use thrift::protocol::{TBinaryInputProtocolFactory, TBinaryOutputProtocolFactory, |
| 30 | TCompactInputProtocolFactory, TCompactOutputProtocolFactory, |
| 31 | TInputProtocolFactory, TOutputProtocolFactory}; |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame^] | 32 | use thrift::server::TServer; |
| 33 | use thrift::transport::{TBufferedReadTransportFactory, TBufferedWriteTransportFactory, |
| 34 | TFramedReadTransportFactory, TFramedWriteTransportFactory, |
| 35 | TReadTransportFactory, TWriteTransportFactory}; |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 36 | use thrift_test::*; |
| 37 | |
| 38 | fn 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 | |
| 48 | fn run() -> thrift::Result<()> { |
| 49 | |
| 50 | // unsupported options: |
| 51 | // --domain-socket |
| 52 | // --named-pipe |
| 53 | // --ssl |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 54 | 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 George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame^] | 61 | (@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 George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 65 | |
| 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 George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame^] | 69 | let server_type = matches.value_of("server_type").unwrap_or("thread-pool"); |
| 70 | let workers = value_t!(matches, "workers", usize).unwrap_or(4); |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 71 | let listen_address = format!("127.0.0.1:{}", port); |
| 72 | |
| 73 | println!("binding to {}", listen_address); |
| 74 | |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame^] | 75 | 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 George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 90 | |
| 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 George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame^] | 111 | 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 George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 129 | } |
| 130 | unknown => { |
| 131 | return Err(format!("unsupported server type {}", unknown).into()); |
| 132 | } |
| 133 | }; |
| 134 | |
| 135 | server.listen(&listen_address) |
| 136 | } |
| 137 | |
| 138 | struct ThriftTestSyncHandlerImpl; |
| 139 | impl ThriftTestSyncHandler for ThriftTestSyncHandlerImpl { |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame^] | 140 | fn handle_test_void(&self) -> thrift::Result<()> { |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 141 | println!("testVoid()"); |
| 142 | Ok(()) |
| 143 | } |
| 144 | |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame^] | 145 | fn handle_test_string(&self, thing: String) -> thrift::Result<String> { |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 146 | println!("testString({})", &thing); |
| 147 | Ok(thing) |
| 148 | } |
| 149 | |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame^] | 150 | fn handle_test_bool(&self, thing: bool) -> thrift::Result<bool> { |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 151 | println!("testBool({})", thing); |
| 152 | Ok(thing) |
| 153 | } |
| 154 | |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame^] | 155 | fn handle_test_byte(&self, thing: i8) -> thrift::Result<i8> { |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 156 | println!("testByte({})", thing); |
| 157 | Ok(thing) |
| 158 | } |
| 159 | |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame^] | 160 | fn handle_test_i32(&self, thing: i32) -> thrift::Result<i32> { |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 161 | println!("testi32({})", thing); |
| 162 | Ok(thing) |
| 163 | } |
| 164 | |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame^] | 165 | fn handle_test_i64(&self, thing: i64) -> thrift::Result<i64> { |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 166 | println!("testi64({})", thing); |
| 167 | Ok(thing) |
| 168 | } |
| 169 | |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame^] | 170 | fn handle_test_double(&self, thing: OrderedFloat<f64>) -> thrift::Result<OrderedFloat<f64>> { |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 171 | println!("testDouble({})", thing); |
| 172 | Ok(thing) |
| 173 | } |
| 174 | |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame^] | 175 | fn handle_test_binary(&self, thing: Vec<u8>) -> thrift::Result<Vec<u8>> { |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 176 | println!("testBinary({:?})", thing); |
| 177 | Ok(thing) |
| 178 | } |
| 179 | |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame^] | 180 | fn handle_test_struct(&self, thing: Xtruct) -> thrift::Result<Xtruct> { |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 181 | println!("testStruct({:?})", thing); |
| 182 | Ok(thing) |
| 183 | } |
| 184 | |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame^] | 185 | fn handle_test_nest(&self, thing: Xtruct2) -> thrift::Result<Xtruct2> { |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 186 | println!("testNest({:?})", thing); |
| 187 | Ok(thing) |
| 188 | } |
| 189 | |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame^] | 190 | fn handle_test_map(&self, thing: BTreeMap<i32, i32>) -> thrift::Result<BTreeMap<i32, i32>> { |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 191 | println!("testMap({:?})", thing); |
| 192 | Ok(thing) |
| 193 | } |
| 194 | |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame^] | 195 | fn handle_test_string_map( |
| 196 | &self, |
| 197 | thing: BTreeMap<String, String>, |
| 198 | ) -> thrift::Result<BTreeMap<String, String>> { |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 199 | println!("testStringMap({:?})", thing); |
| 200 | Ok(thing) |
| 201 | } |
| 202 | |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame^] | 203 | fn handle_test_set(&self, thing: BTreeSet<i32>) -> thrift::Result<BTreeSet<i32>> { |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 204 | println!("testSet({:?})", thing); |
| 205 | Ok(thing) |
| 206 | } |
| 207 | |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame^] | 208 | fn handle_test_list(&self, thing: Vec<i32>) -> thrift::Result<Vec<i32>> { |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 209 | println!("testList({:?})", thing); |
| 210 | Ok(thing) |
| 211 | } |
| 212 | |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame^] | 213 | fn handle_test_enum(&self, thing: Numberz) -> thrift::Result<Numberz> { |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 214 | println!("testEnum({:?})", thing); |
| 215 | Ok(thing) |
| 216 | } |
| 217 | |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame^] | 218 | fn handle_test_typedef(&self, thing: UserId) -> thrift::Result<UserId> { |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 219 | println!("testTypedef({})", thing); |
| 220 | Ok(thing) |
| 221 | } |
| 222 | |
| 223 | /// @return map<i32,map<i32,i32>> - returns a dictionary with these values: |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame^] | 224 | /// {-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 George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 227 | 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 George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame^] | 253 | fn handle_test_insanity( |
| 254 | &self, |
| 255 | argument: Insanity, |
| 256 | ) -> thrift::Result<BTreeMap<UserId, BTreeMap<Numberz, Insanity>>> { |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 257 | 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 George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame^] | 276 | /// 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 George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 288 | 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 George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame^] | 298 | /// if arg == "Xception" throw Xception with errorCode = 1001 and message = |
| 299 | /// arg |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 300 | /// else if arg == "TException" throw TException |
| 301 | /// else do not throw anything |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame^] | 302 | fn handle_test_exception(&self, arg: String) -> thrift::Result<()> { |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 303 | println!("testException({})", arg); |
| 304 | |
| 305 | match &*arg { |
| 306 | "Xception" => { |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame^] | 307 | Err( |
| 308 | (Xception { |
| 309 | error_code: Some(1001), |
| 310 | message: Some(arg), |
| 311 | }) |
| 312 | .into(), |
| 313 | ) |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 314 | } |
| 315 | "TException" => Err("this is a random error".into()), |
| 316 | _ => Ok(()), |
| 317 | } |
| 318 | } |
| 319 | |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame^] | 320 | /// 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 George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 329 | match &*arg0 { |
| 330 | "Xception" => { |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame^] | 331 | Err( |
| 332 | (Xception { |
| 333 | error_code: Some(1001), |
| 334 | message: Some("This is an Xception".to_owned()), |
| 335 | }) |
| 336 | .into(), |
| 337 | ) |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 338 | } |
| 339 | "Xception2" => { |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame^] | 340 | 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 George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 354 | } |
| 355 | _ => { |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame^] | 356 | Ok( |
| 357 | Xtruct { |
| 358 | string_thing: Some(arg1), |
| 359 | byte_thing: None, |
| 360 | i32_thing: None, |
| 361 | i64_thing: None, |
| 362 | }, |
| 363 | ) |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 364 | } |
| 365 | } |
| 366 | } |
| 367 | |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame^] | 368 | fn handle_test_oneway(&self, seconds_to_sleep: i32) -> thrift::Result<()> { |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 369 | thread::sleep(Duration::from_secs(seconds_to_sleep as u64)); |
| 370 | Ok(()) |
| 371 | } |
| 372 | } |