blob: 8623915d478088295553e64d77cfb4e94e4ec948 [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 George55c3e4c2021-03-01 23:19:52 -050018use clap::{clap_app, value_t};
Allen George7ddbcc02020-11-08 09:51:19 -050019use env_logger;
20use log::*;
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;
Allen George55c3e4c2021-03-01 23:19:52 -050027use thrift::protocol::{
28 TBinaryInputProtocol, TBinaryOutputProtocol, TCompactInputProtocol, TCompactOutputProtocol,
29 TInputProtocol, TMultiplexedOutputProtocol, TOutputProtocol,
30};
31use thrift::transport::{
32 TBufferedReadTransport, TBufferedWriteTransport, TFramedReadTransport, TFramedWriteTransport,
33 TIoChannel, TReadTransport, TTcpChannel, TWriteTransport,
34};
Allen George7ddbcc02020-11-08 09:51:19 -050035use thrift::OrderedFloat;
Allen George8b96bfb2016-11-02 08:01:08 -040036use thrift_test::*;
37
38fn main() {
Allen George7ddbcc02020-11-08 09:51:19 -050039 env_logger::init();
Allen Georgebc1344d2017-04-28 10:22:03 -040040
41 debug!("initialized logger - running cross-test client");
42
Allen George8b96bfb2016-11-02 08:01:08 -040043 match run() {
Allen Georgebc1344d2017-04-28 10:22:03 -040044 Ok(()) => info!("cross-test client succeeded"),
Allen George8b96bfb2016-11-02 08:01:08 -040045 Err(e) => {
Allen Georgebc1344d2017-04-28 10:22:03 -040046 info!("cross-test client failed with error {:?}", e);
Allen George8b96bfb2016-11-02 08:01:08 -040047 std::process::exit(1);
48 }
49 }
50}
51
52fn run() -> thrift::Result<()> {
53 // unsupported options:
54 // --domain-socket
Jens Geyer4a33b182020-03-22 13:46:34 +010055 // --pipe
Allen George8b96bfb2016-11-02 08:01:08 -040056 // --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 III9804ab92019-02-07 16:59:05 -050066 (@arg protocol: --protocol +takes_value "Thrift protocol implementation to use (\"binary\", \"compact\", \"multi\", \"multic\")")
Allen George8b96bfb2016-11-02 08:01:08 -040067 (@arg testloops: -n --testloops +takes_value "Number of times to run tests")
Allen George0e22c362017-01-30 07:15:00 -050068 )
Allen Georgebc1344d2017-04-28 10:22:03 -040069 .get_matches();
Allen George8b96bfb2016-11-02 08:01:08 -040070
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 George5cff2792021-02-28 07:43:51 -050077 // 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 George8b96bfb2016-11-02 08:01:08 -040081
Allen Georgebc1344d2017-04-28 10:22:03 -040082 let mut second_service_client = if protocol.starts_with("multi") {
Allen George5cff2792021-02-28 07:43:51 -050083 let shared_stream_clone = shared_stream.try_clone()?;
84 let (i_prot, o_prot) = build(shared_stream_clone, transport, protocol, "SecondService")?;
Allen Georgebc1344d2017-04-28 10:22:03 -040085 Some(SecondServiceSyncClient::new(i_prot, o_prot))
86 } else {
87 None
Allen George8b96bfb2016-11-02 08:01:08 -040088 };
89
Allen George5cff2792021-02-28 07:43:51 -050090 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 Georgebc1344d2017-04-28 10:22:03 -040095 info!(
Allen George0e22c362017-01-30 07:15:00 -050096 "connecting to {}:{} with {}+{} stack",
Allen George55c3e4c2021-03-01 23:19:52 -050097 host, port, protocol, transport
Allen George0e22c362017-01-30 07:15:00 -050098 );
Allen George8b96bfb2016-11-02 08:01:08 -040099
Allen George8b96bfb2016-11-02 08:01:08 -0400100 for _ in 0..testloops {
Allen Georgebc1344d2017-04-28 10:22:03 -0400101 make_thrift_calls(&mut thrift_test_client, &mut second_service_client)?
Allen George8b96bfb2016-11-02 08:01:08 -0400102 }
103
104 Ok(())
105}
106
Allen George5cff2792021-02-28 07:43:51 -0500107fn build(
108 stream: TcpStream,
Allen Georgebc1344d2017-04-28 10:22:03 -0400109 transport: &str,
110 protocol: &str,
111 service_name: &str,
Allen Georgeb0d14132020-03-29 11:48:55 -0400112) -> thrift::Result<(Box<dyn TInputProtocol>, Box<dyn TOutputProtocol>)> {
Allen George5cff2792021-02-28 07:43:51 -0500113 let c = TTcpChannel::with_stream(stream);
114 let (i_chan, o_chan) = c.split()?;
Allen Georgebc1344d2017-04-28 10:22:03 -0400115
Allen Georgeb0d14132020-03-29 11:48:55 -0400116 let (i_tran, o_tran): (Box<dyn TReadTransport>, Box<dyn TWriteTransport>) = match transport {
Allen George55c3e4c2021-03-01 23:19:52 -0500117 "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 Georgebc1344d2017-04-28 10:22:03 -0400125 unmatched => return Err(format!("unsupported transport {}", unmatched).into()),
126 };
127
Allen Georgeb0d14132020-03-29 11:48:55 -0400128 let (i_prot, o_prot): (Box<dyn TInputProtocol>, Box<dyn TOutputProtocol>) = match protocol {
Allen George55c3e4c2021-03-01 23:19:52 -0500129 "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 Georgebc1344d2017-04-28 10:22:03 -0400151 unmatched => return Err(format!("unsupported protocol {}", unmatched).into()),
152 };
153
154 Ok((i_prot, o_prot))
155}
156
Allen George55c3e4c2021-03-01 23:19:52 -0500157type BuildThriftTestClient =
158 ThriftTestSyncClient<Box<dyn TInputProtocol>, Box<dyn TOutputProtocol>>;
159type BuiltSecondServiceClient =
160 SecondServiceSyncClient<Box<dyn TInputProtocol>, Box<dyn TOutputProtocol>>;
Allen George8b96bfb2016-11-02 08:01:08 -0400161
Allen George7ddbcc02020-11-08 09:51:19 -0500162#[allow(clippy::cognitive_complexity)]
Allen Georgebc1344d2017-04-28 10:22:03 -0400163fn 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 George8b96bfb2016-11-02 08:01:08 -0400169
Allen Georgebc1344d2017-04-28 10:22:03 -0400170 info!("testString");
Allen George0e22c362017-01-30 07:15:00 -0500171 verify_expected_result(
Allen Georgebc1344d2017-04-28 10:22:03 -0400172 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 George7ddbcc02020-11-08 09:51:19 -0500186 verify_expected_result(thrift_test_client.test_i32(1_159_348_374), 1_159_348_374)?;
Allen Georgebc1344d2017-04-28 10:22:03 -0400187
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 George0e22c362017-01-30 07:15:00 -0500199 OrderedFloat::from(42.42),
200 )?;
Allen George8b96bfb2016-11-02 08:01:08 -0400201
Allen Georgebc1344d2017-04-28 10:22:03 -0400202 info!("testTypedef");
Allen George8b96bfb2016-11-02 08:01:08 -0400203 {
204 let u_snd: UserId = 2348;
205 let u_cmp: UserId = 2348;
Allen Georgebc1344d2017-04-28 10:22:03 -0400206 verify_expected_result(thrift_test_client.test_typedef(u_snd), u_cmp)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400207 }
208
Allen Georgebc1344d2017-04-28 10:22:03 -0400209 info!("testEnum");
Allen George8b96bfb2016-11-02 08:01:08 -0400210 {
Allen George2e90ef52021-03-01 14:47:04 -0500211 verify_expected_result(thrift_test_client.test_enum(Numberz::TWO), Numberz::TWO)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400212 }
213
Allen Georgebc1344d2017-04-28 10:22:03 -0400214 info!("testBinary");
Allen George8b96bfb2016-11-02 08:01:08 -0400215 {
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 Georgebc1344d2017-04-28 10:22:03 -0400218 verify_expected_result(thrift_test_client.test_binary(b_snd), b_cmp)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400219 }
220
Allen Georgebc1344d2017-04-28 10:22:03 -0400221 info!("testStruct");
Allen George8b96bfb2016-11-02 08:01:08 -0400222 {
223 let x_snd = Xtruct {
224 string_thing: Some("foo".to_owned()),
225 byte_thing: Some(12),
Allen George7ddbcc02020-11-08 09:51:19 -0500226 i32_thing: Some(219_129),
227 i64_thing: Some(12_938_492_818),
Allen George8b96bfb2016-11-02 08:01:08 -0400228 };
229 let x_cmp = Xtruct {
230 string_thing: Some("foo".to_owned()),
231 byte_thing: Some(12),
Allen George7ddbcc02020-11-08 09:51:19 -0500232 i32_thing: Some(219_129),
233 i64_thing: Some(12_938_492_818),
Allen George8b96bfb2016-11-02 08:01:08 -0400234 };
Allen Georgebc1344d2017-04-28 10:22:03 -0400235 verify_expected_result(thrift_test_client.test_struct(x_snd), x_cmp)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400236 }
237
238 // Xtruct again, with optional values
Allen George0e22c362017-01-30 07:15:00 -0500239 // FIXME: apparently the erlang thrift server does not like opt-in-req-out
240 // parameters that are undefined. Joy.
Allen George8b96bfb2016-11-02 08:01:08 -0400241 // {
Allen George0e22c362017-01-30 07:15:00 -0500242 // 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 Georgebc1344d2017-04-28 10:22:03 -0400247 // try!(verify_expected_result(thrift_test_client.test_struct(x_snd), x_cmp));
Allen George8b96bfb2016-11-02 08:01:08 -0400248 // }
249 //
250
Allen Georgebc1344d2017-04-28 10:22:03 -0400251 info!("testNest"); // (FIXME: try Xtruct2 with optional values)
Allen George8b96bfb2016-11-02 08:01:08 -0400252 {
253 let x_snd = Xtruct2 {
254 byte_thing: Some(32),
Allen George55c3e4c2021-03-01 23:19:52 -0500255 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 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 George55c3e4c2021-03-01 23:19:52 -0500265 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 George7ddbcc02020-11-08 09:51:19 -0500271 i32_thing: Some(293_481_098),
Allen George8b96bfb2016-11-02 08:01:08 -0400272 };
Allen Georgebc1344d2017-04-28 10:22:03 -0400273 verify_expected_result(thrift_test_client.test_nest(x_snd), x_cmp)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400274 }
275
Allen Georgebc1344d2017-04-28 10:22:03 -0400276 // do the multiplexed calls while making the main ThriftTest calls
277 if let Some(ref mut client) = second_service_client.as_mut() {
Allen Georgebc1344d2017-04-28 10:22:03 -0400278 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 George8b96bfb2016-11-02 08:01:08 -0400288 {
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 Georgebc1344d2017-04-28 10:22:03 -0400299 verify_expected_result(thrift_test_client.test_list(v_snd), v_cmp)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400300 }
301
Allen Georgebc1344d2017-04-28 10:22:03 -0400302 info!("testSet");
Allen George8b96bfb2016-11-02 08:01:08 -0400303 {
304 let mut s_snd: BTreeSet<i32> = BTreeSet::new();
Allen George7ddbcc02020-11-08 09:51:19 -0500305 s_snd.insert(293_481);
Allen George8b96bfb2016-11-02 08:01:08 -0400306 s_snd.insert(23);
307 s_snd.insert(3234);
308
309 let mut s_cmp: BTreeSet<i32> = BTreeSet::new();
Allen George7ddbcc02020-11-08 09:51:19 -0500310 s_cmp.insert(293_481);
Allen George8b96bfb2016-11-02 08:01:08 -0400311 s_cmp.insert(23);
312 s_cmp.insert(3234);
313
Allen Georgebc1344d2017-04-28 10:22:03 -0400314 verify_expected_result(thrift_test_client.test_set(s_snd), s_cmp)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400315 }
316
Allen Georgebc1344d2017-04-28 10:22:03 -0400317 info!("testMap");
Allen George8b96bfb2016-11-02 08:01:08 -0400318 {
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 Georgebc1344d2017-04-28 10:22:03 -0400329 verify_expected_result(thrift_test_client.test_map(m_snd), m_cmp)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400330 }
331
Allen Georgebc1344d2017-04-28 10:22:03 -0400332 info!("testStringMap");
Allen George8b96bfb2016-11-02 08:01:08 -0400333 {
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 Georgebc1344d2017-04-28 10:22:03 -0400344 verify_expected_result(thrift_test_client.test_string_map(m_snd), m_rcv)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400345 }
346
347 // nested map
Allen George0e22c362017-01-30 07:15:00 -0500348 // expect : {-4 => {-4 => -4, -3 => -3, -2 => -2, -1 => -1, }, 4 => {1 => 1, 2
349 // => 2, 3 => 3, 4 => 4, }, }
Allen Georgebc1344d2017-04-28 10:22:03 -0400350 info!("testMapMap");
Allen George8b96bfb2016-11-02 08:01:08 -0400351 {
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 Georgebc1344d2017-04-28 10:22:03 -0400365 verify_expected_result(thrift_test_client.test_map_map(42), m_cmp)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400366 }
367
Allen Georgebc1344d2017-04-28 10:22:03 -0400368 info!("testMulti");
Allen George8b96bfb2016-11-02 08:01:08 -0400369 {
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 George7ddbcc02020-11-08 09:51:19 -0500377 i32_thing: Some(-123_948),
378 i64_thing: Some(-19_234_123_981),
Allen George8b96bfb2016-11-02 08:01:08 -0400379 };
380
Allen George0e22c362017-01-30 07:15:00 -0500381 verify_expected_result(
Allen George2e90ef52021-03-01 14:47:04 -0500382 thrift_test_client.test_multi(1, -123_948, -19_234_123_981, m_snd, Numberz::EIGHT, 81),
Allen George0e22c362017-01-30 07:15:00 -0500383 s_cmp,
384 )?;
Allen George8b96bfb2016-11-02 08:01:08 -0400385 }
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 George2e90ef52021-03-01 14:47:04 -0500396 arg_map_usermap.insert(Numberz::ONE, 4289);
397 arg_map_usermap.insert(Numberz::EIGHT, 19);
Allen George8b96bfb2016-11-02 08:01:08 -0400398
399 let mut arg_vec_xtructs: Vec<Xtruct> = Vec::new();
Allen George55c3e4c2021-03-01 23:19:52 -0500400 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 George8b96bfb2016-11-02 08:01:08 -0400418
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 George2e90ef52021-03-01 14:47:04 -0500424 s_cmp_nested_1.insert(Numberz::TWO, insanity.clone());
425 s_cmp_nested_1.insert(Numberz::THREE, insanity.clone());
Allen George8b96bfb2016-11-02 08:01:08 -0400426
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 George2e90ef52021-03-01 14:47:04 -0500432 s_cmp_nested_2.insert(Numberz::SIX, empty_insanity);
Allen George8b96bfb2016-11-02 08:01:08 -0400433
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 George7ddbcc02020-11-08 09:51:19 -0500438 verify_expected_result(thrift_test_client.test_insanity(insanity), s_cmp)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400439 }
440
Allen Georgebc1344d2017-04-28 10:22:03 -0400441 info!("testException - remote throws Xception");
Allen George8b96bfb2016-11-02 08:01:08 -0400442 {
Allen Georgebc1344d2017-04-28 10:22:03 -0400443 let r = thrift_test_client.test_exception("Xception".to_owned());
Allen George8b96bfb2016-11-02 08:01:08 -0400444 let x = match r {
Allen George55c3e4c2021-03-01 23:19:52 -0500445 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 George8b96bfb2016-11-02 08:01:08 -0400451 _ => 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 Georgebc1344d2017-04-28 10:22:03 -0400462 info!("testException - remote throws TApplicationException");
Allen George8b96bfb2016-11-02 08:01:08 -0400463 {
Allen Georgebc1344d2017-04-28 10:22:03 -0400464 let r = thrift_test_client.test_exception("TException".to_owned());
Allen George8b96bfb2016-11-02 08:01:08 -0400465 match r {
466 Err(thrift::Error::Application(ref e)) => {
Allen Georgebc1344d2017-04-28 10:22:03 -0400467 info!("received an {:?}", e);
Allen George8b96bfb2016-11-02 08:01:08 -0400468 Ok(())
469 }
470 _ => Err(thrift::Error::User("did not get exception".into())),
471 }?;
472 }
473
Allen Georgebc1344d2017-04-28 10:22:03 -0400474 info!("testException - remote succeeds");
Allen George8b96bfb2016-11-02 08:01:08 -0400475 {
Allen Georgebc1344d2017-04-28 10:22:03 -0400476 let r = thrift_test_client.test_exception("foo".to_owned());
Allen George8b96bfb2016-11-02 08:01:08 -0400477 match r {
478 Ok(_) => Ok(()),
479 _ => Err(thrift::Error::User("received an exception".into())),
480 }?;
481 }
482
Allen Georgebc1344d2017-04-28 10:22:03 -0400483 info!("testMultiException - remote throws Xception");
Allen George8b96bfb2016-11-02 08:01:08 -0400484 {
Allen Georgebc1344d2017-04-28 10:22:03 -0400485 let r =
486 thrift_test_client.test_multi_exception("Xception".to_owned(), "ignored".to_owned());
Allen George8b96bfb2016-11-02 08:01:08 -0400487 let x = match r {
Allen George55c3e4c2021-03-01 23:19:52 -0500488 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 George8b96bfb2016-11-02 08:01:08 -0400494 _ => 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 Georgebc1344d2017-04-28 10:22:03 -0400505 info!("testMultiException - remote throws Xception2");
Allen George8b96bfb2016-11-02 08:01:08 -0400506 {
Allen Georgebc1344d2017-04-28 10:22:03 -0400507 let r =
508 thrift_test_client.test_multi_exception("Xception2".to_owned(), "ignored".to_owned());
Allen George8b96bfb2016-11-02 08:01:08 -0400509 let x = match r {
Allen George55c3e4c2021-03-01 23:19:52 -0500510 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 George8b96bfb2016-11-02 08:01:08 -0400516 _ => Err(thrift::Error::User("did not get exception".into())),
517 }?;
518
519 let x_cmp = Xception2 {
520 error_code: Some(2002),
Allen George55c3e4c2021-03-01 23:19:52 -0500521 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 George8b96bfb2016-11-02 08:01:08 -0400530 };
531
532 verify_expected_result(Ok(x), &x_cmp)?;
533 }
534
Allen Georgebc1344d2017-04-28 10:22:03 -0400535 info!("testMultiException - remote succeeds");
Allen George8b96bfb2016-11-02 08:01:08 -0400536 {
Allen Georgebc1344d2017-04-28 10:22:03 -0400537 let r = thrift_test_client.test_multi_exception("haha".to_owned(), "RETURNED".to_owned());
Allen George8b96bfb2016-11-02 08:01:08 -0400538 let x = match r {
Allen George55c3e4c2021-03-01 23:19:52 -0500539 Err(e) => Err(thrift::Error::User(
540 format!("received an unexpected exception {:?}", e).into(),
541 )),
Allen George8b96bfb2016-11-02 08:01:08 -0400542 _ => r,
543 }?;
544
545 let x_cmp = Xtruct {
546 string_thing: Some("RETURNED".to_owned()),
Allen George0e22c362017-01-30 07:15:00 -0500547 // 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 George8b96bfb2016-11-02 08:01:08 -0400553 };
554
555 verify_expected_result(Ok(x), x_cmp)?;
556 }
557
Allen Georgebc1344d2017-04-28 10:22:03 -0400558 info!("testOneWay - remote sleeps for 1 second");
Allen George8b96bfb2016-11-02 08:01:08 -0400559 {
Allen Georgebc1344d2017-04-28 10:22:03 -0400560 thrift_test_client.test_oneway(1)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400561 }
562
Allen George0e22c362017-01-30 07:15:00 -0500563 // final test to verify that the connection is still writable after the one-way
564 // call
Allen Georgebc1344d2017-04-28 10:22:03 -0400565 thrift_test_client.test_void()
Allen George8b96bfb2016-11-02 08:01:08 -0400566}
567
Allen George0e22c362017-01-30 07:15:00 -0500568fn verify_expected_result<T: Debug + PartialEq + Sized>(
569 actual: Result<T, thrift::Error>,
570 expected: T,
571) -> Result<(), thrift::Error> {
James E. King, III20e16bc2017-11-18 22:37:54 -0500572 info!("*** EXPECTED: Ok({:?})", expected);
573 info!("*** ACTUAL : {:?}", actual);
Allen George8b96bfb2016-11-02 08:01:08 -0400574 match actual {
575 Ok(v) => {
576 if v == expected {
James E. King, III20e16bc2017-11-18 22:37:54 -0500577 info!("*** OK ***");
Allen George8b96bfb2016-11-02 08:01:08 -0400578 Ok(())
579 } else {
James E. King, III20e16bc2017-11-18 22:37:54 -0500580 info!("*** FAILED ***");
Allen George55c3e4c2021-03-01 23:19:52 -0500581 Err(thrift::Error::User(
582 format!("expected {:?} but got {:?}", &expected, &v).into(),
583 ))
Allen George8b96bfb2016-11-02 08:01:08 -0400584 }
585 }
586 Err(e) => Err(e),
587 }
588}