blob: 3206deb441d40b04ed6f63f1041d1bd331b0bc3b [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
18#[macro_use]
Allen Georgebc1344d2017-04-28 10:22:03 -040019extern crate log;
20extern crate env_logger;
21
22#[macro_use]
Allen George8b96bfb2016-11-02 08:01:08 -040023extern crate clap;
24extern crate ordered_float;
25extern crate thrift;
26extern crate thrift_test; // huh. I have to do this to use my lib
27
28use ordered_float::OrderedFloat;
Allen George8b96bfb2016-11-02 08:01:08 -040029use std::collections::{BTreeMap, BTreeSet};
30use std::fmt::Debug;
Allen George8b96bfb2016-11-02 08:01:08 -040031
32use thrift::protocol::{TBinaryInputProtocol, TBinaryOutputProtocol, TCompactInputProtocol,
Allen Georgebc1344d2017-04-28 10:22:03 -040033 TCompactOutputProtocol, TInputProtocol, TMultiplexedOutputProtocol,
34 TOutputProtocol};
Allen George0e22c362017-01-30 07:15:00 -050035use thrift::transport::{ReadHalf, TBufferedReadTransport, TBufferedWriteTransport,
36 TFramedReadTransport, TFramedWriteTransport, TIoChannel, TReadTransport,
37 TTcpChannel, TWriteTransport, WriteHalf};
Allen George8b96bfb2016-11-02 08:01:08 -040038use thrift_test::*;
39
40fn main() {
Allen Georgebc1344d2017-04-28 10:22:03 -040041 env_logger::init().expect("logger setup failed");
42
43 debug!("initialized logger - running cross-test client");
44
Allen George8b96bfb2016-11-02 08:01:08 -040045 match run() {
Allen Georgebc1344d2017-04-28 10:22:03 -040046 Ok(()) => info!("cross-test client succeeded"),
Allen George8b96bfb2016-11-02 08:01:08 -040047 Err(e) => {
Allen Georgebc1344d2017-04-28 10:22:03 -040048 info!("cross-test client failed with error {:?}", e);
Allen George8b96bfb2016-11-02 08:01:08 -040049 std::process::exit(1);
50 }
51 }
52}
53
54fn run() -> thrift::Result<()> {
55 // unsupported options:
56 // --domain-socket
Jens Geyer4a33b182020-03-22 13:46:34 +010057 // --pipe
Allen George8b96bfb2016-11-02 08:01:08 -040058 // --anon-pipes
59 // --ssl
60 // --threads
61 let matches = clap_app!(rust_test_client =>
62 (version: "1.0")
63 (author: "Apache Thrift Developers <dev@thrift.apache.org>")
64 (about: "Rust Thrift test client")
65 (@arg host: --host +takes_value "Host on which the Thrift test server is located")
66 (@arg port: --port +takes_value "Port on which the Thrift test server is listening")
67 (@arg transport: --transport +takes_value "Thrift transport implementation to use (\"buffered\", \"framed\")")
James E. King III9804ab92019-02-07 16:59:05 -050068 (@arg protocol: --protocol +takes_value "Thrift protocol implementation to use (\"binary\", \"compact\", \"multi\", \"multic\")")
Allen George8b96bfb2016-11-02 08:01:08 -040069 (@arg testloops: -n --testloops +takes_value "Number of times to run tests")
Allen George0e22c362017-01-30 07:15:00 -050070 )
Allen Georgebc1344d2017-04-28 10:22:03 -040071 .get_matches();
Allen George8b96bfb2016-11-02 08:01:08 -040072
73 let host = matches.value_of("host").unwrap_or("127.0.0.1");
74 let port = value_t!(matches, "port", u16).unwrap_or(9090);
75 let testloops = value_t!(matches, "testloops", u8).unwrap_or(1);
76 let transport = matches.value_of("transport").unwrap_or("buffered");
77 let protocol = matches.value_of("protocol").unwrap_or("binary");
78
Allen George8b96bfb2016-11-02 08:01:08 -040079
Allen Georgebc1344d2017-04-28 10:22:03 -040080 let mut thrift_test_client = {
81 let (i_prot, o_prot) = build_protocols(host, port, transport, protocol, "ThriftTest")?;
82 ThriftTestSyncClient::new(i_prot, o_prot)
Allen George8b96bfb2016-11-02 08:01:08 -040083 };
Allen George8b96bfb2016-11-02 08:01:08 -040084
Allen Georgebc1344d2017-04-28 10:22:03 -040085 let mut second_service_client = if protocol.starts_with("multi") {
86 let (i_prot, o_prot) = build_protocols(host, port, transport, protocol, "SecondService")?;
87 Some(SecondServiceSyncClient::new(i_prot, o_prot))
88 } else {
89 None
Allen George8b96bfb2016-11-02 08:01:08 -040090 };
91
Allen Georgebc1344d2017-04-28 10:22:03 -040092 info!(
Allen George0e22c362017-01-30 07:15:00 -050093 "connecting to {}:{} with {}+{} stack",
94 host,
95 port,
96 protocol,
97 transport
98 );
Allen George8b96bfb2016-11-02 08:01:08 -040099
Allen George8b96bfb2016-11-02 08:01:08 -0400100 for _ in 0..testloops {
Allen Georgebc1344d2017-04-28 10:22:03 -0400101 make_thrift_calls(&mut thrift_test_client, &mut second_service_client)?
Allen George8b96bfb2016-11-02 08:01:08 -0400102 }
103
104 Ok(())
105}
106
Allen Georgebc1344d2017-04-28 10:22:03 -0400107fn build_protocols(
108 host: &str,
109 port: u16,
110 transport: &str,
111 protocol: &str,
112 service_name: &str,
Allen Georgeb0d14132020-03-29 11:48:55 -0400113) -> thrift::Result<(Box<dyn TInputProtocol>, Box<dyn TOutputProtocol>)> {
Allen Georgebc1344d2017-04-28 10:22:03 -0400114 let (i_chan, o_chan) = tcp_channel(host, port)?;
115
Allen Georgeb0d14132020-03-29 11:48:55 -0400116 let (i_tran, o_tran): (Box<dyn TReadTransport>, Box<dyn TWriteTransport>) = match transport {
Allen Georgebc1344d2017-04-28 10:22:03 -0400117 "buffered" => {
118 (Box::new(TBufferedReadTransport::new(i_chan)),
119 Box::new(TBufferedWriteTransport::new(o_chan)))
120 }
121 "framed" => {
122 (Box::new(TFramedReadTransport::new(i_chan)),
123 Box::new(TFramedWriteTransport::new(o_chan)))
124 }
125 unmatched => return Err(format!("unsupported transport {}", unmatched).into()),
126 };
127
Allen Georgeb0d14132020-03-29 11:48:55 -0400128 let (i_prot, o_prot): (Box<dyn TInputProtocol>, Box<dyn TOutputProtocol>) = match protocol {
James E. King, III39eaae62017-11-19 20:17:33 -0500129 "binary" => {
Allen Georgebc1344d2017-04-28 10:22:03 -0400130 (Box::new(TBinaryInputProtocol::new(i_tran, true)),
131 Box::new(TBinaryOutputProtocol::new(o_tran, true)))
132 }
133 "multi" => {
134 (Box::new(TBinaryInputProtocol::new(i_tran, true)),
135 Box::new(
136 TMultiplexedOutputProtocol::new(
137 service_name,
138 TBinaryOutputProtocol::new(o_tran, true),
139 ),
140 ))
141 }
James E. King, III39eaae62017-11-19 20:17:33 -0500142 "compact" => {
Allen Georgebc1344d2017-04-28 10:22:03 -0400143 (Box::new(TCompactInputProtocol::new(i_tran)),
144 Box::new(TCompactOutputProtocol::new(o_tran)))
145 }
146 "multic" => {
147 (Box::new(TCompactInputProtocol::new(i_tran)),
148 Box::new(TMultiplexedOutputProtocol::new(service_name, TCompactOutputProtocol::new(o_tran)),))
149 }
150 unmatched => return Err(format!("unsupported protocol {}", unmatched).into()),
151 };
152
153 Ok((i_prot, o_prot))
154}
155
Allen George0e22c362017-01-30 07:15:00 -0500156// FIXME: expose "open" through the client interface so I don't have to early
157// open
158fn tcp_channel(
159 host: &str,
160 port: u16,
161) -> thrift::Result<(ReadHalf<TTcpChannel>, WriteHalf<TTcpChannel>)> {
162 let mut c = TTcpChannel::new();
163 c.open(&format!("{}:{}", host, port))?;
164 c.split()
Allen George8b96bfb2016-11-02 08:01:08 -0400165}
166
Allen Georgeb0d14132020-03-29 11:48:55 -0400167type BuildThriftTestClient = ThriftTestSyncClient<Box<dyn TInputProtocol>, Box<dyn TOutputProtocol>>;
168type BuiltSecondServiceClient = SecondServiceSyncClient<Box<dyn TInputProtocol>, Box<dyn TOutputProtocol>>;
Allen George8b96bfb2016-11-02 08:01:08 -0400169
Allen Georgebc1344d2017-04-28 10:22:03 -0400170#[cfg_attr(feature = "cargo-clippy", allow(cyclomatic_complexity))]
171fn make_thrift_calls(
172 thrift_test_client: &mut BuildThriftTestClient,
173 second_service_client: &mut Option<BuiltSecondServiceClient>,
174) -> Result<(), thrift::Error> {
175 info!("testVoid");
176 thrift_test_client.test_void()?;
Allen George8b96bfb2016-11-02 08:01:08 -0400177
Allen Georgebc1344d2017-04-28 10:22:03 -0400178 info!("testString");
Allen George0e22c362017-01-30 07:15:00 -0500179 verify_expected_result(
Allen Georgebc1344d2017-04-28 10:22:03 -0400180 thrift_test_client.test_string("thing".to_owned()),
181 "thing".to_owned(),
182 )?;
183
184 info!("testBool");
185 verify_expected_result(thrift_test_client.test_bool(true), true)?;
186
187 info!("testBool");
188 verify_expected_result(thrift_test_client.test_bool(false), false)?;
189
190 info!("testByte");
191 verify_expected_result(thrift_test_client.test_byte(42), 42)?;
192
193 info!("testi32");
194 verify_expected_result(thrift_test_client.test_i32(1159348374), 1159348374)?;
195
196 info!("testi64");
197 // try!(verify_expected_result(thrift_test_client.test_i64(-8651829879438294565),
198 // -8651829879438294565));
199 verify_expected_result(
200 thrift_test_client.test_i64(i64::min_value()),
201 i64::min_value(),
202 )?;
203
204 info!("testDouble");
205 verify_expected_result(
206 thrift_test_client.test_double(OrderedFloat::from(42.42)),
Allen George0e22c362017-01-30 07:15:00 -0500207 OrderedFloat::from(42.42),
208 )?;
Allen George8b96bfb2016-11-02 08:01:08 -0400209
Allen Georgebc1344d2017-04-28 10:22:03 -0400210 info!("testTypedef");
Allen George8b96bfb2016-11-02 08:01:08 -0400211 {
212 let u_snd: UserId = 2348;
213 let u_cmp: UserId = 2348;
Allen Georgebc1344d2017-04-28 10:22:03 -0400214 verify_expected_result(thrift_test_client.test_typedef(u_snd), u_cmp)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400215 }
216
Allen Georgebc1344d2017-04-28 10:22:03 -0400217 info!("testEnum");
Allen George8b96bfb2016-11-02 08:01:08 -0400218 {
GREATEST Wiggler EvaR!b57d1262018-11-09 07:54:32 -0500219 verify_expected_result(thrift_test_client.test_enum(Numberz::Two), Numberz::Two)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400220 }
221
Allen Georgebc1344d2017-04-28 10:22:03 -0400222 info!("testBinary");
Allen George8b96bfb2016-11-02 08:01:08 -0400223 {
224 let b_snd = vec![0x77, 0x30, 0x30, 0x74, 0x21, 0x20, 0x52, 0x75, 0x73, 0x74];
225 let b_cmp = vec![0x77, 0x30, 0x30, 0x74, 0x21, 0x20, 0x52, 0x75, 0x73, 0x74];
Allen Georgebc1344d2017-04-28 10:22:03 -0400226 verify_expected_result(thrift_test_client.test_binary(b_snd), b_cmp)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400227 }
228
Allen Georgebc1344d2017-04-28 10:22:03 -0400229 info!("testStruct");
Allen George8b96bfb2016-11-02 08:01:08 -0400230 {
231 let x_snd = Xtruct {
232 string_thing: Some("foo".to_owned()),
233 byte_thing: Some(12),
234 i32_thing: Some(219129),
235 i64_thing: Some(12938492818),
236 };
237 let x_cmp = Xtruct {
238 string_thing: Some("foo".to_owned()),
239 byte_thing: Some(12),
240 i32_thing: Some(219129),
241 i64_thing: Some(12938492818),
242 };
Allen Georgebc1344d2017-04-28 10:22:03 -0400243 verify_expected_result(thrift_test_client.test_struct(x_snd), x_cmp)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400244 }
245
246 // Xtruct again, with optional values
Allen George0e22c362017-01-30 07:15:00 -0500247 // FIXME: apparently the erlang thrift server does not like opt-in-req-out
248 // parameters that are undefined. Joy.
Allen George8b96bfb2016-11-02 08:01:08 -0400249 // {
Allen George0e22c362017-01-30 07:15:00 -0500250 // let x_snd = Xtruct { string_thing: Some("foo".to_owned()), byte_thing: None,
251 // i32_thing: None, i64_thing: Some(12938492818) };
252 // let x_cmp = Xtruct { string_thing: Some("foo".to_owned()), byte_thing:
253 // Some(0), i32_thing: Some(0), i64_thing: Some(12938492818) }; // the C++
254 // server is responding correctly
Allen Georgebc1344d2017-04-28 10:22:03 -0400255 // try!(verify_expected_result(thrift_test_client.test_struct(x_snd), x_cmp));
Allen George8b96bfb2016-11-02 08:01:08 -0400256 // }
257 //
258
Allen Georgebc1344d2017-04-28 10:22:03 -0400259 info!("testNest"); // (FIXME: try Xtruct2 with optional values)
Allen George8b96bfb2016-11-02 08:01:08 -0400260 {
261 let x_snd = Xtruct2 {
262 byte_thing: Some(32),
Allen George0e22c362017-01-30 07:15:00 -0500263 struct_thing: Some(
264 Xtruct {
265 string_thing: Some("foo".to_owned()),
266 byte_thing: Some(1),
267 i32_thing: Some(324382098),
268 i64_thing: Some(12938492818),
269 },
270 ),
Allen George8b96bfb2016-11-02 08:01:08 -0400271 i32_thing: Some(293481098),
272 };
273 let x_cmp = Xtruct2 {
274 byte_thing: Some(32),
Allen George0e22c362017-01-30 07:15:00 -0500275 struct_thing: Some(
276 Xtruct {
277 string_thing: Some("foo".to_owned()),
278 byte_thing: Some(1),
279 i32_thing: Some(324382098),
280 i64_thing: Some(12938492818),
281 },
282 ),
Allen George8b96bfb2016-11-02 08:01:08 -0400283 i32_thing: Some(293481098),
284 };
Allen Georgebc1344d2017-04-28 10:22:03 -0400285 verify_expected_result(thrift_test_client.test_nest(x_snd), x_cmp)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400286 }
287
Allen Georgebc1344d2017-04-28 10:22:03 -0400288 // do the multiplexed calls while making the main ThriftTest calls
289 if let Some(ref mut client) = second_service_client.as_mut() {
Allen Georgebc1344d2017-04-28 10:22:03 -0400290 info!("SecondService secondtestString");
291 {
292 verify_expected_result(
293 client.secondtest_string("test_string".to_owned()),
294 "testString(\"test_string\")".to_owned(),
295 )?;
296 }
297 }
298
299 info!("testList");
Allen George8b96bfb2016-11-02 08:01:08 -0400300 {
301 let mut v_snd: Vec<i32> = Vec::new();
302 v_snd.push(29384);
303 v_snd.push(238);
304 v_snd.push(32498);
305
306 let mut v_cmp: Vec<i32> = Vec::new();
307 v_cmp.push(29384);
308 v_cmp.push(238);
309 v_cmp.push(32498);
310
Allen Georgebc1344d2017-04-28 10:22:03 -0400311 verify_expected_result(thrift_test_client.test_list(v_snd), v_cmp)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400312 }
313
Allen Georgebc1344d2017-04-28 10:22:03 -0400314 info!("testSet");
Allen George8b96bfb2016-11-02 08:01:08 -0400315 {
316 let mut s_snd: BTreeSet<i32> = BTreeSet::new();
317 s_snd.insert(293481);
318 s_snd.insert(23);
319 s_snd.insert(3234);
320
321 let mut s_cmp: BTreeSet<i32> = BTreeSet::new();
322 s_cmp.insert(293481);
323 s_cmp.insert(23);
324 s_cmp.insert(3234);
325
Allen Georgebc1344d2017-04-28 10:22:03 -0400326 verify_expected_result(thrift_test_client.test_set(s_snd), s_cmp)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400327 }
328
Allen Georgebc1344d2017-04-28 10:22:03 -0400329 info!("testMap");
Allen George8b96bfb2016-11-02 08:01:08 -0400330 {
331 let mut m_snd: BTreeMap<i32, i32> = BTreeMap::new();
332 m_snd.insert(2, 4);
333 m_snd.insert(4, 6);
334 m_snd.insert(8, 7);
335
336 let mut m_cmp: BTreeMap<i32, i32> = BTreeMap::new();
337 m_cmp.insert(2, 4);
338 m_cmp.insert(4, 6);
339 m_cmp.insert(8, 7);
340
Allen Georgebc1344d2017-04-28 10:22:03 -0400341 verify_expected_result(thrift_test_client.test_map(m_snd), m_cmp)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400342 }
343
Allen Georgebc1344d2017-04-28 10:22:03 -0400344 info!("testStringMap");
Allen George8b96bfb2016-11-02 08:01:08 -0400345 {
346 let mut m_snd: BTreeMap<String, String> = BTreeMap::new();
347 m_snd.insert("2".to_owned(), "4_string".to_owned());
348 m_snd.insert("4".to_owned(), "6_string".to_owned());
349 m_snd.insert("8".to_owned(), "7_string".to_owned());
350
351 let mut m_rcv: BTreeMap<String, String> = BTreeMap::new();
352 m_rcv.insert("2".to_owned(), "4_string".to_owned());
353 m_rcv.insert("4".to_owned(), "6_string".to_owned());
354 m_rcv.insert("8".to_owned(), "7_string".to_owned());
355
Allen Georgebc1344d2017-04-28 10:22:03 -0400356 verify_expected_result(thrift_test_client.test_string_map(m_snd), m_rcv)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400357 }
358
359 // nested map
Allen George0e22c362017-01-30 07:15:00 -0500360 // expect : {-4 => {-4 => -4, -3 => -3, -2 => -2, -1 => -1, }, 4 => {1 => 1, 2
361 // => 2, 3 => 3, 4 => 4, }, }
Allen Georgebc1344d2017-04-28 10:22:03 -0400362 info!("testMapMap");
Allen George8b96bfb2016-11-02 08:01:08 -0400363 {
364 let mut m_cmp_nested_0: BTreeMap<i32, i32> = BTreeMap::new();
365 for i in (-4 as i32)..0 {
366 m_cmp_nested_0.insert(i, i);
367 }
368 let mut m_cmp_nested_1: BTreeMap<i32, i32> = BTreeMap::new();
369 for i in 1..5 {
370 m_cmp_nested_1.insert(i, i);
371 }
372
373 let mut m_cmp: BTreeMap<i32, BTreeMap<i32, i32>> = BTreeMap::new();
374 m_cmp.insert(-4, m_cmp_nested_0);
375 m_cmp.insert(4, m_cmp_nested_1);
376
Allen Georgebc1344d2017-04-28 10:22:03 -0400377 verify_expected_result(thrift_test_client.test_map_map(42), m_cmp)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400378 }
379
Allen Georgebc1344d2017-04-28 10:22:03 -0400380 info!("testMulti");
Allen George8b96bfb2016-11-02 08:01:08 -0400381 {
382 let mut m_snd: BTreeMap<i16, String> = BTreeMap::new();
383 m_snd.insert(1298, "fizz".to_owned());
384 m_snd.insert(-148, "buzz".to_owned());
385
386 let s_cmp = Xtruct {
387 string_thing: Some("Hello2".to_owned()),
388 byte_thing: Some(1),
389 i32_thing: Some(-123948),
390 i64_thing: Some(-19234123981),
391 };
392
Allen George0e22c362017-01-30 07:15:00 -0500393 verify_expected_result(
GREATEST Wiggler EvaR!b57d1262018-11-09 07:54:32 -0500394 thrift_test_client.test_multi(1, -123948, -19234123981, m_snd, Numberz::Eight, 81),
Allen George0e22c362017-01-30 07:15:00 -0500395 s_cmp,
396 )?;
Allen George8b96bfb2016-11-02 08:01:08 -0400397 }
398
399 // Insanity
400 // returns:
401 // { 1 => { 2 => argument,
402 // 3 => argument,
403 // },
404 // 2 => { 6 => <empty Insanity struct>, },
405 // }
406 {
407 let mut arg_map_usermap: BTreeMap<Numberz, i64> = BTreeMap::new();
GREATEST Wiggler EvaR!b57d1262018-11-09 07:54:32 -0500408 arg_map_usermap.insert(Numberz::One, 4289);
409 arg_map_usermap.insert(Numberz::Eight, 19);
Allen George8b96bfb2016-11-02 08:01:08 -0400410
411 let mut arg_vec_xtructs: Vec<Xtruct> = Vec::new();
Allen George0e22c362017-01-30 07:15:00 -0500412 arg_vec_xtructs.push(
413 Xtruct {
414 string_thing: Some("foo".to_owned()),
415 byte_thing: Some(8),
416 i32_thing: Some(29),
417 i64_thing: Some(92384),
418 },
419 );
420 arg_vec_xtructs.push(
421 Xtruct {
422 string_thing: Some("bar".to_owned()),
423 byte_thing: Some(28),
424 i32_thing: Some(2),
425 i64_thing: Some(-1281),
426 },
427 );
428 arg_vec_xtructs.push(
429 Xtruct {
430 string_thing: Some("baz".to_owned()),
431 byte_thing: Some(0),
432 i32_thing: Some(3948539),
433 i64_thing: Some(-12938492),
434 },
435 );
Allen George8b96bfb2016-11-02 08:01:08 -0400436
437 let mut s_cmp_nested_1: BTreeMap<Numberz, Insanity> = BTreeMap::new();
438 let insanity = Insanity {
439 user_map: Some(arg_map_usermap),
440 xtructs: Some(arg_vec_xtructs),
441 };
GREATEST Wiggler EvaR!b57d1262018-11-09 07:54:32 -0500442 s_cmp_nested_1.insert(Numberz::Two, insanity.clone());
443 s_cmp_nested_1.insert(Numberz::Three, insanity.clone());
Allen George8b96bfb2016-11-02 08:01:08 -0400444
445 let mut s_cmp_nested_2: BTreeMap<Numberz, Insanity> = BTreeMap::new();
446 let empty_insanity = Insanity {
447 user_map: Some(BTreeMap::new()),
448 xtructs: Some(Vec::new()),
449 };
GREATEST Wiggler EvaR!b57d1262018-11-09 07:54:32 -0500450 s_cmp_nested_2.insert(Numberz::Six, empty_insanity);
Allen George8b96bfb2016-11-02 08:01:08 -0400451
452 let mut s_cmp: BTreeMap<UserId, BTreeMap<Numberz, Insanity>> = BTreeMap::new();
453 s_cmp.insert(1 as UserId, s_cmp_nested_1);
454 s_cmp.insert(2 as UserId, s_cmp_nested_2);
455
Allen Georgebc1344d2017-04-28 10:22:03 -0400456 verify_expected_result(thrift_test_client.test_insanity(insanity.clone()), s_cmp)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400457 }
458
Allen Georgebc1344d2017-04-28 10:22:03 -0400459 info!("testException - remote throws Xception");
Allen George8b96bfb2016-11-02 08:01:08 -0400460 {
Allen Georgebc1344d2017-04-28 10:22:03 -0400461 let r = thrift_test_client.test_exception("Xception".to_owned());
Allen George8b96bfb2016-11-02 08:01:08 -0400462 let x = match r {
463 Err(thrift::Error::User(ref e)) => {
464 match e.downcast_ref::<Xception>() {
465 Some(x) => Ok(x),
Allen George0e22c362017-01-30 07:15:00 -0500466 None => Err(thrift::Error::User("did not get expected Xception struct".into()),),
Allen George8b96bfb2016-11-02 08:01:08 -0400467 }
468 }
469 _ => Err(thrift::Error::User("did not get exception".into())),
470 }?;
471
472 let x_cmp = Xception {
473 error_code: Some(1001),
474 message: Some("Xception".to_owned()),
475 };
476
477 verify_expected_result(Ok(x), &x_cmp)?;
478 }
479
Allen Georgebc1344d2017-04-28 10:22:03 -0400480 info!("testException - remote throws TApplicationException");
Allen George8b96bfb2016-11-02 08:01:08 -0400481 {
Allen Georgebc1344d2017-04-28 10:22:03 -0400482 let r = thrift_test_client.test_exception("TException".to_owned());
Allen George8b96bfb2016-11-02 08:01:08 -0400483 match r {
484 Err(thrift::Error::Application(ref e)) => {
Allen Georgebc1344d2017-04-28 10:22:03 -0400485 info!("received an {:?}", e);
Allen George8b96bfb2016-11-02 08:01:08 -0400486 Ok(())
487 }
488 _ => Err(thrift::Error::User("did not get exception".into())),
489 }?;
490 }
491
Allen Georgebc1344d2017-04-28 10:22:03 -0400492 info!("testException - remote succeeds");
Allen George8b96bfb2016-11-02 08:01:08 -0400493 {
Allen Georgebc1344d2017-04-28 10:22:03 -0400494 let r = thrift_test_client.test_exception("foo".to_owned());
Allen George8b96bfb2016-11-02 08:01:08 -0400495 match r {
496 Ok(_) => Ok(()),
497 _ => Err(thrift::Error::User("received an exception".into())),
498 }?;
499 }
500
Allen Georgebc1344d2017-04-28 10:22:03 -0400501 info!("testMultiException - remote throws Xception");
Allen George8b96bfb2016-11-02 08:01:08 -0400502 {
Allen Georgebc1344d2017-04-28 10:22:03 -0400503 let r =
504 thrift_test_client.test_multi_exception("Xception".to_owned(), "ignored".to_owned());
Allen George8b96bfb2016-11-02 08:01:08 -0400505 let x = match r {
506 Err(thrift::Error::User(ref e)) => {
507 match e.downcast_ref::<Xception>() {
508 Some(x) => Ok(x),
Allen George0e22c362017-01-30 07:15:00 -0500509 None => Err(thrift::Error::User("did not get expected Xception struct".into()),),
Allen George8b96bfb2016-11-02 08:01:08 -0400510 }
511 }
512 _ => Err(thrift::Error::User("did not get exception".into())),
513 }?;
514
515 let x_cmp = Xception {
516 error_code: Some(1001),
517 message: Some("This is an Xception".to_owned()),
518 };
519
520 verify_expected_result(Ok(x), &x_cmp)?;
521 }
522
Allen Georgebc1344d2017-04-28 10:22:03 -0400523 info!("testMultiException - remote throws Xception2");
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("Xception2".to_owned(), "ignored".to_owned());
Allen George8b96bfb2016-11-02 08:01:08 -0400527 let x = match r {
528 Err(thrift::Error::User(ref e)) => {
529 match e.downcast_ref::<Xception2>() {
530 Some(x) => Ok(x),
Allen George0e22c362017-01-30 07:15:00 -0500531 None => Err(thrift::Error::User("did not get expected Xception struct".into()),),
Allen George8b96bfb2016-11-02 08:01:08 -0400532 }
533 }
534 _ => Err(thrift::Error::User("did not get exception".into())),
535 }?;
536
537 let x_cmp = Xception2 {
538 error_code: Some(2002),
Allen George0e22c362017-01-30 07:15:00 -0500539 struct_thing: Some(
540 Xtruct {
541 string_thing: Some("This is an Xception2".to_owned()),
542 // since this is an OPT_IN_REQ_OUT field the sender sets a default
543 byte_thing: Some(0),
544 // since this is an OPT_IN_REQ_OUT field the sender sets a default
545 i32_thing: Some(0),
546 // since this is an OPT_IN_REQ_OUT field the sender sets a default
547 i64_thing: Some(0),
548 },
549 ),
Allen George8b96bfb2016-11-02 08:01:08 -0400550 };
551
552 verify_expected_result(Ok(x), &x_cmp)?;
553 }
554
Allen Georgebc1344d2017-04-28 10:22:03 -0400555 info!("testMultiException - remote succeeds");
Allen George8b96bfb2016-11-02 08:01:08 -0400556 {
Allen Georgebc1344d2017-04-28 10:22:03 -0400557 let r = thrift_test_client.test_multi_exception("haha".to_owned(), "RETURNED".to_owned());
Allen George8b96bfb2016-11-02 08:01:08 -0400558 let x = match r {
Allen George0e22c362017-01-30 07:15:00 -0500559 Err(e) => Err(thrift::Error::User(format!("received an unexpected exception {:?}", e).into(),),),
Allen George8b96bfb2016-11-02 08:01:08 -0400560 _ => r,
561 }?;
562
563 let x_cmp = Xtruct {
564 string_thing: Some("RETURNED".to_owned()),
Allen George0e22c362017-01-30 07:15:00 -0500565 // since this is an OPT_IN_REQ_OUT field the sender sets a default
566 byte_thing: Some(0),
567 // since this is an OPT_IN_REQ_OUT field the sender sets a default
568 i32_thing: Some(0),
569 // since this is an OPT_IN_REQ_OUT field the sender sets a default
570 i64_thing: Some(0),
Allen George8b96bfb2016-11-02 08:01:08 -0400571 };
572
573 verify_expected_result(Ok(x), x_cmp)?;
574 }
575
Allen Georgebc1344d2017-04-28 10:22:03 -0400576 info!("testOneWay - remote sleeps for 1 second");
Allen George8b96bfb2016-11-02 08:01:08 -0400577 {
Allen Georgebc1344d2017-04-28 10:22:03 -0400578 thrift_test_client.test_oneway(1)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400579 }
580
Allen George0e22c362017-01-30 07:15:00 -0500581 // final test to verify that the connection is still writable after the one-way
582 // call
Allen Georgebc1344d2017-04-28 10:22:03 -0400583 thrift_test_client.test_void()
Allen George8b96bfb2016-11-02 08:01:08 -0400584}
585
Allen George0e22c362017-01-30 07:15:00 -0500586#[cfg_attr(feature = "cargo-clippy", allow(needless_pass_by_value))]
587fn verify_expected_result<T: Debug + PartialEq + Sized>(
588 actual: Result<T, thrift::Error>,
589 expected: T,
590) -> Result<(), thrift::Error> {
James E. King, III20e16bc2017-11-18 22:37:54 -0500591 info!("*** EXPECTED: Ok({:?})", expected);
592 info!("*** ACTUAL : {:?}", actual);
Allen George8b96bfb2016-11-02 08:01:08 -0400593 match actual {
594 Ok(v) => {
595 if v == expected {
James E. King, III20e16bc2017-11-18 22:37:54 -0500596 info!("*** OK ***");
Allen George8b96bfb2016-11-02 08:01:08 -0400597 Ok(())
598 } else {
James E. King, III20e16bc2017-11-18 22:37:54 -0500599 info!("*** FAILED ***");
Allen George0e22c362017-01-30 07:15:00 -0500600 Err(thrift::Error::User(format!("expected {:?} but got {:?}", &expected, &v).into()),)
Allen George8b96bfb2016-11-02 08:01:08 -0400601 }
602 }
603 Err(e) => Err(e),
604 }
605}