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