blob: fbf04f0bf365059a74ba47e7a3ddf3f93c023c21 [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>
Roger Meier49ff8b12012-04-13 09:12:31 +000026#include <thrift/protocol/TBinaryProtocol.h>
27#include <thrift/protocol/TJSONProtocol.h>
28#include <thrift/transport/THttpClient.h>
29#include <thrift/transport/TTransportUtils.h>
30#include <thrift/transport/TSocket.h>
31#include <thrift/transport/TSSLSocket.h>
32#include <thrift/async/TEvhttpClientChannel.h>
33#include <thrift/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;
Roger Meier611f90c2011-12-11 22:08:51 +000085 boost::shared_ptr<TAsyncChannel> channel(new TEvhttpClientChannel(host, "/", host, port, base));
Roger Meier7e056e72011-07-17 07:28:28 +000086 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
Roger Meier611f90c2011-12-11 22:08:51 +0000152 boost::shared_ptr<TTransport> transport;
153 boost::shared_ptr<TProtocol> protocol;
Roger Meierca142b02011-06-07 17:59:07 +0000154
Roger Meier611f90c2011-12-11 22:08:51 +0000155 boost::shared_ptr<TSocket> socket;
156 boost::shared_ptr<TSSLSocketFactory> factory;
Roger Meierca142b02011-06-07 17:59:07 +0000157
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000158 if (ssl) {
Roger Meier611f90c2011-12-11 22:08:51 +0000159 factory = boost::shared_ptr<TSSLSocketFactory>(new TSSLSocketFactory());
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000160 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 != "") {
Roger Meier611f90c2011-12-11 22:08:51 +0000166 socket = boost::shared_ptr<TSocket>(new TSocket(domain_socket));
Roger Meierca142b02011-06-07 17:59:07 +0000167 port = 0;
168 }
169 else {
Roger Meier611f90c2011-12-11 22:08:51 +0000170 socket = boost::shared_ptr<TSocket>(new TSocket(host, port));
Roger Meierca142b02011-06-07 17:59:07 +0000171 }
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) {
Roger Meier611f90c2011-12-11 22:08:51 +0000175 boost::shared_ptr<TTransport> httpSocket(new THttpClient(socket, host, "/service"));
Roger Meierca142b02011-06-07 17:59:07 +0000176 transport = httpSocket;
177 } else if (transport_type.compare("framed") == 0){
Roger Meier611f90c2011-12-11 22:08:51 +0000178 boost::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{
Roger Meier611f90c2011-12-11 22:08:51 +0000181 boost::shared_ptr<TBufferedTransport> bufferedSocket(new TBufferedTransport(socket));
Mark Sleea3302652006-10-25 19:03:32 +0000182 transport = bufferedSocket;
183 }
184
Roger Meierca142b02011-06-07 17:59:07 +0000185 if (protocol_type.compare("json") == 0) {
Roger Meier611f90c2011-12-11 22:08:51 +0000186 boost::shared_ptr<TProtocol> jsonProtocol(new TJSONProtocol(transport));
Roger Meierca142b02011-06-07 17:59:07 +0000187 protocol = jsonProtocol;
188 } else{
Roger Meier611f90c2011-12-11 22:08:51 +0000189 boost::shared_ptr<TBinaryProtocol> binaryProtocol(new TBinaryProtocol(transport));
Roger Meierca142b02011-06-07 17:59:07 +0000190 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
Roger Meier611f90c2011-12-11 22:08:51 +0000208 boost::shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());
Roger Meier7e056e72011-07-17 07:28:28 +0000209
Roger Meier611f90c2011-12-11 22:08:51 +0000210 boost::shared_ptr<TAsyncChannel> channel(new TEvhttpClientChannel(host.c_str(), "/", host.c_str(), port, base));
Roger Meier7e056e72011-07-17 07:28:28 +0000211 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
Roger Meier4fce9602012-05-04 06:22:09 +0000225 int failCount = 0;
Mark Sleee8540632006-05-30 09:24:40 +0000226 int test = 0;
227 for (test = 0; test < numTests; ++test) {
Mark Slee95771002006-06-07 06:53:25 +0000228
Mark Slee95771002006-06-07 06:53:25 +0000229 try {
Mark Sleea3302652006-10-25 19:03:32 +0000230 transport->open();
Mark Slee95771002006-06-07 06:53:25 +0000231 } catch (TTransportException& ttx) {
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000232 printf("Connect failed: %s\n", ttx.what());
Roger Meier5b1e3c72011-12-08 13:20:12 +0000233 return 1;
Mark Sleee8540632006-05-30 09:24:40 +0000234 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000235
Mark Sleed788b2e2006-09-07 01:26:35 +0000236 /**
237 * CONNECT TEST
238 */
239 printf("Test #%d, connect %s:%d\n", test+1, host.c_str(), port);
Mark Slee95771002006-06-07 06:53:25 +0000240
241 uint64_t start = now();
David Reiss0c90f6f2008-02-06 22:18:40 +0000242
Mark Sleee8540632006-05-30 09:24:40 +0000243 /**
244 * VOID TEST
245 */
Mark Sleee129a2d2007-02-21 05:17:48 +0000246 try {
247 printf("testVoid()");
248 testClient.testVoid();
249 printf(" = void\n");
Roger Meier4fce9602012-05-04 06:22:09 +0000250 } catch (TApplicationException& tax) {
Mark Sleee129a2d2007-02-21 05:17:48 +0000251 printf("%s\n", tax.what());
Roger Meier4fce9602012-05-04 06:22:09 +0000252 failCount++;
Mark Sleee129a2d2007-02-21 05:17:48 +0000253 }
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());
Roger Meier4fce9602012-05-04 06:22:09 +0000262 if (s != "Test")
263 failCount++;
David Reiss0c90f6f2008-02-06 22:18:40 +0000264
Mark Sleee8540632006-05-30 09:24:40 +0000265 /**
266 * BYTE TEST
267 */
268 printf("testByte(1)");
269 uint8_t u8 = testClient.testByte(1);
270 printf(" = %d\n", (int)u8);
Roger Meier4fce9602012-05-04 06:22:09 +0000271 if (u8 != 1)
272 failCount++;
David Reiss0c90f6f2008-02-06 22:18:40 +0000273
Mark Sleee8540632006-05-30 09:24:40 +0000274 /**
275 * I32 TEST
276 */
277 printf("testI32(-1)");
278 int32_t i32 = testClient.testI32(-1);
279 printf(" = %d\n", i32);
Roger Meier4fce9602012-05-04 06:22:09 +0000280 if (i32 != -1)
281 failCount++;
Mark Sleee8540632006-05-30 09:24:40 +0000282
283 /**
Mark Sleee8540632006-05-30 09:24:40 +0000284 * I64 TEST
285 */
286 printf("testI64(-34359738368)");
Marc Slemkobf4fd192006-08-15 21:29:39 +0000287 int64_t i64 = testClient.testI64(-34359738368LL);
David Reissbc3dddb2007-08-22 23:20:24 +0000288 printf(" = %"PRId64"\n", i64);
Roger Meier4fce9602012-05-04 06:22:09 +0000289 if (i64 != -34359738368LL)
290 failCount++;
Mark Sleec98d0502006-09-06 02:42:25 +0000291 /**
292 * DOUBLE TEST
293 */
294 printf("testDouble(-5.2098523)");
295 double dub = testClient.testDouble(-5.2098523);
Roger Meiera8cef6e2011-07-17 18:55:59 +0000296 printf(" = %f\n", dub);
Roger Meier4fce9602012-05-04 06:22:09 +0000297 if ((dub - (-5.2098523)) > 0.001)
298 failCount++;
David Reiss0c90f6f2008-02-06 22:18:40 +0000299
Mark Sleee8540632006-05-30 09:24:40 +0000300 /**
301 * STRUCT TEST
302 */
Mark Slee6e536442006-06-30 18:28:50 +0000303 printf("testStruct({\"Zero\", 1, -3, -5})");
Mark Sleee8540632006-05-30 09:24:40 +0000304 Xtruct out;
305 out.string_thing = "Zero";
306 out.byte_thing = 1;
Mark Sleee8540632006-05-30 09:24:40 +0000307 out.i32_thing = -3;
Mark Sleee8540632006-05-30 09:24:40 +0000308 out.i64_thing = -5;
Mark Slee1921d202007-01-24 19:43:06 +0000309 Xtruct in;
310 testClient.testStruct(in, out);
David Reissbc3dddb2007-08-22 23:20:24 +0000311 printf(" = {\"%s\", %d, %d, %"PRId64"}\n",
Mark Sleee8540632006-05-30 09:24:40 +0000312 in.string_thing.c_str(),
313 (int)in.byte_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000314 in.i32_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000315 in.i64_thing);
Roger Meier4fce9602012-05-04 06:22:09 +0000316 if (in != out)
317 failCount++;
David Reiss0c90f6f2008-02-06 22:18:40 +0000318
Mark Sleee8540632006-05-30 09:24:40 +0000319 /**
320 * NESTED STRUCT TEST
321 */
Mark Slee6e536442006-06-30 18:28:50 +0000322 printf("testNest({1, {\"Zero\", 1, -3, -5}), 5}");
Mark Sleee8540632006-05-30 09:24:40 +0000323 Xtruct2 out2;
324 out2.byte_thing = 1;
325 out2.struct_thing = out;
326 out2.i32_thing = 5;
Mark Slee1921d202007-01-24 19:43:06 +0000327 Xtruct2 in2;
328 testClient.testNest(in2, out2);
Mark Sleee8540632006-05-30 09:24:40 +0000329 in = in2.struct_thing;
David Reissbc3dddb2007-08-22 23:20:24 +0000330 printf(" = {%d, {\"%s\", %d, %d, %"PRId64"}, %d}\n",
Mark Sleee8540632006-05-30 09:24:40 +0000331 in2.byte_thing,
332 in.string_thing.c_str(),
333 (int)in.byte_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000334 in.i32_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000335 in.i64_thing,
David Reiss0c90f6f2008-02-06 22:18:40 +0000336 in2.i32_thing);
Roger Meier4fce9602012-05-04 06:22:09 +0000337 if (in2 != out2)
338 failCount++;
Mark Sleee8540632006-05-30 09:24:40 +0000339
340 /**
341 * MAP TEST
342 */
343 map<int32_t,int32_t> mapout;
344 for (int32_t i = 0; i < 5; ++i) {
345 mapout.insert(make_pair(i, i-10));
346 }
347 printf("testMap({");
348 map<int32_t, int32_t>::const_iterator m_iter;
349 bool first = true;
350 for (m_iter = mapout.begin(); m_iter != mapout.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("})");
Mark Slee1921d202007-01-24 19:43:06 +0000359 map<int32_t,int32_t> mapin;
360 testClient.testMap(mapin, mapout);
Mark Sleee8540632006-05-30 09:24:40 +0000361 printf(" = {");
362 first = true;
363 for (m_iter = mapin.begin(); m_iter != mapin.end(); ++m_iter) {
364 if (first) {
365 first = false;
366 } else {
367 printf(", ");
368 }
369 printf("%d => %d", m_iter->first, m_iter->second);
370 }
371 printf("}\n");
Roger Meier4fce9602012-05-04 06:22:09 +0000372 if (mapin != mapout)
373 failCount++;
374
375 /**
376 * STRING MAP TEST
377 * missing
378 */
Mark Sleee8540632006-05-30 09:24:40 +0000379
380 /**
381 * SET TEST
382 */
383 set<int32_t> setout;
384 for (int32_t i = -2; i < 3; ++i) {
385 setout.insert(i);
386 }
387 printf("testSet({");
388 set<int32_t>::const_iterator s_iter;
389 first = true;
390 for (s_iter = setout.begin(); s_iter != setout.end(); ++s_iter) {
391 if (first) {
392 first = false;
393 } else {
394 printf(", ");
395 }
396 printf("%d", *s_iter);
397 }
398 printf("})");
Mark Slee1921d202007-01-24 19:43:06 +0000399 set<int32_t> setin;
400 testClient.testSet(setin, setout);
Mark Sleee8540632006-05-30 09:24:40 +0000401 printf(" = {");
402 first = true;
403 for (s_iter = setin.begin(); s_iter != setin.end(); ++s_iter) {
404 if (first) {
405 first = false;
406 } else {
407 printf(", ");
408 }
409 printf("%d", *s_iter);
410 }
411 printf("}\n");
Roger Meier4fce9602012-05-04 06:22:09 +0000412 if (setin != setout)
413 failCount++;
Mark Sleee8540632006-05-30 09:24:40 +0000414
415 /**
416 * LIST TEST
417 */
Mark Sleeb9acf982006-10-10 01:57:32 +0000418 vector<int32_t> listout;
Mark Sleee8540632006-05-30 09:24:40 +0000419 for (int32_t i = -2; i < 3; ++i) {
420 listout.push_back(i);
421 }
422 printf("testList({");
Mark Sleeb9acf982006-10-10 01:57:32 +0000423 vector<int32_t>::const_iterator l_iter;
Mark Sleee8540632006-05-30 09:24:40 +0000424 first = true;
425 for (l_iter = listout.begin(); l_iter != listout.end(); ++l_iter) {
426 if (first) {
427 first = false;
428 } else {
429 printf(", ");
430 }
431 printf("%d", *l_iter);
432 }
433 printf("})");
Mark Slee1921d202007-01-24 19:43:06 +0000434 vector<int32_t> listin;
435 testClient.testList(listin, listout);
Mark Sleee8540632006-05-30 09:24:40 +0000436 printf(" = {");
437 first = true;
438 for (l_iter = listin.begin(); l_iter != listin.end(); ++l_iter) {
439 if (first) {
440 first = false;
441 } else {
442 printf(", ");
443 }
444 printf("%d", *l_iter);
445 }
446 printf("}\n");
Roger Meier4fce9602012-05-04 06:22:09 +0000447 if (listin != listout)
448 failCount++;
Mark Sleee8540632006-05-30 09:24:40 +0000449
450 /**
451 * ENUM TEST
452 */
453 printf("testEnum(ONE)");
Bryan Duxbury833ae492010-09-27 17:26:02 +0000454 Numberz::type ret = testClient.testEnum(Numberz::ONE);
Mark Sleee8540632006-05-30 09:24:40 +0000455 printf(" = %d\n", ret);
Roger Meier4fce9602012-05-04 06:22:09 +0000456 if (ret != Numberz::ONE)
457 failCount++;
Mark Sleee8540632006-05-30 09:24:40 +0000458
459 printf("testEnum(TWO)");
Bryan Duxbury833ae492010-09-27 17:26:02 +0000460 ret = testClient.testEnum(Numberz::TWO);
Mark Sleee8540632006-05-30 09:24:40 +0000461 printf(" = %d\n", ret);
Roger Meier4fce9602012-05-04 06:22:09 +0000462 if (ret != Numberz::TWO)
463 failCount++;
Mark Sleee8540632006-05-30 09:24:40 +0000464
465 printf("testEnum(THREE)");
Bryan Duxbury833ae492010-09-27 17:26:02 +0000466 ret = testClient.testEnum(Numberz::THREE);
Mark Sleee8540632006-05-30 09:24:40 +0000467 printf(" = %d\n", ret);
Roger Meier4fce9602012-05-04 06:22:09 +0000468 if (ret != Numberz::THREE)
469 failCount++;
Mark Sleee8540632006-05-30 09:24:40 +0000470
471 printf("testEnum(FIVE)");
Bryan Duxbury833ae492010-09-27 17:26:02 +0000472 ret = testClient.testEnum(Numberz::FIVE);
Mark Sleee8540632006-05-30 09:24:40 +0000473 printf(" = %d\n", ret);
Roger Meier4fce9602012-05-04 06:22:09 +0000474 if (ret != Numberz::FIVE)
475 failCount++;
Mark Sleee8540632006-05-30 09:24:40 +0000476
477 printf("testEnum(EIGHT)");
Bryan Duxbury833ae492010-09-27 17:26:02 +0000478 ret = testClient.testEnum(Numberz::EIGHT);
Mark Sleee8540632006-05-30 09:24:40 +0000479 printf(" = %d\n", ret);
Roger Meier4fce9602012-05-04 06:22:09 +0000480 if (ret != Numberz::EIGHT)
481 failCount++;
Mark Sleee8540632006-05-30 09:24:40 +0000482
483 /**
484 * TYPEDEF TEST
485 */
486 printf("testTypedef(309858235082523)");
Marc Slemkobf4fd192006-08-15 21:29:39 +0000487 UserId uid = testClient.testTypedef(309858235082523LL);
David Reissbc3dddb2007-08-22 23:20:24 +0000488 printf(" = %"PRId64"\n", uid);
Roger Meier4fce9602012-05-04 06:22:09 +0000489 if (uid != 309858235082523LL)
490 failCount++;
Mark Sleee8540632006-05-30 09:24:40 +0000491
492 /**
493 * NESTED MAP TEST
494 */
495 printf("testMapMap(1)");
Mark Slee1921d202007-01-24 19:43:06 +0000496 map<int32_t, map<int32_t, int32_t> > mm;
497 testClient.testMapMap(mm, 1);
Mark Sleee8540632006-05-30 09:24:40 +0000498 printf(" = {");
499 map<int32_t, map<int32_t, int32_t> >::const_iterator mi;
500 for (mi = mm.begin(); mi != mm.end(); ++mi) {
501 printf("%d => {", mi->first);
502 map<int32_t, int32_t>::const_iterator mi2;
503 for (mi2 = mi->second.begin(); mi2 != mi->second.end(); ++mi2) {
504 printf("%d => %d, ", mi2->first, mi2->second);
505 }
506 printf("}, ");
507 }
508 printf("}\n");
509
510 /**
511 * INSANITY TEST
512 */
513 Insanity insane;
Bryan Duxbury833ae492010-09-27 17:26:02 +0000514 insane.userMap.insert(make_pair(Numberz::FIVE, 5000));
Mark Sleee8540632006-05-30 09:24:40 +0000515 Xtruct truck;
516 truck.string_thing = "Truck";
517 truck.byte_thing = 8;
Mark Sleee8540632006-05-30 09:24:40 +0000518 truck.i32_thing = 8;
Mark Sleee8540632006-05-30 09:24:40 +0000519 truck.i64_thing = 8;
520 insane.xtructs.push_back(truck);
521 printf("testInsanity()");
Bryan Duxbury833ae492010-09-27 17:26:02 +0000522 map<UserId, map<Numberz::type,Insanity> > whoa;
Mark Slee1921d202007-01-24 19:43:06 +0000523 testClient.testInsanity(whoa, insane);
Mark Sleee8540632006-05-30 09:24:40 +0000524 printf(" = {");
Bryan Duxbury833ae492010-09-27 17:26:02 +0000525 map<UserId, map<Numberz::type,Insanity> >::const_iterator i_iter;
Mark Sleee8540632006-05-30 09:24:40 +0000526 for (i_iter = whoa.begin(); i_iter != whoa.end(); ++i_iter) {
David Reissbc3dddb2007-08-22 23:20:24 +0000527 printf("%"PRId64" => {", i_iter->first);
Bryan Duxbury833ae492010-09-27 17:26:02 +0000528 map<Numberz::type,Insanity>::const_iterator i2_iter;
Mark Sleee8540632006-05-30 09:24:40 +0000529 for (i2_iter = i_iter->second.begin();
530 i2_iter != i_iter->second.end();
531 ++i2_iter) {
532 printf("%d => {", i2_iter->first);
Bryan Duxbury833ae492010-09-27 17:26:02 +0000533 map<Numberz::type, UserId> userMap = i2_iter->second.userMap;
534 map<Numberz::type, UserId>::const_iterator um;
Mark Sleee8540632006-05-30 09:24:40 +0000535 printf("{");
536 for (um = userMap.begin(); um != userMap.end(); ++um) {
David Reissbc3dddb2007-08-22 23:20:24 +0000537 printf("%d => %"PRId64", ", um->first, um->second);
Mark Sleee8540632006-05-30 09:24:40 +0000538 }
539 printf("}, ");
540
Mark Sleeb9acf982006-10-10 01:57:32 +0000541 vector<Xtruct> xtructs = i2_iter->second.xtructs;
542 vector<Xtruct>::const_iterator x;
Mark Sleee8540632006-05-30 09:24:40 +0000543 printf("{");
544 for (x = xtructs.begin(); x != xtructs.end(); ++x) {
David Reissbc3dddb2007-08-22 23:20:24 +0000545 printf("{\"%s\", %d, %d, %"PRId64"}, ",
Mark Sleee8540632006-05-30 09:24:40 +0000546 x->string_thing.c_str(),
547 (int)x->byte_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000548 x->i32_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000549 x->i64_thing);
550 }
551 printf("}");
552
553 printf("}, ");
554 }
555 printf("}, ");
556 }
557 printf("}\n");
558
Marc Slemko71d4e472006-08-15 22:34:04 +0000559 /* test exception */
Mark Slee95771002006-06-07 06:53:25 +0000560
Marc Slemkobf4fd192006-08-15 21:29:39 +0000561 try {
Marc Slemko71d4e472006-08-15 22:34:04 +0000562 printf("testClient.testException(\"Xception\") =>");
563 testClient.testException("Xception");
564 printf(" void\nFAILURE\n");
Roger Meier4fce9602012-05-04 06:22:09 +0000565 failCount++;
David Reiss0c90f6f2008-02-06 22:18:40 +0000566
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000567 } catch(Xception& e) {
Marc Slemko71d4e472006-08-15 22:34:04 +0000568 printf(" {%u, \"%s\"}\n", e.errorCode, e.message.c_str());
Marc Slemkobf4fd192006-08-15 21:29:39 +0000569 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000570
Marc Slemkobf4fd192006-08-15 21:29:39 +0000571 try {
Roger Meier99b36722012-05-03 21:21:43 +0000572 printf("testClient.testException(\"TException\") =>");
573 testClient.testException("TException");
Roger Meierf50df7f2012-05-02 22:49:55 +0000574 printf(" void\nFAILURE\n");
Roger Meier4fce9602012-05-04 06:22:09 +0000575 failCount++;
Roger Meierf50df7f2012-05-02 22:49:55 +0000576
577 } catch(TException& e) {
578 printf(" Caught TException\n");
579 }
580
581 try {
Marc Slemko71d4e472006-08-15 22:34:04 +0000582 printf("testClient.testException(\"success\") =>");
583 testClient.testException("success");
584 printf(" void\n");
585 } catch(...) {
586 printf(" exception\nFAILURE\n");
Roger Meier4fce9602012-05-04 06:22:09 +0000587 failCount++;
Marc Slemko71d4e472006-08-15 22:34:04 +0000588 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000589
Marc Slemko71d4e472006-08-15 22:34:04 +0000590 /* test multi exception */
David Reiss0c90f6f2008-02-06 22:18:40 +0000591
Marc Slemko71d4e472006-08-15 22:34:04 +0000592 try {
593 printf("testClient.testMultiException(\"Xception\", \"test 1\") =>");
Mark Slee1921d202007-01-24 19:43:06 +0000594 Xtruct result;
595 testClient.testMultiException(result, "Xception", "test 1");
Marc Slemko71d4e472006-08-15 22:34:04 +0000596 printf(" result\nFAILURE\n");
Roger Meier4fce9602012-05-04 06:22:09 +0000597 failCount++;
Mark Sleed3d733a2006-09-01 22:19:06 +0000598 } catch(Xception& e) {
Marc Slemko71d4e472006-08-15 22:34:04 +0000599 printf(" {%u, \"%s\"}\n", e.errorCode, e.message.c_str());
600 }
601
602 try {
603 printf("testClient.testMultiException(\"Xception2\", \"test 2\") =>");
Mark Slee1921d202007-01-24 19:43:06 +0000604 Xtruct result;
605 testClient.testMultiException(result, "Xception2", "test 2");
Marc Slemko71d4e472006-08-15 22:34:04 +0000606 printf(" result\nFAILURE\n");
Roger Meier4fce9602012-05-04 06:22:09 +0000607 failCount++;
David Reiss0c90f6f2008-02-06 22:18:40 +0000608
Mark Sleed3d733a2006-09-01 22:19:06 +0000609 } catch(Xception2& e) {
Marc Slemko71d4e472006-08-15 22:34:04 +0000610 printf(" {%u, {\"%s\"}}\n", e.errorCode, e.struct_thing.string_thing.c_str());
Marc Slemkobf4fd192006-08-15 21:29:39 +0000611 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000612
Marc Slemko71d4e472006-08-15 22:34:04 +0000613 try {
614 printf("testClient.testMultiException(\"success\", \"test 3\") =>");
Mark Slee1921d202007-01-24 19:43:06 +0000615 Xtruct result;
616 testClient.testMultiException(result, "success", "test 3");
Marc Slemko71d4e472006-08-15 22:34:04 +0000617 printf(" {{\"%s\"}}\n", result.string_thing.c_str());
618 } catch(...) {
619 printf(" exception\nFAILURE\n");
Roger Meier4fce9602012-05-04 06:22:09 +0000620 failCount++;
Marc Slemko71d4e472006-08-15 22:34:04 +0000621 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000622
David Reissc51986f2009-03-24 20:01:25 +0000623 /* test oneway void */
David Reiss2ab6fe82008-02-18 02:11:44 +0000624 {
David Reiss6ce401d2009-03-24 20:01:58 +0000625 printf("testClient.testOneway(3) =>");
626 uint64_t startOneway = now();
627 testClient.testOneway(3);
628 uint64_t elapsed = now() - startOneway;
David Reiss2ab6fe82008-02-18 02:11:44 +0000629 if (elapsed > 200 * 1000) { // 0.2 seconds
630 printf(" FAILURE - took %.2f ms\n", (double)elapsed/1000.0);
Roger Meier4fce9602012-05-04 06:22:09 +0000631 failCount++;
David Reiss2ab6fe82008-02-18 02:11:44 +0000632 } else {
633 printf(" success - took %.2f ms\n", (double)elapsed/1000.0);
634 }
635 }
636
David Reiss2845b522008-02-18 02:11:52 +0000637 /**
David Reissc51986f2009-03-24 20:01:25 +0000638 * redo a simple test after the oneway to make sure we aren't "off by one" --
639 * if the server treated oneway void like normal void, this next test will
David Reiss2845b522008-02-18 02:11:52 +0000640 * fail since it will get the void confirmation rather than the correct
641 * result. In this circumstance, the client will throw the exception:
642 *
643 * TApplicationException: Wrong method namea
644 */
645 /**
646 * I32 TEST
647 */
648 printf("re-test testI32(-1)");
649 i32 = testClient.testI32(-1);
650 printf(" = %d\n", i32);
Roger Meier4fce9602012-05-04 06:22:09 +0000651 if (i32 != -1)
652 failCount++;
David Reiss2845b522008-02-18 02:11:52 +0000653
654
Marc Slemkobf4fd192006-08-15 21:29:39 +0000655 uint64_t stop = now();
Mark Sleed788b2e2006-09-07 01:26:35 +0000656 uint64_t tot = stop-start;
657
David Reissbc3dddb2007-08-22 23:20:24 +0000658 printf("Total time: %"PRIu64" us\n", stop-start);
David Reiss0c90f6f2008-02-06 22:18:40 +0000659
Mark Sleed788b2e2006-09-07 01:26:35 +0000660 time_tot += tot;
661 if (time_min == 0 || tot < time_min) {
662 time_min = tot;
663 }
664 if (tot > time_max) {
665 time_max = tot;
666 }
667
Mark Sleea3302652006-10-25 19:03:32 +0000668 transport->close();
Mark Sleee8540632006-05-30 09:24:40 +0000669 }
670
Marc Slemko6be374b2006-08-04 03:16:25 +0000671 // printf("\nSocket syscalls: %u", g_socket_syscalls);
Mark Sleee8540632006-05-30 09:24:40 +0000672 printf("\nAll tests done.\n");
Mark Sleed788b2e2006-09-07 01:26:35 +0000673
674 uint64_t time_avg = time_tot / numTests;
675
David Reissbc3dddb2007-08-22 23:20:24 +0000676 printf("Min time: %"PRIu64" us\n", time_min);
677 printf("Max time: %"PRIu64" us\n", time_max);
678 printf("Avg time: %"PRIu64" us\n", time_avg);
Mark Sleed788b2e2006-09-07 01:26:35 +0000679
Roger Meier4fce9602012-05-04 06:22:09 +0000680 return failCount;
Mark Sleee8540632006-05-30 09:24:40 +0000681}