blob: 3f22ad225cc81aa6fb8a82c606cb38436510ddc6 [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>
Roger Meier7e056e72011-07-17 07:28:28 +000032#include <async/TEvhttpClientChannel.h>
33#include <server/TNonblockingServer.h> // <event.h>
Mark Sleee8540632006-05-30 09:24:40 +000034
Marc Slemko6be374b2006-08-04 03:16:25 +000035#include <boost/shared_ptr.hpp>
Roger Meierca142b02011-06-07 17:59:07 +000036#include <boost/program_options.hpp>
Roger Meier7e056e72011-07-17 07:28:28 +000037#include <tr1/functional>
Roger Meierca142b02011-06-07 17:59:07 +000038
Marc Slemko6be374b2006-08-04 03:16:25 +000039#include "ThriftTest.h"
40
41using namespace boost;
42using namespace std;
T Jake Lucianib5e62212009-01-31 22:36:20 +000043using namespace apache::thrift;
44using namespace apache::thrift::protocol;
45using namespace apache::thrift::transport;
Marc Slemkobf4fd192006-08-15 21:29:39 +000046using namespace thrift::test;
Roger Meier7e056e72011-07-17 07:28:28 +000047using namespace apache::thrift::async;
48
49using std::tr1::placeholders::_1;
Marc Slemko6be374b2006-08-04 03:16:25 +000050
51//extern uint32_t g_socket_syscalls;
Mark Slee95771002006-06-07 06:53:25 +000052
53// Current time, microseconds since the epoch
54uint64_t now()
55{
Roger Meier5f9614c2010-11-21 16:59:05 +000056 int64_t ret;
Mark Slee95771002006-06-07 06:53:25 +000057 struct timeval tv;
David Reiss0c90f6f2008-02-06 22:18:40 +000058
Mark Slee95771002006-06-07 06:53:25 +000059 gettimeofday(&tv, NULL);
60 ret = tv.tv_sec;
61 ret = ret*1000*1000 + tv.tv_usec;
62 return ret;
63}
64
Roger Meier7e056e72011-07-17 07:28:28 +000065static void testString_clientReturn(const char* host, int port, event_base *base, TProtocolFactory* protocolFactory, ThriftTestCobClient* client) {
66 try {
67 string s;
68 client->recv_testString(s);
69 cout << "testString: " << s << endl;
70 } catch (TException& exn) {
71 cout << "Error: " << exn.what() << endl;
72 }
73
74 event_base_loopbreak(base); // end test
75}
76
77static void testVoid_clientReturn(const char* host, int port, event_base *base, TProtocolFactory* protocolFactory, ThriftTestCobClient* client) {
78 try {
79 client->recv_testVoid();
80 cout << "testVoid" << endl;
81
82 // next test
83 delete client;
84 shared_ptr<TAsyncChannel> channel(new TEvhttpClientChannel(host, "/", host, port, base));
85 client = new ThriftTestCobClient(channel, protocolFactory);
86 client->testString(tr1::bind(testString_clientReturn, host, port, base, protocolFactory, _1), "Test");
87 } catch (TException& exn) {
88 cout << "Error: " << exn.what() << endl;
89 }
90}
91
Mark Sleee8540632006-05-30 09:24:40 +000092int main(int argc, char** argv) {
93 string host = "localhost";
94 int port = 9090;
95 int numTests = 1;
Bryan Duxburycd9aea12011-02-22 18:12:06 +000096 bool ssl = false;
Roger Meierca142b02011-06-07 17:59:07 +000097 string transport_type = "buffered";
98 string protocol_type = "binary";
99 string domain_socket = "";
Mark Sleee8540632006-05-30 09:24:40 +0000100
Roger Meierca142b02011-06-07 17:59:07 +0000101 program_options::options_description desc("Allowed options");
102 desc.add_options()
103 ("help,h", "produce help message")
104 ("host", program_options::value<string>(&host)->default_value(host), "Host to connect")
105 ("port", program_options::value<int>(&port)->default_value(port), "Port number to connect")
106 ("domain-socket", program_options::value<string>(&domain_socket)->default_value(domain_socket), "Domain Socket (e.g. /tmp/ThriftTest.thrift), instead of host and port")
Roger Meier7e056e72011-07-17 07:28:28 +0000107 ("transport", program_options::value<string>(&transport_type)->default_value(transport_type), "Transport: buffered, framed, http, evhttp")
Roger Meierca142b02011-06-07 17:59:07 +0000108 ("protocol", program_options::value<string>(&protocol_type)->default_value(protocol_type), "Protocol: binary, json")
109 ("ssl", "Encrypted Transport using SSL")
110 ("testloops,n", program_options::value<int>(&numTests)->default_value(numTests), "Number of Tests")
111 ;
112
113 program_options::variables_map vm;
114 program_options::store(program_options::parse_command_line(argc, argv, desc), vm);
115 program_options::notify(vm);
116
117 if (vm.count("help")) {
118 cout << desc << "\n";
119 return 1;
Mark Sleee8540632006-05-30 09:24:40 +0000120 }
Mark Sleea3302652006-10-25 19:03:32 +0000121
Roger Meierca142b02011-06-07 17:59:07 +0000122 try {
123 if (!protocol_type.empty()) {
124 if (protocol_type == "binary") {
125 } else if (protocol_type == "json") {
126 } else {
127 throw invalid_argument("Unknown protocol type "+protocol_type);
128 }
129 }
130
131 if (!transport_type.empty()) {
132 if (transport_type == "buffered") {
133 } else if (transport_type == "framed") {
134 } else if (transport_type == "http") {
Roger Meier7e056e72011-07-17 07:28:28 +0000135 } else if (transport_type == "evhttp") {
Roger Meierca142b02011-06-07 17:59:07 +0000136 } else {
137 throw invalid_argument("Unknown transport type "+transport_type);
138 }
139 }
140
141 } catch (std::exception& e) {
142 cerr << e.what() << endl;
143 cout << desc << "\n";
144 return 1;
145 }
146
147 if (vm.count("ssl")) {
148 ssl = true;
149 }
150
151 shared_ptr<TTransport> transport;
152 shared_ptr<TProtocol> protocol;
153
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000154 shared_ptr<TSocket> socket;
155 shared_ptr<TSSLSocketFactory> factory;
Roger Meierca142b02011-06-07 17:59:07 +0000156
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000157 if (ssl) {
158 factory = shared_ptr<TSSLSocketFactory>(new TSSLSocketFactory());
159 factory->ciphers("ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
160 factory->loadTrustedCertificates("./trusted-ca-certificate.pem");
161 factory->authenticate(true);
162 socket = factory->createSocket(host, port);
163 } else {
Roger Meierca142b02011-06-07 17:59:07 +0000164 if (domain_socket != "") {
165 socket = shared_ptr<TSocket>(new TSocket(domain_socket));
166 port = 0;
167 }
168 else {
169 socket = shared_ptr<TSocket>(new TSocket(host, port));
170 }
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000171 }
Mark Sleea3302652006-10-25 19:03:32 +0000172
Roger Meierca142b02011-06-07 17:59:07 +0000173 if (transport_type.compare("http") == 0) {
174 shared_ptr<TTransport> httpSocket(new THttpClient(socket, host, "/service"));
175 transport = httpSocket;
176 } else if (transport_type.compare("framed") == 0){
Mark Sleea3302652006-10-25 19:03:32 +0000177 shared_ptr<TFramedTransport> framedSocket(new TFramedTransport(socket));
Mark Sleea3302652006-10-25 19:03:32 +0000178 transport = framedSocket;
Roger Meierca142b02011-06-07 17:59:07 +0000179 } else{
Mark Sleea3302652006-10-25 19:03:32 +0000180 shared_ptr<TBufferedTransport> bufferedSocket(new TBufferedTransport(socket));
181 transport = bufferedSocket;
182 }
183
Roger Meierca142b02011-06-07 17:59:07 +0000184 if (protocol_type.compare("json") == 0) {
185 shared_ptr<TProtocol> jsonProtocol(new TJSONProtocol(transport));
186 protocol = jsonProtocol;
187 } else{
188 shared_ptr<TBinaryProtocol> binaryProtocol(new TBinaryProtocol(transport));
189 protocol = binaryProtocol;
190 }
191
192 // Connection info
193 cout << "Connecting (" << transport_type << "/" << protocol_type << ") to: " << domain_socket;
194 if (port != 0) {
195 cout << host << ":" << port;
196 }
197 cout << endl;
198
Roger Meier7e056e72011-07-17 07:28:28 +0000199 if (transport_type.compare("evhttp") == 0) {
200 event_base *base = event_base_new();
201 cout << "Libevent Version: " << event_get_version() << endl;
202 cout << "Libevent Method: " << event_base_get_method(base) << endl;
203#if LIBEVENT_VERSION_NUMBER >= 0x02000000
204 cout << "Libevent Features: 0x" << hex << event_base_get_features(base) << endl;
205#endif
206
207 shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());
208
209 shared_ptr<TAsyncChannel> channel(new TEvhttpClientChannel(host.c_str(), "/", host.c_str(), port, base));
210 ThriftTestCobClient* client = new ThriftTestCobClient(channel, protocolFactory.get());
211 client->testVoid(tr1::bind(testVoid_clientReturn, host.c_str(), port, base, protocolFactory.get(), _1));
212
213 event_base_loop(base, 0);
214 return 0;
215 }
216
217
Roger Meierca142b02011-06-07 17:59:07 +0000218 ThriftTestClient testClient(protocol);
Mark Sleed788b2e2006-09-07 01:26:35 +0000219
220 uint64_t time_min = 0;
221 uint64_t time_max = 0;
222 uint64_t time_tot = 0;
David Reiss0c90f6f2008-02-06 22:18:40 +0000223
Mark Sleee8540632006-05-30 09:24:40 +0000224 int test = 0;
225 for (test = 0; test < numTests; ++test) {
Mark Slee95771002006-06-07 06:53:25 +0000226
Mark Slee95771002006-06-07 06:53:25 +0000227 try {
Mark Sleea3302652006-10-25 19:03:32 +0000228 transport->open();
Mark Slee95771002006-06-07 06:53:25 +0000229 } catch (TTransportException& ttx) {
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000230 printf("Connect failed: %s\n", ttx.what());
Mark Sleee8540632006-05-30 09:24:40 +0000231 continue;
232 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000233
Mark Sleed788b2e2006-09-07 01:26:35 +0000234 /**
235 * CONNECT TEST
236 */
237 printf("Test #%d, connect %s:%d\n", test+1, host.c_str(), port);
Mark Slee95771002006-06-07 06:53:25 +0000238
239 uint64_t start = now();
David Reiss0c90f6f2008-02-06 22:18:40 +0000240
Mark Sleee8540632006-05-30 09:24:40 +0000241 /**
242 * VOID TEST
243 */
Mark Sleee129a2d2007-02-21 05:17:48 +0000244 try {
245 printf("testVoid()");
246 testClient.testVoid();
247 printf(" = void\n");
248 } catch (TApplicationException tax) {
249 printf("%s\n", tax.what());
250 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000251
Mark Sleee8540632006-05-30 09:24:40 +0000252 /**
253 * STRING TEST
254 */
255 printf("testString(\"Test\")");
Mark Slee1921d202007-01-24 19:43:06 +0000256 string s;
257 testClient.testString(s, "Test");
Mark Sleee8540632006-05-30 09:24:40 +0000258 printf(" = \"%s\"\n", s.c_str());
David Reiss0c90f6f2008-02-06 22:18:40 +0000259
Mark Sleee8540632006-05-30 09:24:40 +0000260 /**
261 * BYTE TEST
262 */
263 printf("testByte(1)");
264 uint8_t u8 = testClient.testByte(1);
265 printf(" = %d\n", (int)u8);
David Reiss0c90f6f2008-02-06 22:18:40 +0000266
Mark Sleee8540632006-05-30 09:24:40 +0000267 /**
268 * I32 TEST
269 */
270 printf("testI32(-1)");
271 int32_t i32 = testClient.testI32(-1);
272 printf(" = %d\n", i32);
273
274 /**
Mark Sleee8540632006-05-30 09:24:40 +0000275 * I64 TEST
276 */
277 printf("testI64(-34359738368)");
Marc Slemkobf4fd192006-08-15 21:29:39 +0000278 int64_t i64 = testClient.testI64(-34359738368LL);
David Reissbc3dddb2007-08-22 23:20:24 +0000279 printf(" = %"PRId64"\n", i64);
Mark Sleec98d0502006-09-06 02:42:25 +0000280
281 /**
282 * DOUBLE TEST
283 */
284 printf("testDouble(-5.2098523)");
285 double dub = testClient.testDouble(-5.2098523);
286 printf(" = %lf\n", dub);
David Reiss0c90f6f2008-02-06 22:18:40 +0000287
Mark Sleee8540632006-05-30 09:24:40 +0000288 /**
289 * STRUCT TEST
290 */
Mark Slee6e536442006-06-30 18:28:50 +0000291 printf("testStruct({\"Zero\", 1, -3, -5})");
Mark Sleee8540632006-05-30 09:24:40 +0000292 Xtruct out;
293 out.string_thing = "Zero";
294 out.byte_thing = 1;
Mark Sleee8540632006-05-30 09:24:40 +0000295 out.i32_thing = -3;
Mark Sleee8540632006-05-30 09:24:40 +0000296 out.i64_thing = -5;
Mark Slee1921d202007-01-24 19:43:06 +0000297 Xtruct in;
298 testClient.testStruct(in, out);
David Reissbc3dddb2007-08-22 23:20:24 +0000299 printf(" = {\"%s\", %d, %d, %"PRId64"}\n",
Mark Sleee8540632006-05-30 09:24:40 +0000300 in.string_thing.c_str(),
301 (int)in.byte_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000302 in.i32_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000303 in.i64_thing);
David Reiss0c90f6f2008-02-06 22:18:40 +0000304
Mark Sleee8540632006-05-30 09:24:40 +0000305 /**
306 * NESTED STRUCT TEST
307 */
Mark Slee6e536442006-06-30 18:28:50 +0000308 printf("testNest({1, {\"Zero\", 1, -3, -5}), 5}");
Mark Sleee8540632006-05-30 09:24:40 +0000309 Xtruct2 out2;
310 out2.byte_thing = 1;
311 out2.struct_thing = out;
312 out2.i32_thing = 5;
Mark Slee1921d202007-01-24 19:43:06 +0000313 Xtruct2 in2;
314 testClient.testNest(in2, out2);
Mark Sleee8540632006-05-30 09:24:40 +0000315 in = in2.struct_thing;
David Reissbc3dddb2007-08-22 23:20:24 +0000316 printf(" = {%d, {\"%s\", %d, %d, %"PRId64"}, %d}\n",
Mark Sleee8540632006-05-30 09:24:40 +0000317 in2.byte_thing,
318 in.string_thing.c_str(),
319 (int)in.byte_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000320 in.i32_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000321 in.i64_thing,
David Reiss0c90f6f2008-02-06 22:18:40 +0000322 in2.i32_thing);
Mark Sleee8540632006-05-30 09:24:40 +0000323
324 /**
325 * MAP TEST
326 */
327 map<int32_t,int32_t> mapout;
328 for (int32_t i = 0; i < 5; ++i) {
329 mapout.insert(make_pair(i, i-10));
330 }
331 printf("testMap({");
332 map<int32_t, int32_t>::const_iterator m_iter;
333 bool first = true;
334 for (m_iter = mapout.begin(); m_iter != mapout.end(); ++m_iter) {
335 if (first) {
336 first = false;
337 } else {
338 printf(", ");
339 }
340 printf("%d => %d", m_iter->first, m_iter->second);
341 }
342 printf("})");
Mark Slee1921d202007-01-24 19:43:06 +0000343 map<int32_t,int32_t> mapin;
344 testClient.testMap(mapin, mapout);
Mark Sleee8540632006-05-30 09:24:40 +0000345 printf(" = {");
346 first = true;
347 for (m_iter = mapin.begin(); m_iter != mapin.end(); ++m_iter) {
348 if (first) {
349 first = false;
350 } else {
351 printf(", ");
352 }
353 printf("%d => %d", m_iter->first, m_iter->second);
354 }
355 printf("}\n");
356
357 /**
358 * SET TEST
359 */
360 set<int32_t> setout;
361 for (int32_t i = -2; i < 3; ++i) {
362 setout.insert(i);
363 }
364 printf("testSet({");
365 set<int32_t>::const_iterator s_iter;
366 first = true;
367 for (s_iter = setout.begin(); s_iter != setout.end(); ++s_iter) {
368 if (first) {
369 first = false;
370 } else {
371 printf(", ");
372 }
373 printf("%d", *s_iter);
374 }
375 printf("})");
Mark Slee1921d202007-01-24 19:43:06 +0000376 set<int32_t> setin;
377 testClient.testSet(setin, setout);
Mark Sleee8540632006-05-30 09:24:40 +0000378 printf(" = {");
379 first = true;
380 for (s_iter = setin.begin(); s_iter != setin.end(); ++s_iter) {
381 if (first) {
382 first = false;
383 } else {
384 printf(", ");
385 }
386 printf("%d", *s_iter);
387 }
388 printf("}\n");
389
390 /**
391 * LIST TEST
392 */
Mark Sleeb9acf982006-10-10 01:57:32 +0000393 vector<int32_t> listout;
Mark Sleee8540632006-05-30 09:24:40 +0000394 for (int32_t i = -2; i < 3; ++i) {
395 listout.push_back(i);
396 }
397 printf("testList({");
Mark Sleeb9acf982006-10-10 01:57:32 +0000398 vector<int32_t>::const_iterator l_iter;
Mark Sleee8540632006-05-30 09:24:40 +0000399 first = true;
400 for (l_iter = listout.begin(); l_iter != listout.end(); ++l_iter) {
401 if (first) {
402 first = false;
403 } else {
404 printf(", ");
405 }
406 printf("%d", *l_iter);
407 }
408 printf("})");
Mark Slee1921d202007-01-24 19:43:06 +0000409 vector<int32_t> listin;
410 testClient.testList(listin, listout);
Mark Sleee8540632006-05-30 09:24:40 +0000411 printf(" = {");
412 first = true;
413 for (l_iter = listin.begin(); l_iter != listin.end(); ++l_iter) {
414 if (first) {
415 first = false;
416 } else {
417 printf(", ");
418 }
419 printf("%d", *l_iter);
420 }
421 printf("}\n");
422
423 /**
424 * ENUM TEST
425 */
426 printf("testEnum(ONE)");
Bryan Duxbury833ae492010-09-27 17:26:02 +0000427 Numberz::type ret = testClient.testEnum(Numberz::ONE);
Mark Sleee8540632006-05-30 09:24:40 +0000428 printf(" = %d\n", ret);
429
430 printf("testEnum(TWO)");
Bryan Duxbury833ae492010-09-27 17:26:02 +0000431 ret = testClient.testEnum(Numberz::TWO);
Mark Sleee8540632006-05-30 09:24:40 +0000432 printf(" = %d\n", ret);
433
434 printf("testEnum(THREE)");
Bryan Duxbury833ae492010-09-27 17:26:02 +0000435 ret = testClient.testEnum(Numberz::THREE);
Mark Sleee8540632006-05-30 09:24:40 +0000436 printf(" = %d\n", ret);
437
438 printf("testEnum(FIVE)");
Bryan Duxbury833ae492010-09-27 17:26:02 +0000439 ret = testClient.testEnum(Numberz::FIVE);
Mark Sleee8540632006-05-30 09:24:40 +0000440 printf(" = %d\n", ret);
441
442 printf("testEnum(EIGHT)");
Bryan Duxbury833ae492010-09-27 17:26:02 +0000443 ret = testClient.testEnum(Numberz::EIGHT);
Mark Sleee8540632006-05-30 09:24:40 +0000444 printf(" = %d\n", ret);
445
446 /**
447 * TYPEDEF TEST
448 */
449 printf("testTypedef(309858235082523)");
Marc Slemkobf4fd192006-08-15 21:29:39 +0000450 UserId uid = testClient.testTypedef(309858235082523LL);
David Reissbc3dddb2007-08-22 23:20:24 +0000451 printf(" = %"PRId64"\n", uid);
Mark Sleee8540632006-05-30 09:24:40 +0000452
453 /**
454 * NESTED MAP TEST
455 */
456 printf("testMapMap(1)");
Mark Slee1921d202007-01-24 19:43:06 +0000457 map<int32_t, map<int32_t, int32_t> > mm;
458 testClient.testMapMap(mm, 1);
Mark Sleee8540632006-05-30 09:24:40 +0000459 printf(" = {");
460 map<int32_t, map<int32_t, int32_t> >::const_iterator mi;
461 for (mi = mm.begin(); mi != mm.end(); ++mi) {
462 printf("%d => {", mi->first);
463 map<int32_t, int32_t>::const_iterator mi2;
464 for (mi2 = mi->second.begin(); mi2 != mi->second.end(); ++mi2) {
465 printf("%d => %d, ", mi2->first, mi2->second);
466 }
467 printf("}, ");
468 }
469 printf("}\n");
470
471 /**
472 * INSANITY TEST
473 */
474 Insanity insane;
Bryan Duxbury833ae492010-09-27 17:26:02 +0000475 insane.userMap.insert(make_pair(Numberz::FIVE, 5000));
Mark Sleee8540632006-05-30 09:24:40 +0000476 Xtruct truck;
477 truck.string_thing = "Truck";
478 truck.byte_thing = 8;
Mark Sleee8540632006-05-30 09:24:40 +0000479 truck.i32_thing = 8;
Mark Sleee8540632006-05-30 09:24:40 +0000480 truck.i64_thing = 8;
481 insane.xtructs.push_back(truck);
482 printf("testInsanity()");
Bryan Duxbury833ae492010-09-27 17:26:02 +0000483 map<UserId, map<Numberz::type,Insanity> > whoa;
Mark Slee1921d202007-01-24 19:43:06 +0000484 testClient.testInsanity(whoa, insane);
Mark Sleee8540632006-05-30 09:24:40 +0000485 printf(" = {");
Bryan Duxbury833ae492010-09-27 17:26:02 +0000486 map<UserId, map<Numberz::type,Insanity> >::const_iterator i_iter;
Mark Sleee8540632006-05-30 09:24:40 +0000487 for (i_iter = whoa.begin(); i_iter != whoa.end(); ++i_iter) {
David Reissbc3dddb2007-08-22 23:20:24 +0000488 printf("%"PRId64" => {", i_iter->first);
Bryan Duxbury833ae492010-09-27 17:26:02 +0000489 map<Numberz::type,Insanity>::const_iterator i2_iter;
Mark Sleee8540632006-05-30 09:24:40 +0000490 for (i2_iter = i_iter->second.begin();
491 i2_iter != i_iter->second.end();
492 ++i2_iter) {
493 printf("%d => {", i2_iter->first);
Bryan Duxbury833ae492010-09-27 17:26:02 +0000494 map<Numberz::type, UserId> userMap = i2_iter->second.userMap;
495 map<Numberz::type, UserId>::const_iterator um;
Mark Sleee8540632006-05-30 09:24:40 +0000496 printf("{");
497 for (um = userMap.begin(); um != userMap.end(); ++um) {
David Reissbc3dddb2007-08-22 23:20:24 +0000498 printf("%d => %"PRId64", ", um->first, um->second);
Mark Sleee8540632006-05-30 09:24:40 +0000499 }
500 printf("}, ");
501
Mark Sleeb9acf982006-10-10 01:57:32 +0000502 vector<Xtruct> xtructs = i2_iter->second.xtructs;
503 vector<Xtruct>::const_iterator x;
Mark Sleee8540632006-05-30 09:24:40 +0000504 printf("{");
505 for (x = xtructs.begin(); x != xtructs.end(); ++x) {
David Reissbc3dddb2007-08-22 23:20:24 +0000506 printf("{\"%s\", %d, %d, %"PRId64"}, ",
Mark Sleee8540632006-05-30 09:24:40 +0000507 x->string_thing.c_str(),
508 (int)x->byte_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000509 x->i32_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000510 x->i64_thing);
511 }
512 printf("}");
513
514 printf("}, ");
515 }
516 printf("}, ");
517 }
518 printf("}\n");
519
Marc Slemko71d4e472006-08-15 22:34:04 +0000520 /* test exception */
Mark Slee95771002006-06-07 06:53:25 +0000521
Marc Slemkobf4fd192006-08-15 21:29:39 +0000522 try {
Marc Slemko71d4e472006-08-15 22:34:04 +0000523 printf("testClient.testException(\"Xception\") =>");
524 testClient.testException("Xception");
525 printf(" void\nFAILURE\n");
David Reiss0c90f6f2008-02-06 22:18:40 +0000526
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000527 } catch(Xception& e) {
Marc Slemko71d4e472006-08-15 22:34:04 +0000528 printf(" {%u, \"%s\"}\n", e.errorCode, e.message.c_str());
Marc Slemkobf4fd192006-08-15 21:29:39 +0000529 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000530
Marc Slemkobf4fd192006-08-15 21:29:39 +0000531 try {
Marc Slemko71d4e472006-08-15 22:34:04 +0000532 printf("testClient.testException(\"success\") =>");
533 testClient.testException("success");
534 printf(" void\n");
535 } catch(...) {
536 printf(" exception\nFAILURE\n");
537 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000538
Marc Slemko71d4e472006-08-15 22:34:04 +0000539 /* test multi exception */
David Reiss0c90f6f2008-02-06 22:18:40 +0000540
Marc Slemko71d4e472006-08-15 22:34:04 +0000541 try {
542 printf("testClient.testMultiException(\"Xception\", \"test 1\") =>");
Mark Slee1921d202007-01-24 19:43:06 +0000543 Xtruct result;
544 testClient.testMultiException(result, "Xception", "test 1");
Marc Slemko71d4e472006-08-15 22:34:04 +0000545 printf(" result\nFAILURE\n");
Mark Sleed3d733a2006-09-01 22:19:06 +0000546 } catch(Xception& e) {
Marc Slemko71d4e472006-08-15 22:34:04 +0000547 printf(" {%u, \"%s\"}\n", e.errorCode, e.message.c_str());
548 }
549
550 try {
551 printf("testClient.testMultiException(\"Xception2\", \"test 2\") =>");
Mark Slee1921d202007-01-24 19:43:06 +0000552 Xtruct result;
553 testClient.testMultiException(result, "Xception2", "test 2");
Marc Slemko71d4e472006-08-15 22:34:04 +0000554 printf(" result\nFAILURE\n");
David Reiss0c90f6f2008-02-06 22:18:40 +0000555
Mark Sleed3d733a2006-09-01 22:19:06 +0000556 } catch(Xception2& e) {
Marc Slemko71d4e472006-08-15 22:34:04 +0000557 printf(" {%u, {\"%s\"}}\n", e.errorCode, e.struct_thing.string_thing.c_str());
Marc Slemkobf4fd192006-08-15 21:29:39 +0000558 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000559
Marc Slemko71d4e472006-08-15 22:34:04 +0000560 try {
561 printf("testClient.testMultiException(\"success\", \"test 3\") =>");
Mark Slee1921d202007-01-24 19:43:06 +0000562 Xtruct result;
563 testClient.testMultiException(result, "success", "test 3");
Marc Slemko71d4e472006-08-15 22:34:04 +0000564 printf(" {{\"%s\"}}\n", result.string_thing.c_str());
565 } catch(...) {
566 printf(" exception\nFAILURE\n");
567 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000568
David Reissc51986f2009-03-24 20:01:25 +0000569 /* test oneway void */
David Reiss2ab6fe82008-02-18 02:11:44 +0000570 {
David Reiss6ce401d2009-03-24 20:01:58 +0000571 printf("testClient.testOneway(3) =>");
572 uint64_t startOneway = now();
573 testClient.testOneway(3);
574 uint64_t elapsed = now() - startOneway;
David Reiss2ab6fe82008-02-18 02:11:44 +0000575 if (elapsed > 200 * 1000) { // 0.2 seconds
576 printf(" FAILURE - took %.2f ms\n", (double)elapsed/1000.0);
577 } else {
578 printf(" success - took %.2f ms\n", (double)elapsed/1000.0);
579 }
580 }
581
David Reiss2845b522008-02-18 02:11:52 +0000582 /**
David Reissc51986f2009-03-24 20:01:25 +0000583 * redo a simple test after the oneway to make sure we aren't "off by one" --
584 * if the server treated oneway void like normal void, this next test will
David Reiss2845b522008-02-18 02:11:52 +0000585 * fail since it will get the void confirmation rather than the correct
586 * result. In this circumstance, the client will throw the exception:
587 *
588 * TApplicationException: Wrong method namea
589 */
590 /**
591 * I32 TEST
592 */
593 printf("re-test testI32(-1)");
594 i32 = testClient.testI32(-1);
595 printf(" = %d\n", i32);
596
597
Marc Slemkobf4fd192006-08-15 21:29:39 +0000598 uint64_t stop = now();
Mark Sleed788b2e2006-09-07 01:26:35 +0000599 uint64_t tot = stop-start;
600
David Reissbc3dddb2007-08-22 23:20:24 +0000601 printf("Total time: %"PRIu64" us\n", stop-start);
David Reiss0c90f6f2008-02-06 22:18:40 +0000602
Mark Sleed788b2e2006-09-07 01:26:35 +0000603 time_tot += tot;
604 if (time_min == 0 || tot < time_min) {
605 time_min = tot;
606 }
607 if (tot > time_max) {
608 time_max = tot;
609 }
610
Mark Sleea3302652006-10-25 19:03:32 +0000611 transport->close();
Mark Sleee8540632006-05-30 09:24:40 +0000612 }
613
Marc Slemko6be374b2006-08-04 03:16:25 +0000614 // printf("\nSocket syscalls: %u", g_socket_syscalls);
Mark Sleee8540632006-05-30 09:24:40 +0000615 printf("\nAll tests done.\n");
Mark Sleed788b2e2006-09-07 01:26:35 +0000616
617 uint64_t time_avg = time_tot / numTests;
618
David Reissbc3dddb2007-08-22 23:20:24 +0000619 printf("Min time: %"PRIu64" us\n", time_min);
620 printf("Max time: %"PRIu64" us\n", time_max);
621 printf("Avg time: %"PRIu64" us\n", time_avg);
Mark Sleed788b2e2006-09-07 01:26:35 +0000622
Mark Sleee8540632006-05-30 09:24:40 +0000623 return 0;
624}