blob: d720313e903c96c39ad9b61c2a1c5e46f78777c2 [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
57 // --named-pipe
58 // --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\")")
68 (@arg protocol: --protocol +takes_value "Thrift protocol implementation to use (\"binary\", \"compact\")")
69 (@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,
113) -> thrift::Result<(Box<TInputProtocol>, Box<TOutputProtocol>)> {
114 let (i_chan, o_chan) = tcp_channel(host, port)?;
115
116 let (i_tran, o_tran): (Box<TReadTransport>, Box<TWriteTransport>) = match transport {
117 "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
128 let (i_prot, o_prot): (Box<TInputProtocol>, Box<TOutputProtocol>) = match protocol {
129 "binary" | "multi:binary" => {
130 (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 }
142 "compact" | "multi:compact" => {
143 (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 Georgebc1344d2017-04-28 10:22:03 -0400167type BuildThriftTestClient = ThriftTestSyncClient<Box<TInputProtocol>, Box<TOutputProtocol>>;
168type BuiltSecondServiceClient = SecondServiceSyncClient<Box<TInputProtocol>, Box<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 {
Allen Georgebc1344d2017-04-28 10:22:03 -0400219 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() {
290 info!("SecondService blahBlah");
291 {
292 let r = client.blah_blah();
293 match r {
294 Err(thrift::Error::Application(ref e)) => {
295 info!("received an {:?}", e);
296 Ok(())
297 }
298 _ => Err(thrift::Error::User("did not get exception".into())),
299 }?;
300 }
301
302 info!("SecondService secondtestString");
303 {
304 verify_expected_result(
305 client.secondtest_string("test_string".to_owned()),
306 "testString(\"test_string\")".to_owned(),
307 )?;
308 }
309 }
310
311 info!("testList");
Allen George8b96bfb2016-11-02 08:01:08 -0400312 {
313 let mut v_snd: Vec<i32> = Vec::new();
314 v_snd.push(29384);
315 v_snd.push(238);
316 v_snd.push(32498);
317
318 let mut v_cmp: Vec<i32> = Vec::new();
319 v_cmp.push(29384);
320 v_cmp.push(238);
321 v_cmp.push(32498);
322
Allen Georgebc1344d2017-04-28 10:22:03 -0400323 verify_expected_result(thrift_test_client.test_list(v_snd), v_cmp)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400324 }
325
Allen Georgebc1344d2017-04-28 10:22:03 -0400326 info!("testSet");
Allen George8b96bfb2016-11-02 08:01:08 -0400327 {
328 let mut s_snd: BTreeSet<i32> = BTreeSet::new();
329 s_snd.insert(293481);
330 s_snd.insert(23);
331 s_snd.insert(3234);
332
333 let mut s_cmp: BTreeSet<i32> = BTreeSet::new();
334 s_cmp.insert(293481);
335 s_cmp.insert(23);
336 s_cmp.insert(3234);
337
Allen Georgebc1344d2017-04-28 10:22:03 -0400338 verify_expected_result(thrift_test_client.test_set(s_snd), s_cmp)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400339 }
340
Allen Georgebc1344d2017-04-28 10:22:03 -0400341 info!("testMap");
Allen George8b96bfb2016-11-02 08:01:08 -0400342 {
343 let mut m_snd: BTreeMap<i32, i32> = BTreeMap::new();
344 m_snd.insert(2, 4);
345 m_snd.insert(4, 6);
346 m_snd.insert(8, 7);
347
348 let mut m_cmp: BTreeMap<i32, i32> = BTreeMap::new();
349 m_cmp.insert(2, 4);
350 m_cmp.insert(4, 6);
351 m_cmp.insert(8, 7);
352
Allen Georgebc1344d2017-04-28 10:22:03 -0400353 verify_expected_result(thrift_test_client.test_map(m_snd), m_cmp)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400354 }
355
Allen Georgebc1344d2017-04-28 10:22:03 -0400356 info!("testStringMap");
Allen George8b96bfb2016-11-02 08:01:08 -0400357 {
358 let mut m_snd: BTreeMap<String, String> = BTreeMap::new();
359 m_snd.insert("2".to_owned(), "4_string".to_owned());
360 m_snd.insert("4".to_owned(), "6_string".to_owned());
361 m_snd.insert("8".to_owned(), "7_string".to_owned());
362
363 let mut m_rcv: BTreeMap<String, String> = BTreeMap::new();
364 m_rcv.insert("2".to_owned(), "4_string".to_owned());
365 m_rcv.insert("4".to_owned(), "6_string".to_owned());
366 m_rcv.insert("8".to_owned(), "7_string".to_owned());
367
Allen Georgebc1344d2017-04-28 10:22:03 -0400368 verify_expected_result(thrift_test_client.test_string_map(m_snd), m_rcv)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400369 }
370
371 // nested map
Allen George0e22c362017-01-30 07:15:00 -0500372 // expect : {-4 => {-4 => -4, -3 => -3, -2 => -2, -1 => -1, }, 4 => {1 => 1, 2
373 // => 2, 3 => 3, 4 => 4, }, }
Allen Georgebc1344d2017-04-28 10:22:03 -0400374 info!("testMapMap");
Allen George8b96bfb2016-11-02 08:01:08 -0400375 {
376 let mut m_cmp_nested_0: BTreeMap<i32, i32> = BTreeMap::new();
377 for i in (-4 as i32)..0 {
378 m_cmp_nested_0.insert(i, i);
379 }
380 let mut m_cmp_nested_1: BTreeMap<i32, i32> = BTreeMap::new();
381 for i in 1..5 {
382 m_cmp_nested_1.insert(i, i);
383 }
384
385 let mut m_cmp: BTreeMap<i32, BTreeMap<i32, i32>> = BTreeMap::new();
386 m_cmp.insert(-4, m_cmp_nested_0);
387 m_cmp.insert(4, m_cmp_nested_1);
388
Allen Georgebc1344d2017-04-28 10:22:03 -0400389 verify_expected_result(thrift_test_client.test_map_map(42), m_cmp)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400390 }
391
Allen Georgebc1344d2017-04-28 10:22:03 -0400392 info!("testMulti");
Allen George8b96bfb2016-11-02 08:01:08 -0400393 {
394 let mut m_snd: BTreeMap<i16, String> = BTreeMap::new();
395 m_snd.insert(1298, "fizz".to_owned());
396 m_snd.insert(-148, "buzz".to_owned());
397
398 let s_cmp = Xtruct {
399 string_thing: Some("Hello2".to_owned()),
400 byte_thing: Some(1),
401 i32_thing: Some(-123948),
402 i64_thing: Some(-19234123981),
403 };
404
Allen George0e22c362017-01-30 07:15:00 -0500405 verify_expected_result(
Allen Georgebc1344d2017-04-28 10:22:03 -0400406 thrift_test_client.test_multi(1, -123948, -19234123981, m_snd, Numberz::EIGHT, 81),
Allen George0e22c362017-01-30 07:15:00 -0500407 s_cmp,
408 )?;
Allen George8b96bfb2016-11-02 08:01:08 -0400409 }
410
411 // Insanity
412 // returns:
413 // { 1 => { 2 => argument,
414 // 3 => argument,
415 // },
416 // 2 => { 6 => <empty Insanity struct>, },
417 // }
418 {
419 let mut arg_map_usermap: BTreeMap<Numberz, i64> = BTreeMap::new();
420 arg_map_usermap.insert(Numberz::ONE, 4289);
421 arg_map_usermap.insert(Numberz::EIGHT, 19);
422
423 let mut arg_vec_xtructs: Vec<Xtruct> = Vec::new();
Allen George0e22c362017-01-30 07:15:00 -0500424 arg_vec_xtructs.push(
425 Xtruct {
426 string_thing: Some("foo".to_owned()),
427 byte_thing: Some(8),
428 i32_thing: Some(29),
429 i64_thing: Some(92384),
430 },
431 );
432 arg_vec_xtructs.push(
433 Xtruct {
434 string_thing: Some("bar".to_owned()),
435 byte_thing: Some(28),
436 i32_thing: Some(2),
437 i64_thing: Some(-1281),
438 },
439 );
440 arg_vec_xtructs.push(
441 Xtruct {
442 string_thing: Some("baz".to_owned()),
443 byte_thing: Some(0),
444 i32_thing: Some(3948539),
445 i64_thing: Some(-12938492),
446 },
447 );
Allen George8b96bfb2016-11-02 08:01:08 -0400448
449 let mut s_cmp_nested_1: BTreeMap<Numberz, Insanity> = BTreeMap::new();
450 let insanity = Insanity {
451 user_map: Some(arg_map_usermap),
452 xtructs: Some(arg_vec_xtructs),
453 };
454 s_cmp_nested_1.insert(Numberz::TWO, insanity.clone());
455 s_cmp_nested_1.insert(Numberz::THREE, insanity.clone());
456
457 let mut s_cmp_nested_2: BTreeMap<Numberz, Insanity> = BTreeMap::new();
458 let empty_insanity = Insanity {
459 user_map: Some(BTreeMap::new()),
460 xtructs: Some(Vec::new()),
461 };
462 s_cmp_nested_2.insert(Numberz::SIX, empty_insanity);
463
464 let mut s_cmp: BTreeMap<UserId, BTreeMap<Numberz, Insanity>> = BTreeMap::new();
465 s_cmp.insert(1 as UserId, s_cmp_nested_1);
466 s_cmp.insert(2 as UserId, s_cmp_nested_2);
467
Allen Georgebc1344d2017-04-28 10:22:03 -0400468 verify_expected_result(thrift_test_client.test_insanity(insanity.clone()), s_cmp)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400469 }
470
Allen Georgebc1344d2017-04-28 10:22:03 -0400471 info!("testException - remote throws Xception");
Allen George8b96bfb2016-11-02 08:01:08 -0400472 {
Allen Georgebc1344d2017-04-28 10:22:03 -0400473 let r = thrift_test_client.test_exception("Xception".to_owned());
Allen George8b96bfb2016-11-02 08:01:08 -0400474 let x = match r {
475 Err(thrift::Error::User(ref e)) => {
476 match e.downcast_ref::<Xception>() {
477 Some(x) => Ok(x),
Allen George0e22c362017-01-30 07:15:00 -0500478 None => Err(thrift::Error::User("did not get expected Xception struct".into()),),
Allen George8b96bfb2016-11-02 08:01:08 -0400479 }
480 }
481 _ => Err(thrift::Error::User("did not get exception".into())),
482 }?;
483
484 let x_cmp = Xception {
485 error_code: Some(1001),
486 message: Some("Xception".to_owned()),
487 };
488
489 verify_expected_result(Ok(x), &x_cmp)?;
490 }
491
Allen Georgebc1344d2017-04-28 10:22:03 -0400492 info!("testException - remote throws TApplicationException");
Allen George8b96bfb2016-11-02 08:01:08 -0400493 {
Allen Georgebc1344d2017-04-28 10:22:03 -0400494 let r = thrift_test_client.test_exception("TException".to_owned());
Allen George8b96bfb2016-11-02 08:01:08 -0400495 match r {
496 Err(thrift::Error::Application(ref e)) => {
Allen Georgebc1344d2017-04-28 10:22:03 -0400497 info!("received an {:?}", e);
Allen George8b96bfb2016-11-02 08:01:08 -0400498 Ok(())
499 }
500 _ => Err(thrift::Error::User("did not get exception".into())),
501 }?;
502 }
503
Allen Georgebc1344d2017-04-28 10:22:03 -0400504 info!("testException - remote succeeds");
Allen George8b96bfb2016-11-02 08:01:08 -0400505 {
Allen Georgebc1344d2017-04-28 10:22:03 -0400506 let r = thrift_test_client.test_exception("foo".to_owned());
Allen George8b96bfb2016-11-02 08:01:08 -0400507 match r {
508 Ok(_) => Ok(()),
509 _ => Err(thrift::Error::User("received an exception".into())),
510 }?;
511 }
512
Allen Georgebc1344d2017-04-28 10:22:03 -0400513 info!("testMultiException - remote throws Xception");
Allen George8b96bfb2016-11-02 08:01:08 -0400514 {
Allen Georgebc1344d2017-04-28 10:22:03 -0400515 let r =
516 thrift_test_client.test_multi_exception("Xception".to_owned(), "ignored".to_owned());
Allen George8b96bfb2016-11-02 08:01:08 -0400517 let x = match r {
518 Err(thrift::Error::User(ref e)) => {
519 match e.downcast_ref::<Xception>() {
520 Some(x) => Ok(x),
Allen George0e22c362017-01-30 07:15:00 -0500521 None => Err(thrift::Error::User("did not get expected Xception struct".into()),),
Allen George8b96bfb2016-11-02 08:01:08 -0400522 }
523 }
524 _ => Err(thrift::Error::User("did not get exception".into())),
525 }?;
526
527 let x_cmp = Xception {
528 error_code: Some(1001),
529 message: Some("This is an Xception".to_owned()),
530 };
531
532 verify_expected_result(Ok(x), &x_cmp)?;
533 }
534
Allen Georgebc1344d2017-04-28 10:22:03 -0400535 info!("testMultiException - remote throws Xception2");
Allen George8b96bfb2016-11-02 08:01:08 -0400536 {
Allen Georgebc1344d2017-04-28 10:22:03 -0400537 let r =
538 thrift_test_client.test_multi_exception("Xception2".to_owned(), "ignored".to_owned());
Allen George8b96bfb2016-11-02 08:01:08 -0400539 let x = match r {
540 Err(thrift::Error::User(ref e)) => {
541 match e.downcast_ref::<Xception2>() {
542 Some(x) => Ok(x),
Allen George0e22c362017-01-30 07:15:00 -0500543 None => Err(thrift::Error::User("did not get expected Xception struct".into()),),
Allen George8b96bfb2016-11-02 08:01:08 -0400544 }
545 }
546 _ => Err(thrift::Error::User("did not get exception".into())),
547 }?;
548
549 let x_cmp = Xception2 {
550 error_code: Some(2002),
Allen George0e22c362017-01-30 07:15:00 -0500551 struct_thing: Some(
552 Xtruct {
553 string_thing: Some("This is an Xception2".to_owned()),
554 // since this is an OPT_IN_REQ_OUT field the sender sets a default
555 byte_thing: Some(0),
556 // since this is an OPT_IN_REQ_OUT field the sender sets a default
557 i32_thing: Some(0),
558 // since this is an OPT_IN_REQ_OUT field the sender sets a default
559 i64_thing: Some(0),
560 },
561 ),
Allen George8b96bfb2016-11-02 08:01:08 -0400562 };
563
564 verify_expected_result(Ok(x), &x_cmp)?;
565 }
566
Allen Georgebc1344d2017-04-28 10:22:03 -0400567 info!("testMultiException - remote succeeds");
Allen George8b96bfb2016-11-02 08:01:08 -0400568 {
Allen Georgebc1344d2017-04-28 10:22:03 -0400569 let r = thrift_test_client.test_multi_exception("haha".to_owned(), "RETURNED".to_owned());
Allen George8b96bfb2016-11-02 08:01:08 -0400570 let x = match r {
Allen George0e22c362017-01-30 07:15:00 -0500571 Err(e) => Err(thrift::Error::User(format!("received an unexpected exception {:?}", e).into(),),),
Allen George8b96bfb2016-11-02 08:01:08 -0400572 _ => r,
573 }?;
574
575 let x_cmp = Xtruct {
576 string_thing: Some("RETURNED".to_owned()),
Allen George0e22c362017-01-30 07:15:00 -0500577 // since this is an OPT_IN_REQ_OUT field the sender sets a default
578 byte_thing: Some(0),
579 // since this is an OPT_IN_REQ_OUT field the sender sets a default
580 i32_thing: Some(0),
581 // since this is an OPT_IN_REQ_OUT field the sender sets a default
582 i64_thing: Some(0),
Allen George8b96bfb2016-11-02 08:01:08 -0400583 };
584
585 verify_expected_result(Ok(x), x_cmp)?;
586 }
587
Allen Georgebc1344d2017-04-28 10:22:03 -0400588 info!("testOneWay - remote sleeps for 1 second");
Allen George8b96bfb2016-11-02 08:01:08 -0400589 {
Allen Georgebc1344d2017-04-28 10:22:03 -0400590 thrift_test_client.test_oneway(1)?;
Allen George8b96bfb2016-11-02 08:01:08 -0400591 }
592
Allen George0e22c362017-01-30 07:15:00 -0500593 // final test to verify that the connection is still writable after the one-way
594 // call
Allen Georgebc1344d2017-04-28 10:22:03 -0400595 thrift_test_client.test_void()
Allen George8b96bfb2016-11-02 08:01:08 -0400596}
597
Allen George0e22c362017-01-30 07:15:00 -0500598#[cfg_attr(feature = "cargo-clippy", allow(needless_pass_by_value))]
599fn verify_expected_result<T: Debug + PartialEq + Sized>(
600 actual: Result<T, thrift::Error>,
601 expected: T,
602) -> Result<(), thrift::Error> {
Allen George8b96bfb2016-11-02 08:01:08 -0400603 match actual {
604 Ok(v) => {
605 if v == expected {
606 Ok(())
607 } else {
Allen George0e22c362017-01-30 07:15:00 -0500608 Err(thrift::Error::User(format!("expected {:?} but got {:?}", &expected, &v).into()),)
Allen George8b96bfb2016-11-02 08:01:08 -0400609 }
610 }
611 Err(e) => Err(e),
612 }
613}