blob: fd3a18550b9c057bdf6adecc6cbbe03e3605671c [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));
255 verify_expected_result(
256 thrift_test_client.test_i64(i64::min_value()),
257 i64::min_value(),
258 )?;
259
260 info!("testDouble");
261 verify_expected_result(
262 thrift_test_client.test_double(OrderedFloat::from(42.42)),
Allen George0e22c362017-01-30 07:15:00 -0500263 OrderedFloat::from(42.42),
264 )?;
Allen George8b96bfb2016-11-02 08:01:08 -0400265
Allen Georgebc1344d2017-04-28 10:22:03 -0400266 info!("testTypedef");
Allen George8b96bfb2016-11-02 08:01:08 -0400267 {
268 let u_snd: UserId = 2348;
269 let u_cmp: UserId = 2348;
Allen Georgebc1344d2017-04-28 10:22:03 -0400270 verify_expected_result(thrift_test_client.test_typedef(u_snd), u_cmp)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400271 }
272
Allen Georgebc1344d2017-04-28 10:22:03 -0400273 info!("testEnum");
Allen George8b96bfb2016-11-02 08:01:08 -0400274 {
Allen George2e90ef52021-03-01 14:47:04 -0500275 verify_expected_result(thrift_test_client.test_enum(Numberz::TWO), Numberz::TWO)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400276 }
277
Allen Georgebc1344d2017-04-28 10:22:03 -0400278 info!("testBinary");
Allen George8b96bfb2016-11-02 08:01:08 -0400279 {
280 let b_snd = vec![0x77, 0x30, 0x30, 0x74, 0x21, 0x20, 0x52, 0x75, 0x73, 0x74];
281 let b_cmp = vec![0x77, 0x30, 0x30, 0x74, 0x21, 0x20, 0x52, 0x75, 0x73, 0x74];
Allen Georgebc1344d2017-04-28 10:22:03 -0400282 verify_expected_result(thrift_test_client.test_binary(b_snd), b_cmp)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400283 }
284
Allen Georgebc1344d2017-04-28 10:22:03 -0400285 info!("testStruct");
Allen George8b96bfb2016-11-02 08:01:08 -0400286 {
287 let x_snd = 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 };
293 let x_cmp = Xtruct {
294 string_thing: Some("foo".to_owned()),
295 byte_thing: Some(12),
Allen George7ddbcc02020-11-08 09:51:19 -0500296 i32_thing: Some(219_129),
297 i64_thing: Some(12_938_492_818),
Allen George8b96bfb2016-11-02 08:01:08 -0400298 };
Allen Georgebc1344d2017-04-28 10:22:03 -0400299 verify_expected_result(thrift_test_client.test_struct(x_snd), x_cmp)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400300 }
301
302 // Xtruct again, with optional values
Allen George0e22c362017-01-30 07:15:00 -0500303 // FIXME: apparently the erlang thrift server does not like opt-in-req-out
304 // parameters that are undefined. Joy.
Allen George8b96bfb2016-11-02 08:01:08 -0400305 // {
Allen George0e22c362017-01-30 07:15:00 -0500306 // let x_snd = Xtruct { string_thing: Some("foo".to_owned()), byte_thing: None,
307 // i32_thing: None, i64_thing: Some(12938492818) };
308 // let x_cmp = Xtruct { string_thing: Some("foo".to_owned()), byte_thing:
309 // Some(0), i32_thing: Some(0), i64_thing: Some(12938492818) }; // the C++
310 // server is responding correctly
Allen Georgebc1344d2017-04-28 10:22:03 -0400311 // try!(verify_expected_result(thrift_test_client.test_struct(x_snd), x_cmp));
Allen George8b96bfb2016-11-02 08:01:08 -0400312 // }
313 //
314
Allen Georgebc1344d2017-04-28 10:22:03 -0400315 info!("testNest"); // (FIXME: try Xtruct2 with optional values)
Allen George8b96bfb2016-11-02 08:01:08 -0400316 {
317 let x_snd = Xtruct2 {
318 byte_thing: Some(32),
Allen George55c3e4c2021-03-01 23:19:52 -0500319 struct_thing: Some(Xtruct {
320 string_thing: Some("foo".to_owned()),
321 byte_thing: Some(1),
322 i32_thing: Some(324_382_098),
323 i64_thing: Some(12_938_492_818),
324 }),
Allen George7ddbcc02020-11-08 09:51:19 -0500325 i32_thing: Some(293_481_098),
Allen George8b96bfb2016-11-02 08:01:08 -0400326 };
327 let x_cmp = Xtruct2 {
328 byte_thing: Some(32),
Allen George55c3e4c2021-03-01 23:19:52 -0500329 struct_thing: Some(Xtruct {
330 string_thing: Some("foo".to_owned()),
331 byte_thing: Some(1),
332 i32_thing: Some(324_382_098),
333 i64_thing: Some(12_938_492_818),
334 }),
Allen George7ddbcc02020-11-08 09:51:19 -0500335 i32_thing: Some(293_481_098),
Allen George8b96bfb2016-11-02 08:01:08 -0400336 };
Allen Georgebc1344d2017-04-28 10:22:03 -0400337 verify_expected_result(thrift_test_client.test_nest(x_snd), x_cmp)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400338 }
339
Allen Georgebc1344d2017-04-28 10:22:03 -0400340 // do the multiplexed calls while making the main ThriftTest calls
341 if let Some(ref mut client) = second_service_client.as_mut() {
Allen Georgebc1344d2017-04-28 10:22:03 -0400342 info!("SecondService secondtestString");
343 {
344 verify_expected_result(
345 client.secondtest_string("test_string".to_owned()),
346 "testString(\"test_string\")".to_owned(),
347 )?;
348 }
349 }
350
351 info!("testList");
Allen George8b96bfb2016-11-02 08:01:08 -0400352 {
Tdxdxoz85d82bf2022-07-17 14:14:12 +0200353 let v_snd: Vec<i32> = vec![29384, 238, 32498];
Allen George8b96bfb2016-11-02 08:01:08 -0400354
Tdxdxoz85d82bf2022-07-17 14:14:12 +0200355 let v_cmp: Vec<i32> = vec![29384, 238, 32498];
Allen George8b96bfb2016-11-02 08:01:08 -0400356
Allen Georgebc1344d2017-04-28 10:22:03 -0400357 verify_expected_result(thrift_test_client.test_list(v_snd), v_cmp)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400358 }
359
Allen Georgebc1344d2017-04-28 10:22:03 -0400360 info!("testSet");
Allen George8b96bfb2016-11-02 08:01:08 -0400361 {
Tdxdxoz85d82bf2022-07-17 14:14:12 +0200362 let s_snd: BTreeSet<i32> = BTreeSet::from([293_481, 23, 3234]);
Allen George8b96bfb2016-11-02 08:01:08 -0400363
Tdxdxoz85d82bf2022-07-17 14:14:12 +0200364 let s_cmp: BTreeSet<i32> = BTreeSet::from([293_481, 23, 3234]);
Allen George8b96bfb2016-11-02 08:01:08 -0400365
Allen Georgebc1344d2017-04-28 10:22:03 -0400366 verify_expected_result(thrift_test_client.test_set(s_snd), s_cmp)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400367 }
368
Allen Georgebc1344d2017-04-28 10:22:03 -0400369 info!("testMap");
Allen George8b96bfb2016-11-02 08:01:08 -0400370 {
Tdxdxoz85d82bf2022-07-17 14:14:12 +0200371 let m_snd: BTreeMap<i32, i32> = BTreeMap::from([(2, 4), (4, 6), (8, 7)]);
Allen George8b96bfb2016-11-02 08:01:08 -0400372
Tdxdxoz85d82bf2022-07-17 14:14:12 +0200373 let m_cmp: BTreeMap<i32, i32> = BTreeMap::from([(2, 4), (4, 6), (8, 7)]);
Allen George8b96bfb2016-11-02 08:01:08 -0400374
Allen Georgebc1344d2017-04-28 10:22:03 -0400375 verify_expected_result(thrift_test_client.test_map(m_snd), m_cmp)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400376 }
377
Allen Georgebc1344d2017-04-28 10:22:03 -0400378 info!("testStringMap");
Allen George8b96bfb2016-11-02 08:01:08 -0400379 {
Tdxdxoz85d82bf2022-07-17 14:14:12 +0200380 let m_snd: 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
Tdxdxoz85d82bf2022-07-17 14:14:12 +0200386 let m_rcv: BTreeMap<String, String> = BTreeMap::from([
387 ("2".to_owned(), "4_string".to_owned()),
388 ("4".to_owned(), "6_string".to_owned()),
389 ("8".to_owned(), "7_string".to_owned()),
390 ]);
Allen George8b96bfb2016-11-02 08:01:08 -0400391
Allen Georgebc1344d2017-04-28 10:22:03 -0400392 verify_expected_result(thrift_test_client.test_string_map(m_snd), m_rcv)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400393 }
394
395 // nested map
Allen George0e22c362017-01-30 07:15:00 -0500396 // expect : {-4 => {-4 => -4, -3 => -3, -2 => -2, -1 => -1, }, 4 => {1 => 1, 2
397 // => 2, 3 => 3, 4 => 4, }, }
Allen Georgebc1344d2017-04-28 10:22:03 -0400398 info!("testMapMap");
Allen George8b96bfb2016-11-02 08:01:08 -0400399 {
Tdxdxoz85d82bf2022-07-17 14:14:12 +0200400 let m_cmp_nested_0: BTreeMap<i32, i32> = (-4..0).map(|i| (i, i)).collect();
401 let m_cmp_nested_1: BTreeMap<i32, i32> = (1..5).map(|i| (i, i)).collect();
Allen George8b96bfb2016-11-02 08:01:08 -0400402
Tdxdxoz85d82bf2022-07-17 14:14:12 +0200403 let m_cmp: BTreeMap<i32, BTreeMap<i32, i32>> =
404 BTreeMap::from([(-4, m_cmp_nested_0), (4, m_cmp_nested_1)]);
Allen George8b96bfb2016-11-02 08:01:08 -0400405
Allen Georgebc1344d2017-04-28 10:22:03 -0400406 verify_expected_result(thrift_test_client.test_map_map(42), m_cmp)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400407 }
408
Allen Georgebc1344d2017-04-28 10:22:03 -0400409 info!("testMulti");
Allen George8b96bfb2016-11-02 08:01:08 -0400410 {
Tdxdxoz85d82bf2022-07-17 14:14:12 +0200411 let m_snd: BTreeMap<i16, String> =
412 BTreeMap::from([(1298, "fizz".to_owned()), (-148, "buzz".to_owned())]);
Allen George8b96bfb2016-11-02 08:01:08 -0400413
414 let s_cmp = Xtruct {
415 string_thing: Some("Hello2".to_owned()),
416 byte_thing: Some(1),
Allen George7ddbcc02020-11-08 09:51:19 -0500417 i32_thing: Some(-123_948),
418 i64_thing: Some(-19_234_123_981),
Allen George8b96bfb2016-11-02 08:01:08 -0400419 };
420
Allen George0e22c362017-01-30 07:15:00 -0500421 verify_expected_result(
Allen George2e90ef52021-03-01 14:47:04 -0500422 thrift_test_client.test_multi(1, -123_948, -19_234_123_981, m_snd, Numberz::EIGHT, 81),
Allen George0e22c362017-01-30 07:15:00 -0500423 s_cmp,
424 )?;
Allen George8b96bfb2016-11-02 08:01:08 -0400425 }
426
427 // Insanity
428 // returns:
429 // { 1 => { 2 => argument,
430 // 3 => argument,
431 // },
432 // 2 => { 6 => <empty Insanity struct>, },
433 // }
434 {
Tdxdxoz85d82bf2022-07-17 14:14:12 +0200435 let arg_map_usermap: BTreeMap<Numberz, i64> =
436 BTreeMap::from([(Numberz::ONE, 4289), (Numberz::EIGHT, 19)]);
Allen George8b96bfb2016-11-02 08:01:08 -0400437
Tdxdxoz85d82bf2022-07-17 14:14:12 +0200438 let arg_vec_xtructs: Vec<Xtruct> = vec![
439 Xtruct {
440 string_thing: Some("foo".to_owned()),
441 byte_thing: Some(8),
442 i32_thing: Some(29),
443 i64_thing: Some(92384),
444 },
445 Xtruct {
446 string_thing: Some("bar".to_owned()),
447 byte_thing: Some(28),
448 i32_thing: Some(2),
449 i64_thing: Some(-1281),
450 },
451 Xtruct {
452 string_thing: Some("baz".to_owned()),
453 byte_thing: Some(0),
454 i32_thing: Some(3_948_539),
455 i64_thing: Some(-12_938_492),
456 },
457 ];
Allen George8b96bfb2016-11-02 08:01:08 -0400458
Allen George8b96bfb2016-11-02 08:01:08 -0400459 let insanity = Insanity {
460 user_map: Some(arg_map_usermap),
461 xtructs: Some(arg_vec_xtructs),
462 };
Tdxdxoz85d82bf2022-07-17 14:14:12 +0200463 let s_cmp_nested_1: BTreeMap<Numberz, Insanity> = BTreeMap::from([
464 (Numberz::TWO, insanity.clone()),
465 (Numberz::THREE, insanity.clone()),
466 ]);
Allen George8b96bfb2016-11-02 08:01:08 -0400467
Allen George8b96bfb2016-11-02 08:01:08 -0400468 let empty_insanity = Insanity {
469 user_map: Some(BTreeMap::new()),
470 xtructs: Some(Vec::new()),
471 };
Tdxdxoz85d82bf2022-07-17 14:14:12 +0200472 let s_cmp_nested_2: BTreeMap<Numberz, Insanity> =
473 BTreeMap::from([(Numberz::SIX, empty_insanity)]);
Allen George8b96bfb2016-11-02 08:01:08 -0400474
Tdxdxoz85d82bf2022-07-17 14:14:12 +0200475 let s_cmp: BTreeMap<UserId, BTreeMap<Numberz, Insanity>> =
476 BTreeMap::from([(1, s_cmp_nested_1), (2, s_cmp_nested_2)]);
Allen George8b96bfb2016-11-02 08:01:08 -0400477
Allen George7ddbcc02020-11-08 09:51:19 -0500478 verify_expected_result(thrift_test_client.test_insanity(insanity), s_cmp)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400479 }
480
Allen Georgebc1344d2017-04-28 10:22:03 -0400481 info!("testException - remote throws Xception");
Allen George8b96bfb2016-11-02 08:01:08 -0400482 {
Allen Georgebc1344d2017-04-28 10:22:03 -0400483 let r = thrift_test_client.test_exception("Xception".to_owned());
Allen George8b96bfb2016-11-02 08:01:08 -0400484 let x = match r {
Allen George55c3e4c2021-03-01 23:19:52 -0500485 Err(thrift::Error::User(ref e)) => match e.downcast_ref::<Xception>() {
486 Some(x) => Ok(x),
487 None => Err(thrift::Error::User(
488 "did not get expected Xception struct".into(),
489 )),
490 },
Allen George8b96bfb2016-11-02 08:01:08 -0400491 _ => Err(thrift::Error::User("did not get exception".into())),
492 }?;
493
494 let x_cmp = Xception {
495 error_code: Some(1001),
496 message: Some("Xception".to_owned()),
497 };
498
499 verify_expected_result(Ok(x), &x_cmp)?;
500 }
501
Allen Georgebc1344d2017-04-28 10:22:03 -0400502 info!("testException - remote throws TApplicationException");
Allen George8b96bfb2016-11-02 08:01:08 -0400503 {
Allen Georgebc1344d2017-04-28 10:22:03 -0400504 let r = thrift_test_client.test_exception("TException".to_owned());
Allen George8b96bfb2016-11-02 08:01:08 -0400505 match r {
506 Err(thrift::Error::Application(ref e)) => {
Allen Georgebc1344d2017-04-28 10:22:03 -0400507 info!("received an {:?}", e);
Allen George8b96bfb2016-11-02 08:01:08 -0400508 Ok(())
509 }
510 _ => Err(thrift::Error::User("did not get exception".into())),
511 }?;
512 }
513
Allen Georgebc1344d2017-04-28 10:22:03 -0400514 info!("testException - remote succeeds");
Allen George8b96bfb2016-11-02 08:01:08 -0400515 {
Allen Georgebc1344d2017-04-28 10:22:03 -0400516 let r = thrift_test_client.test_exception("foo".to_owned());
Allen George8b96bfb2016-11-02 08:01:08 -0400517 match r {
518 Ok(_) => Ok(()),
519 _ => Err(thrift::Error::User("received an exception".into())),
520 }?;
521 }
522
Allen Georgebc1344d2017-04-28 10:22:03 -0400523 info!("testMultiException - remote throws Xception");
Allen George8b96bfb2016-11-02 08:01:08 -0400524 {
Allen Georgebc1344d2017-04-28 10:22:03 -0400525 let r =
526 thrift_test_client.test_multi_exception("Xception".to_owned(), "ignored".to_owned());
Allen George8b96bfb2016-11-02 08:01:08 -0400527 let x = match r {
Allen George55c3e4c2021-03-01 23:19:52 -0500528 Err(thrift::Error::User(ref e)) => match e.downcast_ref::<Xception>() {
529 Some(x) => Ok(x),
530 None => Err(thrift::Error::User(
531 "did not get expected Xception struct".into(),
532 )),
533 },
Allen George8b96bfb2016-11-02 08:01:08 -0400534 _ => Err(thrift::Error::User("did not get exception".into())),
535 }?;
536
537 let x_cmp = Xception {
538 error_code: Some(1001),
539 message: Some("This is an Xception".to_owned()),
540 };
541
542 verify_expected_result(Ok(x), &x_cmp)?;
543 }
544
Allen Georgebc1344d2017-04-28 10:22:03 -0400545 info!("testMultiException - remote throws Xception2");
Allen George8b96bfb2016-11-02 08:01:08 -0400546 {
Allen Georgebc1344d2017-04-28 10:22:03 -0400547 let r =
548 thrift_test_client.test_multi_exception("Xception2".to_owned(), "ignored".to_owned());
Allen George8b96bfb2016-11-02 08:01:08 -0400549 let x = match r {
Allen George55c3e4c2021-03-01 23:19:52 -0500550 Err(thrift::Error::User(ref e)) => match e.downcast_ref::<Xception2>() {
551 Some(x) => Ok(x),
552 None => Err(thrift::Error::User(
553 "did not get expected Xception struct".into(),
554 )),
555 },
Allen George8b96bfb2016-11-02 08:01:08 -0400556 _ => Err(thrift::Error::User("did not get exception".into())),
557 }?;
558
559 let x_cmp = Xception2 {
560 error_code: Some(2002),
Allen George55c3e4c2021-03-01 23:19:52 -0500561 struct_thing: Some(Xtruct {
562 string_thing: Some("This is an Xception2".to_owned()),
563 // since this is an OPT_IN_REQ_OUT field the sender sets a default
564 byte_thing: Some(0),
565 // since this is an OPT_IN_REQ_OUT field the sender sets a default
566 i32_thing: Some(0),
567 // since this is an OPT_IN_REQ_OUT field the sender sets a default
568 i64_thing: Some(0),
569 }),
Allen George8b96bfb2016-11-02 08:01:08 -0400570 };
571
572 verify_expected_result(Ok(x), &x_cmp)?;
573 }
574
Allen Georgebc1344d2017-04-28 10:22:03 -0400575 info!("testMultiException - remote succeeds");
Allen George8b96bfb2016-11-02 08:01:08 -0400576 {
Allen Georgebc1344d2017-04-28 10:22:03 -0400577 let r = thrift_test_client.test_multi_exception("haha".to_owned(), "RETURNED".to_owned());
Allen George8b96bfb2016-11-02 08:01:08 -0400578 let x = match r {
Allen George55c3e4c2021-03-01 23:19:52 -0500579 Err(e) => Err(thrift::Error::User(
580 format!("received an unexpected exception {:?}", e).into(),
581 )),
Allen George8b96bfb2016-11-02 08:01:08 -0400582 _ => r,
583 }?;
584
585 let x_cmp = Xtruct {
586 string_thing: Some("RETURNED".to_owned()),
Allen George0e22c362017-01-30 07:15:00 -0500587 // since this is an OPT_IN_REQ_OUT field the sender sets a default
588 byte_thing: Some(0),
589 // since this is an OPT_IN_REQ_OUT field the sender sets a default
590 i32_thing: Some(0),
591 // since this is an OPT_IN_REQ_OUT field the sender sets a default
592 i64_thing: Some(0),
Allen George8b96bfb2016-11-02 08:01:08 -0400593 };
594
595 verify_expected_result(Ok(x), x_cmp)?;
596 }
597
Allen Georgebc1344d2017-04-28 10:22:03 -0400598 info!("testOneWay - remote sleeps for 1 second");
Allen George8b96bfb2016-11-02 08:01:08 -0400599 {
Allen Georgebc1344d2017-04-28 10:22:03 -0400600 thrift_test_client.test_oneway(1)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400601 }
602
Allen George0e22c362017-01-30 07:15:00 -0500603 // final test to verify that the connection is still writable after the one-way
604 // call
Allen Georgebc1344d2017-04-28 10:22:03 -0400605 thrift_test_client.test_void()
Allen George8b96bfb2016-11-02 08:01:08 -0400606}
607
Allen George0e22c362017-01-30 07:15:00 -0500608fn verify_expected_result<T: Debug + PartialEq + Sized>(
609 actual: Result<T, thrift::Error>,
610 expected: T,
611) -> Result<(), thrift::Error> {
James E. King, III20e16bc2017-11-18 22:37:54 -0500612 info!("*** EXPECTED: Ok({:?})", expected);
613 info!("*** ACTUAL : {:?}", actual);
Allen George8b96bfb2016-11-02 08:01:08 -0400614 match actual {
615 Ok(v) => {
616 if v == expected {
James E. King, III20e16bc2017-11-18 22:37:54 -0500617 info!("*** OK ***");
Allen George8b96bfb2016-11-02 08:01:08 -0400618 Ok(())
619 } else {
James E. King, III20e16bc2017-11-18 22:37:54 -0500620 info!("*** FAILED ***");
Allen George55c3e4c2021-03-01 23:19:52 -0500621 Err(thrift::Error::User(
622 format!("expected {:?} but got {:?}", &expected, &v).into(),
623 ))
Allen George8b96bfb2016-11-02 08:01:08 -0400624 }
625 }
626 Err(e) => Err(e),
627 }
628}