blob: a44bac3c9cf78f6996514359295ccff91c7df4da [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 log::*;
Allen Georgebc1344d2017-04-28 10:22:03 -040020
Allen George8b96bfb2016-11-02 08:01:08 -040021use std::collections::{BTreeMap, BTreeSet};
22use std::fmt::Debug;
tokcumf0336412022-03-30 11:39:08 +020023use std::net::{TcpStream, ToSocketAddrs};
24
25#[cfg(unix)]
26use std::os::unix::net::UnixStream;
27#[cfg(unix)]
28use std::path::Path;
Allen George8b96bfb2016-11-02 08:01:08 -040029
Allen George55c3e4c2021-03-01 23:19:52 -050030use thrift::protocol::{
31 TBinaryInputProtocol, TBinaryOutputProtocol, TCompactInputProtocol, TCompactOutputProtocol,
32 TInputProtocol, TMultiplexedOutputProtocol, TOutputProtocol,
33};
34use thrift::transport::{
35 TBufferedReadTransport, TBufferedWriteTransport, TFramedReadTransport, TFramedWriteTransport,
36 TIoChannel, TReadTransport, TTcpChannel, TWriteTransport,
37};
Allen George7ddbcc02020-11-08 09:51:19 -050038use thrift::OrderedFloat;
Allen George8b96bfb2016-11-02 08:01:08 -040039use thrift_test::*;
40
tokcumf0336412022-03-30 11:39:08 +020041type ThriftClientPair = (
42 ThriftTestSyncClient<Box<dyn TInputProtocol>, Box<dyn TOutputProtocol>>,
43 Option<SecondServiceSyncClient<Box<dyn TInputProtocol>, Box<dyn TOutputProtocol>>>,
44);
45
Allen George8b96bfb2016-11-02 08:01:08 -040046fn main() {
Allen George7ddbcc02020-11-08 09:51:19 -050047 env_logger::init();
Allen Georgebc1344d2017-04-28 10:22:03 -040048
49 debug!("initialized logger - running cross-test client");
50
Allen George8b96bfb2016-11-02 08:01:08 -040051 match run() {
Allen Georgebc1344d2017-04-28 10:22:03 -040052 Ok(()) => info!("cross-test client succeeded"),
Allen George8b96bfb2016-11-02 08:01:08 -040053 Err(e) => {
Allen Georgebc1344d2017-04-28 10:22:03 -040054 info!("cross-test client failed with error {:?}", e);
Allen George8b96bfb2016-11-02 08:01:08 -040055 std::process::exit(1);
56 }
57 }
58}
59
60fn run() -> thrift::Result<()> {
61 // unsupported options:
Jens Geyer4a33b182020-03-22 13:46:34 +010062 // --pipe
Allen George8b96bfb2016-11-02 08:01:08 -040063 // --anon-pipes
64 // --ssl
65 // --threads
66 let matches = clap_app!(rust_test_client =>
67 (version: "1.0")
68 (author: "Apache Thrift Developers <dev@thrift.apache.org>")
69 (about: "Rust Thrift test client")
70 (@arg host: --host +takes_value "Host on which the Thrift test server is located")
71 (@arg port: --port +takes_value "Port on which the Thrift test server is listening")
tokcumf0336412022-03-30 11:39:08 +020072 (@arg domain_socket: --("domain-socket") +takes_value "Unix Domain Socket on which the Thrift test server is listening")
James E. King III9804ab92019-02-07 16:59:05 -050073 (@arg protocol: --protocol +takes_value "Thrift protocol implementation to use (\"binary\", \"compact\", \"multi\", \"multic\")")
tokcumf0336412022-03-30 11:39:08 +020074 (@arg transport: --transport +takes_value "Thrift transport implementation to use (\"buffered\", \"framed\")")
Allen George8b96bfb2016-11-02 08:01:08 -040075 (@arg testloops: -n --testloops +takes_value "Number of times to run tests")
Allen George0e22c362017-01-30 07:15:00 -050076 )
Allen Georgebc1344d2017-04-28 10:22:03 -040077 .get_matches();
Allen George8b96bfb2016-11-02 08:01:08 -040078
79 let host = matches.value_of("host").unwrap_or("127.0.0.1");
80 let port = value_t!(matches, "port", u16).unwrap_or(9090);
tokcumf0336412022-03-30 11:39:08 +020081 let domain_socket = matches.value_of("domain_socket");
Allen George8b96bfb2016-11-02 08:01:08 -040082 let protocol = matches.value_of("protocol").unwrap_or("binary");
tokcumf0336412022-03-30 11:39:08 +020083 let transport = matches.value_of("transport").unwrap_or("buffered");
84 let testloops = value_t!(matches, "testloops", u8).unwrap_or(1);
Allen George8b96bfb2016-11-02 08:01:08 -040085
tokcumf0336412022-03-30 11:39:08 +020086 let (mut thrift_test_client, mut second_service_client) = match domain_socket {
87 None => {
88 let listen_address = format!("{}:{}", host, port);
89 info!(
90 "Client binds to {} with {}+{} stack",
91 listen_address, protocol, transport
92 );
93 bind(listen_address.as_str(), protocol, transport)?
94 }
95 Some(domain_socket) => {
96 info!(
97 "Client binds to {} (UDS) with {}+{} stack",
98 domain_socket, protocol, transport
99 );
100 bind_uds(domain_socket, protocol, transport)?
101 }
Allen George8b96bfb2016-11-02 08:01:08 -0400102 };
103
Allen George8b96bfb2016-11-02 08:01:08 -0400104 for _ in 0..testloops {
Allen Georgebc1344d2017-04-28 10:22:03 -0400105 make_thrift_calls(&mut thrift_test_client, &mut second_service_client)?
Allen George8b96bfb2016-11-02 08:01:08 -0400106 }
107
108 Ok(())
109}
110
tokcumf0336412022-03-30 11:39:08 +0200111fn bind<A: ToSocketAddrs>(
112 listen_address: A,
113 protocol: &str,
114 transport: &str,
115) -> Result<ThriftClientPair, thrift::Error> {
116 // create a TCPStream that will be shared by all Thrift clients
117 // service calls from multiple Thrift clients will be interleaved over the same connection
118 // this isn't a problem for us because we're single-threaded and all calls block to completion
119 let shared_stream = TcpStream::connect(listen_address)?;
120
121 let second_service_client = if protocol.starts_with("multi") {
122 let shared_stream_clone = shared_stream.try_clone()?;
123 let channel = TTcpChannel::with_stream(shared_stream_clone);
124 let (i_prot, o_prot) = build(channel, transport, protocol, "SecondService")?;
125 Some(SecondServiceSyncClient::new(i_prot, o_prot))
126 } else {
127 None
128 };
129
130 let thrift_test_client = {
131 let channel = TTcpChannel::with_stream(shared_stream);
132 let (i_prot, o_prot) = build(channel, transport, protocol, "ThriftTest")?;
133 ThriftTestSyncClient::new(i_prot, o_prot)
134 };
135
136 Ok((thrift_test_client, second_service_client))
137}
138
139#[cfg(unix)]
140fn bind_uds<P: AsRef<Path>>(
141 domain_socket: P,
142 protocol: &str,
143 transport: &str,
144) -> Result<ThriftClientPair, thrift::Error> {
145 // create a UnixStream that will be shared by all Thrift clients
146 // service calls from multiple Thrift clients will be interleaved over the same connection
147 // this isn't a problem for us because we're single-threaded and all calls block to completion
148 let shared_stream = UnixStream::connect(domain_socket)?;
149
150 let second_service_client = if protocol.starts_with("multi") {
151 let shared_stream_clone = shared_stream.try_clone()?;
152 let (i_prot, o_prot) = build(shared_stream_clone, transport, protocol, "SecondService")?;
153 Some(SecondServiceSyncClient::new(i_prot, o_prot))
154 } else {
155 None
156 };
157
158 let thrift_test_client = {
159 let (i_prot, o_prot) = build(shared_stream, transport, protocol, "ThriftTest")?;
160 ThriftTestSyncClient::new(i_prot, o_prot)
161 };
162
163 Ok((thrift_test_client, second_service_client))
164}
165
166fn build<C: TIoChannel + 'static>(
167 channel: C,
Allen Georgebc1344d2017-04-28 10:22:03 -0400168 transport: &str,
169 protocol: &str,
170 service_name: &str,
Allen Georgeb0d14132020-03-29 11:48:55 -0400171) -> thrift::Result<(Box<dyn TInputProtocol>, Box<dyn TOutputProtocol>)> {
tokcumf0336412022-03-30 11:39:08 +0200172 let (i_chan, o_chan) = channel.split()?;
Allen Georgebc1344d2017-04-28 10:22:03 -0400173
Allen Georgeb0d14132020-03-29 11:48:55 -0400174 let (i_tran, o_tran): (Box<dyn TReadTransport>, Box<dyn TWriteTransport>) = match transport {
Allen George55c3e4c2021-03-01 23:19:52 -0500175 "buffered" => (
176 Box::new(TBufferedReadTransport::new(i_chan)),
177 Box::new(TBufferedWriteTransport::new(o_chan)),
178 ),
179 "framed" => (
180 Box::new(TFramedReadTransport::new(i_chan)),
181 Box::new(TFramedWriteTransport::new(o_chan)),
182 ),
Allen Georgebc1344d2017-04-28 10:22:03 -0400183 unmatched => return Err(format!("unsupported transport {}", unmatched).into()),
184 };
185
Allen Georgeb0d14132020-03-29 11:48:55 -0400186 let (i_prot, o_prot): (Box<dyn TInputProtocol>, Box<dyn TOutputProtocol>) = match protocol {
Allen George55c3e4c2021-03-01 23:19:52 -0500187 "binary" => (
188 Box::new(TBinaryInputProtocol::new(i_tran, true)),
189 Box::new(TBinaryOutputProtocol::new(o_tran, true)),
190 ),
191 "multi" => (
192 Box::new(TBinaryInputProtocol::new(i_tran, true)),
193 Box::new(TMultiplexedOutputProtocol::new(
194 service_name,
195 TBinaryOutputProtocol::new(o_tran, true),
196 )),
197 ),
198 "compact" => (
199 Box::new(TCompactInputProtocol::new(i_tran)),
200 Box::new(TCompactOutputProtocol::new(o_tran)),
201 ),
202 "multic" => (
203 Box::new(TCompactInputProtocol::new(i_tran)),
204 Box::new(TMultiplexedOutputProtocol::new(
205 service_name,
206 TCompactOutputProtocol::new(o_tran),
207 )),
208 ),
Allen Georgebc1344d2017-04-28 10:22:03 -0400209 unmatched => return Err(format!("unsupported protocol {}", unmatched).into()),
210 };
211
212 Ok((i_prot, o_prot))
213}
214
Allen George55c3e4c2021-03-01 23:19:52 -0500215type BuildThriftTestClient =
216 ThriftTestSyncClient<Box<dyn TInputProtocol>, Box<dyn TOutputProtocol>>;
217type BuiltSecondServiceClient =
218 SecondServiceSyncClient<Box<dyn TInputProtocol>, Box<dyn TOutputProtocol>>;
Allen George8b96bfb2016-11-02 08:01:08 -0400219
Allen George7ddbcc02020-11-08 09:51:19 -0500220#[allow(clippy::cognitive_complexity)]
Allen Georgebc1344d2017-04-28 10:22:03 -0400221fn make_thrift_calls(
222 thrift_test_client: &mut BuildThriftTestClient,
223 second_service_client: &mut Option<BuiltSecondServiceClient>,
224) -> Result<(), thrift::Error> {
225 info!("testVoid");
226 thrift_test_client.test_void()?;
Allen George8b96bfb2016-11-02 08:01:08 -0400227
Allen Georgebc1344d2017-04-28 10:22:03 -0400228 info!("testString");
Allen George0e22c362017-01-30 07:15:00 -0500229 verify_expected_result(
Allen Georgebc1344d2017-04-28 10:22:03 -0400230 thrift_test_client.test_string("thing".to_owned()),
231 "thing".to_owned(),
232 )?;
233
234 info!("testBool");
235 verify_expected_result(thrift_test_client.test_bool(true), true)?;
236
237 info!("testBool");
238 verify_expected_result(thrift_test_client.test_bool(false), false)?;
239
240 info!("testByte");
241 verify_expected_result(thrift_test_client.test_byte(42), 42)?;
242
243 info!("testi32");
Allen George7ddbcc02020-11-08 09:51:19 -0500244 verify_expected_result(thrift_test_client.test_i32(1_159_348_374), 1_159_348_374)?;
Allen Georgebc1344d2017-04-28 10:22:03 -0400245
246 info!("testi64");
247 // try!(verify_expected_result(thrift_test_client.test_i64(-8651829879438294565),
248 // -8651829879438294565));
249 verify_expected_result(
250 thrift_test_client.test_i64(i64::min_value()),
251 i64::min_value(),
252 )?;
253
254 info!("testDouble");
255 verify_expected_result(
256 thrift_test_client.test_double(OrderedFloat::from(42.42)),
Allen George0e22c362017-01-30 07:15:00 -0500257 OrderedFloat::from(42.42),
258 )?;
Allen George8b96bfb2016-11-02 08:01:08 -0400259
Allen Georgebc1344d2017-04-28 10:22:03 -0400260 info!("testTypedef");
Allen George8b96bfb2016-11-02 08:01:08 -0400261 {
262 let u_snd: UserId = 2348;
263 let u_cmp: UserId = 2348;
Allen Georgebc1344d2017-04-28 10:22:03 -0400264 verify_expected_result(thrift_test_client.test_typedef(u_snd), u_cmp)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400265 }
266
Allen Georgebc1344d2017-04-28 10:22:03 -0400267 info!("testEnum");
Allen George8b96bfb2016-11-02 08:01:08 -0400268 {
Allen George2e90ef52021-03-01 14:47:04 -0500269 verify_expected_result(thrift_test_client.test_enum(Numberz::TWO), Numberz::TWO)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400270 }
271
Allen Georgebc1344d2017-04-28 10:22:03 -0400272 info!("testBinary");
Allen George8b96bfb2016-11-02 08:01:08 -0400273 {
274 let b_snd = vec![0x77, 0x30, 0x30, 0x74, 0x21, 0x20, 0x52, 0x75, 0x73, 0x74];
275 let b_cmp = vec![0x77, 0x30, 0x30, 0x74, 0x21, 0x20, 0x52, 0x75, 0x73, 0x74];
Allen Georgebc1344d2017-04-28 10:22:03 -0400276 verify_expected_result(thrift_test_client.test_binary(b_snd), b_cmp)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400277 }
278
Allen Georgebc1344d2017-04-28 10:22:03 -0400279 info!("testStruct");
Allen George8b96bfb2016-11-02 08:01:08 -0400280 {
281 let x_snd = Xtruct {
282 string_thing: Some("foo".to_owned()),
283 byte_thing: Some(12),
Allen George7ddbcc02020-11-08 09:51:19 -0500284 i32_thing: Some(219_129),
285 i64_thing: Some(12_938_492_818),
Allen George8b96bfb2016-11-02 08:01:08 -0400286 };
287 let x_cmp = Xtruct {
288 string_thing: Some("foo".to_owned()),
289 byte_thing: Some(12),
Allen George7ddbcc02020-11-08 09:51:19 -0500290 i32_thing: Some(219_129),
291 i64_thing: Some(12_938_492_818),
Allen George8b96bfb2016-11-02 08:01:08 -0400292 };
Allen Georgebc1344d2017-04-28 10:22:03 -0400293 verify_expected_result(thrift_test_client.test_struct(x_snd), x_cmp)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400294 }
295
296 // Xtruct again, with optional values
Allen George0e22c362017-01-30 07:15:00 -0500297 // FIXME: apparently the erlang thrift server does not like opt-in-req-out
298 // parameters that are undefined. Joy.
Allen George8b96bfb2016-11-02 08:01:08 -0400299 // {
Allen George0e22c362017-01-30 07:15:00 -0500300 // let x_snd = Xtruct { string_thing: Some("foo".to_owned()), byte_thing: None,
301 // i32_thing: None, i64_thing: Some(12938492818) };
302 // let x_cmp = Xtruct { string_thing: Some("foo".to_owned()), byte_thing:
303 // Some(0), i32_thing: Some(0), i64_thing: Some(12938492818) }; // the C++
304 // server is responding correctly
Allen Georgebc1344d2017-04-28 10:22:03 -0400305 // try!(verify_expected_result(thrift_test_client.test_struct(x_snd), x_cmp));
Allen George8b96bfb2016-11-02 08:01:08 -0400306 // }
307 //
308
Allen Georgebc1344d2017-04-28 10:22:03 -0400309 info!("testNest"); // (FIXME: try Xtruct2 with optional values)
Allen George8b96bfb2016-11-02 08:01:08 -0400310 {
311 let x_snd = Xtruct2 {
312 byte_thing: Some(32),
Allen George55c3e4c2021-03-01 23:19:52 -0500313 struct_thing: Some(Xtruct {
314 string_thing: Some("foo".to_owned()),
315 byte_thing: Some(1),
316 i32_thing: Some(324_382_098),
317 i64_thing: Some(12_938_492_818),
318 }),
Allen George7ddbcc02020-11-08 09:51:19 -0500319 i32_thing: Some(293_481_098),
Allen George8b96bfb2016-11-02 08:01:08 -0400320 };
321 let x_cmp = Xtruct2 {
322 byte_thing: Some(32),
Allen George55c3e4c2021-03-01 23:19:52 -0500323 struct_thing: Some(Xtruct {
324 string_thing: Some("foo".to_owned()),
325 byte_thing: Some(1),
326 i32_thing: Some(324_382_098),
327 i64_thing: Some(12_938_492_818),
328 }),
Allen George7ddbcc02020-11-08 09:51:19 -0500329 i32_thing: Some(293_481_098),
Allen George8b96bfb2016-11-02 08:01:08 -0400330 };
Allen Georgebc1344d2017-04-28 10:22:03 -0400331 verify_expected_result(thrift_test_client.test_nest(x_snd), x_cmp)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400332 }
333
Allen Georgebc1344d2017-04-28 10:22:03 -0400334 // do the multiplexed calls while making the main ThriftTest calls
335 if let Some(ref mut client) = second_service_client.as_mut() {
Allen Georgebc1344d2017-04-28 10:22:03 -0400336 info!("SecondService secondtestString");
337 {
338 verify_expected_result(
339 client.secondtest_string("test_string".to_owned()),
340 "testString(\"test_string\")".to_owned(),
341 )?;
342 }
343 }
344
345 info!("testList");
Allen George8b96bfb2016-11-02 08:01:08 -0400346 {
Tdxdxoz85d82bf2022-07-17 14:14:12 +0200347 let v_snd: Vec<i32> = vec![29384, 238, 32498];
Allen George8b96bfb2016-11-02 08:01:08 -0400348
Tdxdxoz85d82bf2022-07-17 14:14:12 +0200349 let v_cmp: Vec<i32> = vec![29384, 238, 32498];
Allen George8b96bfb2016-11-02 08:01:08 -0400350
Allen Georgebc1344d2017-04-28 10:22:03 -0400351 verify_expected_result(thrift_test_client.test_list(v_snd), v_cmp)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400352 }
353
Allen Georgebc1344d2017-04-28 10:22:03 -0400354 info!("testSet");
Allen George8b96bfb2016-11-02 08:01:08 -0400355 {
Tdxdxoz85d82bf2022-07-17 14:14:12 +0200356 let s_snd: BTreeSet<i32> = BTreeSet::from([293_481, 23, 3234]);
Allen George8b96bfb2016-11-02 08:01:08 -0400357
Tdxdxoz85d82bf2022-07-17 14:14:12 +0200358 let s_cmp: BTreeSet<i32> = BTreeSet::from([293_481, 23, 3234]);
Allen George8b96bfb2016-11-02 08:01:08 -0400359
Allen Georgebc1344d2017-04-28 10:22:03 -0400360 verify_expected_result(thrift_test_client.test_set(s_snd), s_cmp)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400361 }
362
Allen Georgebc1344d2017-04-28 10:22:03 -0400363 info!("testMap");
Allen George8b96bfb2016-11-02 08:01:08 -0400364 {
Tdxdxoz85d82bf2022-07-17 14:14:12 +0200365 let m_snd: BTreeMap<i32, i32> = BTreeMap::from([(2, 4), (4, 6), (8, 7)]);
Allen George8b96bfb2016-11-02 08:01:08 -0400366
Tdxdxoz85d82bf2022-07-17 14:14:12 +0200367 let m_cmp: BTreeMap<i32, i32> = BTreeMap::from([(2, 4), (4, 6), (8, 7)]);
Allen George8b96bfb2016-11-02 08:01:08 -0400368
Allen Georgebc1344d2017-04-28 10:22:03 -0400369 verify_expected_result(thrift_test_client.test_map(m_snd), m_cmp)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400370 }
371
Allen Georgebc1344d2017-04-28 10:22:03 -0400372 info!("testStringMap");
Allen George8b96bfb2016-11-02 08:01:08 -0400373 {
Tdxdxoz85d82bf2022-07-17 14:14:12 +0200374 let m_snd: BTreeMap<String, String> = BTreeMap::from([
375 ("2".to_owned(), "4_string".to_owned()),
376 ("4".to_owned(), "6_string".to_owned()),
377 ("8".to_owned(), "7_string".to_owned()),
378 ]);
Allen George8b96bfb2016-11-02 08:01:08 -0400379
Tdxdxoz85d82bf2022-07-17 14:14:12 +0200380 let m_rcv: BTreeMap<String, String> = BTreeMap::from([
381 ("2".to_owned(), "4_string".to_owned()),
382 ("4".to_owned(), "6_string".to_owned()),
383 ("8".to_owned(), "7_string".to_owned()),
384 ]);
Allen George8b96bfb2016-11-02 08:01:08 -0400385
Allen Georgebc1344d2017-04-28 10:22:03 -0400386 verify_expected_result(thrift_test_client.test_string_map(m_snd), m_rcv)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400387 }
388
389 // nested map
Allen George0e22c362017-01-30 07:15:00 -0500390 // expect : {-4 => {-4 => -4, -3 => -3, -2 => -2, -1 => -1, }, 4 => {1 => 1, 2
391 // => 2, 3 => 3, 4 => 4, }, }
Allen Georgebc1344d2017-04-28 10:22:03 -0400392 info!("testMapMap");
Allen George8b96bfb2016-11-02 08:01:08 -0400393 {
Tdxdxoz85d82bf2022-07-17 14:14:12 +0200394 let m_cmp_nested_0: BTreeMap<i32, i32> = (-4..0).map(|i| (i, i)).collect();
395 let m_cmp_nested_1: BTreeMap<i32, i32> = (1..5).map(|i| (i, i)).collect();
Allen George8b96bfb2016-11-02 08:01:08 -0400396
Tdxdxoz85d82bf2022-07-17 14:14:12 +0200397 let m_cmp: BTreeMap<i32, BTreeMap<i32, i32>> =
398 BTreeMap::from([(-4, m_cmp_nested_0), (4, m_cmp_nested_1)]);
Allen George8b96bfb2016-11-02 08:01:08 -0400399
Allen Georgebc1344d2017-04-28 10:22:03 -0400400 verify_expected_result(thrift_test_client.test_map_map(42), m_cmp)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400401 }
402
Allen Georgebc1344d2017-04-28 10:22:03 -0400403 info!("testMulti");
Allen George8b96bfb2016-11-02 08:01:08 -0400404 {
Tdxdxoz85d82bf2022-07-17 14:14:12 +0200405 let m_snd: BTreeMap<i16, String> =
406 BTreeMap::from([(1298, "fizz".to_owned()), (-148, "buzz".to_owned())]);
Allen George8b96bfb2016-11-02 08:01:08 -0400407
408 let s_cmp = Xtruct {
409 string_thing: Some("Hello2".to_owned()),
410 byte_thing: Some(1),
Allen George7ddbcc02020-11-08 09:51:19 -0500411 i32_thing: Some(-123_948),
412 i64_thing: Some(-19_234_123_981),
Allen George8b96bfb2016-11-02 08:01:08 -0400413 };
414
Allen George0e22c362017-01-30 07:15:00 -0500415 verify_expected_result(
Allen George2e90ef52021-03-01 14:47:04 -0500416 thrift_test_client.test_multi(1, -123_948, -19_234_123_981, m_snd, Numberz::EIGHT, 81),
Allen George0e22c362017-01-30 07:15:00 -0500417 s_cmp,
418 )?;
Allen George8b96bfb2016-11-02 08:01:08 -0400419 }
420
421 // Insanity
422 // returns:
423 // { 1 => { 2 => argument,
424 // 3 => argument,
425 // },
426 // 2 => { 6 => <empty Insanity struct>, },
427 // }
428 {
Tdxdxoz85d82bf2022-07-17 14:14:12 +0200429 let arg_map_usermap: BTreeMap<Numberz, i64> =
430 BTreeMap::from([(Numberz::ONE, 4289), (Numberz::EIGHT, 19)]);
Allen George8b96bfb2016-11-02 08:01:08 -0400431
Tdxdxoz85d82bf2022-07-17 14:14:12 +0200432 let arg_vec_xtructs: Vec<Xtruct> = vec![
433 Xtruct {
434 string_thing: Some("foo".to_owned()),
435 byte_thing: Some(8),
436 i32_thing: Some(29),
437 i64_thing: Some(92384),
438 },
439 Xtruct {
440 string_thing: Some("bar".to_owned()),
441 byte_thing: Some(28),
442 i32_thing: Some(2),
443 i64_thing: Some(-1281),
444 },
445 Xtruct {
446 string_thing: Some("baz".to_owned()),
447 byte_thing: Some(0),
448 i32_thing: Some(3_948_539),
449 i64_thing: Some(-12_938_492),
450 },
451 ];
Allen George8b96bfb2016-11-02 08:01:08 -0400452
Allen George8b96bfb2016-11-02 08:01:08 -0400453 let insanity = Insanity {
454 user_map: Some(arg_map_usermap),
455 xtructs: Some(arg_vec_xtructs),
456 };
Tdxdxoz85d82bf2022-07-17 14:14:12 +0200457 let s_cmp_nested_1: BTreeMap<Numberz, Insanity> = BTreeMap::from([
458 (Numberz::TWO, insanity.clone()),
459 (Numberz::THREE, insanity.clone()),
460 ]);
Allen George8b96bfb2016-11-02 08:01:08 -0400461
Allen George8b96bfb2016-11-02 08:01:08 -0400462 let empty_insanity = Insanity {
463 user_map: Some(BTreeMap::new()),
464 xtructs: Some(Vec::new()),
465 };
Tdxdxoz85d82bf2022-07-17 14:14:12 +0200466 let s_cmp_nested_2: BTreeMap<Numberz, Insanity> =
467 BTreeMap::from([(Numberz::SIX, empty_insanity)]);
Allen George8b96bfb2016-11-02 08:01:08 -0400468
Tdxdxoz85d82bf2022-07-17 14:14:12 +0200469 let s_cmp: BTreeMap<UserId, BTreeMap<Numberz, Insanity>> =
470 BTreeMap::from([(1, s_cmp_nested_1), (2, s_cmp_nested_2)]);
Allen George8b96bfb2016-11-02 08:01:08 -0400471
Allen George7ddbcc02020-11-08 09:51:19 -0500472 verify_expected_result(thrift_test_client.test_insanity(insanity), s_cmp)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400473 }
474
Allen Georgebc1344d2017-04-28 10:22:03 -0400475 info!("testException - remote throws Xception");
Allen George8b96bfb2016-11-02 08:01:08 -0400476 {
Allen Georgebc1344d2017-04-28 10:22:03 -0400477 let r = thrift_test_client.test_exception("Xception".to_owned());
Allen George8b96bfb2016-11-02 08:01:08 -0400478 let x = match r {
Allen George55c3e4c2021-03-01 23:19:52 -0500479 Err(thrift::Error::User(ref e)) => match e.downcast_ref::<Xception>() {
480 Some(x) => Ok(x),
481 None => Err(thrift::Error::User(
482 "did not get expected Xception struct".into(),
483 )),
484 },
Allen George8b96bfb2016-11-02 08:01:08 -0400485 _ => Err(thrift::Error::User("did not get exception".into())),
486 }?;
487
488 let x_cmp = Xception {
489 error_code: Some(1001),
490 message: Some("Xception".to_owned()),
491 };
492
493 verify_expected_result(Ok(x), &x_cmp)?;
494 }
495
Allen Georgebc1344d2017-04-28 10:22:03 -0400496 info!("testException - remote throws TApplicationException");
Allen George8b96bfb2016-11-02 08:01:08 -0400497 {
Allen Georgebc1344d2017-04-28 10:22:03 -0400498 let r = thrift_test_client.test_exception("TException".to_owned());
Allen George8b96bfb2016-11-02 08:01:08 -0400499 match r {
500 Err(thrift::Error::Application(ref e)) => {
Allen Georgebc1344d2017-04-28 10:22:03 -0400501 info!("received an {:?}", e);
Allen George8b96bfb2016-11-02 08:01:08 -0400502 Ok(())
503 }
504 _ => Err(thrift::Error::User("did not get exception".into())),
505 }?;
506 }
507
Allen Georgebc1344d2017-04-28 10:22:03 -0400508 info!("testException - remote succeeds");
Allen George8b96bfb2016-11-02 08:01:08 -0400509 {
Allen Georgebc1344d2017-04-28 10:22:03 -0400510 let r = thrift_test_client.test_exception("foo".to_owned());
Allen George8b96bfb2016-11-02 08:01:08 -0400511 match r {
512 Ok(_) => Ok(()),
513 _ => Err(thrift::Error::User("received an exception".into())),
514 }?;
515 }
516
Allen Georgebc1344d2017-04-28 10:22:03 -0400517 info!("testMultiException - remote throws Xception");
Allen George8b96bfb2016-11-02 08:01:08 -0400518 {
Allen Georgebc1344d2017-04-28 10:22:03 -0400519 let r =
520 thrift_test_client.test_multi_exception("Xception".to_owned(), "ignored".to_owned());
Allen George8b96bfb2016-11-02 08:01:08 -0400521 let x = match r {
Allen George55c3e4c2021-03-01 23:19:52 -0500522 Err(thrift::Error::User(ref e)) => match e.downcast_ref::<Xception>() {
523 Some(x) => Ok(x),
524 None => Err(thrift::Error::User(
525 "did not get expected Xception struct".into(),
526 )),
527 },
Allen George8b96bfb2016-11-02 08:01:08 -0400528 _ => Err(thrift::Error::User("did not get exception".into())),
529 }?;
530
531 let x_cmp = Xception {
532 error_code: Some(1001),
533 message: Some("This is an Xception".to_owned()),
534 };
535
536 verify_expected_result(Ok(x), &x_cmp)?;
537 }
538
Allen Georgebc1344d2017-04-28 10:22:03 -0400539 info!("testMultiException - remote throws Xception2");
Allen George8b96bfb2016-11-02 08:01:08 -0400540 {
Allen Georgebc1344d2017-04-28 10:22:03 -0400541 let r =
542 thrift_test_client.test_multi_exception("Xception2".to_owned(), "ignored".to_owned());
Allen George8b96bfb2016-11-02 08:01:08 -0400543 let x = match r {
Allen George55c3e4c2021-03-01 23:19:52 -0500544 Err(thrift::Error::User(ref e)) => match e.downcast_ref::<Xception2>() {
545 Some(x) => Ok(x),
546 None => Err(thrift::Error::User(
547 "did not get expected Xception struct".into(),
548 )),
549 },
Allen George8b96bfb2016-11-02 08:01:08 -0400550 _ => Err(thrift::Error::User("did not get exception".into())),
551 }?;
552
553 let x_cmp = Xception2 {
554 error_code: Some(2002),
Allen George55c3e4c2021-03-01 23:19:52 -0500555 struct_thing: Some(Xtruct {
556 string_thing: Some("This is an Xception2".to_owned()),
557 // since this is an OPT_IN_REQ_OUT field the sender sets a default
558 byte_thing: Some(0),
559 // since this is an OPT_IN_REQ_OUT field the sender sets a default
560 i32_thing: Some(0),
561 // since this is an OPT_IN_REQ_OUT field the sender sets a default
562 i64_thing: Some(0),
563 }),
Allen George8b96bfb2016-11-02 08:01:08 -0400564 };
565
566 verify_expected_result(Ok(x), &x_cmp)?;
567 }
568
Allen Georgebc1344d2017-04-28 10:22:03 -0400569 info!("testMultiException - remote succeeds");
Allen George8b96bfb2016-11-02 08:01:08 -0400570 {
Allen Georgebc1344d2017-04-28 10:22:03 -0400571 let r = thrift_test_client.test_multi_exception("haha".to_owned(), "RETURNED".to_owned());
Allen George8b96bfb2016-11-02 08:01:08 -0400572 let x = match r {
Allen George55c3e4c2021-03-01 23:19:52 -0500573 Err(e) => Err(thrift::Error::User(
574 format!("received an unexpected exception {:?}", e).into(),
575 )),
Allen George8b96bfb2016-11-02 08:01:08 -0400576 _ => r,
577 }?;
578
579 let x_cmp = Xtruct {
580 string_thing: Some("RETURNED".to_owned()),
Allen George0e22c362017-01-30 07:15:00 -0500581 // since this is an OPT_IN_REQ_OUT field the sender sets a default
582 byte_thing: Some(0),
583 // since this is an OPT_IN_REQ_OUT field the sender sets a default
584 i32_thing: Some(0),
585 // since this is an OPT_IN_REQ_OUT field the sender sets a default
586 i64_thing: Some(0),
Allen George8b96bfb2016-11-02 08:01:08 -0400587 };
588
589 verify_expected_result(Ok(x), x_cmp)?;
590 }
591
Allen Georgebc1344d2017-04-28 10:22:03 -0400592 info!("testOneWay - remote sleeps for 1 second");
Allen George8b96bfb2016-11-02 08:01:08 -0400593 {
Allen Georgebc1344d2017-04-28 10:22:03 -0400594 thrift_test_client.test_oneway(1)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400595 }
596
Allen George0e22c362017-01-30 07:15:00 -0500597 // final test to verify that the connection is still writable after the one-way
598 // call
Allen Georgebc1344d2017-04-28 10:22:03 -0400599 thrift_test_client.test_void()
Allen George8b96bfb2016-11-02 08:01:08 -0400600}
601
Allen George0e22c362017-01-30 07:15:00 -0500602fn verify_expected_result<T: Debug + PartialEq + Sized>(
603 actual: Result<T, thrift::Error>,
604 expected: T,
605) -> Result<(), thrift::Error> {
James E. King, III20e16bc2017-11-18 22:37:54 -0500606 info!("*** EXPECTED: Ok({:?})", expected);
607 info!("*** ACTUAL : {:?}", actual);
Allen George8b96bfb2016-11-02 08:01:08 -0400608 match actual {
609 Ok(v) => {
610 if v == expected {
James E. King, III20e16bc2017-11-18 22:37:54 -0500611 info!("*** OK ***");
Allen George8b96bfb2016-11-02 08:01:08 -0400612 Ok(())
613 } else {
James E. King, III20e16bc2017-11-18 22:37:54 -0500614 info!("*** FAILED ***");
Allen George55c3e4c2021-03-01 23:19:52 -0500615 Err(thrift::Error::User(
616 format!("expected {:?} but got {:?}", &expected, &v).into(),
617 ))
Allen George8b96bfb2016-11-02 08:01:08 -0400618 }
619 }
620 Err(e) => Err(e),
621 }
622}