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