blob: 476f9eb25745660aa7aba3bac94c868d869469e1 [file] [log] [blame]
Allen George8b96bfb2016-11-02 08:01:08 -04001// 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 George7ddbcc02020-11-08 09:51:19 -050018use env_logger;
19use log::*;
20use clap::{clap_app, value_t};
Allen Georgebc1344d2017-04-28 10:22:03 -040021
Allen George8b96bfb2016-11-02 08:01:08 -040022use std::collections::{BTreeMap, BTreeSet};
23use std::fmt::Debug;
Allen George5cff2792021-02-28 07:43:51 -050024use std::net::TcpStream;
Allen George8b96bfb2016-11-02 08:01:08 -040025
Allen George7ddbcc02020-11-08 09:51:19 -050026use thrift;
27use thrift::OrderedFloat;
Allen George8b96bfb2016-11-02 08:01:08 -040028use thrift::protocol::{TBinaryInputProtocol, TBinaryOutputProtocol, TCompactInputProtocol,
Allen Georgebc1344d2017-04-28 10:22:03 -040029 TCompactOutputProtocol, TInputProtocol, TMultiplexedOutputProtocol,
30 TOutputProtocol};
Allen George5cff2792021-02-28 07:43:51 -050031use thrift::transport::{TBufferedReadTransport, TBufferedWriteTransport,
Allen George0e22c362017-01-30 07:15:00 -050032 TFramedReadTransport, TFramedWriteTransport, TIoChannel, TReadTransport,
Allen George5cff2792021-02-28 07:43:51 -050033 TTcpChannel, TWriteTransport};
Allen George8b96bfb2016-11-02 08:01:08 -040034use thrift_test::*;
35
36fn main() {
Allen George7ddbcc02020-11-08 09:51:19 -050037 env_logger::init();
Allen Georgebc1344d2017-04-28 10:22:03 -040038
39 debug!("initialized logger - running cross-test client");
40
Allen George8b96bfb2016-11-02 08:01:08 -040041 match run() {
Allen Georgebc1344d2017-04-28 10:22:03 -040042 Ok(()) => info!("cross-test client succeeded"),
Allen George8b96bfb2016-11-02 08:01:08 -040043 Err(e) => {
Allen Georgebc1344d2017-04-28 10:22:03 -040044 info!("cross-test client failed with error {:?}", e);
Allen George8b96bfb2016-11-02 08:01:08 -040045 std::process::exit(1);
46 }
47 }
48}
49
50fn run() -> thrift::Result<()> {
51 // unsupported options:
52 // --domain-socket
Jens Geyer4a33b182020-03-22 13:46:34 +010053 // --pipe
Allen George8b96bfb2016-11-02 08:01:08 -040054 // --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 III9804ab92019-02-07 16:59:05 -050064 (@arg protocol: --protocol +takes_value "Thrift protocol implementation to use (\"binary\", \"compact\", \"multi\", \"multic\")")
Allen George8b96bfb2016-11-02 08:01:08 -040065 (@arg testloops: -n --testloops +takes_value "Number of times to run tests")
Allen George0e22c362017-01-30 07:15:00 -050066 )
Allen Georgebc1344d2017-04-28 10:22:03 -040067 .get_matches();
Allen George8b96bfb2016-11-02 08:01:08 -040068
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 George5cff2792021-02-28 07:43:51 -050075 // 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 George8b96bfb2016-11-02 08:01:08 -040079
Allen Georgebc1344d2017-04-28 10:22:03 -040080 let mut second_service_client = if protocol.starts_with("multi") {
Allen George5cff2792021-02-28 07:43:51 -050081 let shared_stream_clone = shared_stream.try_clone()?;
82 let (i_prot, o_prot) = build(shared_stream_clone, transport, protocol, "SecondService")?;
Allen Georgebc1344d2017-04-28 10:22:03 -040083 Some(SecondServiceSyncClient::new(i_prot, o_prot))
84 } else {
85 None
Allen George8b96bfb2016-11-02 08:01:08 -040086 };
87
Allen George5cff2792021-02-28 07:43:51 -050088 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 Georgebc1344d2017-04-28 10:22:03 -040093 info!(
Allen George0e22c362017-01-30 07:15:00 -050094 "connecting to {}:{} with {}+{} stack",
95 host,
96 port,
97 protocol,
98 transport
99 );
Allen George8b96bfb2016-11-02 08:01:08 -0400100
Allen George8b96bfb2016-11-02 08:01:08 -0400101 for _ in 0..testloops {
Allen Georgebc1344d2017-04-28 10:22:03 -0400102 make_thrift_calls(&mut thrift_test_client, &mut second_service_client)?
Allen George8b96bfb2016-11-02 08:01:08 -0400103 }
104
105 Ok(())
106}
107
Allen George5cff2792021-02-28 07:43:51 -0500108fn build(
109 stream: TcpStream,
Allen Georgebc1344d2017-04-28 10:22:03 -0400110 transport: &str,
111 protocol: &str,
112 service_name: &str,
Allen Georgeb0d14132020-03-29 11:48:55 -0400113) -> thrift::Result<(Box<dyn TInputProtocol>, Box<dyn TOutputProtocol>)> {
Allen George5cff2792021-02-28 07:43:51 -0500114 let c = TTcpChannel::with_stream(stream);
115 let (i_chan, o_chan) = c.split()?;
Allen Georgebc1344d2017-04-28 10:22:03 -0400116
Allen Georgeb0d14132020-03-29 11:48:55 -0400117 let (i_tran, o_tran): (Box<dyn TReadTransport>, Box<dyn TWriteTransport>) = match transport {
Allen Georgebc1344d2017-04-28 10:22:03 -0400118 "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 Georgeb0d14132020-03-29 11:48:55 -0400129 let (i_prot, o_prot): (Box<dyn TInputProtocol>, Box<dyn TOutputProtocol>) = match protocol {
James E. King, III39eaae62017-11-19 20:17:33 -0500130 "binary" => {
Allen Georgebc1344d2017-04-28 10:22:03 -0400131 (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, III39eaae62017-11-19 20:17:33 -0500143 "compact" => {
Allen Georgebc1344d2017-04-28 10:22:03 -0400144 (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 Georgeb0d14132020-03-29 11:48:55 -0400157type BuildThriftTestClient = ThriftTestSyncClient<Box<dyn TInputProtocol>, Box<dyn TOutputProtocol>>;
158type BuiltSecondServiceClient = SecondServiceSyncClient<Box<dyn TInputProtocol>, Box<dyn TOutputProtocol>>;
Allen George8b96bfb2016-11-02 08:01:08 -0400159
Allen George7ddbcc02020-11-08 09:51:19 -0500160#[allow(clippy::cognitive_complexity)]
Allen Georgebc1344d2017-04-28 10:22:03 -0400161fn 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 George8b96bfb2016-11-02 08:01:08 -0400167
Allen Georgebc1344d2017-04-28 10:22:03 -0400168 info!("testString");
Allen George0e22c362017-01-30 07:15:00 -0500169 verify_expected_result(
Allen Georgebc1344d2017-04-28 10:22:03 -0400170 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 George7ddbcc02020-11-08 09:51:19 -0500184 verify_expected_result(thrift_test_client.test_i32(1_159_348_374), 1_159_348_374)?;
Allen Georgebc1344d2017-04-28 10:22:03 -0400185
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 George0e22c362017-01-30 07:15:00 -0500197 OrderedFloat::from(42.42),
198 )?;
Allen George8b96bfb2016-11-02 08:01:08 -0400199
Allen Georgebc1344d2017-04-28 10:22:03 -0400200 info!("testTypedef");
Allen George8b96bfb2016-11-02 08:01:08 -0400201 {
202 let u_snd: UserId = 2348;
203 let u_cmp: UserId = 2348;
Allen Georgebc1344d2017-04-28 10:22:03 -0400204 verify_expected_result(thrift_test_client.test_typedef(u_snd), u_cmp)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400205 }
206
Allen Georgebc1344d2017-04-28 10:22:03 -0400207 info!("testEnum");
Allen George8b96bfb2016-11-02 08:01:08 -0400208 {
Allen George2e90ef52021-03-01 14:47:04 -0500209 verify_expected_result(thrift_test_client.test_enum(Numberz::TWO), Numberz::TWO)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400210 }
211
Allen Georgebc1344d2017-04-28 10:22:03 -0400212 info!("testBinary");
Allen George8b96bfb2016-11-02 08:01:08 -0400213 {
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 Georgebc1344d2017-04-28 10:22:03 -0400216 verify_expected_result(thrift_test_client.test_binary(b_snd), b_cmp)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400217 }
218
Allen Georgebc1344d2017-04-28 10:22:03 -0400219 info!("testStruct");
Allen George8b96bfb2016-11-02 08:01:08 -0400220 {
221 let x_snd = Xtruct {
222 string_thing: Some("foo".to_owned()),
223 byte_thing: Some(12),
Allen George7ddbcc02020-11-08 09:51:19 -0500224 i32_thing: Some(219_129),
225 i64_thing: Some(12_938_492_818),
Allen George8b96bfb2016-11-02 08:01:08 -0400226 };
227 let x_cmp = Xtruct {
228 string_thing: Some("foo".to_owned()),
229 byte_thing: Some(12),
Allen George7ddbcc02020-11-08 09:51:19 -0500230 i32_thing: Some(219_129),
231 i64_thing: Some(12_938_492_818),
Allen George8b96bfb2016-11-02 08:01:08 -0400232 };
Allen Georgebc1344d2017-04-28 10:22:03 -0400233 verify_expected_result(thrift_test_client.test_struct(x_snd), x_cmp)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400234 }
235
236 // Xtruct again, with optional values
Allen George0e22c362017-01-30 07:15:00 -0500237 // FIXME: apparently the erlang thrift server does not like opt-in-req-out
238 // parameters that are undefined. Joy.
Allen George8b96bfb2016-11-02 08:01:08 -0400239 // {
Allen George0e22c362017-01-30 07:15:00 -0500240 // 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 Georgebc1344d2017-04-28 10:22:03 -0400245 // try!(verify_expected_result(thrift_test_client.test_struct(x_snd), x_cmp));
Allen George8b96bfb2016-11-02 08:01:08 -0400246 // }
247 //
248
Allen Georgebc1344d2017-04-28 10:22:03 -0400249 info!("testNest"); // (FIXME: try Xtruct2 with optional values)
Allen George8b96bfb2016-11-02 08:01:08 -0400250 {
251 let x_snd = Xtruct2 {
252 byte_thing: Some(32),
Allen George0e22c362017-01-30 07:15:00 -0500253 struct_thing: Some(
254 Xtruct {
255 string_thing: Some("foo".to_owned()),
256 byte_thing: Some(1),
Allen George7ddbcc02020-11-08 09:51:19 -0500257 i32_thing: Some(324_382_098),
258 i64_thing: Some(12_938_492_818),
Allen George0e22c362017-01-30 07:15:00 -0500259 },
260 ),
Allen George7ddbcc02020-11-08 09:51:19 -0500261 i32_thing: Some(293_481_098),
Allen George8b96bfb2016-11-02 08:01:08 -0400262 };
263 let x_cmp = Xtruct2 {
264 byte_thing: Some(32),
Allen George0e22c362017-01-30 07:15:00 -0500265 struct_thing: Some(
266 Xtruct {
267 string_thing: Some("foo".to_owned()),
268 byte_thing: Some(1),
Allen George7ddbcc02020-11-08 09:51:19 -0500269 i32_thing: Some(324_382_098),
270 i64_thing: Some(12_938_492_818),
Allen George0e22c362017-01-30 07:15:00 -0500271 },
272 ),
Allen George7ddbcc02020-11-08 09:51:19 -0500273 i32_thing: Some(293_481_098),
Allen George8b96bfb2016-11-02 08:01:08 -0400274 };
Allen Georgebc1344d2017-04-28 10:22:03 -0400275 verify_expected_result(thrift_test_client.test_nest(x_snd), x_cmp)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400276 }
277
Allen Georgebc1344d2017-04-28 10:22:03 -0400278 // do the multiplexed calls while making the main ThriftTest calls
279 if let Some(ref mut client) = second_service_client.as_mut() {
Allen Georgebc1344d2017-04-28 10:22:03 -0400280 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 George8b96bfb2016-11-02 08:01:08 -0400290 {
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 Georgebc1344d2017-04-28 10:22:03 -0400301 verify_expected_result(thrift_test_client.test_list(v_snd), v_cmp)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400302 }
303
Allen Georgebc1344d2017-04-28 10:22:03 -0400304 info!("testSet");
Allen George8b96bfb2016-11-02 08:01:08 -0400305 {
306 let mut s_snd: BTreeSet<i32> = BTreeSet::new();
Allen George7ddbcc02020-11-08 09:51:19 -0500307 s_snd.insert(293_481);
Allen George8b96bfb2016-11-02 08:01:08 -0400308 s_snd.insert(23);
309 s_snd.insert(3234);
310
311 let mut s_cmp: BTreeSet<i32> = BTreeSet::new();
Allen George7ddbcc02020-11-08 09:51:19 -0500312 s_cmp.insert(293_481);
Allen George8b96bfb2016-11-02 08:01:08 -0400313 s_cmp.insert(23);
314 s_cmp.insert(3234);
315
Allen Georgebc1344d2017-04-28 10:22:03 -0400316 verify_expected_result(thrift_test_client.test_set(s_snd), s_cmp)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400317 }
318
Allen Georgebc1344d2017-04-28 10:22:03 -0400319 info!("testMap");
Allen George8b96bfb2016-11-02 08:01:08 -0400320 {
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 Georgebc1344d2017-04-28 10:22:03 -0400331 verify_expected_result(thrift_test_client.test_map(m_snd), m_cmp)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400332 }
333
Allen Georgebc1344d2017-04-28 10:22:03 -0400334 info!("testStringMap");
Allen George8b96bfb2016-11-02 08:01:08 -0400335 {
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 Georgebc1344d2017-04-28 10:22:03 -0400346 verify_expected_result(thrift_test_client.test_string_map(m_snd), m_rcv)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400347 }
348
349 // nested map
Allen George0e22c362017-01-30 07:15:00 -0500350 // expect : {-4 => {-4 => -4, -3 => -3, -2 => -2, -1 => -1, }, 4 => {1 => 1, 2
351 // => 2, 3 => 3, 4 => 4, }, }
Allen Georgebc1344d2017-04-28 10:22:03 -0400352 info!("testMapMap");
Allen George8b96bfb2016-11-02 08:01:08 -0400353 {
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 Georgebc1344d2017-04-28 10:22:03 -0400367 verify_expected_result(thrift_test_client.test_map_map(42), m_cmp)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400368 }
369
Allen Georgebc1344d2017-04-28 10:22:03 -0400370 info!("testMulti");
Allen George8b96bfb2016-11-02 08:01:08 -0400371 {
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 George7ddbcc02020-11-08 09:51:19 -0500379 i32_thing: Some(-123_948),
380 i64_thing: Some(-19_234_123_981),
Allen George8b96bfb2016-11-02 08:01:08 -0400381 };
382
Allen George0e22c362017-01-30 07:15:00 -0500383 verify_expected_result(
Allen George2e90ef52021-03-01 14:47:04 -0500384 thrift_test_client.test_multi(1, -123_948, -19_234_123_981, m_snd, Numberz::EIGHT, 81),
Allen George0e22c362017-01-30 07:15:00 -0500385 s_cmp,
386 )?;
Allen George8b96bfb2016-11-02 08:01:08 -0400387 }
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 George2e90ef52021-03-01 14:47:04 -0500398 arg_map_usermap.insert(Numberz::ONE, 4289);
399 arg_map_usermap.insert(Numberz::EIGHT, 19);
Allen George8b96bfb2016-11-02 08:01:08 -0400400
401 let mut arg_vec_xtructs: Vec<Xtruct> = Vec::new();
Allen George0e22c362017-01-30 07:15:00 -0500402 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 George7ddbcc02020-11-08 09:51:19 -0500422 i32_thing: Some(3_948_539),
423 i64_thing: Some(-12_938_492),
Allen George0e22c362017-01-30 07:15:00 -0500424 },
425 );
Allen George8b96bfb2016-11-02 08:01:08 -0400426
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 George2e90ef52021-03-01 14:47:04 -0500432 s_cmp_nested_1.insert(Numberz::TWO, insanity.clone());
433 s_cmp_nested_1.insert(Numberz::THREE, insanity.clone());
Allen George8b96bfb2016-11-02 08:01:08 -0400434
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 George2e90ef52021-03-01 14:47:04 -0500440 s_cmp_nested_2.insert(Numberz::SIX, empty_insanity);
Allen George8b96bfb2016-11-02 08:01:08 -0400441
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 George7ddbcc02020-11-08 09:51:19 -0500446 verify_expected_result(thrift_test_client.test_insanity(insanity), s_cmp)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400447 }
448
Allen Georgebc1344d2017-04-28 10:22:03 -0400449 info!("testException - remote throws Xception");
Allen George8b96bfb2016-11-02 08:01:08 -0400450 {
Allen Georgebc1344d2017-04-28 10:22:03 -0400451 let r = thrift_test_client.test_exception("Xception".to_owned());
Allen George8b96bfb2016-11-02 08:01:08 -0400452 let x = match r {
453 Err(thrift::Error::User(ref e)) => {
454 match e.downcast_ref::<Xception>() {
455 Some(x) => Ok(x),
Allen George0e22c362017-01-30 07:15:00 -0500456 None => Err(thrift::Error::User("did not get expected Xception struct".into()),),
Allen George8b96bfb2016-11-02 08:01:08 -0400457 }
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 Georgebc1344d2017-04-28 10:22:03 -0400470 info!("testException - remote throws TApplicationException");
Allen George8b96bfb2016-11-02 08:01:08 -0400471 {
Allen Georgebc1344d2017-04-28 10:22:03 -0400472 let r = thrift_test_client.test_exception("TException".to_owned());
Allen George8b96bfb2016-11-02 08:01:08 -0400473 match r {
474 Err(thrift::Error::Application(ref e)) => {
Allen Georgebc1344d2017-04-28 10:22:03 -0400475 info!("received an {:?}", e);
Allen George8b96bfb2016-11-02 08:01:08 -0400476 Ok(())
477 }
478 _ => Err(thrift::Error::User("did not get exception".into())),
479 }?;
480 }
481
Allen Georgebc1344d2017-04-28 10:22:03 -0400482 info!("testException - remote succeeds");
Allen George8b96bfb2016-11-02 08:01:08 -0400483 {
Allen Georgebc1344d2017-04-28 10:22:03 -0400484 let r = thrift_test_client.test_exception("foo".to_owned());
Allen George8b96bfb2016-11-02 08:01:08 -0400485 match r {
486 Ok(_) => Ok(()),
487 _ => Err(thrift::Error::User("received an exception".into())),
488 }?;
489 }
490
Allen Georgebc1344d2017-04-28 10:22:03 -0400491 info!("testMultiException - remote throws Xception");
Allen George8b96bfb2016-11-02 08:01:08 -0400492 {
Allen Georgebc1344d2017-04-28 10:22:03 -0400493 let r =
494 thrift_test_client.test_multi_exception("Xception".to_owned(), "ignored".to_owned());
Allen George8b96bfb2016-11-02 08:01:08 -0400495 let x = match r {
496 Err(thrift::Error::User(ref e)) => {
497 match e.downcast_ref::<Xception>() {
498 Some(x) => Ok(x),
Allen George0e22c362017-01-30 07:15:00 -0500499 None => Err(thrift::Error::User("did not get expected Xception struct".into()),),
Allen George8b96bfb2016-11-02 08:01:08 -0400500 }
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 Georgebc1344d2017-04-28 10:22:03 -0400513 info!("testMultiException - remote throws Xception2");
Allen George8b96bfb2016-11-02 08:01:08 -0400514 {
Allen Georgebc1344d2017-04-28 10:22:03 -0400515 let r =
516 thrift_test_client.test_multi_exception("Xception2".to_owned(), "ignored".to_owned());
Allen George8b96bfb2016-11-02 08:01:08 -0400517 let x = match r {
518 Err(thrift::Error::User(ref e)) => {
519 match e.downcast_ref::<Xception2>() {
520 Some(x) => Ok(x),
Allen George0e22c362017-01-30 07:15:00 -0500521 None => Err(thrift::Error::User("did not get expected Xception struct".into()),),
Allen George8b96bfb2016-11-02 08:01:08 -0400522 }
523 }
524 _ => Err(thrift::Error::User("did not get exception".into())),
525 }?;
526
527 let x_cmp = Xception2 {
528 error_code: Some(2002),
Allen George0e22c362017-01-30 07:15:00 -0500529 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 George8b96bfb2016-11-02 08:01:08 -0400540 };
541
542 verify_expected_result(Ok(x), &x_cmp)?;
543 }
544
Allen Georgebc1344d2017-04-28 10:22:03 -0400545 info!("testMultiException - remote succeeds");
Allen George8b96bfb2016-11-02 08:01:08 -0400546 {
Allen Georgebc1344d2017-04-28 10:22:03 -0400547 let r = thrift_test_client.test_multi_exception("haha".to_owned(), "RETURNED".to_owned());
Allen George8b96bfb2016-11-02 08:01:08 -0400548 let x = match r {
Allen George0e22c362017-01-30 07:15:00 -0500549 Err(e) => Err(thrift::Error::User(format!("received an unexpected exception {:?}", e).into(),),),
Allen George8b96bfb2016-11-02 08:01:08 -0400550 _ => r,
551 }?;
552
553 let x_cmp = Xtruct {
554 string_thing: Some("RETURNED".to_owned()),
Allen George0e22c362017-01-30 07:15:00 -0500555 // 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 George8b96bfb2016-11-02 08:01:08 -0400561 };
562
563 verify_expected_result(Ok(x), x_cmp)?;
564 }
565
Allen Georgebc1344d2017-04-28 10:22:03 -0400566 info!("testOneWay - remote sleeps for 1 second");
Allen George8b96bfb2016-11-02 08:01:08 -0400567 {
Allen Georgebc1344d2017-04-28 10:22:03 -0400568 thrift_test_client.test_oneway(1)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400569 }
570
Allen George0e22c362017-01-30 07:15:00 -0500571 // final test to verify that the connection is still writable after the one-way
572 // call
Allen Georgebc1344d2017-04-28 10:22:03 -0400573 thrift_test_client.test_void()
Allen George8b96bfb2016-11-02 08:01:08 -0400574}
575
Allen George0e22c362017-01-30 07:15:00 -0500576fn verify_expected_result<T: Debug + PartialEq + Sized>(
577 actual: Result<T, thrift::Error>,
578 expected: T,
579) -> Result<(), thrift::Error> {
James E. King, III20e16bc2017-11-18 22:37:54 -0500580 info!("*** EXPECTED: Ok({:?})", expected);
581 info!("*** ACTUAL : {:?}", actual);
Allen George8b96bfb2016-11-02 08:01:08 -0400582 match actual {
583 Ok(v) => {
584 if v == expected {
James E. King, III20e16bc2017-11-18 22:37:54 -0500585 info!("*** OK ***");
Allen George8b96bfb2016-11-02 08:01:08 -0400586 Ok(())
587 } else {
James E. King, III20e16bc2017-11-18 22:37:54 -0500588 info!("*** FAILED ***");
Allen George0e22c362017-01-30 07:15:00 -0500589 Err(thrift::Error::User(format!("expected {:?} but got {:?}", &expected, &v).into()),)
Allen George8b96bfb2016-11-02 08:01:08 -0400590 }
591 }
592 Err(e) => Err(e),
593 }
594}