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