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] |
Allen George | bc1344d | 2017-04-28 10:22:03 -0400 | [diff] [blame] | 19 | extern crate log; |
| 20 | extern crate env_logger; |
| 21 | |
| 22 | #[macro_use] |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 23 | extern crate clap; |
| 24 | extern crate ordered_float; |
| 25 | extern crate thrift; |
| 26 | extern crate thrift_test; // huh. I have to do this to use my lib |
| 27 | |
| 28 | use ordered_float::OrderedFloat; |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 29 | use std::collections::{BTreeMap, BTreeSet}; |
| 30 | use std::fmt::Debug; |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 31 | |
| 32 | use thrift::protocol::{TBinaryInputProtocol, TBinaryOutputProtocol, TCompactInputProtocol, |
Allen George | bc1344d | 2017-04-28 10:22:03 -0400 | [diff] [blame] | 33 | TCompactOutputProtocol, TInputProtocol, TMultiplexedOutputProtocol, |
| 34 | TOutputProtocol}; |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 35 | use thrift::transport::{ReadHalf, TBufferedReadTransport, TBufferedWriteTransport, |
| 36 | TFramedReadTransport, TFramedWriteTransport, TIoChannel, TReadTransport, |
| 37 | TTcpChannel, TWriteTransport, WriteHalf}; |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 38 | use thrift_test::*; |
| 39 | |
| 40 | fn main() { |
Allen George | bc1344d | 2017-04-28 10:22:03 -0400 | [diff] [blame] | 41 | env_logger::init().expect("logger setup failed"); |
| 42 | |
| 43 | debug!("initialized logger - running cross-test client"); |
| 44 | |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 45 | match run() { |
Allen George | bc1344d | 2017-04-28 10:22:03 -0400 | [diff] [blame] | 46 | Ok(()) => info!("cross-test client succeeded"), |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 47 | Err(e) => { |
Allen George | bc1344d | 2017-04-28 10:22:03 -0400 | [diff] [blame] | 48 | info!("cross-test client failed with error {:?}", e); |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 49 | std::process::exit(1); |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | fn run() -> thrift::Result<()> { |
| 55 | // unsupported options: |
| 56 | // --domain-socket |
Jens Geyer | 4a33b18 | 2020-03-22 13:46:34 +0100 | [diff] [blame] | 57 | // --pipe |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 58 | // --anon-pipes |
| 59 | // --ssl |
| 60 | // --threads |
| 61 | let matches = clap_app!(rust_test_client => |
| 62 | (version: "1.0") |
| 63 | (author: "Apache Thrift Developers <dev@thrift.apache.org>") |
| 64 | (about: "Rust Thrift test client") |
| 65 | (@arg host: --host +takes_value "Host on which the Thrift test server is located") |
| 66 | (@arg port: --port +takes_value "Port on which the Thrift test server is listening") |
| 67 | (@arg transport: --transport +takes_value "Thrift transport implementation to use (\"buffered\", \"framed\")") |
James E. King III | 9804ab9 | 2019-02-07 16:59:05 -0500 | [diff] [blame] | 68 | (@arg protocol: --protocol +takes_value "Thrift protocol implementation to use (\"binary\", \"compact\", \"multi\", \"multic\")") |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 69 | (@arg testloops: -n --testloops +takes_value "Number of times to run tests") |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 70 | ) |
Allen George | bc1344d | 2017-04-28 10:22:03 -0400 | [diff] [blame] | 71 | .get_matches(); |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 72 | |
| 73 | let host = matches.value_of("host").unwrap_or("127.0.0.1"); |
| 74 | let port = value_t!(matches, "port", u16).unwrap_or(9090); |
| 75 | let testloops = value_t!(matches, "testloops", u8).unwrap_or(1); |
| 76 | let transport = matches.value_of("transport").unwrap_or("buffered"); |
| 77 | let protocol = matches.value_of("protocol").unwrap_or("binary"); |
| 78 | |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 79 | |
Allen George | bc1344d | 2017-04-28 10:22:03 -0400 | [diff] [blame] | 80 | let mut thrift_test_client = { |
| 81 | let (i_prot, o_prot) = build_protocols(host, port, transport, protocol, "ThriftTest")?; |
| 82 | ThriftTestSyncClient::new(i_prot, o_prot) |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 83 | }; |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 84 | |
Allen George | bc1344d | 2017-04-28 10:22:03 -0400 | [diff] [blame] | 85 | let mut second_service_client = if protocol.starts_with("multi") { |
| 86 | let (i_prot, o_prot) = build_protocols(host, port, transport, protocol, "SecondService")?; |
| 87 | Some(SecondServiceSyncClient::new(i_prot, o_prot)) |
| 88 | } else { |
| 89 | None |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 90 | }; |
| 91 | |
Allen George | bc1344d | 2017-04-28 10:22:03 -0400 | [diff] [blame] | 92 | info!( |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 93 | "connecting to {}:{} with {}+{} stack", |
| 94 | host, |
| 95 | port, |
| 96 | protocol, |
| 97 | transport |
| 98 | ); |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 99 | |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 100 | for _ in 0..testloops { |
Allen George | bc1344d | 2017-04-28 10:22:03 -0400 | [diff] [blame] | 101 | make_thrift_calls(&mut thrift_test_client, &mut second_service_client)? |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | Ok(()) |
| 105 | } |
| 106 | |
Allen George | bc1344d | 2017-04-28 10:22:03 -0400 | [diff] [blame] | 107 | fn build_protocols( |
| 108 | host: &str, |
| 109 | port: u16, |
| 110 | transport: &str, |
| 111 | protocol: &str, |
| 112 | service_name: &str, |
Allen George | b0d1413 | 2020-03-29 11:48:55 -0400 | [diff] [blame] | 113 | ) -> thrift::Result<(Box<dyn TInputProtocol>, Box<dyn TOutputProtocol>)> { |
Allen George | bc1344d | 2017-04-28 10:22:03 -0400 | [diff] [blame] | 114 | let (i_chan, o_chan) = tcp_channel(host, port)?; |
| 115 | |
Allen George | b0d1413 | 2020-03-29 11:48:55 -0400 | [diff] [blame] | 116 | let (i_tran, o_tran): (Box<dyn TReadTransport>, Box<dyn TWriteTransport>) = match transport { |
Allen George | bc1344d | 2017-04-28 10:22:03 -0400 | [diff] [blame] | 117 | "buffered" => { |
| 118 | (Box::new(TBufferedReadTransport::new(i_chan)), |
| 119 | Box::new(TBufferedWriteTransport::new(o_chan))) |
| 120 | } |
| 121 | "framed" => { |
| 122 | (Box::new(TFramedReadTransport::new(i_chan)), |
| 123 | Box::new(TFramedWriteTransport::new(o_chan))) |
| 124 | } |
| 125 | unmatched => return Err(format!("unsupported transport {}", unmatched).into()), |
| 126 | }; |
| 127 | |
Allen George | b0d1413 | 2020-03-29 11:48:55 -0400 | [diff] [blame] | 128 | let (i_prot, o_prot): (Box<dyn TInputProtocol>, Box<dyn TOutputProtocol>) = match protocol { |
James E. King, III | 39eaae6 | 2017-11-19 20:17:33 -0500 | [diff] [blame] | 129 | "binary" => { |
Allen George | bc1344d | 2017-04-28 10:22:03 -0400 | [diff] [blame] | 130 | (Box::new(TBinaryInputProtocol::new(i_tran, true)), |
| 131 | Box::new(TBinaryOutputProtocol::new(o_tran, true))) |
| 132 | } |
| 133 | "multi" => { |
| 134 | (Box::new(TBinaryInputProtocol::new(i_tran, true)), |
| 135 | Box::new( |
| 136 | TMultiplexedOutputProtocol::new( |
| 137 | service_name, |
| 138 | TBinaryOutputProtocol::new(o_tran, true), |
| 139 | ), |
| 140 | )) |
| 141 | } |
James E. King, III | 39eaae6 | 2017-11-19 20:17:33 -0500 | [diff] [blame] | 142 | "compact" => { |
Allen George | bc1344d | 2017-04-28 10:22:03 -0400 | [diff] [blame] | 143 | (Box::new(TCompactInputProtocol::new(i_tran)), |
| 144 | Box::new(TCompactOutputProtocol::new(o_tran))) |
| 145 | } |
| 146 | "multic" => { |
| 147 | (Box::new(TCompactInputProtocol::new(i_tran)), |
| 148 | Box::new(TMultiplexedOutputProtocol::new(service_name, TCompactOutputProtocol::new(o_tran)),)) |
| 149 | } |
| 150 | unmatched => return Err(format!("unsupported protocol {}", unmatched).into()), |
| 151 | }; |
| 152 | |
| 153 | Ok((i_prot, o_prot)) |
| 154 | } |
| 155 | |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 156 | // FIXME: expose "open" through the client interface so I don't have to early |
| 157 | // open |
| 158 | fn tcp_channel( |
| 159 | host: &str, |
| 160 | port: u16, |
| 161 | ) -> thrift::Result<(ReadHalf<TTcpChannel>, WriteHalf<TTcpChannel>)> { |
| 162 | let mut c = TTcpChannel::new(); |
| 163 | c.open(&format!("{}:{}", host, port))?; |
| 164 | c.split() |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 165 | } |
| 166 | |
Allen George | b0d1413 | 2020-03-29 11:48:55 -0400 | [diff] [blame] | 167 | type BuildThriftTestClient = ThriftTestSyncClient<Box<dyn TInputProtocol>, Box<dyn TOutputProtocol>>; |
| 168 | type BuiltSecondServiceClient = SecondServiceSyncClient<Box<dyn TInputProtocol>, Box<dyn TOutputProtocol>>; |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 169 | |
Allen George | bc1344d | 2017-04-28 10:22:03 -0400 | [diff] [blame] | 170 | #[cfg_attr(feature = "cargo-clippy", allow(cyclomatic_complexity))] |
| 171 | fn make_thrift_calls( |
| 172 | thrift_test_client: &mut BuildThriftTestClient, |
| 173 | second_service_client: &mut Option<BuiltSecondServiceClient>, |
| 174 | ) -> Result<(), thrift::Error> { |
| 175 | info!("testVoid"); |
| 176 | thrift_test_client.test_void()?; |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 177 | |
Allen George | bc1344d | 2017-04-28 10:22:03 -0400 | [diff] [blame] | 178 | info!("testString"); |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 179 | verify_expected_result( |
Allen George | bc1344d | 2017-04-28 10:22:03 -0400 | [diff] [blame] | 180 | thrift_test_client.test_string("thing".to_owned()), |
| 181 | "thing".to_owned(), |
| 182 | )?; |
| 183 | |
| 184 | info!("testBool"); |
| 185 | verify_expected_result(thrift_test_client.test_bool(true), true)?; |
| 186 | |
| 187 | info!("testBool"); |
| 188 | verify_expected_result(thrift_test_client.test_bool(false), false)?; |
| 189 | |
| 190 | info!("testByte"); |
| 191 | verify_expected_result(thrift_test_client.test_byte(42), 42)?; |
| 192 | |
| 193 | info!("testi32"); |
| 194 | verify_expected_result(thrift_test_client.test_i32(1159348374), 1159348374)?; |
| 195 | |
| 196 | info!("testi64"); |
| 197 | // try!(verify_expected_result(thrift_test_client.test_i64(-8651829879438294565), |
| 198 | // -8651829879438294565)); |
| 199 | verify_expected_result( |
| 200 | thrift_test_client.test_i64(i64::min_value()), |
| 201 | i64::min_value(), |
| 202 | )?; |
| 203 | |
| 204 | info!("testDouble"); |
| 205 | verify_expected_result( |
| 206 | thrift_test_client.test_double(OrderedFloat::from(42.42)), |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 207 | OrderedFloat::from(42.42), |
| 208 | )?; |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 209 | |
Allen George | bc1344d | 2017-04-28 10:22:03 -0400 | [diff] [blame] | 210 | info!("testTypedef"); |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 211 | { |
| 212 | let u_snd: UserId = 2348; |
| 213 | let u_cmp: UserId = 2348; |
Allen George | bc1344d | 2017-04-28 10:22:03 -0400 | [diff] [blame] | 214 | verify_expected_result(thrift_test_client.test_typedef(u_snd), u_cmp)?; |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 215 | } |
| 216 | |
Allen George | bc1344d | 2017-04-28 10:22:03 -0400 | [diff] [blame] | 217 | info!("testEnum"); |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 218 | { |
GREATEST Wiggler EvaR! | b57d126 | 2018-11-09 07:54:32 -0500 | [diff] [blame] | 219 | verify_expected_result(thrift_test_client.test_enum(Numberz::Two), Numberz::Two)?; |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 220 | } |
| 221 | |
Allen George | bc1344d | 2017-04-28 10:22:03 -0400 | [diff] [blame] | 222 | info!("testBinary"); |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 223 | { |
| 224 | let b_snd = vec![0x77, 0x30, 0x30, 0x74, 0x21, 0x20, 0x52, 0x75, 0x73, 0x74]; |
| 225 | let b_cmp = vec![0x77, 0x30, 0x30, 0x74, 0x21, 0x20, 0x52, 0x75, 0x73, 0x74]; |
Allen George | bc1344d | 2017-04-28 10:22:03 -0400 | [diff] [blame] | 226 | verify_expected_result(thrift_test_client.test_binary(b_snd), b_cmp)?; |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 227 | } |
| 228 | |
Allen George | bc1344d | 2017-04-28 10:22:03 -0400 | [diff] [blame] | 229 | info!("testStruct"); |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 230 | { |
| 231 | let x_snd = Xtruct { |
| 232 | string_thing: Some("foo".to_owned()), |
| 233 | byte_thing: Some(12), |
| 234 | i32_thing: Some(219129), |
| 235 | i64_thing: Some(12938492818), |
| 236 | }; |
| 237 | let x_cmp = Xtruct { |
| 238 | string_thing: Some("foo".to_owned()), |
| 239 | byte_thing: Some(12), |
| 240 | i32_thing: Some(219129), |
| 241 | i64_thing: Some(12938492818), |
| 242 | }; |
Allen George | bc1344d | 2017-04-28 10:22:03 -0400 | [diff] [blame] | 243 | verify_expected_result(thrift_test_client.test_struct(x_snd), x_cmp)?; |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | // Xtruct again, with optional values |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 247 | // FIXME: apparently the erlang thrift server does not like opt-in-req-out |
| 248 | // parameters that are undefined. Joy. |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 249 | // { |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 250 | // let x_snd = Xtruct { string_thing: Some("foo".to_owned()), byte_thing: None, |
| 251 | // i32_thing: None, i64_thing: Some(12938492818) }; |
| 252 | // let x_cmp = Xtruct { string_thing: Some("foo".to_owned()), byte_thing: |
| 253 | // Some(0), i32_thing: Some(0), i64_thing: Some(12938492818) }; // the C++ |
| 254 | // server is responding correctly |
Allen George | bc1344d | 2017-04-28 10:22:03 -0400 | [diff] [blame] | 255 | // try!(verify_expected_result(thrift_test_client.test_struct(x_snd), x_cmp)); |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 256 | // } |
| 257 | // |
| 258 | |
Allen George | bc1344d | 2017-04-28 10:22:03 -0400 | [diff] [blame] | 259 | info!("testNest"); // (FIXME: try Xtruct2 with optional values) |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 260 | { |
| 261 | let x_snd = Xtruct2 { |
| 262 | byte_thing: Some(32), |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 263 | struct_thing: Some( |
| 264 | Xtruct { |
| 265 | string_thing: Some("foo".to_owned()), |
| 266 | byte_thing: Some(1), |
| 267 | i32_thing: Some(324382098), |
| 268 | i64_thing: Some(12938492818), |
| 269 | }, |
| 270 | ), |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 271 | i32_thing: Some(293481098), |
| 272 | }; |
| 273 | let x_cmp = Xtruct2 { |
| 274 | byte_thing: Some(32), |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 275 | struct_thing: Some( |
| 276 | Xtruct { |
| 277 | string_thing: Some("foo".to_owned()), |
| 278 | byte_thing: Some(1), |
| 279 | i32_thing: Some(324382098), |
| 280 | i64_thing: Some(12938492818), |
| 281 | }, |
| 282 | ), |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 283 | i32_thing: Some(293481098), |
| 284 | }; |
Allen George | bc1344d | 2017-04-28 10:22:03 -0400 | [diff] [blame] | 285 | verify_expected_result(thrift_test_client.test_nest(x_snd), x_cmp)?; |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 286 | } |
| 287 | |
Allen George | bc1344d | 2017-04-28 10:22:03 -0400 | [diff] [blame] | 288 | // do the multiplexed calls while making the main ThriftTest calls |
| 289 | if let Some(ref mut client) = second_service_client.as_mut() { |
Allen George | bc1344d | 2017-04-28 10:22:03 -0400 | [diff] [blame] | 290 | info!("SecondService secondtestString"); |
| 291 | { |
| 292 | verify_expected_result( |
| 293 | client.secondtest_string("test_string".to_owned()), |
| 294 | "testString(\"test_string\")".to_owned(), |
| 295 | )?; |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | info!("testList"); |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 300 | { |
| 301 | let mut v_snd: Vec<i32> = Vec::new(); |
| 302 | v_snd.push(29384); |
| 303 | v_snd.push(238); |
| 304 | v_snd.push(32498); |
| 305 | |
| 306 | let mut v_cmp: Vec<i32> = Vec::new(); |
| 307 | v_cmp.push(29384); |
| 308 | v_cmp.push(238); |
| 309 | v_cmp.push(32498); |
| 310 | |
Allen George | bc1344d | 2017-04-28 10:22:03 -0400 | [diff] [blame] | 311 | verify_expected_result(thrift_test_client.test_list(v_snd), v_cmp)?; |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 312 | } |
| 313 | |
Allen George | bc1344d | 2017-04-28 10:22:03 -0400 | [diff] [blame] | 314 | info!("testSet"); |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 315 | { |
| 316 | let mut s_snd: BTreeSet<i32> = BTreeSet::new(); |
| 317 | s_snd.insert(293481); |
| 318 | s_snd.insert(23); |
| 319 | s_snd.insert(3234); |
| 320 | |
| 321 | let mut s_cmp: BTreeSet<i32> = BTreeSet::new(); |
| 322 | s_cmp.insert(293481); |
| 323 | s_cmp.insert(23); |
| 324 | s_cmp.insert(3234); |
| 325 | |
Allen George | bc1344d | 2017-04-28 10:22:03 -0400 | [diff] [blame] | 326 | verify_expected_result(thrift_test_client.test_set(s_snd), s_cmp)?; |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 327 | } |
| 328 | |
Allen George | bc1344d | 2017-04-28 10:22:03 -0400 | [diff] [blame] | 329 | info!("testMap"); |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 330 | { |
| 331 | let mut m_snd: BTreeMap<i32, i32> = BTreeMap::new(); |
| 332 | m_snd.insert(2, 4); |
| 333 | m_snd.insert(4, 6); |
| 334 | m_snd.insert(8, 7); |
| 335 | |
| 336 | let mut m_cmp: BTreeMap<i32, i32> = BTreeMap::new(); |
| 337 | m_cmp.insert(2, 4); |
| 338 | m_cmp.insert(4, 6); |
| 339 | m_cmp.insert(8, 7); |
| 340 | |
Allen George | bc1344d | 2017-04-28 10:22:03 -0400 | [diff] [blame] | 341 | verify_expected_result(thrift_test_client.test_map(m_snd), m_cmp)?; |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 342 | } |
| 343 | |
Allen George | bc1344d | 2017-04-28 10:22:03 -0400 | [diff] [blame] | 344 | info!("testStringMap"); |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 345 | { |
| 346 | let mut m_snd: BTreeMap<String, String> = BTreeMap::new(); |
| 347 | m_snd.insert("2".to_owned(), "4_string".to_owned()); |
| 348 | m_snd.insert("4".to_owned(), "6_string".to_owned()); |
| 349 | m_snd.insert("8".to_owned(), "7_string".to_owned()); |
| 350 | |
| 351 | let mut m_rcv: BTreeMap<String, String> = BTreeMap::new(); |
| 352 | m_rcv.insert("2".to_owned(), "4_string".to_owned()); |
| 353 | m_rcv.insert("4".to_owned(), "6_string".to_owned()); |
| 354 | m_rcv.insert("8".to_owned(), "7_string".to_owned()); |
| 355 | |
Allen George | bc1344d | 2017-04-28 10:22:03 -0400 | [diff] [blame] | 356 | verify_expected_result(thrift_test_client.test_string_map(m_snd), m_rcv)?; |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | // nested map |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 360 | // expect : {-4 => {-4 => -4, -3 => -3, -2 => -2, -1 => -1, }, 4 => {1 => 1, 2 |
| 361 | // => 2, 3 => 3, 4 => 4, }, } |
Allen George | bc1344d | 2017-04-28 10:22:03 -0400 | [diff] [blame] | 362 | info!("testMapMap"); |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 363 | { |
| 364 | let mut m_cmp_nested_0: BTreeMap<i32, i32> = BTreeMap::new(); |
| 365 | for i in (-4 as i32)..0 { |
| 366 | m_cmp_nested_0.insert(i, i); |
| 367 | } |
| 368 | let mut m_cmp_nested_1: BTreeMap<i32, i32> = BTreeMap::new(); |
| 369 | for i in 1..5 { |
| 370 | m_cmp_nested_1.insert(i, i); |
| 371 | } |
| 372 | |
| 373 | let mut m_cmp: BTreeMap<i32, BTreeMap<i32, i32>> = BTreeMap::new(); |
| 374 | m_cmp.insert(-4, m_cmp_nested_0); |
| 375 | m_cmp.insert(4, m_cmp_nested_1); |
| 376 | |
Allen George | bc1344d | 2017-04-28 10:22:03 -0400 | [diff] [blame] | 377 | verify_expected_result(thrift_test_client.test_map_map(42), m_cmp)?; |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 378 | } |
| 379 | |
Allen George | bc1344d | 2017-04-28 10:22:03 -0400 | [diff] [blame] | 380 | info!("testMulti"); |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 381 | { |
| 382 | let mut m_snd: BTreeMap<i16, String> = BTreeMap::new(); |
| 383 | m_snd.insert(1298, "fizz".to_owned()); |
| 384 | m_snd.insert(-148, "buzz".to_owned()); |
| 385 | |
| 386 | let s_cmp = Xtruct { |
| 387 | string_thing: Some("Hello2".to_owned()), |
| 388 | byte_thing: Some(1), |
| 389 | i32_thing: Some(-123948), |
| 390 | i64_thing: Some(-19234123981), |
| 391 | }; |
| 392 | |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 393 | verify_expected_result( |
GREATEST Wiggler EvaR! | b57d126 | 2018-11-09 07:54:32 -0500 | [diff] [blame] | 394 | thrift_test_client.test_multi(1, -123948, -19234123981, m_snd, Numberz::Eight, 81), |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 395 | s_cmp, |
| 396 | )?; |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | // Insanity |
| 400 | // returns: |
| 401 | // { 1 => { 2 => argument, |
| 402 | // 3 => argument, |
| 403 | // }, |
| 404 | // 2 => { 6 => <empty Insanity struct>, }, |
| 405 | // } |
| 406 | { |
| 407 | let mut arg_map_usermap: BTreeMap<Numberz, i64> = BTreeMap::new(); |
GREATEST Wiggler EvaR! | b57d126 | 2018-11-09 07:54:32 -0500 | [diff] [blame] | 408 | arg_map_usermap.insert(Numberz::One, 4289); |
| 409 | arg_map_usermap.insert(Numberz::Eight, 19); |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 410 | |
| 411 | let mut arg_vec_xtructs: Vec<Xtruct> = Vec::new(); |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 412 | arg_vec_xtructs.push( |
| 413 | Xtruct { |
| 414 | string_thing: Some("foo".to_owned()), |
| 415 | byte_thing: Some(8), |
| 416 | i32_thing: Some(29), |
| 417 | i64_thing: Some(92384), |
| 418 | }, |
| 419 | ); |
| 420 | arg_vec_xtructs.push( |
| 421 | Xtruct { |
| 422 | string_thing: Some("bar".to_owned()), |
| 423 | byte_thing: Some(28), |
| 424 | i32_thing: Some(2), |
| 425 | i64_thing: Some(-1281), |
| 426 | }, |
| 427 | ); |
| 428 | arg_vec_xtructs.push( |
| 429 | Xtruct { |
| 430 | string_thing: Some("baz".to_owned()), |
| 431 | byte_thing: Some(0), |
| 432 | i32_thing: Some(3948539), |
| 433 | i64_thing: Some(-12938492), |
| 434 | }, |
| 435 | ); |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 436 | |
| 437 | let mut s_cmp_nested_1: BTreeMap<Numberz, Insanity> = BTreeMap::new(); |
| 438 | let insanity = Insanity { |
| 439 | user_map: Some(arg_map_usermap), |
| 440 | xtructs: Some(arg_vec_xtructs), |
| 441 | }; |
GREATEST Wiggler EvaR! | b57d126 | 2018-11-09 07:54:32 -0500 | [diff] [blame] | 442 | s_cmp_nested_1.insert(Numberz::Two, insanity.clone()); |
| 443 | s_cmp_nested_1.insert(Numberz::Three, insanity.clone()); |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 444 | |
| 445 | let mut s_cmp_nested_2: BTreeMap<Numberz, Insanity> = BTreeMap::new(); |
| 446 | let empty_insanity = Insanity { |
| 447 | user_map: Some(BTreeMap::new()), |
| 448 | xtructs: Some(Vec::new()), |
| 449 | }; |
GREATEST Wiggler EvaR! | b57d126 | 2018-11-09 07:54:32 -0500 | [diff] [blame] | 450 | s_cmp_nested_2.insert(Numberz::Six, empty_insanity); |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 451 | |
| 452 | let mut s_cmp: BTreeMap<UserId, BTreeMap<Numberz, Insanity>> = BTreeMap::new(); |
| 453 | s_cmp.insert(1 as UserId, s_cmp_nested_1); |
| 454 | s_cmp.insert(2 as UserId, s_cmp_nested_2); |
| 455 | |
Allen George | bc1344d | 2017-04-28 10:22:03 -0400 | [diff] [blame] | 456 | verify_expected_result(thrift_test_client.test_insanity(insanity.clone()), s_cmp)?; |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 457 | } |
| 458 | |
Allen George | bc1344d | 2017-04-28 10:22:03 -0400 | [diff] [blame] | 459 | info!("testException - remote throws Xception"); |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 460 | { |
Allen George | bc1344d | 2017-04-28 10:22:03 -0400 | [diff] [blame] | 461 | let r = thrift_test_client.test_exception("Xception".to_owned()); |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 462 | let x = match r { |
| 463 | Err(thrift::Error::User(ref e)) => { |
| 464 | match e.downcast_ref::<Xception>() { |
| 465 | Some(x) => Ok(x), |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 466 | None => Err(thrift::Error::User("did not get expected Xception struct".into()),), |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 467 | } |
| 468 | } |
| 469 | _ => Err(thrift::Error::User("did not get exception".into())), |
| 470 | }?; |
| 471 | |
| 472 | let x_cmp = Xception { |
| 473 | error_code: Some(1001), |
| 474 | message: Some("Xception".to_owned()), |
| 475 | }; |
| 476 | |
| 477 | verify_expected_result(Ok(x), &x_cmp)?; |
| 478 | } |
| 479 | |
Allen George | bc1344d | 2017-04-28 10:22:03 -0400 | [diff] [blame] | 480 | info!("testException - remote throws TApplicationException"); |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 481 | { |
Allen George | bc1344d | 2017-04-28 10:22:03 -0400 | [diff] [blame] | 482 | let r = thrift_test_client.test_exception("TException".to_owned()); |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 483 | match r { |
| 484 | Err(thrift::Error::Application(ref e)) => { |
Allen George | bc1344d | 2017-04-28 10:22:03 -0400 | [diff] [blame] | 485 | info!("received an {:?}", e); |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 486 | Ok(()) |
| 487 | } |
| 488 | _ => Err(thrift::Error::User("did not get exception".into())), |
| 489 | }?; |
| 490 | } |
| 491 | |
Allen George | bc1344d | 2017-04-28 10:22:03 -0400 | [diff] [blame] | 492 | info!("testException - remote succeeds"); |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 493 | { |
Allen George | bc1344d | 2017-04-28 10:22:03 -0400 | [diff] [blame] | 494 | let r = thrift_test_client.test_exception("foo".to_owned()); |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 495 | match r { |
| 496 | Ok(_) => Ok(()), |
| 497 | _ => Err(thrift::Error::User("received an exception".into())), |
| 498 | }?; |
| 499 | } |
| 500 | |
Allen George | bc1344d | 2017-04-28 10:22:03 -0400 | [diff] [blame] | 501 | info!("testMultiException - remote throws Xception"); |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 502 | { |
Allen George | bc1344d | 2017-04-28 10:22:03 -0400 | [diff] [blame] | 503 | let r = |
| 504 | thrift_test_client.test_multi_exception("Xception".to_owned(), "ignored".to_owned()); |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 505 | let x = match r { |
| 506 | Err(thrift::Error::User(ref e)) => { |
| 507 | match e.downcast_ref::<Xception>() { |
| 508 | Some(x) => Ok(x), |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 509 | None => Err(thrift::Error::User("did not get expected Xception struct".into()),), |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 510 | } |
| 511 | } |
| 512 | _ => Err(thrift::Error::User("did not get exception".into())), |
| 513 | }?; |
| 514 | |
| 515 | let x_cmp = Xception { |
| 516 | error_code: Some(1001), |
| 517 | message: Some("This is an Xception".to_owned()), |
| 518 | }; |
| 519 | |
| 520 | verify_expected_result(Ok(x), &x_cmp)?; |
| 521 | } |
| 522 | |
Allen George | bc1344d | 2017-04-28 10:22:03 -0400 | [diff] [blame] | 523 | info!("testMultiException - remote throws Xception2"); |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 524 | { |
Allen George | bc1344d | 2017-04-28 10:22:03 -0400 | [diff] [blame] | 525 | let r = |
| 526 | thrift_test_client.test_multi_exception("Xception2".to_owned(), "ignored".to_owned()); |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 527 | let x = match r { |
| 528 | Err(thrift::Error::User(ref e)) => { |
| 529 | match e.downcast_ref::<Xception2>() { |
| 530 | Some(x) => Ok(x), |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 531 | None => Err(thrift::Error::User("did not get expected Xception struct".into()),), |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 532 | } |
| 533 | } |
| 534 | _ => Err(thrift::Error::User("did not get exception".into())), |
| 535 | }?; |
| 536 | |
| 537 | let x_cmp = Xception2 { |
| 538 | error_code: Some(2002), |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 539 | struct_thing: Some( |
| 540 | Xtruct { |
| 541 | string_thing: Some("This is an Xception2".to_owned()), |
| 542 | // since this is an OPT_IN_REQ_OUT field the sender sets a default |
| 543 | byte_thing: Some(0), |
| 544 | // since this is an OPT_IN_REQ_OUT field the sender sets a default |
| 545 | i32_thing: Some(0), |
| 546 | // since this is an OPT_IN_REQ_OUT field the sender sets a default |
| 547 | i64_thing: Some(0), |
| 548 | }, |
| 549 | ), |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 550 | }; |
| 551 | |
| 552 | verify_expected_result(Ok(x), &x_cmp)?; |
| 553 | } |
| 554 | |
Allen George | bc1344d | 2017-04-28 10:22:03 -0400 | [diff] [blame] | 555 | info!("testMultiException - remote succeeds"); |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 556 | { |
Allen George | bc1344d | 2017-04-28 10:22:03 -0400 | [diff] [blame] | 557 | let r = thrift_test_client.test_multi_exception("haha".to_owned(), "RETURNED".to_owned()); |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 558 | let x = match r { |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 559 | Err(e) => Err(thrift::Error::User(format!("received an unexpected exception {:?}", e).into(),),), |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 560 | _ => r, |
| 561 | }?; |
| 562 | |
| 563 | let x_cmp = Xtruct { |
| 564 | string_thing: Some("RETURNED".to_owned()), |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 565 | // since this is an OPT_IN_REQ_OUT field the sender sets a default |
| 566 | byte_thing: Some(0), |
| 567 | // since this is an OPT_IN_REQ_OUT field the sender sets a default |
| 568 | i32_thing: Some(0), |
| 569 | // since this is an OPT_IN_REQ_OUT field the sender sets a default |
| 570 | i64_thing: Some(0), |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 571 | }; |
| 572 | |
| 573 | verify_expected_result(Ok(x), x_cmp)?; |
| 574 | } |
| 575 | |
Allen George | bc1344d | 2017-04-28 10:22:03 -0400 | [diff] [blame] | 576 | info!("testOneWay - remote sleeps for 1 second"); |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 577 | { |
Allen George | bc1344d | 2017-04-28 10:22:03 -0400 | [diff] [blame] | 578 | thrift_test_client.test_oneway(1)?; |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 579 | } |
| 580 | |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 581 | // final test to verify that the connection is still writable after the one-way |
| 582 | // call |
Allen George | bc1344d | 2017-04-28 10:22:03 -0400 | [diff] [blame] | 583 | thrift_test_client.test_void() |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 584 | } |
| 585 | |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 586 | #[cfg_attr(feature = "cargo-clippy", allow(needless_pass_by_value))] |
| 587 | fn verify_expected_result<T: Debug + PartialEq + Sized>( |
| 588 | actual: Result<T, thrift::Error>, |
| 589 | expected: T, |
| 590 | ) -> Result<(), thrift::Error> { |
James E. King, III | 20e16bc | 2017-11-18 22:37:54 -0500 | [diff] [blame] | 591 | info!("*** EXPECTED: Ok({:?})", expected); |
| 592 | info!("*** ACTUAL : {:?}", actual); |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 593 | match actual { |
| 594 | Ok(v) => { |
| 595 | if v == expected { |
James E. King, III | 20e16bc | 2017-11-18 22:37:54 -0500 | [diff] [blame] | 596 | info!("*** OK ***"); |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 597 | Ok(()) |
| 598 | } else { |
James E. King, III | 20e16bc | 2017-11-18 22:37:54 -0500 | [diff] [blame] | 599 | info!("*** FAILED ***"); |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 600 | Err(thrift::Error::User(format!("expected {:?} but got {:?}", &expected, &v).into()),) |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 601 | } |
| 602 | } |
| 603 | Err(e) => Err(e), |
| 604 | } |
| 605 | } |