blob: 9aed551058240c3a755c39472b9a9b142777dfb9 [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) {
Roger Meiera8cef6e2011-07-17 18:55:59 +000066 (void) host;
67 (void) port;
68 (void) protocolFactory;
Roger Meier7e056e72011-07-17 07:28:28 +000069 try {
70 string s;
71 client->recv_testString(s);
72 cout << "testString: " << s << endl;
73 } catch (TException& exn) {
74 cout << "Error: " << exn.what() << endl;
75 }
76
77 event_base_loopbreak(base); // end test
78}
79
80static void testVoid_clientReturn(const char* host, int port, event_base *base, TProtocolFactory* protocolFactory, ThriftTestCobClient* client) {
81 try {
82 client->recv_testVoid();
83 cout << "testVoid" << endl;
84
85 // next test
86 delete client;
87 shared_ptr<TAsyncChannel> channel(new TEvhttpClientChannel(host, "/", host, port, base));
88 client = new ThriftTestCobClient(channel, protocolFactory);
89 client->testString(tr1::bind(testString_clientReturn, host, port, base, protocolFactory, _1), "Test");
90 } catch (TException& exn) {
91 cout << "Error: " << exn.what() << endl;
92 }
93}
94
Mark Sleee8540632006-05-30 09:24:40 +000095int main(int argc, char** argv) {
96 string host = "localhost";
97 int port = 9090;
98 int numTests = 1;
Bryan Duxburycd9aea12011-02-22 18:12:06 +000099 bool ssl = false;
Roger Meierca142b02011-06-07 17:59:07 +0000100 string transport_type = "buffered";
101 string protocol_type = "binary";
102 string domain_socket = "";
Mark Sleee8540632006-05-30 09:24:40 +0000103
Roger Meierca142b02011-06-07 17:59:07 +0000104 program_options::options_description desc("Allowed options");
105 desc.add_options()
106 ("help,h", "produce help message")
107 ("host", program_options::value<string>(&host)->default_value(host), "Host to connect")
108 ("port", program_options::value<int>(&port)->default_value(port), "Port number to connect")
109 ("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 +0000110 ("transport", program_options::value<string>(&transport_type)->default_value(transport_type), "Transport: buffered, framed, http, evhttp")
Roger Meierca142b02011-06-07 17:59:07 +0000111 ("protocol", program_options::value<string>(&protocol_type)->default_value(protocol_type), "Protocol: binary, json")
112 ("ssl", "Encrypted Transport using SSL")
113 ("testloops,n", program_options::value<int>(&numTests)->default_value(numTests), "Number of Tests")
114 ;
115
116 program_options::variables_map vm;
117 program_options::store(program_options::parse_command_line(argc, argv, desc), vm);
118 program_options::notify(vm);
119
120 if (vm.count("help")) {
121 cout << desc << "\n";
122 return 1;
Mark Sleee8540632006-05-30 09:24:40 +0000123 }
Mark Sleea3302652006-10-25 19:03:32 +0000124
Roger Meierca142b02011-06-07 17:59:07 +0000125 try {
126 if (!protocol_type.empty()) {
127 if (protocol_type == "binary") {
128 } else if (protocol_type == "json") {
129 } else {
130 throw invalid_argument("Unknown protocol type "+protocol_type);
131 }
132 }
133
134 if (!transport_type.empty()) {
135 if (transport_type == "buffered") {
136 } else if (transport_type == "framed") {
137 } else if (transport_type == "http") {
Roger Meier7e056e72011-07-17 07:28:28 +0000138 } else if (transport_type == "evhttp") {
Roger Meierca142b02011-06-07 17:59:07 +0000139 } else {
140 throw invalid_argument("Unknown transport type "+transport_type);
141 }
142 }
143
144 } catch (std::exception& e) {
145 cerr << e.what() << endl;
146 cout << desc << "\n";
147 return 1;
148 }
149
150 if (vm.count("ssl")) {
151 ssl = true;
152 }
153
154 shared_ptr<TTransport> transport;
155 shared_ptr<TProtocol> protocol;
156
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000157 shared_ptr<TSocket> socket;
158 shared_ptr<TSSLSocketFactory> factory;
Roger Meierca142b02011-06-07 17:59:07 +0000159
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000160 if (ssl) {
161 factory = shared_ptr<TSSLSocketFactory>(new TSSLSocketFactory());
162 factory->ciphers("ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
163 factory->loadTrustedCertificates("./trusted-ca-certificate.pem");
164 factory->authenticate(true);
165 socket = factory->createSocket(host, port);
166 } else {
Roger Meierca142b02011-06-07 17:59:07 +0000167 if (domain_socket != "") {
168 socket = shared_ptr<TSocket>(new TSocket(domain_socket));
169 port = 0;
170 }
171 else {
172 socket = shared_ptr<TSocket>(new TSocket(host, port));
173 }
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000174 }
Mark Sleea3302652006-10-25 19:03:32 +0000175
Roger Meierca142b02011-06-07 17:59:07 +0000176 if (transport_type.compare("http") == 0) {
177 shared_ptr<TTransport> httpSocket(new THttpClient(socket, host, "/service"));
178 transport = httpSocket;
179 } else if (transport_type.compare("framed") == 0){
Mark Sleea3302652006-10-25 19:03:32 +0000180 shared_ptr<TFramedTransport> framedSocket(new TFramedTransport(socket));
Mark Sleea3302652006-10-25 19:03:32 +0000181 transport = framedSocket;
Roger Meierca142b02011-06-07 17:59:07 +0000182 } else{
Mark Sleea3302652006-10-25 19:03:32 +0000183 shared_ptr<TBufferedTransport> bufferedSocket(new TBufferedTransport(socket));
184 transport = bufferedSocket;
185 }
186
Roger Meierca142b02011-06-07 17:59:07 +0000187 if (protocol_type.compare("json") == 0) {
188 shared_ptr<TProtocol> jsonProtocol(new TJSONProtocol(transport));
189 protocol = jsonProtocol;
190 } else{
191 shared_ptr<TBinaryProtocol> binaryProtocol(new TBinaryProtocol(transport));
192 protocol = binaryProtocol;
193 }
194
195 // Connection info
196 cout << "Connecting (" << transport_type << "/" << protocol_type << ") to: " << domain_socket;
197 if (port != 0) {
198 cout << host << ":" << port;
199 }
200 cout << endl;
201
Roger Meier7e056e72011-07-17 07:28:28 +0000202 if (transport_type.compare("evhttp") == 0) {
203 event_base *base = event_base_new();
204 cout << "Libevent Version: " << event_get_version() << endl;
205 cout << "Libevent Method: " << event_base_get_method(base) << endl;
206#if LIBEVENT_VERSION_NUMBER >= 0x02000000
207 cout << "Libevent Features: 0x" << hex << event_base_get_features(base) << endl;
208#endif
209
210 shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());
211
212 shared_ptr<TAsyncChannel> channel(new TEvhttpClientChannel(host.c_str(), "/", host.c_str(), port, base));
213 ThriftTestCobClient* client = new ThriftTestCobClient(channel, protocolFactory.get());
214 client->testVoid(tr1::bind(testVoid_clientReturn, host.c_str(), port, base, protocolFactory.get(), _1));
215
216 event_base_loop(base, 0);
217 return 0;
218 }
219
220
Roger Meierca142b02011-06-07 17:59:07 +0000221 ThriftTestClient testClient(protocol);
Mark Sleed788b2e2006-09-07 01:26:35 +0000222
223 uint64_t time_min = 0;
224 uint64_t time_max = 0;
225 uint64_t time_tot = 0;
David Reiss0c90f6f2008-02-06 22:18:40 +0000226
Mark Sleee8540632006-05-30 09:24:40 +0000227 int test = 0;
228 for (test = 0; test < numTests; ++test) {
Mark Slee95771002006-06-07 06:53:25 +0000229
Mark Slee95771002006-06-07 06:53:25 +0000230 try {
Mark Sleea3302652006-10-25 19:03:32 +0000231 transport->open();
Mark Slee95771002006-06-07 06:53:25 +0000232 } catch (TTransportException& ttx) {
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000233 printf("Connect failed: %s\n", ttx.what());
Mark Sleee8540632006-05-30 09:24:40 +0000234 continue;
235 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000236
Mark Sleed788b2e2006-09-07 01:26:35 +0000237 /**
238 * CONNECT TEST
239 */
240 printf("Test #%d, connect %s:%d\n", test+1, host.c_str(), port);
Mark Slee95771002006-06-07 06:53:25 +0000241
242 uint64_t start = now();
David Reiss0c90f6f2008-02-06 22:18:40 +0000243
Mark Sleee8540632006-05-30 09:24:40 +0000244 /**
245 * VOID TEST
246 */
Mark Sleee129a2d2007-02-21 05:17:48 +0000247 try {
248 printf("testVoid()");
249 testClient.testVoid();
250 printf(" = void\n");
251 } catch (TApplicationException tax) {
252 printf("%s\n", tax.what());
253 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000254
Mark Sleee8540632006-05-30 09:24:40 +0000255 /**
256 * STRING TEST
257 */
258 printf("testString(\"Test\")");
Mark Slee1921d202007-01-24 19:43:06 +0000259 string s;
260 testClient.testString(s, "Test");
Mark Sleee8540632006-05-30 09:24:40 +0000261 printf(" = \"%s\"\n", s.c_str());
David Reiss0c90f6f2008-02-06 22:18:40 +0000262
Mark Sleee8540632006-05-30 09:24:40 +0000263 /**
264 * BYTE TEST
265 */
266 printf("testByte(1)");
267 uint8_t u8 = testClient.testByte(1);
268 printf(" = %d\n", (int)u8);
David Reiss0c90f6f2008-02-06 22:18:40 +0000269
Mark Sleee8540632006-05-30 09:24:40 +0000270 /**
271 * I32 TEST
272 */
273 printf("testI32(-1)");
274 int32_t i32 = testClient.testI32(-1);
275 printf(" = %d\n", i32);
276
277 /**
Mark Sleee8540632006-05-30 09:24:40 +0000278 * I64 TEST
279 */
280 printf("testI64(-34359738368)");
Marc Slemkobf4fd192006-08-15 21:29:39 +0000281 int64_t i64 = testClient.testI64(-34359738368LL);
David Reissbc3dddb2007-08-22 23:20:24 +0000282 printf(" = %"PRId64"\n", i64);
Mark Sleec98d0502006-09-06 02:42:25 +0000283
284 /**
285 * DOUBLE TEST
286 */
287 printf("testDouble(-5.2098523)");
288 double dub = testClient.testDouble(-5.2098523);
Roger Meiera8cef6e2011-07-17 18:55:59 +0000289 printf(" = %f\n", dub);
David Reiss0c90f6f2008-02-06 22:18:40 +0000290
Mark Sleee8540632006-05-30 09:24:40 +0000291 /**
292 * STRUCT TEST
293 */
Mark Slee6e536442006-06-30 18:28:50 +0000294 printf("testStruct({\"Zero\", 1, -3, -5})");
Mark Sleee8540632006-05-30 09:24:40 +0000295 Xtruct out;
296 out.string_thing = "Zero";
297 out.byte_thing = 1;
Mark Sleee8540632006-05-30 09:24:40 +0000298 out.i32_thing = -3;
Mark Sleee8540632006-05-30 09:24:40 +0000299 out.i64_thing = -5;
Mark Slee1921d202007-01-24 19:43:06 +0000300 Xtruct in;
301 testClient.testStruct(in, out);
David Reissbc3dddb2007-08-22 23:20:24 +0000302 printf(" = {\"%s\", %d, %d, %"PRId64"}\n",
Mark Sleee8540632006-05-30 09:24:40 +0000303 in.string_thing.c_str(),
304 (int)in.byte_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000305 in.i32_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000306 in.i64_thing);
David Reiss0c90f6f2008-02-06 22:18:40 +0000307
Mark Sleee8540632006-05-30 09:24:40 +0000308 /**
309 * NESTED STRUCT TEST
310 */
Mark Slee6e536442006-06-30 18:28:50 +0000311 printf("testNest({1, {\"Zero\", 1, -3, -5}), 5}");
Mark Sleee8540632006-05-30 09:24:40 +0000312 Xtruct2 out2;
313 out2.byte_thing = 1;
314 out2.struct_thing = out;
315 out2.i32_thing = 5;
Mark Slee1921d202007-01-24 19:43:06 +0000316 Xtruct2 in2;
317 testClient.testNest(in2, out2);
Mark Sleee8540632006-05-30 09:24:40 +0000318 in = in2.struct_thing;
David Reissbc3dddb2007-08-22 23:20:24 +0000319 printf(" = {%d, {\"%s\", %d, %d, %"PRId64"}, %d}\n",
Mark Sleee8540632006-05-30 09:24:40 +0000320 in2.byte_thing,
321 in.string_thing.c_str(),
322 (int)in.byte_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000323 in.i32_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000324 in.i64_thing,
David Reiss0c90f6f2008-02-06 22:18:40 +0000325 in2.i32_thing);
Mark Sleee8540632006-05-30 09:24:40 +0000326
327 /**
328 * MAP TEST
329 */
330 map<int32_t,int32_t> mapout;
331 for (int32_t i = 0; i < 5; ++i) {
332 mapout.insert(make_pair(i, i-10));
333 }
334 printf("testMap({");
335 map<int32_t, int32_t>::const_iterator m_iter;
336 bool first = true;
337 for (m_iter = mapout.begin(); m_iter != mapout.end(); ++m_iter) {
338 if (first) {
339 first = false;
340 } else {
341 printf(", ");
342 }
343 printf("%d => %d", m_iter->first, m_iter->second);
344 }
345 printf("})");
Mark Slee1921d202007-01-24 19:43:06 +0000346 map<int32_t,int32_t> mapin;
347 testClient.testMap(mapin, mapout);
Mark Sleee8540632006-05-30 09:24:40 +0000348 printf(" = {");
349 first = true;
350 for (m_iter = mapin.begin(); m_iter != mapin.end(); ++m_iter) {
351 if (first) {
352 first = false;
353 } else {
354 printf(", ");
355 }
356 printf("%d => %d", m_iter->first, m_iter->second);
357 }
358 printf("}\n");
359
360 /**
361 * SET TEST
362 */
363 set<int32_t> setout;
364 for (int32_t i = -2; i < 3; ++i) {
365 setout.insert(i);
366 }
367 printf("testSet({");
368 set<int32_t>::const_iterator s_iter;
369 first = true;
370 for (s_iter = setout.begin(); s_iter != setout.end(); ++s_iter) {
371 if (first) {
372 first = false;
373 } else {
374 printf(", ");
375 }
376 printf("%d", *s_iter);
377 }
378 printf("})");
Mark Slee1921d202007-01-24 19:43:06 +0000379 set<int32_t> setin;
380 testClient.testSet(setin, setout);
Mark Sleee8540632006-05-30 09:24:40 +0000381 printf(" = {");
382 first = true;
383 for (s_iter = setin.begin(); s_iter != setin.end(); ++s_iter) {
384 if (first) {
385 first = false;
386 } else {
387 printf(", ");
388 }
389 printf("%d", *s_iter);
390 }
391 printf("}\n");
392
393 /**
394 * LIST TEST
395 */
Mark Sleeb9acf982006-10-10 01:57:32 +0000396 vector<int32_t> listout;
Mark Sleee8540632006-05-30 09:24:40 +0000397 for (int32_t i = -2; i < 3; ++i) {
398 listout.push_back(i);
399 }
400 printf("testList({");
Mark Sleeb9acf982006-10-10 01:57:32 +0000401 vector<int32_t>::const_iterator l_iter;
Mark Sleee8540632006-05-30 09:24:40 +0000402 first = true;
403 for (l_iter = listout.begin(); l_iter != listout.end(); ++l_iter) {
404 if (first) {
405 first = false;
406 } else {
407 printf(", ");
408 }
409 printf("%d", *l_iter);
410 }
411 printf("})");
Mark Slee1921d202007-01-24 19:43:06 +0000412 vector<int32_t> listin;
413 testClient.testList(listin, listout);
Mark Sleee8540632006-05-30 09:24:40 +0000414 printf(" = {");
415 first = true;
416 for (l_iter = listin.begin(); l_iter != listin.end(); ++l_iter) {
417 if (first) {
418 first = false;
419 } else {
420 printf(", ");
421 }
422 printf("%d", *l_iter);
423 }
424 printf("}\n");
425
426 /**
427 * ENUM TEST
428 */
429 printf("testEnum(ONE)");
Bryan Duxbury833ae492010-09-27 17:26:02 +0000430 Numberz::type ret = testClient.testEnum(Numberz::ONE);
Mark Sleee8540632006-05-30 09:24:40 +0000431 printf(" = %d\n", ret);
432
433 printf("testEnum(TWO)");
Bryan Duxbury833ae492010-09-27 17:26:02 +0000434 ret = testClient.testEnum(Numberz::TWO);
Mark Sleee8540632006-05-30 09:24:40 +0000435 printf(" = %d\n", ret);
436
437 printf("testEnum(THREE)");
Bryan Duxbury833ae492010-09-27 17:26:02 +0000438 ret = testClient.testEnum(Numberz::THREE);
Mark Sleee8540632006-05-30 09:24:40 +0000439 printf(" = %d\n", ret);
440
441 printf("testEnum(FIVE)");
Bryan Duxbury833ae492010-09-27 17:26:02 +0000442 ret = testClient.testEnum(Numberz::FIVE);
Mark Sleee8540632006-05-30 09:24:40 +0000443 printf(" = %d\n", ret);
444
445 printf("testEnum(EIGHT)");
Bryan Duxbury833ae492010-09-27 17:26:02 +0000446 ret = testClient.testEnum(Numberz::EIGHT);
Mark Sleee8540632006-05-30 09:24:40 +0000447 printf(" = %d\n", ret);
448
449 /**
450 * TYPEDEF TEST
451 */
452 printf("testTypedef(309858235082523)");
Marc Slemkobf4fd192006-08-15 21:29:39 +0000453 UserId uid = testClient.testTypedef(309858235082523LL);
David Reissbc3dddb2007-08-22 23:20:24 +0000454 printf(" = %"PRId64"\n", uid);
Mark Sleee8540632006-05-30 09:24:40 +0000455
456 /**
457 * NESTED MAP TEST
458 */
459 printf("testMapMap(1)");
Mark Slee1921d202007-01-24 19:43:06 +0000460 map<int32_t, map<int32_t, int32_t> > mm;
461 testClient.testMapMap(mm, 1);
Mark Sleee8540632006-05-30 09:24:40 +0000462 printf(" = {");
463 map<int32_t, map<int32_t, int32_t> >::const_iterator mi;
464 for (mi = mm.begin(); mi != mm.end(); ++mi) {
465 printf("%d => {", mi->first);
466 map<int32_t, int32_t>::const_iterator mi2;
467 for (mi2 = mi->second.begin(); mi2 != mi->second.end(); ++mi2) {
468 printf("%d => %d, ", mi2->first, mi2->second);
469 }
470 printf("}, ");
471 }
472 printf("}\n");
473
474 /**
475 * INSANITY TEST
476 */
477 Insanity insane;
Bryan Duxbury833ae492010-09-27 17:26:02 +0000478 insane.userMap.insert(make_pair(Numberz::FIVE, 5000));
Mark Sleee8540632006-05-30 09:24:40 +0000479 Xtruct truck;
480 truck.string_thing = "Truck";
481 truck.byte_thing = 8;
Mark Sleee8540632006-05-30 09:24:40 +0000482 truck.i32_thing = 8;
Mark Sleee8540632006-05-30 09:24:40 +0000483 truck.i64_thing = 8;
484 insane.xtructs.push_back(truck);
485 printf("testInsanity()");
Bryan Duxbury833ae492010-09-27 17:26:02 +0000486 map<UserId, map<Numberz::type,Insanity> > whoa;
Mark Slee1921d202007-01-24 19:43:06 +0000487 testClient.testInsanity(whoa, insane);
Mark Sleee8540632006-05-30 09:24:40 +0000488 printf(" = {");
Bryan Duxbury833ae492010-09-27 17:26:02 +0000489 map<UserId, map<Numberz::type,Insanity> >::const_iterator i_iter;
Mark Sleee8540632006-05-30 09:24:40 +0000490 for (i_iter = whoa.begin(); i_iter != whoa.end(); ++i_iter) {
David Reissbc3dddb2007-08-22 23:20:24 +0000491 printf("%"PRId64" => {", i_iter->first);
Bryan Duxbury833ae492010-09-27 17:26:02 +0000492 map<Numberz::type,Insanity>::const_iterator i2_iter;
Mark Sleee8540632006-05-30 09:24:40 +0000493 for (i2_iter = i_iter->second.begin();
494 i2_iter != i_iter->second.end();
495 ++i2_iter) {
496 printf("%d => {", i2_iter->first);
Bryan Duxbury833ae492010-09-27 17:26:02 +0000497 map<Numberz::type, UserId> userMap = i2_iter->second.userMap;
498 map<Numberz::type, UserId>::const_iterator um;
Mark Sleee8540632006-05-30 09:24:40 +0000499 printf("{");
500 for (um = userMap.begin(); um != userMap.end(); ++um) {
David Reissbc3dddb2007-08-22 23:20:24 +0000501 printf("%d => %"PRId64", ", um->first, um->second);
Mark Sleee8540632006-05-30 09:24:40 +0000502 }
503 printf("}, ");
504
Mark Sleeb9acf982006-10-10 01:57:32 +0000505 vector<Xtruct> xtructs = i2_iter->second.xtructs;
506 vector<Xtruct>::const_iterator x;
Mark Sleee8540632006-05-30 09:24:40 +0000507 printf("{");
508 for (x = xtructs.begin(); x != xtructs.end(); ++x) {
David Reissbc3dddb2007-08-22 23:20:24 +0000509 printf("{\"%s\", %d, %d, %"PRId64"}, ",
Mark Sleee8540632006-05-30 09:24:40 +0000510 x->string_thing.c_str(),
511 (int)x->byte_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000512 x->i32_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000513 x->i64_thing);
514 }
515 printf("}");
516
517 printf("}, ");
518 }
519 printf("}, ");
520 }
521 printf("}\n");
522
Marc Slemko71d4e472006-08-15 22:34:04 +0000523 /* test exception */
Mark Slee95771002006-06-07 06:53:25 +0000524
Marc Slemkobf4fd192006-08-15 21:29:39 +0000525 try {
Marc Slemko71d4e472006-08-15 22:34:04 +0000526 printf("testClient.testException(\"Xception\") =>");
527 testClient.testException("Xception");
528 printf(" void\nFAILURE\n");
David Reiss0c90f6f2008-02-06 22:18:40 +0000529
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000530 } catch(Xception& e) {
Marc Slemko71d4e472006-08-15 22:34:04 +0000531 printf(" {%u, \"%s\"}\n", e.errorCode, e.message.c_str());
Marc Slemkobf4fd192006-08-15 21:29:39 +0000532 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000533
Marc Slemkobf4fd192006-08-15 21:29:39 +0000534 try {
Marc Slemko71d4e472006-08-15 22:34:04 +0000535 printf("testClient.testException(\"success\") =>");
536 testClient.testException("success");
537 printf(" void\n");
538 } catch(...) {
539 printf(" exception\nFAILURE\n");
540 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000541
Marc Slemko71d4e472006-08-15 22:34:04 +0000542 /* test multi exception */
David Reiss0c90f6f2008-02-06 22:18:40 +0000543
Marc Slemko71d4e472006-08-15 22:34:04 +0000544 try {
545 printf("testClient.testMultiException(\"Xception\", \"test 1\") =>");
Mark Slee1921d202007-01-24 19:43:06 +0000546 Xtruct result;
547 testClient.testMultiException(result, "Xception", "test 1");
Marc Slemko71d4e472006-08-15 22:34:04 +0000548 printf(" result\nFAILURE\n");
Mark Sleed3d733a2006-09-01 22:19:06 +0000549 } catch(Xception& e) {
Marc Slemko71d4e472006-08-15 22:34:04 +0000550 printf(" {%u, \"%s\"}\n", e.errorCode, e.message.c_str());
551 }
552
553 try {
554 printf("testClient.testMultiException(\"Xception2\", \"test 2\") =>");
Mark Slee1921d202007-01-24 19:43:06 +0000555 Xtruct result;
556 testClient.testMultiException(result, "Xception2", "test 2");
Marc Slemko71d4e472006-08-15 22:34:04 +0000557 printf(" result\nFAILURE\n");
David Reiss0c90f6f2008-02-06 22:18:40 +0000558
Mark Sleed3d733a2006-09-01 22:19:06 +0000559 } catch(Xception2& e) {
Marc Slemko71d4e472006-08-15 22:34:04 +0000560 printf(" {%u, {\"%s\"}}\n", e.errorCode, e.struct_thing.string_thing.c_str());
Marc Slemkobf4fd192006-08-15 21:29:39 +0000561 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000562
Marc Slemko71d4e472006-08-15 22:34:04 +0000563 try {
564 printf("testClient.testMultiException(\"success\", \"test 3\") =>");
Mark Slee1921d202007-01-24 19:43:06 +0000565 Xtruct result;
566 testClient.testMultiException(result, "success", "test 3");
Marc Slemko71d4e472006-08-15 22:34:04 +0000567 printf(" {{\"%s\"}}\n", result.string_thing.c_str());
568 } catch(...) {
569 printf(" exception\nFAILURE\n");
570 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000571
David Reissc51986f2009-03-24 20:01:25 +0000572 /* test oneway void */
David Reiss2ab6fe82008-02-18 02:11:44 +0000573 {
David Reiss6ce401d2009-03-24 20:01:58 +0000574 printf("testClient.testOneway(3) =>");
575 uint64_t startOneway = now();
576 testClient.testOneway(3);
577 uint64_t elapsed = now() - startOneway;
David Reiss2ab6fe82008-02-18 02:11:44 +0000578 if (elapsed > 200 * 1000) { // 0.2 seconds
579 printf(" FAILURE - took %.2f ms\n", (double)elapsed/1000.0);
580 } else {
581 printf(" success - took %.2f ms\n", (double)elapsed/1000.0);
582 }
583 }
584
David Reiss2845b522008-02-18 02:11:52 +0000585 /**
David Reissc51986f2009-03-24 20:01:25 +0000586 * redo a simple test after the oneway to make sure we aren't "off by one" --
587 * if the server treated oneway void like normal void, this next test will
David Reiss2845b522008-02-18 02:11:52 +0000588 * fail since it will get the void confirmation rather than the correct
589 * result. In this circumstance, the client will throw the exception:
590 *
591 * TApplicationException: Wrong method namea
592 */
593 /**
594 * I32 TEST
595 */
596 printf("re-test testI32(-1)");
597 i32 = testClient.testI32(-1);
598 printf(" = %d\n", i32);
599
600
Marc Slemkobf4fd192006-08-15 21:29:39 +0000601 uint64_t stop = now();
Mark Sleed788b2e2006-09-07 01:26:35 +0000602 uint64_t tot = stop-start;
603
David Reissbc3dddb2007-08-22 23:20:24 +0000604 printf("Total time: %"PRIu64" us\n", stop-start);
David Reiss0c90f6f2008-02-06 22:18:40 +0000605
Mark Sleed788b2e2006-09-07 01:26:35 +0000606 time_tot += tot;
607 if (time_min == 0 || tot < time_min) {
608 time_min = tot;
609 }
610 if (tot > time_max) {
611 time_max = tot;
612 }
613
Mark Sleea3302652006-10-25 19:03:32 +0000614 transport->close();
Mark Sleee8540632006-05-30 09:24:40 +0000615 }
616
Marc Slemko6be374b2006-08-04 03:16:25 +0000617 // printf("\nSocket syscalls: %u", g_socket_syscalls);
Mark Sleee8540632006-05-30 09:24:40 +0000618 printf("\nAll tests done.\n");
Mark Sleed788b2e2006-09-07 01:26:35 +0000619
620 uint64_t time_avg = time_tot / numTests;
621
David Reissbc3dddb2007-08-22 23:20:24 +0000622 printf("Min time: %"PRIu64" us\n", time_min);
623 printf("Max time: %"PRIu64" us\n", time_max);
624 printf("Avg time: %"PRIu64" us\n", time_avg);
Mark Sleed788b2e2006-09-07 01:26:35 +0000625
Mark Sleee8540632006-05-30 09:24:40 +0000626 return 0;
627}