blob: ceea8389dd8e0d0a0ef9d9d47db6572e36c0d459 [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
Roger Meierd3b9dca2011-06-24 14:01:10 +000020#define __STDC_FORMAT_MACROS
21#include <inttypes.h>
22
Roger Meierca142b02011-06-07 17:59:07 +000023#include <iostream>
Mark Sleee8540632006-05-30 09:24:40 +000024#include <unistd.h>
Mark Slee95771002006-06-07 06:53:25 +000025#include <sys/time.h>
Marc Slemko6be374b2006-08-04 03:16:25 +000026#include <protocol/TBinaryProtocol.h>
Roger Meierca142b02011-06-07 17:59:07 +000027#include <protocol/TJSONProtocol.h>
28#include <transport/THttpClient.h>
Mark Sleea3302652006-10-25 19:03:32 +000029#include <transport/TTransportUtils.h>
Marc Slemko6be374b2006-08-04 03:16:25 +000030#include <transport/TSocket.h>
Bryan Duxburycd9aea12011-02-22 18:12:06 +000031#include <transport/TSSLSocket.h>
Mark Sleee8540632006-05-30 09:24:40 +000032
Marc Slemko6be374b2006-08-04 03:16:25 +000033#include <boost/shared_ptr.hpp>
Roger Meierca142b02011-06-07 17:59:07 +000034#include <boost/program_options.hpp>
35
Marc Slemko6be374b2006-08-04 03:16:25 +000036#include "ThriftTest.h"
37
38using namespace boost;
39using namespace std;
T Jake Lucianib5e62212009-01-31 22:36:20 +000040using namespace apache::thrift;
41using namespace apache::thrift::protocol;
42using namespace apache::thrift::transport;
Marc Slemkobf4fd192006-08-15 21:29:39 +000043using namespace thrift::test;
Marc Slemko6be374b2006-08-04 03:16:25 +000044
45//extern uint32_t g_socket_syscalls;
Mark Slee95771002006-06-07 06:53:25 +000046
47// Current time, microseconds since the epoch
48uint64_t now()
49{
Roger Meier5f9614c2010-11-21 16:59:05 +000050 int64_t ret;
Mark Slee95771002006-06-07 06:53:25 +000051 struct timeval tv;
David Reiss0c90f6f2008-02-06 22:18:40 +000052
Mark Slee95771002006-06-07 06:53:25 +000053 gettimeofday(&tv, NULL);
54 ret = tv.tv_sec;
55 ret = ret*1000*1000 + tv.tv_usec;
56 return ret;
57}
58
Mark Sleee8540632006-05-30 09:24:40 +000059int main(int argc, char** argv) {
60 string host = "localhost";
61 int port = 9090;
62 int numTests = 1;
Bryan Duxburycd9aea12011-02-22 18:12:06 +000063 bool ssl = false;
Roger Meierca142b02011-06-07 17:59:07 +000064 string transport_type = "buffered";
65 string protocol_type = "binary";
66 string domain_socket = "";
Mark Sleee8540632006-05-30 09:24:40 +000067
Roger Meierca142b02011-06-07 17:59:07 +000068 program_options::options_description desc("Allowed options");
69 desc.add_options()
70 ("help,h", "produce help message")
71 ("host", program_options::value<string>(&host)->default_value(host), "Host to connect")
72 ("port", program_options::value<int>(&port)->default_value(port), "Port number to connect")
73 ("domain-socket", program_options::value<string>(&domain_socket)->default_value(domain_socket), "Domain Socket (e.g. /tmp/ThriftTest.thrift), instead of host and port")
74 ("transport", program_options::value<string>(&transport_type)->default_value(transport_type), "Transport: buffered, framed, http")
75 ("protocol", program_options::value<string>(&protocol_type)->default_value(protocol_type), "Protocol: binary, json")
76 ("ssl", "Encrypted Transport using SSL")
77 ("testloops,n", program_options::value<int>(&numTests)->default_value(numTests), "Number of Tests")
78 ;
79
80 program_options::variables_map vm;
81 program_options::store(program_options::parse_command_line(argc, argv, desc), vm);
82 program_options::notify(vm);
83
84 if (vm.count("help")) {
85 cout << desc << "\n";
86 return 1;
Mark Sleee8540632006-05-30 09:24:40 +000087 }
Mark Sleea3302652006-10-25 19:03:32 +000088
Roger Meierca142b02011-06-07 17:59:07 +000089 try {
90 if (!protocol_type.empty()) {
91 if (protocol_type == "binary") {
92 } else if (protocol_type == "json") {
93 } else {
94 throw invalid_argument("Unknown protocol type "+protocol_type);
95 }
96 }
97
98 if (!transport_type.empty()) {
99 if (transport_type == "buffered") {
100 } else if (transport_type == "framed") {
101 } else if (transport_type == "http") {
102 } else {
103 throw invalid_argument("Unknown transport type "+transport_type);
104 }
105 }
106
107 } catch (std::exception& e) {
108 cerr << e.what() << endl;
109 cout << desc << "\n";
110 return 1;
111 }
112
113 if (vm.count("ssl")) {
114 ssl = true;
115 }
116
117 shared_ptr<TTransport> transport;
118 shared_ptr<TProtocol> protocol;
119
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000120 shared_ptr<TSocket> socket;
121 shared_ptr<TSSLSocketFactory> factory;
Roger Meierca142b02011-06-07 17:59:07 +0000122
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000123 if (ssl) {
124 factory = shared_ptr<TSSLSocketFactory>(new TSSLSocketFactory());
125 factory->ciphers("ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
126 factory->loadTrustedCertificates("./trusted-ca-certificate.pem");
127 factory->authenticate(true);
128 socket = factory->createSocket(host, port);
129 } else {
Roger Meierca142b02011-06-07 17:59:07 +0000130 if (domain_socket != "") {
131 socket = shared_ptr<TSocket>(new TSocket(domain_socket));
132 port = 0;
133 }
134 else {
135 socket = shared_ptr<TSocket>(new TSocket(host, port));
136 }
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000137 }
Mark Sleea3302652006-10-25 19:03:32 +0000138
Roger Meierca142b02011-06-07 17:59:07 +0000139 if (transport_type.compare("http") == 0) {
140 shared_ptr<TTransport> httpSocket(new THttpClient(socket, host, "/service"));
141 transport = httpSocket;
142 } else if (transport_type.compare("framed") == 0){
Mark Sleea3302652006-10-25 19:03:32 +0000143 shared_ptr<TFramedTransport> framedSocket(new TFramedTransport(socket));
Mark Sleea3302652006-10-25 19:03:32 +0000144 transport = framedSocket;
Roger Meierca142b02011-06-07 17:59:07 +0000145 } else{
Mark Sleea3302652006-10-25 19:03:32 +0000146 shared_ptr<TBufferedTransport> bufferedSocket(new TBufferedTransport(socket));
147 transport = bufferedSocket;
148 }
149
Roger Meierca142b02011-06-07 17:59:07 +0000150 if (protocol_type.compare("json") == 0) {
151 shared_ptr<TProtocol> jsonProtocol(new TJSONProtocol(transport));
152 protocol = jsonProtocol;
153 } else{
154 shared_ptr<TBinaryProtocol> binaryProtocol(new TBinaryProtocol(transport));
155 protocol = binaryProtocol;
156 }
157
158 // Connection info
159 cout << "Connecting (" << transport_type << "/" << protocol_type << ") to: " << domain_socket;
160 if (port != 0) {
161 cout << host << ":" << port;
162 }
163 cout << endl;
164
165 ThriftTestClient testClient(protocol);
Mark Sleed788b2e2006-09-07 01:26:35 +0000166
167 uint64_t time_min = 0;
168 uint64_t time_max = 0;
169 uint64_t time_tot = 0;
David Reiss0c90f6f2008-02-06 22:18:40 +0000170
Mark Sleee8540632006-05-30 09:24:40 +0000171 int test = 0;
172 for (test = 0; test < numTests; ++test) {
Mark Slee95771002006-06-07 06:53:25 +0000173
Mark Slee95771002006-06-07 06:53:25 +0000174 try {
Mark Sleea3302652006-10-25 19:03:32 +0000175 transport->open();
Mark Slee95771002006-06-07 06:53:25 +0000176 } catch (TTransportException& ttx) {
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000177 printf("Connect failed: %s\n", ttx.what());
Mark Sleee8540632006-05-30 09:24:40 +0000178 continue;
179 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000180
Mark Sleed788b2e2006-09-07 01:26:35 +0000181 /**
182 * CONNECT TEST
183 */
184 printf("Test #%d, connect %s:%d\n", test+1, host.c_str(), port);
Mark Slee95771002006-06-07 06:53:25 +0000185
186 uint64_t start = now();
David Reiss0c90f6f2008-02-06 22:18:40 +0000187
Mark Sleee8540632006-05-30 09:24:40 +0000188 /**
189 * VOID TEST
190 */
Mark Sleee129a2d2007-02-21 05:17:48 +0000191 try {
192 printf("testVoid()");
193 testClient.testVoid();
194 printf(" = void\n");
195 } catch (TApplicationException tax) {
196 printf("%s\n", tax.what());
197 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000198
Mark Sleee8540632006-05-30 09:24:40 +0000199 /**
200 * STRING TEST
201 */
202 printf("testString(\"Test\")");
Mark Slee1921d202007-01-24 19:43:06 +0000203 string s;
204 testClient.testString(s, "Test");
Mark Sleee8540632006-05-30 09:24:40 +0000205 printf(" = \"%s\"\n", s.c_str());
David Reiss0c90f6f2008-02-06 22:18:40 +0000206
Mark Sleee8540632006-05-30 09:24:40 +0000207 /**
208 * BYTE TEST
209 */
210 printf("testByte(1)");
211 uint8_t u8 = testClient.testByte(1);
212 printf(" = %d\n", (int)u8);
David Reiss0c90f6f2008-02-06 22:18:40 +0000213
Mark Sleee8540632006-05-30 09:24:40 +0000214 /**
215 * I32 TEST
216 */
217 printf("testI32(-1)");
218 int32_t i32 = testClient.testI32(-1);
219 printf(" = %d\n", i32);
220
221 /**
Mark Sleee8540632006-05-30 09:24:40 +0000222 * I64 TEST
223 */
224 printf("testI64(-34359738368)");
Marc Slemkobf4fd192006-08-15 21:29:39 +0000225 int64_t i64 = testClient.testI64(-34359738368LL);
David Reissbc3dddb2007-08-22 23:20:24 +0000226 printf(" = %"PRId64"\n", i64);
Mark Sleec98d0502006-09-06 02:42:25 +0000227
228 /**
229 * DOUBLE TEST
230 */
231 printf("testDouble(-5.2098523)");
232 double dub = testClient.testDouble(-5.2098523);
233 printf(" = %lf\n", dub);
David Reiss0c90f6f2008-02-06 22:18:40 +0000234
Mark Sleee8540632006-05-30 09:24:40 +0000235 /**
236 * STRUCT TEST
237 */
Mark Slee6e536442006-06-30 18:28:50 +0000238 printf("testStruct({\"Zero\", 1, -3, -5})");
Mark Sleee8540632006-05-30 09:24:40 +0000239 Xtruct out;
240 out.string_thing = "Zero";
241 out.byte_thing = 1;
Mark Sleee8540632006-05-30 09:24:40 +0000242 out.i32_thing = -3;
Mark Sleee8540632006-05-30 09:24:40 +0000243 out.i64_thing = -5;
Mark Slee1921d202007-01-24 19:43:06 +0000244 Xtruct in;
245 testClient.testStruct(in, out);
David Reissbc3dddb2007-08-22 23:20:24 +0000246 printf(" = {\"%s\", %d, %d, %"PRId64"}\n",
Mark Sleee8540632006-05-30 09:24:40 +0000247 in.string_thing.c_str(),
248 (int)in.byte_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000249 in.i32_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000250 in.i64_thing);
David Reiss0c90f6f2008-02-06 22:18:40 +0000251
Mark Sleee8540632006-05-30 09:24:40 +0000252 /**
253 * NESTED STRUCT TEST
254 */
Mark Slee6e536442006-06-30 18:28:50 +0000255 printf("testNest({1, {\"Zero\", 1, -3, -5}), 5}");
Mark Sleee8540632006-05-30 09:24:40 +0000256 Xtruct2 out2;
257 out2.byte_thing = 1;
258 out2.struct_thing = out;
259 out2.i32_thing = 5;
Mark Slee1921d202007-01-24 19:43:06 +0000260 Xtruct2 in2;
261 testClient.testNest(in2, out2);
Mark Sleee8540632006-05-30 09:24:40 +0000262 in = in2.struct_thing;
David Reissbc3dddb2007-08-22 23:20:24 +0000263 printf(" = {%d, {\"%s\", %d, %d, %"PRId64"}, %d}\n",
Mark Sleee8540632006-05-30 09:24:40 +0000264 in2.byte_thing,
265 in.string_thing.c_str(),
266 (int)in.byte_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000267 in.i32_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000268 in.i64_thing,
David Reiss0c90f6f2008-02-06 22:18:40 +0000269 in2.i32_thing);
Mark Sleee8540632006-05-30 09:24:40 +0000270
271 /**
272 * MAP TEST
273 */
274 map<int32_t,int32_t> mapout;
275 for (int32_t i = 0; i < 5; ++i) {
276 mapout.insert(make_pair(i, i-10));
277 }
278 printf("testMap({");
279 map<int32_t, int32_t>::const_iterator m_iter;
280 bool first = true;
281 for (m_iter = mapout.begin(); m_iter != mapout.end(); ++m_iter) {
282 if (first) {
283 first = false;
284 } else {
285 printf(", ");
286 }
287 printf("%d => %d", m_iter->first, m_iter->second);
288 }
289 printf("})");
Mark Slee1921d202007-01-24 19:43:06 +0000290 map<int32_t,int32_t> mapin;
291 testClient.testMap(mapin, mapout);
Mark Sleee8540632006-05-30 09:24:40 +0000292 printf(" = {");
293 first = true;
294 for (m_iter = mapin.begin(); m_iter != mapin.end(); ++m_iter) {
295 if (first) {
296 first = false;
297 } else {
298 printf(", ");
299 }
300 printf("%d => %d", m_iter->first, m_iter->second);
301 }
302 printf("}\n");
303
304 /**
305 * SET TEST
306 */
307 set<int32_t> setout;
308 for (int32_t i = -2; i < 3; ++i) {
309 setout.insert(i);
310 }
311 printf("testSet({");
312 set<int32_t>::const_iterator s_iter;
313 first = true;
314 for (s_iter = setout.begin(); s_iter != setout.end(); ++s_iter) {
315 if (first) {
316 first = false;
317 } else {
318 printf(", ");
319 }
320 printf("%d", *s_iter);
321 }
322 printf("})");
Mark Slee1921d202007-01-24 19:43:06 +0000323 set<int32_t> setin;
324 testClient.testSet(setin, setout);
Mark Sleee8540632006-05-30 09:24:40 +0000325 printf(" = {");
326 first = true;
327 for (s_iter = setin.begin(); s_iter != setin.end(); ++s_iter) {
328 if (first) {
329 first = false;
330 } else {
331 printf(", ");
332 }
333 printf("%d", *s_iter);
334 }
335 printf("}\n");
336
337 /**
338 * LIST TEST
339 */
Mark Sleeb9acf982006-10-10 01:57:32 +0000340 vector<int32_t> listout;
Mark Sleee8540632006-05-30 09:24:40 +0000341 for (int32_t i = -2; i < 3; ++i) {
342 listout.push_back(i);
343 }
344 printf("testList({");
Mark Sleeb9acf982006-10-10 01:57:32 +0000345 vector<int32_t>::const_iterator l_iter;
Mark Sleee8540632006-05-30 09:24:40 +0000346 first = true;
347 for (l_iter = listout.begin(); l_iter != listout.end(); ++l_iter) {
348 if (first) {
349 first = false;
350 } else {
351 printf(", ");
352 }
353 printf("%d", *l_iter);
354 }
355 printf("})");
Mark Slee1921d202007-01-24 19:43:06 +0000356 vector<int32_t> listin;
357 testClient.testList(listin, listout);
Mark Sleee8540632006-05-30 09:24:40 +0000358 printf(" = {");
359 first = true;
360 for (l_iter = listin.begin(); l_iter != listin.end(); ++l_iter) {
361 if (first) {
362 first = false;
363 } else {
364 printf(", ");
365 }
366 printf("%d", *l_iter);
367 }
368 printf("}\n");
369
370 /**
371 * ENUM TEST
372 */
373 printf("testEnum(ONE)");
Bryan Duxbury833ae492010-09-27 17:26:02 +0000374 Numberz::type ret = testClient.testEnum(Numberz::ONE);
Mark Sleee8540632006-05-30 09:24:40 +0000375 printf(" = %d\n", ret);
376
377 printf("testEnum(TWO)");
Bryan Duxbury833ae492010-09-27 17:26:02 +0000378 ret = testClient.testEnum(Numberz::TWO);
Mark Sleee8540632006-05-30 09:24:40 +0000379 printf(" = %d\n", ret);
380
381 printf("testEnum(THREE)");
Bryan Duxbury833ae492010-09-27 17:26:02 +0000382 ret = testClient.testEnum(Numberz::THREE);
Mark Sleee8540632006-05-30 09:24:40 +0000383 printf(" = %d\n", ret);
384
385 printf("testEnum(FIVE)");
Bryan Duxbury833ae492010-09-27 17:26:02 +0000386 ret = testClient.testEnum(Numberz::FIVE);
Mark Sleee8540632006-05-30 09:24:40 +0000387 printf(" = %d\n", ret);
388
389 printf("testEnum(EIGHT)");
Bryan Duxbury833ae492010-09-27 17:26:02 +0000390 ret = testClient.testEnum(Numberz::EIGHT);
Mark Sleee8540632006-05-30 09:24:40 +0000391 printf(" = %d\n", ret);
392
393 /**
394 * TYPEDEF TEST
395 */
396 printf("testTypedef(309858235082523)");
Marc Slemkobf4fd192006-08-15 21:29:39 +0000397 UserId uid = testClient.testTypedef(309858235082523LL);
David Reissbc3dddb2007-08-22 23:20:24 +0000398 printf(" = %"PRId64"\n", uid);
Mark Sleee8540632006-05-30 09:24:40 +0000399
400 /**
401 * NESTED MAP TEST
402 */
403 printf("testMapMap(1)");
Mark Slee1921d202007-01-24 19:43:06 +0000404 map<int32_t, map<int32_t, int32_t> > mm;
405 testClient.testMapMap(mm, 1);
Mark Sleee8540632006-05-30 09:24:40 +0000406 printf(" = {");
407 map<int32_t, map<int32_t, int32_t> >::const_iterator mi;
408 for (mi = mm.begin(); mi != mm.end(); ++mi) {
409 printf("%d => {", mi->first);
410 map<int32_t, int32_t>::const_iterator mi2;
411 for (mi2 = mi->second.begin(); mi2 != mi->second.end(); ++mi2) {
412 printf("%d => %d, ", mi2->first, mi2->second);
413 }
414 printf("}, ");
415 }
416 printf("}\n");
417
418 /**
419 * INSANITY TEST
420 */
421 Insanity insane;
Bryan Duxbury833ae492010-09-27 17:26:02 +0000422 insane.userMap.insert(make_pair(Numberz::FIVE, 5000));
Mark Sleee8540632006-05-30 09:24:40 +0000423 Xtruct truck;
424 truck.string_thing = "Truck";
425 truck.byte_thing = 8;
Mark Sleee8540632006-05-30 09:24:40 +0000426 truck.i32_thing = 8;
Mark Sleee8540632006-05-30 09:24:40 +0000427 truck.i64_thing = 8;
428 insane.xtructs.push_back(truck);
429 printf("testInsanity()");
Bryan Duxbury833ae492010-09-27 17:26:02 +0000430 map<UserId, map<Numberz::type,Insanity> > whoa;
Mark Slee1921d202007-01-24 19:43:06 +0000431 testClient.testInsanity(whoa, insane);
Mark Sleee8540632006-05-30 09:24:40 +0000432 printf(" = {");
Bryan Duxbury833ae492010-09-27 17:26:02 +0000433 map<UserId, map<Numberz::type,Insanity> >::const_iterator i_iter;
Mark Sleee8540632006-05-30 09:24:40 +0000434 for (i_iter = whoa.begin(); i_iter != whoa.end(); ++i_iter) {
David Reissbc3dddb2007-08-22 23:20:24 +0000435 printf("%"PRId64" => {", i_iter->first);
Bryan Duxbury833ae492010-09-27 17:26:02 +0000436 map<Numberz::type,Insanity>::const_iterator i2_iter;
Mark Sleee8540632006-05-30 09:24:40 +0000437 for (i2_iter = i_iter->second.begin();
438 i2_iter != i_iter->second.end();
439 ++i2_iter) {
440 printf("%d => {", i2_iter->first);
Bryan Duxbury833ae492010-09-27 17:26:02 +0000441 map<Numberz::type, UserId> userMap = i2_iter->second.userMap;
442 map<Numberz::type, UserId>::const_iterator um;
Mark Sleee8540632006-05-30 09:24:40 +0000443 printf("{");
444 for (um = userMap.begin(); um != userMap.end(); ++um) {
David Reissbc3dddb2007-08-22 23:20:24 +0000445 printf("%d => %"PRId64", ", um->first, um->second);
Mark Sleee8540632006-05-30 09:24:40 +0000446 }
447 printf("}, ");
448
Mark Sleeb9acf982006-10-10 01:57:32 +0000449 vector<Xtruct> xtructs = i2_iter->second.xtructs;
450 vector<Xtruct>::const_iterator x;
Mark Sleee8540632006-05-30 09:24:40 +0000451 printf("{");
452 for (x = xtructs.begin(); x != xtructs.end(); ++x) {
David Reissbc3dddb2007-08-22 23:20:24 +0000453 printf("{\"%s\", %d, %d, %"PRId64"}, ",
Mark Sleee8540632006-05-30 09:24:40 +0000454 x->string_thing.c_str(),
455 (int)x->byte_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000456 x->i32_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000457 x->i64_thing);
458 }
459 printf("}");
460
461 printf("}, ");
462 }
463 printf("}, ");
464 }
465 printf("}\n");
466
Marc Slemko71d4e472006-08-15 22:34:04 +0000467 /* test exception */
Mark Slee95771002006-06-07 06:53:25 +0000468
Marc Slemkobf4fd192006-08-15 21:29:39 +0000469 try {
Marc Slemko71d4e472006-08-15 22:34:04 +0000470 printf("testClient.testException(\"Xception\") =>");
471 testClient.testException("Xception");
472 printf(" void\nFAILURE\n");
David Reiss0c90f6f2008-02-06 22:18:40 +0000473
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000474 } catch(Xception& e) {
Marc Slemko71d4e472006-08-15 22:34:04 +0000475 printf(" {%u, \"%s\"}\n", e.errorCode, e.message.c_str());
Marc Slemkobf4fd192006-08-15 21:29:39 +0000476 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000477
Marc Slemkobf4fd192006-08-15 21:29:39 +0000478 try {
Marc Slemko71d4e472006-08-15 22:34:04 +0000479 printf("testClient.testException(\"success\") =>");
480 testClient.testException("success");
481 printf(" void\n");
482 } catch(...) {
483 printf(" exception\nFAILURE\n");
484 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000485
Marc Slemko71d4e472006-08-15 22:34:04 +0000486 /* test multi exception */
David Reiss0c90f6f2008-02-06 22:18:40 +0000487
Marc Slemko71d4e472006-08-15 22:34:04 +0000488 try {
489 printf("testClient.testMultiException(\"Xception\", \"test 1\") =>");
Mark Slee1921d202007-01-24 19:43:06 +0000490 Xtruct result;
491 testClient.testMultiException(result, "Xception", "test 1");
Marc Slemko71d4e472006-08-15 22:34:04 +0000492 printf(" result\nFAILURE\n");
Mark Sleed3d733a2006-09-01 22:19:06 +0000493 } catch(Xception& e) {
Marc Slemko71d4e472006-08-15 22:34:04 +0000494 printf(" {%u, \"%s\"}\n", e.errorCode, e.message.c_str());
495 }
496
497 try {
498 printf("testClient.testMultiException(\"Xception2\", \"test 2\") =>");
Mark Slee1921d202007-01-24 19:43:06 +0000499 Xtruct result;
500 testClient.testMultiException(result, "Xception2", "test 2");
Marc Slemko71d4e472006-08-15 22:34:04 +0000501 printf(" result\nFAILURE\n");
David Reiss0c90f6f2008-02-06 22:18:40 +0000502
Mark Sleed3d733a2006-09-01 22:19:06 +0000503 } catch(Xception2& e) {
Marc Slemko71d4e472006-08-15 22:34:04 +0000504 printf(" {%u, {\"%s\"}}\n", e.errorCode, e.struct_thing.string_thing.c_str());
Marc Slemkobf4fd192006-08-15 21:29:39 +0000505 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000506
Marc Slemko71d4e472006-08-15 22:34:04 +0000507 try {
508 printf("testClient.testMultiException(\"success\", \"test 3\") =>");
Mark Slee1921d202007-01-24 19:43:06 +0000509 Xtruct result;
510 testClient.testMultiException(result, "success", "test 3");
Marc Slemko71d4e472006-08-15 22:34:04 +0000511 printf(" {{\"%s\"}}\n", result.string_thing.c_str());
512 } catch(...) {
513 printf(" exception\nFAILURE\n");
514 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000515
David Reissc51986f2009-03-24 20:01:25 +0000516 /* test oneway void */
David Reiss2ab6fe82008-02-18 02:11:44 +0000517 {
David Reiss6ce401d2009-03-24 20:01:58 +0000518 printf("testClient.testOneway(3) =>");
519 uint64_t startOneway = now();
520 testClient.testOneway(3);
521 uint64_t elapsed = now() - startOneway;
David Reiss2ab6fe82008-02-18 02:11:44 +0000522 if (elapsed > 200 * 1000) { // 0.2 seconds
523 printf(" FAILURE - took %.2f ms\n", (double)elapsed/1000.0);
524 } else {
525 printf(" success - took %.2f ms\n", (double)elapsed/1000.0);
526 }
527 }
528
David Reiss2845b522008-02-18 02:11:52 +0000529 /**
David Reissc51986f2009-03-24 20:01:25 +0000530 * redo a simple test after the oneway to make sure we aren't "off by one" --
531 * if the server treated oneway void like normal void, this next test will
David Reiss2845b522008-02-18 02:11:52 +0000532 * fail since it will get the void confirmation rather than the correct
533 * result. In this circumstance, the client will throw the exception:
534 *
535 * TApplicationException: Wrong method namea
536 */
537 /**
538 * I32 TEST
539 */
540 printf("re-test testI32(-1)");
541 i32 = testClient.testI32(-1);
542 printf(" = %d\n", i32);
543
544
Marc Slemkobf4fd192006-08-15 21:29:39 +0000545 uint64_t stop = now();
Mark Sleed788b2e2006-09-07 01:26:35 +0000546 uint64_t tot = stop-start;
547
David Reissbc3dddb2007-08-22 23:20:24 +0000548 printf("Total time: %"PRIu64" us\n", stop-start);
David Reiss0c90f6f2008-02-06 22:18:40 +0000549
Mark Sleed788b2e2006-09-07 01:26:35 +0000550 time_tot += tot;
551 if (time_min == 0 || tot < time_min) {
552 time_min = tot;
553 }
554 if (tot > time_max) {
555 time_max = tot;
556 }
557
Mark Sleea3302652006-10-25 19:03:32 +0000558 transport->close();
Mark Sleee8540632006-05-30 09:24:40 +0000559 }
560
Marc Slemko6be374b2006-08-04 03:16:25 +0000561 // printf("\nSocket syscalls: %u", g_socket_syscalls);
Mark Sleee8540632006-05-30 09:24:40 +0000562 printf("\nAll tests done.\n");
Mark Sleed788b2e2006-09-07 01:26:35 +0000563
564 uint64_t time_avg = time_tot / numTests;
565
David Reissbc3dddb2007-08-22 23:20:24 +0000566 printf("Min time: %"PRIu64" us\n", time_min);
567 printf("Max time: %"PRIu64" us\n", time_max);
568 printf("Avg time: %"PRIu64" us\n", time_avg);
Mark Sleed788b2e2006-09-07 01:26:35 +0000569
Mark Sleee8540632006-05-30 09:24:40 +0000570 return 0;
571}