blob: 6cbc238ae41e7e3fc82ff205fe8ac82f81229a37 [file] [log] [blame]
Allen George8b96bfb2016-11-02 08:01:08 -04001// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements. See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership. The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License. You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied. See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
Allen George7ddbcc02020-11-08 09:51:19 -050018use env_logger;
19use log::*;
20use clap::{clap_app, value_t};
Allen Georgebc1344d2017-04-28 10:22:03 -040021
Allen George8b96bfb2016-11-02 08:01:08 -040022use std::collections::{BTreeMap, BTreeSet};
23use std::fmt::Debug;
Allen George8b96bfb2016-11-02 08:01:08 -040024
Allen George7ddbcc02020-11-08 09:51:19 -050025use thrift;
26use thrift::OrderedFloat;
Allen George8b96bfb2016-11-02 08:01:08 -040027use thrift::protocol::{TBinaryInputProtocol, TBinaryOutputProtocol, TCompactInputProtocol,
Allen Georgebc1344d2017-04-28 10:22:03 -040028 TCompactOutputProtocol, TInputProtocol, TMultiplexedOutputProtocol,
29 TOutputProtocol};
Allen George0e22c362017-01-30 07:15:00 -050030use thrift::transport::{ReadHalf, TBufferedReadTransport, TBufferedWriteTransport,
31 TFramedReadTransport, TFramedWriteTransport, TIoChannel, TReadTransport,
32 TTcpChannel, TWriteTransport, WriteHalf};
Allen George8b96bfb2016-11-02 08:01:08 -040033use thrift_test::*;
34
35fn main() {
Allen George7ddbcc02020-11-08 09:51:19 -050036 env_logger::init();
Allen Georgebc1344d2017-04-28 10:22:03 -040037
38 debug!("initialized logger - running cross-test client");
39
Allen George8b96bfb2016-11-02 08:01:08 -040040 match run() {
Allen Georgebc1344d2017-04-28 10:22:03 -040041 Ok(()) => info!("cross-test client succeeded"),
Allen George8b96bfb2016-11-02 08:01:08 -040042 Err(e) => {
Allen Georgebc1344d2017-04-28 10:22:03 -040043 info!("cross-test client failed with error {:?}", e);
Allen George8b96bfb2016-11-02 08:01:08 -040044 std::process::exit(1);
45 }
46 }
47}
48
49fn run() -> thrift::Result<()> {
50 // unsupported options:
51 // --domain-socket
Jens Geyer4a33b182020-03-22 13:46:34 +010052 // --pipe
Allen George8b96bfb2016-11-02 08:01:08 -040053 // --anon-pipes
54 // --ssl
55 // --threads
56 let matches = clap_app!(rust_test_client =>
57 (version: "1.0")
58 (author: "Apache Thrift Developers <dev@thrift.apache.org>")
59 (about: "Rust Thrift test client")
60 (@arg host: --host +takes_value "Host on which the Thrift test server is located")
61 (@arg port: --port +takes_value "Port on which the Thrift test server is listening")
62 (@arg transport: --transport +takes_value "Thrift transport implementation to use (\"buffered\", \"framed\")")
James E. King III9804ab92019-02-07 16:59:05 -050063 (@arg protocol: --protocol +takes_value "Thrift protocol implementation to use (\"binary\", \"compact\", \"multi\", \"multic\")")
Allen George8b96bfb2016-11-02 08:01:08 -040064 (@arg testloops: -n --testloops +takes_value "Number of times to run tests")
Allen George0e22c362017-01-30 07:15:00 -050065 )
Allen Georgebc1344d2017-04-28 10:22:03 -040066 .get_matches();
Allen George8b96bfb2016-11-02 08:01:08 -040067
68 let host = matches.value_of("host").unwrap_or("127.0.0.1");
69 let port = value_t!(matches, "port", u16).unwrap_or(9090);
70 let testloops = value_t!(matches, "testloops", u8).unwrap_or(1);
71 let transport = matches.value_of("transport").unwrap_or("buffered");
72 let protocol = matches.value_of("protocol").unwrap_or("binary");
73
Allen George8b96bfb2016-11-02 08:01:08 -040074
Allen Georgebc1344d2017-04-28 10:22:03 -040075 let mut thrift_test_client = {
76 let (i_prot, o_prot) = build_protocols(host, port, transport, protocol, "ThriftTest")?;
77 ThriftTestSyncClient::new(i_prot, o_prot)
Allen George8b96bfb2016-11-02 08:01:08 -040078 };
Allen George8b96bfb2016-11-02 08:01:08 -040079
Allen Georgebc1344d2017-04-28 10:22:03 -040080 let mut second_service_client = if protocol.starts_with("multi") {
81 let (i_prot, o_prot) = build_protocols(host, port, transport, protocol, "SecondService")?;
82 Some(SecondServiceSyncClient::new(i_prot, o_prot))
83 } else {
84 None
Allen George8b96bfb2016-11-02 08:01:08 -040085 };
86
Allen Georgebc1344d2017-04-28 10:22:03 -040087 info!(
Allen George0e22c362017-01-30 07:15:00 -050088 "connecting to {}:{} with {}+{} stack",
89 host,
90 port,
91 protocol,
92 transport
93 );
Allen George8b96bfb2016-11-02 08:01:08 -040094
Allen George8b96bfb2016-11-02 08:01:08 -040095 for _ in 0..testloops {
Allen Georgebc1344d2017-04-28 10:22:03 -040096 make_thrift_calls(&mut thrift_test_client, &mut second_service_client)?
Allen George8b96bfb2016-11-02 08:01:08 -040097 }
98
99 Ok(())
100}
101
Allen Georgebc1344d2017-04-28 10:22:03 -0400102fn build_protocols(
103 host: &str,
104 port: u16,
105 transport: &str,
106 protocol: &str,
107 service_name: &str,
Allen Georgeb0d14132020-03-29 11:48:55 -0400108) -> thrift::Result<(Box<dyn TInputProtocol>, Box<dyn TOutputProtocol>)> {
Allen Georgebc1344d2017-04-28 10:22:03 -0400109 let (i_chan, o_chan) = tcp_channel(host, port)?;
110
Allen Georgeb0d14132020-03-29 11:48:55 -0400111 let (i_tran, o_tran): (Box<dyn TReadTransport>, Box<dyn TWriteTransport>) = match transport {
Allen Georgebc1344d2017-04-28 10:22:03 -0400112 "buffered" => {
113 (Box::new(TBufferedReadTransport::new(i_chan)),
114 Box::new(TBufferedWriteTransport::new(o_chan)))
115 }
116 "framed" => {
117 (Box::new(TFramedReadTransport::new(i_chan)),
118 Box::new(TFramedWriteTransport::new(o_chan)))
119 }
120 unmatched => return Err(format!("unsupported transport {}", unmatched).into()),
121 };
122
Allen Georgeb0d14132020-03-29 11:48:55 -0400123 let (i_prot, o_prot): (Box<dyn TInputProtocol>, Box<dyn TOutputProtocol>) = match protocol {
James E. King, III39eaae62017-11-19 20:17:33 -0500124 "binary" => {
Allen Georgebc1344d2017-04-28 10:22:03 -0400125 (Box::new(TBinaryInputProtocol::new(i_tran, true)),
126 Box::new(TBinaryOutputProtocol::new(o_tran, true)))
127 }
128 "multi" => {
129 (Box::new(TBinaryInputProtocol::new(i_tran, true)),
130 Box::new(
131 TMultiplexedOutputProtocol::new(
132 service_name,
133 TBinaryOutputProtocol::new(o_tran, true),
134 ),
135 ))
136 }
James E. King, III39eaae62017-11-19 20:17:33 -0500137 "compact" => {
Allen Georgebc1344d2017-04-28 10:22:03 -0400138 (Box::new(TCompactInputProtocol::new(i_tran)),
139 Box::new(TCompactOutputProtocol::new(o_tran)))
140 }
141 "multic" => {
142 (Box::new(TCompactInputProtocol::new(i_tran)),
143 Box::new(TMultiplexedOutputProtocol::new(service_name, TCompactOutputProtocol::new(o_tran)),))
144 }
145 unmatched => return Err(format!("unsupported protocol {}", unmatched).into()),
146 };
147
148 Ok((i_prot, o_prot))
149}
150
Allen George0e22c362017-01-30 07:15:00 -0500151// FIXME: expose "open" through the client interface so I don't have to early
152// open
153fn tcp_channel(
154 host: &str,
155 port: u16,
156) -> thrift::Result<(ReadHalf<TTcpChannel>, WriteHalf<TTcpChannel>)> {
157 let mut c = TTcpChannel::new();
158 c.open(&format!("{}:{}", host, port))?;
159 c.split()
Allen George8b96bfb2016-11-02 08:01:08 -0400160}
161
Allen Georgeb0d14132020-03-29 11:48:55 -0400162type BuildThriftTestClient = ThriftTestSyncClient<Box<dyn TInputProtocol>, Box<dyn TOutputProtocol>>;
163type BuiltSecondServiceClient = SecondServiceSyncClient<Box<dyn TInputProtocol>, Box<dyn TOutputProtocol>>;
Allen George8b96bfb2016-11-02 08:01:08 -0400164
Allen George7ddbcc02020-11-08 09:51:19 -0500165#[allow(clippy::cognitive_complexity)]
Allen Georgebc1344d2017-04-28 10:22:03 -0400166fn make_thrift_calls(
167 thrift_test_client: &mut BuildThriftTestClient,
168 second_service_client: &mut Option<BuiltSecondServiceClient>,
169) -> Result<(), thrift::Error> {
170 info!("testVoid");
171 thrift_test_client.test_void()?;
Allen George8b96bfb2016-11-02 08:01:08 -0400172
Allen Georgebc1344d2017-04-28 10:22:03 -0400173 info!("testString");
Allen George0e22c362017-01-30 07:15:00 -0500174 verify_expected_result(
Allen Georgebc1344d2017-04-28 10:22:03 -0400175 thrift_test_client.test_string("thing".to_owned()),
176 "thing".to_owned(),
177 )?;
178
179 info!("testBool");
180 verify_expected_result(thrift_test_client.test_bool(true), true)?;
181
182 info!("testBool");
183 verify_expected_result(thrift_test_client.test_bool(false), false)?;
184
185 info!("testByte");
186 verify_expected_result(thrift_test_client.test_byte(42), 42)?;
187
188 info!("testi32");
Allen George7ddbcc02020-11-08 09:51:19 -0500189 verify_expected_result(thrift_test_client.test_i32(1_159_348_374), 1_159_348_374)?;
Allen Georgebc1344d2017-04-28 10:22:03 -0400190
191 info!("testi64");
192 // try!(verify_expected_result(thrift_test_client.test_i64(-8651829879438294565),
193 // -8651829879438294565));
194 verify_expected_result(
195 thrift_test_client.test_i64(i64::min_value()),
196 i64::min_value(),
197 )?;
198
199 info!("testDouble");
200 verify_expected_result(
201 thrift_test_client.test_double(OrderedFloat::from(42.42)),
Allen George0e22c362017-01-30 07:15:00 -0500202 OrderedFloat::from(42.42),
203 )?;
Allen George8b96bfb2016-11-02 08:01:08 -0400204
Allen Georgebc1344d2017-04-28 10:22:03 -0400205 info!("testTypedef");
Allen George8b96bfb2016-11-02 08:01:08 -0400206 {
207 let u_snd: UserId = 2348;
208 let u_cmp: UserId = 2348;
Allen Georgebc1344d2017-04-28 10:22:03 -0400209 verify_expected_result(thrift_test_client.test_typedef(u_snd), u_cmp)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400210 }
211
Allen Georgebc1344d2017-04-28 10:22:03 -0400212 info!("testEnum");
Allen George8b96bfb2016-11-02 08:01:08 -0400213 {
GREATEST Wiggler EvaR!b57d1262018-11-09 07:54:32 -0500214 verify_expected_result(thrift_test_client.test_enum(Numberz::Two), Numberz::Two)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400215 }
216
Allen Georgebc1344d2017-04-28 10:22:03 -0400217 info!("testBinary");
Allen George8b96bfb2016-11-02 08:01:08 -0400218 {
219 let b_snd = vec![0x77, 0x30, 0x30, 0x74, 0x21, 0x20, 0x52, 0x75, 0x73, 0x74];
220 let b_cmp = vec![0x77, 0x30, 0x30, 0x74, 0x21, 0x20, 0x52, 0x75, 0x73, 0x74];
Allen Georgebc1344d2017-04-28 10:22:03 -0400221 verify_expected_result(thrift_test_client.test_binary(b_snd), b_cmp)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400222 }
223
Allen Georgebc1344d2017-04-28 10:22:03 -0400224 info!("testStruct");
Allen George8b96bfb2016-11-02 08:01:08 -0400225 {
226 let x_snd = Xtruct {
227 string_thing: Some("foo".to_owned()),
228 byte_thing: Some(12),
Allen George7ddbcc02020-11-08 09:51:19 -0500229 i32_thing: Some(219_129),
230 i64_thing: Some(12_938_492_818),
Allen George8b96bfb2016-11-02 08:01:08 -0400231 };
232 let x_cmp = Xtruct {
233 string_thing: Some("foo".to_owned()),
234 byte_thing: Some(12),
Allen George7ddbcc02020-11-08 09:51:19 -0500235 i32_thing: Some(219_129),
236 i64_thing: Some(12_938_492_818),
Allen George8b96bfb2016-11-02 08:01:08 -0400237 };
Allen Georgebc1344d2017-04-28 10:22:03 -0400238 verify_expected_result(thrift_test_client.test_struct(x_snd), x_cmp)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400239 }
240
241 // Xtruct again, with optional values
Allen George0e22c362017-01-30 07:15:00 -0500242 // FIXME: apparently the erlang thrift server does not like opt-in-req-out
243 // parameters that are undefined. Joy.
Allen George8b96bfb2016-11-02 08:01:08 -0400244 // {
Allen George0e22c362017-01-30 07:15:00 -0500245 // let x_snd = Xtruct { string_thing: Some("foo".to_owned()), byte_thing: None,
246 // i32_thing: None, i64_thing: Some(12938492818) };
247 // let x_cmp = Xtruct { string_thing: Some("foo".to_owned()), byte_thing:
248 // Some(0), i32_thing: Some(0), i64_thing: Some(12938492818) }; // the C++
249 // server is responding correctly
Allen Georgebc1344d2017-04-28 10:22:03 -0400250 // try!(verify_expected_result(thrift_test_client.test_struct(x_snd), x_cmp));
Allen George8b96bfb2016-11-02 08:01:08 -0400251 // }
252 //
253
Allen Georgebc1344d2017-04-28 10:22:03 -0400254 info!("testNest"); // (FIXME: try Xtruct2 with optional values)
Allen George8b96bfb2016-11-02 08:01:08 -0400255 {
256 let x_snd = Xtruct2 {
257 byte_thing: Some(32),
Allen George0e22c362017-01-30 07:15:00 -0500258 struct_thing: Some(
259 Xtruct {
260 string_thing: Some("foo".to_owned()),
261 byte_thing: Some(1),
Allen George7ddbcc02020-11-08 09:51:19 -0500262 i32_thing: Some(324_382_098),
263 i64_thing: Some(12_938_492_818),
Allen George0e22c362017-01-30 07:15:00 -0500264 },
265 ),
Allen George7ddbcc02020-11-08 09:51:19 -0500266 i32_thing: Some(293_481_098),
Allen George8b96bfb2016-11-02 08:01:08 -0400267 };
268 let x_cmp = Xtruct2 {
269 byte_thing: Some(32),
Allen George0e22c362017-01-30 07:15:00 -0500270 struct_thing: Some(
271 Xtruct {
272 string_thing: Some("foo".to_owned()),
273 byte_thing: Some(1),
Allen George7ddbcc02020-11-08 09:51:19 -0500274 i32_thing: Some(324_382_098),
275 i64_thing: Some(12_938_492_818),
Allen George0e22c362017-01-30 07:15:00 -0500276 },
277 ),
Allen George7ddbcc02020-11-08 09:51:19 -0500278 i32_thing: Some(293_481_098),
Allen George8b96bfb2016-11-02 08:01:08 -0400279 };
Allen Georgebc1344d2017-04-28 10:22:03 -0400280 verify_expected_result(thrift_test_client.test_nest(x_snd), x_cmp)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400281 }
282
Allen Georgebc1344d2017-04-28 10:22:03 -0400283 // do the multiplexed calls while making the main ThriftTest calls
284 if let Some(ref mut client) = second_service_client.as_mut() {
Allen Georgebc1344d2017-04-28 10:22:03 -0400285 info!("SecondService secondtestString");
286 {
287 verify_expected_result(
288 client.secondtest_string("test_string".to_owned()),
289 "testString(\"test_string\")".to_owned(),
290 )?;
291 }
292 }
293
294 info!("testList");
Allen George8b96bfb2016-11-02 08:01:08 -0400295 {
296 let mut v_snd: Vec<i32> = Vec::new();
297 v_snd.push(29384);
298 v_snd.push(238);
299 v_snd.push(32498);
300
301 let mut v_cmp: Vec<i32> = Vec::new();
302 v_cmp.push(29384);
303 v_cmp.push(238);
304 v_cmp.push(32498);
305
Allen Georgebc1344d2017-04-28 10:22:03 -0400306 verify_expected_result(thrift_test_client.test_list(v_snd), v_cmp)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400307 }
308
Allen Georgebc1344d2017-04-28 10:22:03 -0400309 info!("testSet");
Allen George8b96bfb2016-11-02 08:01:08 -0400310 {
311 let mut s_snd: BTreeSet<i32> = BTreeSet::new();
Allen George7ddbcc02020-11-08 09:51:19 -0500312 s_snd.insert(293_481);
Allen George8b96bfb2016-11-02 08:01:08 -0400313 s_snd.insert(23);
314 s_snd.insert(3234);
315
316 let mut s_cmp: BTreeSet<i32> = BTreeSet::new();
Allen George7ddbcc02020-11-08 09:51:19 -0500317 s_cmp.insert(293_481);
Allen George8b96bfb2016-11-02 08:01:08 -0400318 s_cmp.insert(23);
319 s_cmp.insert(3234);
320
Allen Georgebc1344d2017-04-28 10:22:03 -0400321 verify_expected_result(thrift_test_client.test_set(s_snd), s_cmp)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400322 }
323
Allen Georgebc1344d2017-04-28 10:22:03 -0400324 info!("testMap");
Allen George8b96bfb2016-11-02 08:01:08 -0400325 {
326 let mut m_snd: BTreeMap<i32, i32> = BTreeMap::new();
327 m_snd.insert(2, 4);
328 m_snd.insert(4, 6);
329 m_snd.insert(8, 7);
330
331 let mut m_cmp: BTreeMap<i32, i32> = BTreeMap::new();
332 m_cmp.insert(2, 4);
333 m_cmp.insert(4, 6);
334 m_cmp.insert(8, 7);
335
Allen Georgebc1344d2017-04-28 10:22:03 -0400336 verify_expected_result(thrift_test_client.test_map(m_snd), m_cmp)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400337 }
338
Allen Georgebc1344d2017-04-28 10:22:03 -0400339 info!("testStringMap");
Allen George8b96bfb2016-11-02 08:01:08 -0400340 {
341 let mut m_snd: BTreeMap<String, String> = BTreeMap::new();
342 m_snd.insert("2".to_owned(), "4_string".to_owned());
343 m_snd.insert("4".to_owned(), "6_string".to_owned());
344 m_snd.insert("8".to_owned(), "7_string".to_owned());
345
346 let mut m_rcv: BTreeMap<String, String> = BTreeMap::new();
347 m_rcv.insert("2".to_owned(), "4_string".to_owned());
348 m_rcv.insert("4".to_owned(), "6_string".to_owned());
349 m_rcv.insert("8".to_owned(), "7_string".to_owned());
350
Allen Georgebc1344d2017-04-28 10:22:03 -0400351 verify_expected_result(thrift_test_client.test_string_map(m_snd), m_rcv)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400352 }
353
354 // nested map
Allen George0e22c362017-01-30 07:15:00 -0500355 // expect : {-4 => {-4 => -4, -3 => -3, -2 => -2, -1 => -1, }, 4 => {1 => 1, 2
356 // => 2, 3 => 3, 4 => 4, }, }
Allen Georgebc1344d2017-04-28 10:22:03 -0400357 info!("testMapMap");
Allen George8b96bfb2016-11-02 08:01:08 -0400358 {
359 let mut m_cmp_nested_0: BTreeMap<i32, i32> = BTreeMap::new();
360 for i in (-4 as i32)..0 {
361 m_cmp_nested_0.insert(i, i);
362 }
363 let mut m_cmp_nested_1: BTreeMap<i32, i32> = BTreeMap::new();
364 for i in 1..5 {
365 m_cmp_nested_1.insert(i, i);
366 }
367
368 let mut m_cmp: BTreeMap<i32, BTreeMap<i32, i32>> = BTreeMap::new();
369 m_cmp.insert(-4, m_cmp_nested_0);
370 m_cmp.insert(4, m_cmp_nested_1);
371
Allen Georgebc1344d2017-04-28 10:22:03 -0400372 verify_expected_result(thrift_test_client.test_map_map(42), m_cmp)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400373 }
374
Allen Georgebc1344d2017-04-28 10:22:03 -0400375 info!("testMulti");
Allen George8b96bfb2016-11-02 08:01:08 -0400376 {
377 let mut m_snd: BTreeMap<i16, String> = BTreeMap::new();
378 m_snd.insert(1298, "fizz".to_owned());
379 m_snd.insert(-148, "buzz".to_owned());
380
381 let s_cmp = Xtruct {
382 string_thing: Some("Hello2".to_owned()),
383 byte_thing: Some(1),
Allen George7ddbcc02020-11-08 09:51:19 -0500384 i32_thing: Some(-123_948),
385 i64_thing: Some(-19_234_123_981),
Allen George8b96bfb2016-11-02 08:01:08 -0400386 };
387
Allen George0e22c362017-01-30 07:15:00 -0500388 verify_expected_result(
Allen George7ddbcc02020-11-08 09:51:19 -0500389 thrift_test_client.test_multi(1, -123_948, -19_234_123_981, m_snd, Numberz::Eight, 81),
Allen George0e22c362017-01-30 07:15:00 -0500390 s_cmp,
391 )?;
Allen George8b96bfb2016-11-02 08:01:08 -0400392 }
393
394 // Insanity
395 // returns:
396 // { 1 => { 2 => argument,
397 // 3 => argument,
398 // },
399 // 2 => { 6 => <empty Insanity struct>, },
400 // }
401 {
402 let mut arg_map_usermap: BTreeMap<Numberz, i64> = BTreeMap::new();
GREATEST Wiggler EvaR!b57d1262018-11-09 07:54:32 -0500403 arg_map_usermap.insert(Numberz::One, 4289);
404 arg_map_usermap.insert(Numberz::Eight, 19);
Allen George8b96bfb2016-11-02 08:01:08 -0400405
406 let mut arg_vec_xtructs: Vec<Xtruct> = Vec::new();
Allen George0e22c362017-01-30 07:15:00 -0500407 arg_vec_xtructs.push(
408 Xtruct {
409 string_thing: Some("foo".to_owned()),
410 byte_thing: Some(8),
411 i32_thing: Some(29),
412 i64_thing: Some(92384),
413 },
414 );
415 arg_vec_xtructs.push(
416 Xtruct {
417 string_thing: Some("bar".to_owned()),
418 byte_thing: Some(28),
419 i32_thing: Some(2),
420 i64_thing: Some(-1281),
421 },
422 );
423 arg_vec_xtructs.push(
424 Xtruct {
425 string_thing: Some("baz".to_owned()),
426 byte_thing: Some(0),
Allen George7ddbcc02020-11-08 09:51:19 -0500427 i32_thing: Some(3_948_539),
428 i64_thing: Some(-12_938_492),
Allen George0e22c362017-01-30 07:15:00 -0500429 },
430 );
Allen George8b96bfb2016-11-02 08:01:08 -0400431
432 let mut s_cmp_nested_1: BTreeMap<Numberz, Insanity> = BTreeMap::new();
433 let insanity = Insanity {
434 user_map: Some(arg_map_usermap),
435 xtructs: Some(arg_vec_xtructs),
436 };
GREATEST Wiggler EvaR!b57d1262018-11-09 07:54:32 -0500437 s_cmp_nested_1.insert(Numberz::Two, insanity.clone());
438 s_cmp_nested_1.insert(Numberz::Three, insanity.clone());
Allen George8b96bfb2016-11-02 08:01:08 -0400439
440 let mut s_cmp_nested_2: BTreeMap<Numberz, Insanity> = BTreeMap::new();
441 let empty_insanity = Insanity {
442 user_map: Some(BTreeMap::new()),
443 xtructs: Some(Vec::new()),
444 };
GREATEST Wiggler EvaR!b57d1262018-11-09 07:54:32 -0500445 s_cmp_nested_2.insert(Numberz::Six, empty_insanity);
Allen George8b96bfb2016-11-02 08:01:08 -0400446
447 let mut s_cmp: BTreeMap<UserId, BTreeMap<Numberz, Insanity>> = BTreeMap::new();
448 s_cmp.insert(1 as UserId, s_cmp_nested_1);
449 s_cmp.insert(2 as UserId, s_cmp_nested_2);
450
Allen George7ddbcc02020-11-08 09:51:19 -0500451 verify_expected_result(thrift_test_client.test_insanity(insanity), s_cmp)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400452 }
453
Allen Georgebc1344d2017-04-28 10:22:03 -0400454 info!("testException - remote throws Xception");
Allen George8b96bfb2016-11-02 08:01:08 -0400455 {
Allen Georgebc1344d2017-04-28 10:22:03 -0400456 let r = thrift_test_client.test_exception("Xception".to_owned());
Allen George8b96bfb2016-11-02 08:01:08 -0400457 let x = match r {
458 Err(thrift::Error::User(ref e)) => {
459 match e.downcast_ref::<Xception>() {
460 Some(x) => Ok(x),
Allen George0e22c362017-01-30 07:15:00 -0500461 None => Err(thrift::Error::User("did not get expected Xception struct".into()),),
Allen George8b96bfb2016-11-02 08:01:08 -0400462 }
463 }
464 _ => Err(thrift::Error::User("did not get exception".into())),
465 }?;
466
467 let x_cmp = Xception {
468 error_code: Some(1001),
469 message: Some("Xception".to_owned()),
470 };
471
472 verify_expected_result(Ok(x), &x_cmp)?;
473 }
474
Allen Georgebc1344d2017-04-28 10:22:03 -0400475 info!("testException - remote throws TApplicationException");
Allen George8b96bfb2016-11-02 08:01:08 -0400476 {
Allen Georgebc1344d2017-04-28 10:22:03 -0400477 let r = thrift_test_client.test_exception("TException".to_owned());
Allen George8b96bfb2016-11-02 08:01:08 -0400478 match r {
479 Err(thrift::Error::Application(ref e)) => {
Allen Georgebc1344d2017-04-28 10:22:03 -0400480 info!("received an {:?}", e);
Allen George8b96bfb2016-11-02 08:01:08 -0400481 Ok(())
482 }
483 _ => Err(thrift::Error::User("did not get exception".into())),
484 }?;
485 }
486
Allen Georgebc1344d2017-04-28 10:22:03 -0400487 info!("testException - remote succeeds");
Allen George8b96bfb2016-11-02 08:01:08 -0400488 {
Allen Georgebc1344d2017-04-28 10:22:03 -0400489 let r = thrift_test_client.test_exception("foo".to_owned());
Allen George8b96bfb2016-11-02 08:01:08 -0400490 match r {
491 Ok(_) => Ok(()),
492 _ => Err(thrift::Error::User("received an exception".into())),
493 }?;
494 }
495
Allen Georgebc1344d2017-04-28 10:22:03 -0400496 info!("testMultiException - remote throws Xception");
Allen George8b96bfb2016-11-02 08:01:08 -0400497 {
Allen Georgebc1344d2017-04-28 10:22:03 -0400498 let r =
499 thrift_test_client.test_multi_exception("Xception".to_owned(), "ignored".to_owned());
Allen George8b96bfb2016-11-02 08:01:08 -0400500 let x = match r {
501 Err(thrift::Error::User(ref e)) => {
502 match e.downcast_ref::<Xception>() {
503 Some(x) => Ok(x),
Allen George0e22c362017-01-30 07:15:00 -0500504 None => Err(thrift::Error::User("did not get expected Xception struct".into()),),
Allen George8b96bfb2016-11-02 08:01:08 -0400505 }
506 }
507 _ => Err(thrift::Error::User("did not get exception".into())),
508 }?;
509
510 let x_cmp = Xception {
511 error_code: Some(1001),
512 message: Some("This is an Xception".to_owned()),
513 };
514
515 verify_expected_result(Ok(x), &x_cmp)?;
516 }
517
Allen Georgebc1344d2017-04-28 10:22:03 -0400518 info!("testMultiException - remote throws Xception2");
Allen George8b96bfb2016-11-02 08:01:08 -0400519 {
Allen Georgebc1344d2017-04-28 10:22:03 -0400520 let r =
521 thrift_test_client.test_multi_exception("Xception2".to_owned(), "ignored".to_owned());
Allen George8b96bfb2016-11-02 08:01:08 -0400522 let x = match r {
523 Err(thrift::Error::User(ref e)) => {
524 match e.downcast_ref::<Xception2>() {
525 Some(x) => Ok(x),
Allen George0e22c362017-01-30 07:15:00 -0500526 None => Err(thrift::Error::User("did not get expected Xception struct".into()),),
Allen George8b96bfb2016-11-02 08:01:08 -0400527 }
528 }
529 _ => Err(thrift::Error::User("did not get exception".into())),
530 }?;
531
532 let x_cmp = Xception2 {
533 error_code: Some(2002),
Allen George0e22c362017-01-30 07:15:00 -0500534 struct_thing: Some(
535 Xtruct {
536 string_thing: Some("This is an Xception2".to_owned()),
537 // since this is an OPT_IN_REQ_OUT field the sender sets a default
538 byte_thing: Some(0),
539 // since this is an OPT_IN_REQ_OUT field the sender sets a default
540 i32_thing: Some(0),
541 // since this is an OPT_IN_REQ_OUT field the sender sets a default
542 i64_thing: Some(0),
543 },
544 ),
Allen George8b96bfb2016-11-02 08:01:08 -0400545 };
546
547 verify_expected_result(Ok(x), &x_cmp)?;
548 }
549
Allen Georgebc1344d2017-04-28 10:22:03 -0400550 info!("testMultiException - remote succeeds");
Allen George8b96bfb2016-11-02 08:01:08 -0400551 {
Allen Georgebc1344d2017-04-28 10:22:03 -0400552 let r = thrift_test_client.test_multi_exception("haha".to_owned(), "RETURNED".to_owned());
Allen George8b96bfb2016-11-02 08:01:08 -0400553 let x = match r {
Allen George0e22c362017-01-30 07:15:00 -0500554 Err(e) => Err(thrift::Error::User(format!("received an unexpected exception {:?}", e).into(),),),
Allen George8b96bfb2016-11-02 08:01:08 -0400555 _ => r,
556 }?;
557
558 let x_cmp = Xtruct {
559 string_thing: Some("RETURNED".to_owned()),
Allen George0e22c362017-01-30 07:15:00 -0500560 // 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),
Allen George8b96bfb2016-11-02 08:01:08 -0400566 };
567
568 verify_expected_result(Ok(x), x_cmp)?;
569 }
570
Allen Georgebc1344d2017-04-28 10:22:03 -0400571 info!("testOneWay - remote sleeps for 1 second");
Allen George8b96bfb2016-11-02 08:01:08 -0400572 {
Allen Georgebc1344d2017-04-28 10:22:03 -0400573 thrift_test_client.test_oneway(1)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400574 }
575
Allen George0e22c362017-01-30 07:15:00 -0500576 // final test to verify that the connection is still writable after the one-way
577 // call
Allen Georgebc1344d2017-04-28 10:22:03 -0400578 thrift_test_client.test_void()
Allen George8b96bfb2016-11-02 08:01:08 -0400579}
580
Allen George0e22c362017-01-30 07:15:00 -0500581fn verify_expected_result<T: Debug + PartialEq + Sized>(
582 actual: Result<T, thrift::Error>,
583 expected: T,
584) -> Result<(), thrift::Error> {
James E. King, III20e16bc2017-11-18 22:37:54 -0500585 info!("*** EXPECTED: Ok({:?})", expected);
586 info!("*** ACTUAL : {:?}", actual);
Allen George8b96bfb2016-11-02 08:01:08 -0400587 match actual {
588 Ok(v) => {
589 if v == expected {
James E. King, III20e16bc2017-11-18 22:37:54 -0500590 info!("*** OK ***");
Allen George8b96bfb2016-11-02 08:01:08 -0400591 Ok(())
592 } else {
James E. King, III20e16bc2017-11-18 22:37:54 -0500593 info!("*** FAILED ***");
Allen George0e22c362017-01-30 07:15:00 -0500594 Err(thrift::Error::User(format!("expected {:?} but got {:?}", &expected, &v).into()),)
Allen George8b96bfb2016-11-02 08:01:08 -0400595 }
596 }
597 Err(e) => Err(e),
598 }
599}