blob: c1d6e070087b0fc79a9eb04435dd814ebb470b66 [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
Marc Slemko6be374b2006-08-04 03:16:25 +000049//extern uint32_t g_socket_syscalls;
Mark Slee95771002006-06-07 06:53:25 +000050
51// Current time, microseconds since the epoch
52uint64_t now()
53{
Roger Meier5f9614c2010-11-21 16:59:05 +000054 int64_t ret;
Mark Slee95771002006-06-07 06:53:25 +000055 struct timeval tv;
David Reiss0c90f6f2008-02-06 22:18:40 +000056
Mark Slee95771002006-06-07 06:53:25 +000057 gettimeofday(&tv, NULL);
58 ret = tv.tv_sec;
59 ret = ret*1000*1000 + tv.tv_usec;
60 return ret;
61}
62
Roger Meier7e056e72011-07-17 07:28:28 +000063static void testString_clientReturn(const char* host, int port, event_base *base, TProtocolFactory* protocolFactory, ThriftTestCobClient* client) {
Roger Meiera8cef6e2011-07-17 18:55:59 +000064 (void) host;
65 (void) port;
66 (void) protocolFactory;
Roger Meier7e056e72011-07-17 07:28:28 +000067 try {
68 string s;
69 client->recv_testString(s);
70 cout << "testString: " << s << endl;
71 } catch (TException& exn) {
72 cout << "Error: " << exn.what() << endl;
73 }
74
75 event_base_loopbreak(base); // end test
76}
77
78static void testVoid_clientReturn(const char* host, int port, event_base *base, TProtocolFactory* protocolFactory, ThriftTestCobClient* client) {
79 try {
80 client->recv_testVoid();
81 cout << "testVoid" << endl;
82
83 // next test
84 delete client;
85 shared_ptr<TAsyncChannel> channel(new TEvhttpClientChannel(host, "/", host, port, base));
86 client = new ThriftTestCobClient(channel, protocolFactory);
Roger Meier3faaedf2011-10-02 10:51:45 +000087 client->testString(tr1::bind(testString_clientReturn, host, port, base, protocolFactory, std::tr1::placeholders::_1), "Test");
Roger Meier7e056e72011-07-17 07:28:28 +000088 } catch (TException& exn) {
89 cout << "Error: " << exn.what() << endl;
90 }
91}
92
Mark Sleee8540632006-05-30 09:24:40 +000093int main(int argc, char** argv) {
94 string host = "localhost";
95 int port = 9090;
96 int numTests = 1;
Bryan Duxburycd9aea12011-02-22 18:12:06 +000097 bool ssl = false;
Roger Meierca142b02011-06-07 17:59:07 +000098 string transport_type = "buffered";
99 string protocol_type = "binary";
100 string domain_socket = "";
Mark Sleee8540632006-05-30 09:24:40 +0000101
Roger Meierca142b02011-06-07 17:59:07 +0000102 program_options::options_description desc("Allowed options");
103 desc.add_options()
104 ("help,h", "produce help message")
105 ("host", program_options::value<string>(&host)->default_value(host), "Host to connect")
106 ("port", program_options::value<int>(&port)->default_value(port), "Port number to connect")
107 ("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 +0000108 ("transport", program_options::value<string>(&transport_type)->default_value(transport_type), "Transport: buffered, framed, http, evhttp")
Roger Meierca142b02011-06-07 17:59:07 +0000109 ("protocol", program_options::value<string>(&protocol_type)->default_value(protocol_type), "Protocol: binary, json")
110 ("ssl", "Encrypted Transport using SSL")
111 ("testloops,n", program_options::value<int>(&numTests)->default_value(numTests), "Number of Tests")
112 ;
113
114 program_options::variables_map vm;
115 program_options::store(program_options::parse_command_line(argc, argv, desc), vm);
116 program_options::notify(vm);
117
118 if (vm.count("help")) {
119 cout << desc << "\n";
120 return 1;
Mark Sleee8540632006-05-30 09:24:40 +0000121 }
Mark Sleea3302652006-10-25 19:03:32 +0000122
Roger Meierca142b02011-06-07 17:59:07 +0000123 try {
124 if (!protocol_type.empty()) {
125 if (protocol_type == "binary") {
126 } else if (protocol_type == "json") {
127 } else {
128 throw invalid_argument("Unknown protocol type "+protocol_type);
129 }
130 }
131
132 if (!transport_type.empty()) {
133 if (transport_type == "buffered") {
134 } else if (transport_type == "framed") {
135 } else if (transport_type == "http") {
Roger Meier7e056e72011-07-17 07:28:28 +0000136 } else if (transport_type == "evhttp") {
Roger Meierca142b02011-06-07 17:59:07 +0000137 } else {
138 throw invalid_argument("Unknown transport type "+transport_type);
139 }
140 }
141
142 } catch (std::exception& e) {
143 cerr << e.what() << endl;
144 cout << desc << "\n";
145 return 1;
146 }
147
148 if (vm.count("ssl")) {
149 ssl = true;
150 }
151
152 shared_ptr<TTransport> transport;
153 shared_ptr<TProtocol> protocol;
154
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000155 shared_ptr<TSocket> socket;
156 shared_ptr<TSSLSocketFactory> factory;
Roger Meierca142b02011-06-07 17:59:07 +0000157
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000158 if (ssl) {
159 factory = shared_ptr<TSSLSocketFactory>(new TSSLSocketFactory());
160 factory->ciphers("ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
161 factory->loadTrustedCertificates("./trusted-ca-certificate.pem");
162 factory->authenticate(true);
163 socket = factory->createSocket(host, port);
164 } else {
Roger Meierca142b02011-06-07 17:59:07 +0000165 if (domain_socket != "") {
166 socket = shared_ptr<TSocket>(new TSocket(domain_socket));
167 port = 0;
168 }
169 else {
170 socket = shared_ptr<TSocket>(new TSocket(host, port));
171 }
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000172 }
Mark Sleea3302652006-10-25 19:03:32 +0000173
Roger Meierca142b02011-06-07 17:59:07 +0000174 if (transport_type.compare("http") == 0) {
175 shared_ptr<TTransport> httpSocket(new THttpClient(socket, host, "/service"));
176 transport = httpSocket;
177 } else if (transport_type.compare("framed") == 0){
Mark Sleea3302652006-10-25 19:03:32 +0000178 shared_ptr<TFramedTransport> framedSocket(new TFramedTransport(socket));
Mark Sleea3302652006-10-25 19:03:32 +0000179 transport = framedSocket;
Roger Meierca142b02011-06-07 17:59:07 +0000180 } else{
Mark Sleea3302652006-10-25 19:03:32 +0000181 shared_ptr<TBufferedTransport> bufferedSocket(new TBufferedTransport(socket));
182 transport = bufferedSocket;
183 }
184
Roger Meierca142b02011-06-07 17:59:07 +0000185 if (protocol_type.compare("json") == 0) {
186 shared_ptr<TProtocol> jsonProtocol(new TJSONProtocol(transport));
187 protocol = jsonProtocol;
188 } else{
189 shared_ptr<TBinaryProtocol> binaryProtocol(new TBinaryProtocol(transport));
190 protocol = binaryProtocol;
191 }
192
193 // Connection info
194 cout << "Connecting (" << transport_type << "/" << protocol_type << ") to: " << domain_socket;
195 if (port != 0) {
196 cout << host << ":" << port;
197 }
198 cout << endl;
199
Roger Meier7e056e72011-07-17 07:28:28 +0000200 if (transport_type.compare("evhttp") == 0) {
201 event_base *base = event_base_new();
202 cout << "Libevent Version: " << event_get_version() << endl;
203 cout << "Libevent Method: " << event_base_get_method(base) << endl;
204#if LIBEVENT_VERSION_NUMBER >= 0x02000000
205 cout << "Libevent Features: 0x" << hex << event_base_get_features(base) << endl;
206#endif
207
208 shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());
209
210 shared_ptr<TAsyncChannel> channel(new TEvhttpClientChannel(host.c_str(), "/", host.c_str(), port, base));
211 ThriftTestCobClient* client = new ThriftTestCobClient(channel, protocolFactory.get());
Roger Meier3faaedf2011-10-02 10:51:45 +0000212 client->testVoid(tr1::bind(testVoid_clientReturn, host.c_str(), port, base, protocolFactory.get(), std::tr1::placeholders::_1));
Roger Meier7e056e72011-07-17 07:28:28 +0000213
214 event_base_loop(base, 0);
215 return 0;
216 }
217
218
Roger Meierca142b02011-06-07 17:59:07 +0000219 ThriftTestClient testClient(protocol);
Mark Sleed788b2e2006-09-07 01:26:35 +0000220
221 uint64_t time_min = 0;
222 uint64_t time_max = 0;
223 uint64_t time_tot = 0;
David Reiss0c90f6f2008-02-06 22:18:40 +0000224
Mark Sleee8540632006-05-30 09:24:40 +0000225 int test = 0;
226 for (test = 0; test < numTests; ++test) {
Mark Slee95771002006-06-07 06:53:25 +0000227
Mark Slee95771002006-06-07 06:53:25 +0000228 try {
Mark Sleea3302652006-10-25 19:03:32 +0000229 transport->open();
Mark Slee95771002006-06-07 06:53:25 +0000230 } catch (TTransportException& ttx) {
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000231 printf("Connect failed: %s\n", ttx.what());
Mark Sleee8540632006-05-30 09:24:40 +0000232 continue;
233 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000234
Mark Sleed788b2e2006-09-07 01:26:35 +0000235 /**
236 * CONNECT TEST
237 */
238 printf("Test #%d, connect %s:%d\n", test+1, host.c_str(), port);
Mark Slee95771002006-06-07 06:53:25 +0000239
240 uint64_t start = now();
David Reiss0c90f6f2008-02-06 22:18:40 +0000241
Mark Sleee8540632006-05-30 09:24:40 +0000242 /**
243 * VOID TEST
244 */
Mark Sleee129a2d2007-02-21 05:17:48 +0000245 try {
246 printf("testVoid()");
247 testClient.testVoid();
248 printf(" = void\n");
249 } catch (TApplicationException tax) {
250 printf("%s\n", tax.what());
251 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000252
Mark Sleee8540632006-05-30 09:24:40 +0000253 /**
254 * STRING TEST
255 */
256 printf("testString(\"Test\")");
Mark Slee1921d202007-01-24 19:43:06 +0000257 string s;
258 testClient.testString(s, "Test");
Mark Sleee8540632006-05-30 09:24:40 +0000259 printf(" = \"%s\"\n", s.c_str());
David Reiss0c90f6f2008-02-06 22:18:40 +0000260
Mark Sleee8540632006-05-30 09:24:40 +0000261 /**
262 * BYTE TEST
263 */
264 printf("testByte(1)");
265 uint8_t u8 = testClient.testByte(1);
266 printf(" = %d\n", (int)u8);
David Reiss0c90f6f2008-02-06 22:18:40 +0000267
Mark Sleee8540632006-05-30 09:24:40 +0000268 /**
269 * I32 TEST
270 */
271 printf("testI32(-1)");
272 int32_t i32 = testClient.testI32(-1);
273 printf(" = %d\n", i32);
274
275 /**
Mark Sleee8540632006-05-30 09:24:40 +0000276 * I64 TEST
277 */
278 printf("testI64(-34359738368)");
Marc Slemkobf4fd192006-08-15 21:29:39 +0000279 int64_t i64 = testClient.testI64(-34359738368LL);
David Reissbc3dddb2007-08-22 23:20:24 +0000280 printf(" = %"PRId64"\n", i64);
Mark Sleec98d0502006-09-06 02:42:25 +0000281
282 /**
283 * DOUBLE TEST
284 */
285 printf("testDouble(-5.2098523)");
286 double dub = testClient.testDouble(-5.2098523);
Roger Meiera8cef6e2011-07-17 18:55:59 +0000287 printf(" = %f\n", dub);
David Reiss0c90f6f2008-02-06 22:18:40 +0000288
Mark Sleee8540632006-05-30 09:24:40 +0000289 /**
290 * STRUCT TEST
291 */
Mark Slee6e536442006-06-30 18:28:50 +0000292 printf("testStruct({\"Zero\", 1, -3, -5})");
Mark Sleee8540632006-05-30 09:24:40 +0000293 Xtruct out;
294 out.string_thing = "Zero";
295 out.byte_thing = 1;
Mark Sleee8540632006-05-30 09:24:40 +0000296 out.i32_thing = -3;
Mark Sleee8540632006-05-30 09:24:40 +0000297 out.i64_thing = -5;
Mark Slee1921d202007-01-24 19:43:06 +0000298 Xtruct in;
299 testClient.testStruct(in, out);
David Reissbc3dddb2007-08-22 23:20:24 +0000300 printf(" = {\"%s\", %d, %d, %"PRId64"}\n",
Mark Sleee8540632006-05-30 09:24:40 +0000301 in.string_thing.c_str(),
302 (int)in.byte_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000303 in.i32_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000304 in.i64_thing);
David Reiss0c90f6f2008-02-06 22:18:40 +0000305
Mark Sleee8540632006-05-30 09:24:40 +0000306 /**
307 * NESTED STRUCT TEST
308 */
Mark Slee6e536442006-06-30 18:28:50 +0000309 printf("testNest({1, {\"Zero\", 1, -3, -5}), 5}");
Mark Sleee8540632006-05-30 09:24:40 +0000310 Xtruct2 out2;
311 out2.byte_thing = 1;
312 out2.struct_thing = out;
313 out2.i32_thing = 5;
Mark Slee1921d202007-01-24 19:43:06 +0000314 Xtruct2 in2;
315 testClient.testNest(in2, out2);
Mark Sleee8540632006-05-30 09:24:40 +0000316 in = in2.struct_thing;
David Reissbc3dddb2007-08-22 23:20:24 +0000317 printf(" = {%d, {\"%s\", %d, %d, %"PRId64"}, %d}\n",
Mark Sleee8540632006-05-30 09:24:40 +0000318 in2.byte_thing,
319 in.string_thing.c_str(),
320 (int)in.byte_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000321 in.i32_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000322 in.i64_thing,
David Reiss0c90f6f2008-02-06 22:18:40 +0000323 in2.i32_thing);
Mark Sleee8540632006-05-30 09:24:40 +0000324
325 /**
326 * MAP TEST
327 */
328 map<int32_t,int32_t> mapout;
329 for (int32_t i = 0; i < 5; ++i) {
330 mapout.insert(make_pair(i, i-10));
331 }
332 printf("testMap({");
333 map<int32_t, int32_t>::const_iterator m_iter;
334 bool first = true;
335 for (m_iter = mapout.begin(); m_iter != mapout.end(); ++m_iter) {
336 if (first) {
337 first = false;
338 } else {
339 printf(", ");
340 }
341 printf("%d => %d", m_iter->first, m_iter->second);
342 }
343 printf("})");
Mark Slee1921d202007-01-24 19:43:06 +0000344 map<int32_t,int32_t> mapin;
345 testClient.testMap(mapin, mapout);
Mark Sleee8540632006-05-30 09:24:40 +0000346 printf(" = {");
347 first = true;
348 for (m_iter = mapin.begin(); m_iter != mapin.end(); ++m_iter) {
349 if (first) {
350 first = false;
351 } else {
352 printf(", ");
353 }
354 printf("%d => %d", m_iter->first, m_iter->second);
355 }
356 printf("}\n");
357
358 /**
359 * SET TEST
360 */
361 set<int32_t> setout;
362 for (int32_t i = -2; i < 3; ++i) {
363 setout.insert(i);
364 }
365 printf("testSet({");
366 set<int32_t>::const_iterator s_iter;
367 first = true;
368 for (s_iter = setout.begin(); s_iter != setout.end(); ++s_iter) {
369 if (first) {
370 first = false;
371 } else {
372 printf(", ");
373 }
374 printf("%d", *s_iter);
375 }
376 printf("})");
Mark Slee1921d202007-01-24 19:43:06 +0000377 set<int32_t> setin;
378 testClient.testSet(setin, setout);
Mark Sleee8540632006-05-30 09:24:40 +0000379 printf(" = {");
380 first = true;
381 for (s_iter = setin.begin(); s_iter != setin.end(); ++s_iter) {
382 if (first) {
383 first = false;
384 } else {
385 printf(", ");
386 }
387 printf("%d", *s_iter);
388 }
389 printf("}\n");
390
391 /**
392 * LIST TEST
393 */
Mark Sleeb9acf982006-10-10 01:57:32 +0000394 vector<int32_t> listout;
Mark Sleee8540632006-05-30 09:24:40 +0000395 for (int32_t i = -2; i < 3; ++i) {
396 listout.push_back(i);
397 }
398 printf("testList({");
Mark Sleeb9acf982006-10-10 01:57:32 +0000399 vector<int32_t>::const_iterator l_iter;
Mark Sleee8540632006-05-30 09:24:40 +0000400 first = true;
401 for (l_iter = listout.begin(); l_iter != listout.end(); ++l_iter) {
402 if (first) {
403 first = false;
404 } else {
405 printf(", ");
406 }
407 printf("%d", *l_iter);
408 }
409 printf("})");
Mark Slee1921d202007-01-24 19:43:06 +0000410 vector<int32_t> listin;
411 testClient.testList(listin, listout);
Mark Sleee8540632006-05-30 09:24:40 +0000412 printf(" = {");
413 first = true;
414 for (l_iter = listin.begin(); l_iter != listin.end(); ++l_iter) {
415 if (first) {
416 first = false;
417 } else {
418 printf(", ");
419 }
420 printf("%d", *l_iter);
421 }
422 printf("}\n");
423
424 /**
425 * ENUM TEST
426 */
427 printf("testEnum(ONE)");
Bryan Duxbury833ae492010-09-27 17:26:02 +0000428 Numberz::type ret = testClient.testEnum(Numberz::ONE);
Mark Sleee8540632006-05-30 09:24:40 +0000429 printf(" = %d\n", ret);
430
431 printf("testEnum(TWO)");
Bryan Duxbury833ae492010-09-27 17:26:02 +0000432 ret = testClient.testEnum(Numberz::TWO);
Mark Sleee8540632006-05-30 09:24:40 +0000433 printf(" = %d\n", ret);
434
435 printf("testEnum(THREE)");
Bryan Duxbury833ae492010-09-27 17:26:02 +0000436 ret = testClient.testEnum(Numberz::THREE);
Mark Sleee8540632006-05-30 09:24:40 +0000437 printf(" = %d\n", ret);
438
439 printf("testEnum(FIVE)");
Bryan Duxbury833ae492010-09-27 17:26:02 +0000440 ret = testClient.testEnum(Numberz::FIVE);
Mark Sleee8540632006-05-30 09:24:40 +0000441 printf(" = %d\n", ret);
442
443 printf("testEnum(EIGHT)");
Bryan Duxbury833ae492010-09-27 17:26:02 +0000444 ret = testClient.testEnum(Numberz::EIGHT);
Mark Sleee8540632006-05-30 09:24:40 +0000445 printf(" = %d\n", ret);
446
447 /**
448 * TYPEDEF TEST
449 */
450 printf("testTypedef(309858235082523)");
Marc Slemkobf4fd192006-08-15 21:29:39 +0000451 UserId uid = testClient.testTypedef(309858235082523LL);
David Reissbc3dddb2007-08-22 23:20:24 +0000452 printf(" = %"PRId64"\n", uid);
Mark Sleee8540632006-05-30 09:24:40 +0000453
454 /**
455 * NESTED MAP TEST
456 */
457 printf("testMapMap(1)");
Mark Slee1921d202007-01-24 19:43:06 +0000458 map<int32_t, map<int32_t, int32_t> > mm;
459 testClient.testMapMap(mm, 1);
Mark Sleee8540632006-05-30 09:24:40 +0000460 printf(" = {");
461 map<int32_t, map<int32_t, int32_t> >::const_iterator mi;
462 for (mi = mm.begin(); mi != mm.end(); ++mi) {
463 printf("%d => {", mi->first);
464 map<int32_t, int32_t>::const_iterator mi2;
465 for (mi2 = mi->second.begin(); mi2 != mi->second.end(); ++mi2) {
466 printf("%d => %d, ", mi2->first, mi2->second);
467 }
468 printf("}, ");
469 }
470 printf("}\n");
471
472 /**
473 * INSANITY TEST
474 */
475 Insanity insane;
Bryan Duxbury833ae492010-09-27 17:26:02 +0000476 insane.userMap.insert(make_pair(Numberz::FIVE, 5000));
Mark Sleee8540632006-05-30 09:24:40 +0000477 Xtruct truck;
478 truck.string_thing = "Truck";
479 truck.byte_thing = 8;
Mark Sleee8540632006-05-30 09:24:40 +0000480 truck.i32_thing = 8;
Mark Sleee8540632006-05-30 09:24:40 +0000481 truck.i64_thing = 8;
482 insane.xtructs.push_back(truck);
483 printf("testInsanity()");
Bryan Duxbury833ae492010-09-27 17:26:02 +0000484 map<UserId, map<Numberz::type,Insanity> > whoa;
Mark Slee1921d202007-01-24 19:43:06 +0000485 testClient.testInsanity(whoa, insane);
Mark Sleee8540632006-05-30 09:24:40 +0000486 printf(" = {");
Bryan Duxbury833ae492010-09-27 17:26:02 +0000487 map<UserId, map<Numberz::type,Insanity> >::const_iterator i_iter;
Mark Sleee8540632006-05-30 09:24:40 +0000488 for (i_iter = whoa.begin(); i_iter != whoa.end(); ++i_iter) {
David Reissbc3dddb2007-08-22 23:20:24 +0000489 printf("%"PRId64" => {", i_iter->first);
Bryan Duxbury833ae492010-09-27 17:26:02 +0000490 map<Numberz::type,Insanity>::const_iterator i2_iter;
Mark Sleee8540632006-05-30 09:24:40 +0000491 for (i2_iter = i_iter->second.begin();
492 i2_iter != i_iter->second.end();
493 ++i2_iter) {
494 printf("%d => {", i2_iter->first);
Bryan Duxbury833ae492010-09-27 17:26:02 +0000495 map<Numberz::type, UserId> userMap = i2_iter->second.userMap;
496 map<Numberz::type, UserId>::const_iterator um;
Mark Sleee8540632006-05-30 09:24:40 +0000497 printf("{");
498 for (um = userMap.begin(); um != userMap.end(); ++um) {
David Reissbc3dddb2007-08-22 23:20:24 +0000499 printf("%d => %"PRId64", ", um->first, um->second);
Mark Sleee8540632006-05-30 09:24:40 +0000500 }
501 printf("}, ");
502
Mark Sleeb9acf982006-10-10 01:57:32 +0000503 vector<Xtruct> xtructs = i2_iter->second.xtructs;
504 vector<Xtruct>::const_iterator x;
Mark Sleee8540632006-05-30 09:24:40 +0000505 printf("{");
506 for (x = xtructs.begin(); x != xtructs.end(); ++x) {
David Reissbc3dddb2007-08-22 23:20:24 +0000507 printf("{\"%s\", %d, %d, %"PRId64"}, ",
Mark Sleee8540632006-05-30 09:24:40 +0000508 x->string_thing.c_str(),
509 (int)x->byte_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000510 x->i32_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000511 x->i64_thing);
512 }
513 printf("}");
514
515 printf("}, ");
516 }
517 printf("}, ");
518 }
519 printf("}\n");
520
Marc Slemko71d4e472006-08-15 22:34:04 +0000521 /* test exception */
Mark Slee95771002006-06-07 06:53:25 +0000522
Marc Slemkobf4fd192006-08-15 21:29:39 +0000523 try {
Marc Slemko71d4e472006-08-15 22:34:04 +0000524 printf("testClient.testException(\"Xception\") =>");
525 testClient.testException("Xception");
526 printf(" void\nFAILURE\n");
David Reiss0c90f6f2008-02-06 22:18:40 +0000527
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000528 } catch(Xception& e) {
Marc Slemko71d4e472006-08-15 22:34:04 +0000529 printf(" {%u, \"%s\"}\n", e.errorCode, e.message.c_str());
Marc Slemkobf4fd192006-08-15 21:29:39 +0000530 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000531
Marc Slemkobf4fd192006-08-15 21:29:39 +0000532 try {
Marc Slemko71d4e472006-08-15 22:34:04 +0000533 printf("testClient.testException(\"success\") =>");
534 testClient.testException("success");
535 printf(" void\n");
536 } catch(...) {
537 printf(" exception\nFAILURE\n");
538 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000539
Marc Slemko71d4e472006-08-15 22:34:04 +0000540 /* test multi exception */
David Reiss0c90f6f2008-02-06 22:18:40 +0000541
Marc Slemko71d4e472006-08-15 22:34:04 +0000542 try {
543 printf("testClient.testMultiException(\"Xception\", \"test 1\") =>");
Mark Slee1921d202007-01-24 19:43:06 +0000544 Xtruct result;
545 testClient.testMultiException(result, "Xception", "test 1");
Marc Slemko71d4e472006-08-15 22:34:04 +0000546 printf(" result\nFAILURE\n");
Mark Sleed3d733a2006-09-01 22:19:06 +0000547 } catch(Xception& e) {
Marc Slemko71d4e472006-08-15 22:34:04 +0000548 printf(" {%u, \"%s\"}\n", e.errorCode, e.message.c_str());
549 }
550
551 try {
552 printf("testClient.testMultiException(\"Xception2\", \"test 2\") =>");
Mark Slee1921d202007-01-24 19:43:06 +0000553 Xtruct result;
554 testClient.testMultiException(result, "Xception2", "test 2");
Marc Slemko71d4e472006-08-15 22:34:04 +0000555 printf(" result\nFAILURE\n");
David Reiss0c90f6f2008-02-06 22:18:40 +0000556
Mark Sleed3d733a2006-09-01 22:19:06 +0000557 } catch(Xception2& e) {
Marc Slemko71d4e472006-08-15 22:34:04 +0000558 printf(" {%u, {\"%s\"}}\n", e.errorCode, e.struct_thing.string_thing.c_str());
Marc Slemkobf4fd192006-08-15 21:29:39 +0000559 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000560
Marc Slemko71d4e472006-08-15 22:34:04 +0000561 try {
562 printf("testClient.testMultiException(\"success\", \"test 3\") =>");
Mark Slee1921d202007-01-24 19:43:06 +0000563 Xtruct result;
564 testClient.testMultiException(result, "success", "test 3");
Marc Slemko71d4e472006-08-15 22:34:04 +0000565 printf(" {{\"%s\"}}\n", result.string_thing.c_str());
566 } catch(...) {
567 printf(" exception\nFAILURE\n");
568 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000569
David Reissc51986f2009-03-24 20:01:25 +0000570 /* test oneway void */
David Reiss2ab6fe82008-02-18 02:11:44 +0000571 {
David Reiss6ce401d2009-03-24 20:01:58 +0000572 printf("testClient.testOneway(3) =>");
573 uint64_t startOneway = now();
574 testClient.testOneway(3);
575 uint64_t elapsed = now() - startOneway;
David Reiss2ab6fe82008-02-18 02:11:44 +0000576 if (elapsed > 200 * 1000) { // 0.2 seconds
577 printf(" FAILURE - took %.2f ms\n", (double)elapsed/1000.0);
578 } else {
579 printf(" success - took %.2f ms\n", (double)elapsed/1000.0);
580 }
581 }
582
David Reiss2845b522008-02-18 02:11:52 +0000583 /**
David Reissc51986f2009-03-24 20:01:25 +0000584 * redo a simple test after the oneway to make sure we aren't "off by one" --
585 * if the server treated oneway void like normal void, this next test will
David Reiss2845b522008-02-18 02:11:52 +0000586 * fail since it will get the void confirmation rather than the correct
587 * result. In this circumstance, the client will throw the exception:
588 *
589 * TApplicationException: Wrong method namea
590 */
591 /**
592 * I32 TEST
593 */
594 printf("re-test testI32(-1)");
595 i32 = testClient.testI32(-1);
596 printf(" = %d\n", i32);
597
598
Marc Slemkobf4fd192006-08-15 21:29:39 +0000599 uint64_t stop = now();
Mark Sleed788b2e2006-09-07 01:26:35 +0000600 uint64_t tot = stop-start;
601
David Reissbc3dddb2007-08-22 23:20:24 +0000602 printf("Total time: %"PRIu64" us\n", stop-start);
David Reiss0c90f6f2008-02-06 22:18:40 +0000603
Mark Sleed788b2e2006-09-07 01:26:35 +0000604 time_tot += tot;
605 if (time_min == 0 || tot < time_min) {
606 time_min = tot;
607 }
608 if (tot > time_max) {
609 time_max = tot;
610 }
611
Mark Sleea3302652006-10-25 19:03:32 +0000612 transport->close();
Mark Sleee8540632006-05-30 09:24:40 +0000613 }
614
Marc Slemko6be374b2006-08-04 03:16:25 +0000615 // printf("\nSocket syscalls: %u", g_socket_syscalls);
Mark Sleee8540632006-05-30 09:24:40 +0000616 printf("\nAll tests done.\n");
Mark Sleed788b2e2006-09-07 01:26:35 +0000617
618 uint64_t time_avg = time_tot / numTests;
619
David Reissbc3dddb2007-08-22 23:20:24 +0000620 printf("Min time: %"PRIu64" us\n", time_min);
621 printf("Max time: %"PRIu64" us\n", time_max);
622 printf("Avg time: %"PRIu64" us\n", time_avg);
Mark Sleed788b2e2006-09-07 01:26:35 +0000623
Mark Sleee8540632006-05-30 09:24:40 +0000624 return 0;
625}