blob: 801ccc4b504fe28001c769ab36bc8c32a01de5ac [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
Jiayu Liub6b6dc72022-10-08 14:28:44 +0800234 info!("testUuid");
235 verify_expected_result(
236 thrift_test_client.test_uuid(uuid::uuid!("00010203-0405-0607-0809-0a0b0c0d0e0f")),
237 uuid::uuid!("00010203-0405-0607-0809-0a0b0c0d0e0f"),
238 )?;
239
Allen Georgebc1344d2017-04-28 10:22:03 -0400240 info!("testBool");
241 verify_expected_result(thrift_test_client.test_bool(true), true)?;
242
243 info!("testBool");
244 verify_expected_result(thrift_test_client.test_bool(false), false)?;
245
246 info!("testByte");
247 verify_expected_result(thrift_test_client.test_byte(42), 42)?;
248
249 info!("testi32");
Allen George7ddbcc02020-11-08 09:51:19 -0500250 verify_expected_result(thrift_test_client.test_i32(1_159_348_374), 1_159_348_374)?;
Allen Georgebc1344d2017-04-28 10:22:03 -0400251
252 info!("testi64");
253 // try!(verify_expected_result(thrift_test_client.test_i64(-8651829879438294565),
254 // -8651829879438294565));
Cameron Martinda54fc82025-01-12 08:55:45 +0000255 verify_expected_result(thrift_test_client.test_i64(i64::MIN), i64::MIN)?;
Allen Georgebc1344d2017-04-28 10:22:03 -0400256
257 info!("testDouble");
258 verify_expected_result(
259 thrift_test_client.test_double(OrderedFloat::from(42.42)),
Allen George0e22c362017-01-30 07:15:00 -0500260 OrderedFloat::from(42.42),
261 )?;
Allen George8b96bfb2016-11-02 08:01:08 -0400262
Allen Georgebc1344d2017-04-28 10:22:03 -0400263 info!("testTypedef");
Allen George8b96bfb2016-11-02 08:01:08 -0400264 {
265 let u_snd: UserId = 2348;
266 let u_cmp: UserId = 2348;
Allen Georgebc1344d2017-04-28 10:22:03 -0400267 verify_expected_result(thrift_test_client.test_typedef(u_snd), u_cmp)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400268 }
269
Allen Georgebc1344d2017-04-28 10:22:03 -0400270 info!("testEnum");
Allen George8b96bfb2016-11-02 08:01:08 -0400271 {
Allen George2e90ef52021-03-01 14:47:04 -0500272 verify_expected_result(thrift_test_client.test_enum(Numberz::TWO), Numberz::TWO)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400273 }
274
Allen Georgebc1344d2017-04-28 10:22:03 -0400275 info!("testBinary");
Allen George8b96bfb2016-11-02 08:01:08 -0400276 {
277 let b_snd = vec![0x77, 0x30, 0x30, 0x74, 0x21, 0x20, 0x52, 0x75, 0x73, 0x74];
278 let b_cmp = vec![0x77, 0x30, 0x30, 0x74, 0x21, 0x20, 0x52, 0x75, 0x73, 0x74];
Allen Georgebc1344d2017-04-28 10:22:03 -0400279 verify_expected_result(thrift_test_client.test_binary(b_snd), b_cmp)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400280 }
281
Allen Georgebc1344d2017-04-28 10:22:03 -0400282 info!("testStruct");
Allen George8b96bfb2016-11-02 08:01:08 -0400283 {
284 let x_snd = Xtruct {
285 string_thing: Some("foo".to_owned()),
286 byte_thing: Some(12),
Allen George7ddbcc02020-11-08 09:51:19 -0500287 i32_thing: Some(219_129),
288 i64_thing: Some(12_938_492_818),
Allen George8b96bfb2016-11-02 08:01:08 -0400289 };
290 let x_cmp = Xtruct {
291 string_thing: Some("foo".to_owned()),
292 byte_thing: Some(12),
Allen George7ddbcc02020-11-08 09:51:19 -0500293 i32_thing: Some(219_129),
294 i64_thing: Some(12_938_492_818),
Allen George8b96bfb2016-11-02 08:01:08 -0400295 };
Allen Georgebc1344d2017-04-28 10:22:03 -0400296 verify_expected_result(thrift_test_client.test_struct(x_snd), x_cmp)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400297 }
298
299 // Xtruct again, with optional values
Allen George0e22c362017-01-30 07:15:00 -0500300 // FIXME: apparently the erlang thrift server does not like opt-in-req-out
301 // parameters that are undefined. Joy.
Allen George8b96bfb2016-11-02 08:01:08 -0400302 // {
Allen George0e22c362017-01-30 07:15:00 -0500303 // let x_snd = Xtruct { string_thing: Some("foo".to_owned()), byte_thing: None,
304 // i32_thing: None, i64_thing: Some(12938492818) };
305 // let x_cmp = Xtruct { string_thing: Some("foo".to_owned()), byte_thing:
306 // Some(0), i32_thing: Some(0), i64_thing: Some(12938492818) }; // the C++
307 // server is responding correctly
Allen Georgebc1344d2017-04-28 10:22:03 -0400308 // try!(verify_expected_result(thrift_test_client.test_struct(x_snd), x_cmp));
Allen George8b96bfb2016-11-02 08:01:08 -0400309 // }
310 //
311
Allen Georgebc1344d2017-04-28 10:22:03 -0400312 info!("testNest"); // (FIXME: try Xtruct2 with optional values)
Allen George8b96bfb2016-11-02 08:01:08 -0400313 {
314 let x_snd = Xtruct2 {
315 byte_thing: Some(32),
Allen George55c3e4c2021-03-01 23:19:52 -0500316 struct_thing: Some(Xtruct {
317 string_thing: Some("foo".to_owned()),
318 byte_thing: Some(1),
319 i32_thing: Some(324_382_098),
320 i64_thing: Some(12_938_492_818),
321 }),
Allen George7ddbcc02020-11-08 09:51:19 -0500322 i32_thing: Some(293_481_098),
Allen George8b96bfb2016-11-02 08:01:08 -0400323 };
324 let x_cmp = Xtruct2 {
325 byte_thing: Some(32),
Allen George55c3e4c2021-03-01 23:19:52 -0500326 struct_thing: Some(Xtruct {
327 string_thing: Some("foo".to_owned()),
328 byte_thing: Some(1),
329 i32_thing: Some(324_382_098),
330 i64_thing: Some(12_938_492_818),
331 }),
Allen George7ddbcc02020-11-08 09:51:19 -0500332 i32_thing: Some(293_481_098),
Allen George8b96bfb2016-11-02 08:01:08 -0400333 };
Allen Georgebc1344d2017-04-28 10:22:03 -0400334 verify_expected_result(thrift_test_client.test_nest(x_snd), x_cmp)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400335 }
336
Allen Georgebc1344d2017-04-28 10:22:03 -0400337 // do the multiplexed calls while making the main ThriftTest calls
338 if let Some(ref mut client) = second_service_client.as_mut() {
Allen Georgebc1344d2017-04-28 10:22:03 -0400339 info!("SecondService secondtestString");
340 {
341 verify_expected_result(
342 client.secondtest_string("test_string".to_owned()),
343 "testString(\"test_string\")".to_owned(),
344 )?;
345 }
346 }
347
348 info!("testList");
Allen George8b96bfb2016-11-02 08:01:08 -0400349 {
Tdxdxoz85d82bf2022-07-17 14:14:12 +0200350 let v_snd: Vec<i32> = vec![29384, 238, 32498];
Allen George8b96bfb2016-11-02 08:01:08 -0400351
Tdxdxoz85d82bf2022-07-17 14:14:12 +0200352 let v_cmp: Vec<i32> = vec![29384, 238, 32498];
Allen George8b96bfb2016-11-02 08:01:08 -0400353
Allen Georgebc1344d2017-04-28 10:22:03 -0400354 verify_expected_result(thrift_test_client.test_list(v_snd), v_cmp)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400355 }
356
Allen Georgebc1344d2017-04-28 10:22:03 -0400357 info!("testSet");
Allen George8b96bfb2016-11-02 08:01:08 -0400358 {
Tdxdxoz85d82bf2022-07-17 14:14:12 +0200359 let s_snd: BTreeSet<i32> = BTreeSet::from([293_481, 23, 3234]);
Allen George8b96bfb2016-11-02 08:01:08 -0400360
Tdxdxoz85d82bf2022-07-17 14:14:12 +0200361 let s_cmp: BTreeSet<i32> = BTreeSet::from([293_481, 23, 3234]);
Allen George8b96bfb2016-11-02 08:01:08 -0400362
Allen Georgebc1344d2017-04-28 10:22:03 -0400363 verify_expected_result(thrift_test_client.test_set(s_snd), s_cmp)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400364 }
365
Allen Georgebc1344d2017-04-28 10:22:03 -0400366 info!("testMap");
Allen George8b96bfb2016-11-02 08:01:08 -0400367 {
Tdxdxoz85d82bf2022-07-17 14:14:12 +0200368 let m_snd: BTreeMap<i32, i32> = BTreeMap::from([(2, 4), (4, 6), (8, 7)]);
Allen George8b96bfb2016-11-02 08:01:08 -0400369
Tdxdxoz85d82bf2022-07-17 14:14:12 +0200370 let m_cmp: BTreeMap<i32, i32> = BTreeMap::from([(2, 4), (4, 6), (8, 7)]);
Allen George8b96bfb2016-11-02 08:01:08 -0400371
Allen Georgebc1344d2017-04-28 10:22:03 -0400372 verify_expected_result(thrift_test_client.test_map(m_snd), m_cmp)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400373 }
374
Allen Georgebc1344d2017-04-28 10:22:03 -0400375 info!("testStringMap");
Allen George8b96bfb2016-11-02 08:01:08 -0400376 {
Tdxdxoz85d82bf2022-07-17 14:14:12 +0200377 let m_snd: BTreeMap<String, String> = BTreeMap::from([
378 ("2".to_owned(), "4_string".to_owned()),
379 ("4".to_owned(), "6_string".to_owned()),
380 ("8".to_owned(), "7_string".to_owned()),
381 ]);
Allen George8b96bfb2016-11-02 08:01:08 -0400382
Tdxdxoz85d82bf2022-07-17 14:14:12 +0200383 let m_rcv: BTreeMap<String, String> = BTreeMap::from([
384 ("2".to_owned(), "4_string".to_owned()),
385 ("4".to_owned(), "6_string".to_owned()),
386 ("8".to_owned(), "7_string".to_owned()),
387 ]);
Allen George8b96bfb2016-11-02 08:01:08 -0400388
Allen Georgebc1344d2017-04-28 10:22:03 -0400389 verify_expected_result(thrift_test_client.test_string_map(m_snd), m_rcv)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400390 }
391
392 // nested map
Allen George0e22c362017-01-30 07:15:00 -0500393 // expect : {-4 => {-4 => -4, -3 => -3, -2 => -2, -1 => -1, }, 4 => {1 => 1, 2
394 // => 2, 3 => 3, 4 => 4, }, }
Allen Georgebc1344d2017-04-28 10:22:03 -0400395 info!("testMapMap");
Allen George8b96bfb2016-11-02 08:01:08 -0400396 {
Tdxdxoz85d82bf2022-07-17 14:14:12 +0200397 let m_cmp_nested_0: BTreeMap<i32, i32> = (-4..0).map(|i| (i, i)).collect();
398 let m_cmp_nested_1: BTreeMap<i32, i32> = (1..5).map(|i| (i, i)).collect();
Allen George8b96bfb2016-11-02 08:01:08 -0400399
Tdxdxoz85d82bf2022-07-17 14:14:12 +0200400 let m_cmp: BTreeMap<i32, BTreeMap<i32, i32>> =
401 BTreeMap::from([(-4, m_cmp_nested_0), (4, m_cmp_nested_1)]);
Allen George8b96bfb2016-11-02 08:01:08 -0400402
Allen Georgebc1344d2017-04-28 10:22:03 -0400403 verify_expected_result(thrift_test_client.test_map_map(42), m_cmp)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400404 }
405
Allen Georgebc1344d2017-04-28 10:22:03 -0400406 info!("testMulti");
Allen George8b96bfb2016-11-02 08:01:08 -0400407 {
Tdxdxoz85d82bf2022-07-17 14:14:12 +0200408 let m_snd: BTreeMap<i16, String> =
409 BTreeMap::from([(1298, "fizz".to_owned()), (-148, "buzz".to_owned())]);
Allen George8b96bfb2016-11-02 08:01:08 -0400410
411 let s_cmp = Xtruct {
412 string_thing: Some("Hello2".to_owned()),
413 byte_thing: Some(1),
Allen George7ddbcc02020-11-08 09:51:19 -0500414 i32_thing: Some(-123_948),
415 i64_thing: Some(-19_234_123_981),
Allen George8b96bfb2016-11-02 08:01:08 -0400416 };
417
Allen George0e22c362017-01-30 07:15:00 -0500418 verify_expected_result(
Allen George2e90ef52021-03-01 14:47:04 -0500419 thrift_test_client.test_multi(1, -123_948, -19_234_123_981, m_snd, Numberz::EIGHT, 81),
Allen George0e22c362017-01-30 07:15:00 -0500420 s_cmp,
421 )?;
Allen George8b96bfb2016-11-02 08:01:08 -0400422 }
423
424 // Insanity
425 // returns:
426 // { 1 => { 2 => argument,
427 // 3 => argument,
428 // },
429 // 2 => { 6 => <empty Insanity struct>, },
430 // }
431 {
Tdxdxoz85d82bf2022-07-17 14:14:12 +0200432 let arg_map_usermap: BTreeMap<Numberz, i64> =
433 BTreeMap::from([(Numberz::ONE, 4289), (Numberz::EIGHT, 19)]);
Allen George8b96bfb2016-11-02 08:01:08 -0400434
Tdxdxoz85d82bf2022-07-17 14:14:12 +0200435 let arg_vec_xtructs: Vec<Xtruct> = vec![
436 Xtruct {
437 string_thing: Some("foo".to_owned()),
438 byte_thing: Some(8),
439 i32_thing: Some(29),
440 i64_thing: Some(92384),
441 },
442 Xtruct {
443 string_thing: Some("bar".to_owned()),
444 byte_thing: Some(28),
445 i32_thing: Some(2),
446 i64_thing: Some(-1281),
447 },
448 Xtruct {
449 string_thing: Some("baz".to_owned()),
450 byte_thing: Some(0),
451 i32_thing: Some(3_948_539),
452 i64_thing: Some(-12_938_492),
453 },
454 ];
Allen George8b96bfb2016-11-02 08:01:08 -0400455
Allen George8b96bfb2016-11-02 08:01:08 -0400456 let insanity = Insanity {
457 user_map: Some(arg_map_usermap),
458 xtructs: Some(arg_vec_xtructs),
459 };
Tdxdxoz85d82bf2022-07-17 14:14:12 +0200460 let s_cmp_nested_1: BTreeMap<Numberz, Insanity> = BTreeMap::from([
461 (Numberz::TWO, insanity.clone()),
462 (Numberz::THREE, insanity.clone()),
463 ]);
Allen George8b96bfb2016-11-02 08:01:08 -0400464
Allen George8b96bfb2016-11-02 08:01:08 -0400465 let empty_insanity = Insanity {
466 user_map: Some(BTreeMap::new()),
467 xtructs: Some(Vec::new()),
468 };
Tdxdxoz85d82bf2022-07-17 14:14:12 +0200469 let s_cmp_nested_2: BTreeMap<Numberz, Insanity> =
470 BTreeMap::from([(Numberz::SIX, empty_insanity)]);
Allen George8b96bfb2016-11-02 08:01:08 -0400471
Tdxdxoz85d82bf2022-07-17 14:14:12 +0200472 let s_cmp: BTreeMap<UserId, BTreeMap<Numberz, Insanity>> =
473 BTreeMap::from([(1, s_cmp_nested_1), (2, s_cmp_nested_2)]);
Allen George8b96bfb2016-11-02 08:01:08 -0400474
Allen George7ddbcc02020-11-08 09:51:19 -0500475 verify_expected_result(thrift_test_client.test_insanity(insanity), s_cmp)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400476 }
477
Allen Georgebc1344d2017-04-28 10:22:03 -0400478 info!("testException - remote throws Xception");
Allen George8b96bfb2016-11-02 08:01:08 -0400479 {
Allen Georgebc1344d2017-04-28 10:22:03 -0400480 let r = thrift_test_client.test_exception("Xception".to_owned());
Allen George8b96bfb2016-11-02 08:01:08 -0400481 let x = match r {
Allen George55c3e4c2021-03-01 23:19:52 -0500482 Err(thrift::Error::User(ref e)) => match e.downcast_ref::<Xception>() {
483 Some(x) => Ok(x),
484 None => Err(thrift::Error::User(
485 "did not get expected Xception struct".into(),
486 )),
487 },
Allen George8b96bfb2016-11-02 08:01:08 -0400488 _ => Err(thrift::Error::User("did not get exception".into())),
489 }?;
490
491 let x_cmp = Xception {
492 error_code: Some(1001),
493 message: Some("Xception".to_owned()),
494 };
495
496 verify_expected_result(Ok(x), &x_cmp)?;
497 }
498
Allen Georgebc1344d2017-04-28 10:22:03 -0400499 info!("testException - remote throws TApplicationException");
Allen George8b96bfb2016-11-02 08:01:08 -0400500 {
Allen Georgebc1344d2017-04-28 10:22:03 -0400501 let r = thrift_test_client.test_exception("TException".to_owned());
Allen George8b96bfb2016-11-02 08:01:08 -0400502 match r {
503 Err(thrift::Error::Application(ref e)) => {
Allen Georgebc1344d2017-04-28 10:22:03 -0400504 info!("received an {:?}", e);
Allen George8b96bfb2016-11-02 08:01:08 -0400505 Ok(())
506 }
507 _ => Err(thrift::Error::User("did not get exception".into())),
508 }?;
509 }
510
Allen Georgebc1344d2017-04-28 10:22:03 -0400511 info!("testException - remote succeeds");
Allen George8b96bfb2016-11-02 08:01:08 -0400512 {
Allen Georgebc1344d2017-04-28 10:22:03 -0400513 let r = thrift_test_client.test_exception("foo".to_owned());
Allen George8b96bfb2016-11-02 08:01:08 -0400514 match r {
515 Ok(_) => Ok(()),
516 _ => Err(thrift::Error::User("received an exception".into())),
517 }?;
518 }
519
Allen Georgebc1344d2017-04-28 10:22:03 -0400520 info!("testMultiException - remote throws Xception");
Allen George8b96bfb2016-11-02 08:01:08 -0400521 {
Allen Georgebc1344d2017-04-28 10:22:03 -0400522 let r =
523 thrift_test_client.test_multi_exception("Xception".to_owned(), "ignored".to_owned());
Allen George8b96bfb2016-11-02 08:01:08 -0400524 let x = match r {
Allen George55c3e4c2021-03-01 23:19:52 -0500525 Err(thrift::Error::User(ref e)) => match e.downcast_ref::<Xception>() {
526 Some(x) => Ok(x),
527 None => Err(thrift::Error::User(
528 "did not get expected Xception struct".into(),
529 )),
530 },
Allen George8b96bfb2016-11-02 08:01:08 -0400531 _ => Err(thrift::Error::User("did not get exception".into())),
532 }?;
533
534 let x_cmp = Xception {
535 error_code: Some(1001),
536 message: Some("This is an Xception".to_owned()),
537 };
538
539 verify_expected_result(Ok(x), &x_cmp)?;
540 }
541
Allen Georgebc1344d2017-04-28 10:22:03 -0400542 info!("testMultiException - remote throws Xception2");
Allen George8b96bfb2016-11-02 08:01:08 -0400543 {
Allen Georgebc1344d2017-04-28 10:22:03 -0400544 let r =
545 thrift_test_client.test_multi_exception("Xception2".to_owned(), "ignored".to_owned());
Allen George8b96bfb2016-11-02 08:01:08 -0400546 let x = match r {
Allen George55c3e4c2021-03-01 23:19:52 -0500547 Err(thrift::Error::User(ref e)) => match e.downcast_ref::<Xception2>() {
548 Some(x) => Ok(x),
549 None => Err(thrift::Error::User(
550 "did not get expected Xception struct".into(),
551 )),
552 },
Allen George8b96bfb2016-11-02 08:01:08 -0400553 _ => Err(thrift::Error::User("did not get exception".into())),
554 }?;
555
556 let x_cmp = Xception2 {
557 error_code: Some(2002),
Allen George55c3e4c2021-03-01 23:19:52 -0500558 struct_thing: Some(Xtruct {
559 string_thing: Some("This is an Xception2".to_owned()),
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),
566 }),
Allen George8b96bfb2016-11-02 08:01:08 -0400567 };
568
569 verify_expected_result(Ok(x), &x_cmp)?;
570 }
571
Allen Georgebc1344d2017-04-28 10:22:03 -0400572 info!("testMultiException - remote succeeds");
Allen George8b96bfb2016-11-02 08:01:08 -0400573 {
Allen Georgebc1344d2017-04-28 10:22:03 -0400574 let r = thrift_test_client.test_multi_exception("haha".to_owned(), "RETURNED".to_owned());
Allen George8b96bfb2016-11-02 08:01:08 -0400575 let x = match r {
Allen George55c3e4c2021-03-01 23:19:52 -0500576 Err(e) => Err(thrift::Error::User(
577 format!("received an unexpected exception {:?}", e).into(),
578 )),
Allen George8b96bfb2016-11-02 08:01:08 -0400579 _ => r,
580 }?;
581
582 let x_cmp = Xtruct {
583 string_thing: Some("RETURNED".to_owned()),
Allen George0e22c362017-01-30 07:15:00 -0500584 // since this is an OPT_IN_REQ_OUT field the sender sets a default
585 byte_thing: Some(0),
586 // since this is an OPT_IN_REQ_OUT field the sender sets a default
587 i32_thing: Some(0),
588 // since this is an OPT_IN_REQ_OUT field the sender sets a default
589 i64_thing: Some(0),
Allen George8b96bfb2016-11-02 08:01:08 -0400590 };
591
592 verify_expected_result(Ok(x), x_cmp)?;
593 }
594
Allen Georgebc1344d2017-04-28 10:22:03 -0400595 info!("testOneWay - remote sleeps for 1 second");
Allen George8b96bfb2016-11-02 08:01:08 -0400596 {
Allen Georgebc1344d2017-04-28 10:22:03 -0400597 thrift_test_client.test_oneway(1)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400598 }
599
Allen George0e22c362017-01-30 07:15:00 -0500600 // final test to verify that the connection is still writable after the one-way
601 // call
Allen Georgebc1344d2017-04-28 10:22:03 -0400602 thrift_test_client.test_void()
Allen George8b96bfb2016-11-02 08:01:08 -0400603}
604
Allen George0e22c362017-01-30 07:15:00 -0500605fn verify_expected_result<T: Debug + PartialEq + Sized>(
606 actual: Result<T, thrift::Error>,
607 expected: T,
608) -> Result<(), thrift::Error> {
James E. King, III20e16bc2017-11-18 22:37:54 -0500609 info!("*** EXPECTED: Ok({:?})", expected);
610 info!("*** ACTUAL : {:?}", actual);
Allen George8b96bfb2016-11-02 08:01:08 -0400611 match actual {
612 Ok(v) => {
613 if v == expected {
James E. King, III20e16bc2017-11-18 22:37:54 -0500614 info!("*** OK ***");
Allen George8b96bfb2016-11-02 08:01:08 -0400615 Ok(())
616 } else {
James E. King, III20e16bc2017-11-18 22:37:54 -0500617 info!("*** FAILED ***");
Allen George55c3e4c2021-03-01 23:19:52 -0500618 Err(thrift::Error::User(
619 format!("expected {:?} but got {:?}", &expected, &v).into(),
620 ))
Allen George8b96bfb2016-11-02 08:01:08 -0400621 }
622 }
623 Err(e) => Err(e),
624 }
625}