blob: 1c0254b633f736d6b482e4391771739fa43218f0 [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>
Roger Meier49ff8b12012-04-13 09:12:31 +000024#include <thrift/protocol/TBinaryProtocol.h>
Roger Meier023192f2014-02-12 09:35:12 +010025#include <thrift/protocol/TCompactProtocol.h>
Roger Meier49ff8b12012-04-13 09:12:31 +000026#include <thrift/protocol/TJSONProtocol.h>
27#include <thrift/transport/THttpClient.h>
28#include <thrift/transport/TTransportUtils.h>
29#include <thrift/transport/TSocket.h>
30#include <thrift/transport/TSSLSocket.h>
31#include <thrift/async/TEvhttpClientChannel.h>
32#include <thrift/server/TNonblockingServer.h> // <event.h>
Mark Sleee8540632006-05-30 09:24:40 +000033
Marc Slemko6be374b2006-08-04 03:16:25 +000034#include <boost/shared_ptr.hpp>
Roger Meierca142b02011-06-07 17:59:07 +000035#include <boost/program_options.hpp>
cdwijayarathnaa07ec0b2014-08-09 17:45:56 +053036#include <boost/filesystem.hpp>
Jake Farrell5d02b802014-01-07 21:42:01 -050037#include <thrift/cxxfunctional.h>
38#if _WIN32
Konrad Grochowski16a23a62014-11-13 15:33:38 +010039#include <thrift/windows/TWinsockSingleton.h>
Jake Farrell5d02b802014-01-07 21:42:01 -050040#endif
Roger Meierca142b02011-06-07 17:59:07 +000041
Marc Slemko6be374b2006-08-04 03:16:25 +000042#include "ThriftTest.h"
43
Marc Slemko6be374b2006-08-04 03:16:25 +000044using namespace std;
T Jake Lucianib5e62212009-01-31 22:36:20 +000045using namespace apache::thrift;
46using namespace apache::thrift::protocol;
47using namespace apache::thrift::transport;
Marc Slemkobf4fd192006-08-15 21:29:39 +000048using namespace thrift::test;
Roger Meier7e056e72011-07-17 07:28:28 +000049using namespace apache::thrift::async;
50
cdwijayarathnaa07ec0b2014-08-09 17:45:56 +053051// Length of argv[0] - Length of script dir
52#define EXECUTABLE_FILE_NAME_LENGTH 19
53
Mark Slee95771002006-06-07 06:53:25 +000054// Current time, microseconds since the epoch
Konrad Grochowski16a23a62014-11-13 15:33:38 +010055uint64_t now() {
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
Jake Farrell5d02b802014-01-07 21:42:01 -050059 THRIFT_GETTIMEOFDAY(&tv, NULL);
Mark Slee95771002006-06-07 06:53:25 +000060 ret = tv.tv_sec;
Konrad Grochowski16a23a62014-11-13 15:33:38 +010061 ret = ret * 1000 * 1000 + tv.tv_usec;
Mark Slee95771002006-06-07 06:53:25 +000062 return ret;
63}
64
Konrad Grochowski16a23a62014-11-13 15:33:38 +010065static void testString_clientReturn(const char* host,
66 int port,
67 event_base* base,
68 TProtocolFactory* protocolFactory,
69 ThriftTestCobClient* client) {
70 (void)host;
71 (void)port;
72 (void)protocolFactory;
Roger Meier7e056e72011-07-17 07:28:28 +000073 try {
74 string s;
75 client->recv_testString(s);
76 cout << "testString: " << s << endl;
77 } catch (TException& exn) {
Jake Farrell5d02b802014-01-07 21:42:01 -050078 cout << "Error: " << exn.what() << endl;
Roger Meier7e056e72011-07-17 07:28:28 +000079 }
80
81 event_base_loopbreak(base); // end test
82}
83
Konrad Grochowski16a23a62014-11-13 15:33:38 +010084static void testVoid_clientReturn(const char* host,
85 int port,
86 event_base* base,
87 TProtocolFactory* protocolFactory,
88 ThriftTestCobClient* client) {
Roger Meier7e056e72011-07-17 07:28:28 +000089 try {
90 client->recv_testVoid();
91 cout << "testVoid" << endl;
92
93 // next test
94 delete client;
Roger Meier611f90c2011-12-11 22:08:51 +000095 boost::shared_ptr<TAsyncChannel> channel(new TEvhttpClientChannel(host, "/", host, port, base));
Roger Meier7e056e72011-07-17 07:28:28 +000096 client = new ThriftTestCobClient(channel, protocolFactory);
Konrad Grochowski16a23a62014-11-13 15:33:38 +010097 client->testString(tcxx::bind(testString_clientReturn,
98 host,
99 port,
100 base,
101 protocolFactory,
102 tcxx::placeholders::_1),
103 "Test");
Roger Meier7e056e72011-07-17 07:28:28 +0000104 } catch (TException& exn) {
Jake Farrell5d02b802014-01-07 21:42:01 -0500105 cout << "Error: " << exn.what() << endl;
Roger Meier7e056e72011-07-17 07:28:28 +0000106 }
107}
108
Mark Sleee8540632006-05-30 09:24:40 +0000109int main(int argc, char** argv) {
cdwijayarathnaa07ec0b2014-08-09 17:45:56 +0530110 string file_path = boost::filesystem::system_complete(argv[0]).string();
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100111 string dir_path = file_path.substr(0, file_path.size() - EXECUTABLE_FILE_NAME_LENGTH);
Jake Farrell5d02b802014-01-07 21:42:01 -0500112#if _WIN32
113 transport::TWinsockSingleton::create();
114#endif
Mark Sleee8540632006-05-30 09:24:40 +0000115 string host = "localhost";
116 int port = 9090;
117 int numTests = 1;
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000118 bool ssl = false;
Roger Meierca142b02011-06-07 17:59:07 +0000119 string transport_type = "buffered";
120 string protocol_type = "binary";
121 string domain_socket = "";
Jens Geyerf4598682014-05-08 23:18:44 +0200122 bool noinsane = false;
Mark Sleee8540632006-05-30 09:24:40 +0000123
Jake Farrell5d02b802014-01-07 21:42:01 -0500124 boost::program_options::options_description desc("Allowed options");
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100125 desc.add_options()("help,h",
126 "produce help message")("host",
127 boost::program_options::value<string>(&host)
128 ->default_value(host),
129 "Host to connect")("port",
130 boost::program_options::value<int>(
131 &port)->default_value(port),
132 "Port number to connect")(
133 "domain-socket",
134 boost::program_options::value<string>(&domain_socket)->default_value(domain_socket),
135 "Domain Socket (e.g. /tmp/ThriftTest.thrift), instead of host and port")(
136 "transport",
137 boost::program_options::value<string>(&transport_type)->default_value(transport_type),
138 "Transport: buffered, framed, http, evhttp")(
139 "protocol",
140 boost::program_options::value<string>(&protocol_type)->default_value(protocol_type),
141 "Protocol: binary, compact, json")("ssl", "Encrypted Transport using SSL")(
142 "testloops,n",
143 boost::program_options::value<int>(&numTests)->default_value(numTests),
144 "Number of Tests")("noinsane", "Do not run insanity test");
Roger Meierca142b02011-06-07 17:59:07 +0000145
Jake Farrell5d02b802014-01-07 21:42:01 -0500146 boost::program_options::variables_map vm;
147 boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), vm);
148 boost::program_options::notify(vm);
Roger Meierca142b02011-06-07 17:59:07 +0000149
150 if (vm.count("help")) {
151 cout << desc << "\n";
152 return 1;
Mark Sleee8540632006-05-30 09:24:40 +0000153 }
Mark Sleea3302652006-10-25 19:03:32 +0000154
Jake Farrell5d02b802014-01-07 21:42:01 -0500155 try {
Roger Meierca142b02011-06-07 17:59:07 +0000156 if (!protocol_type.empty()) {
157 if (protocol_type == "binary") {
Roger Meier284101c2014-03-11 21:20:35 +0100158 } else if (protocol_type == "compact") {
Roger Meierca142b02011-06-07 17:59:07 +0000159 } else if (protocol_type == "json") {
160 } else {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100161 throw invalid_argument("Unknown protocol type " + protocol_type);
Roger Meierca142b02011-06-07 17:59:07 +0000162 }
163 }
164
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100165 if (!transport_type.empty()) {
Roger Meierca142b02011-06-07 17:59:07 +0000166 if (transport_type == "buffered") {
167 } else if (transport_type == "framed") {
168 } else if (transport_type == "http") {
Roger Meier7e056e72011-07-17 07:28:28 +0000169 } else if (transport_type == "evhttp") {
Roger Meierca142b02011-06-07 17:59:07 +0000170 } else {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100171 throw invalid_argument("Unknown transport type " + transport_type);
Roger Meierca142b02011-06-07 17:59:07 +0000172 }
173 }
174
175 } catch (std::exception& e) {
176 cerr << e.what() << endl;
177 cout << desc << "\n";
178 return 1;
179 }
180
181 if (vm.count("ssl")) {
182 ssl = true;
183 }
cdwijayarathnaa07ec0b2014-08-09 17:45:56 +0530184
Jens Geyerf4598682014-05-08 23:18:44 +0200185 if (vm.count("noinsane")) {
186 noinsane = true;
187 }
Roger Meierca142b02011-06-07 17:59:07 +0000188
Roger Meier611f90c2011-12-11 22:08:51 +0000189 boost::shared_ptr<TTransport> transport;
190 boost::shared_ptr<TProtocol> protocol;
Roger Meierca142b02011-06-07 17:59:07 +0000191
Roger Meier611f90c2011-12-11 22:08:51 +0000192 boost::shared_ptr<TSocket> socket;
193 boost::shared_ptr<TSSLSocketFactory> factory;
Roger Meierca142b02011-06-07 17:59:07 +0000194
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000195 if (ssl) {
Roger Meier611f90c2011-12-11 22:08:51 +0000196 factory = boost::shared_ptr<TSSLSocketFactory>(new TSSLSocketFactory());
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000197 factory->ciphers("ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
cdwijayarathnaa07ec0b2014-08-09 17:45:56 +0530198 factory->loadTrustedCertificates((dir_path + "../keys/CA.pem").c_str());
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000199 factory->authenticate(true);
200 socket = factory->createSocket(host, port);
201 } else {
Roger Meierca142b02011-06-07 17:59:07 +0000202 if (domain_socket != "") {
Roger Meier611f90c2011-12-11 22:08:51 +0000203 socket = boost::shared_ptr<TSocket>(new TSocket(domain_socket));
Roger Meierca142b02011-06-07 17:59:07 +0000204 port = 0;
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100205 } else {
Roger Meier611f90c2011-12-11 22:08:51 +0000206 socket = boost::shared_ptr<TSocket>(new TSocket(host, port));
Roger Meierca142b02011-06-07 17:59:07 +0000207 }
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000208 }
Mark Sleea3302652006-10-25 19:03:32 +0000209
Roger Meierca142b02011-06-07 17:59:07 +0000210 if (transport_type.compare("http") == 0) {
Roger Meier611f90c2011-12-11 22:08:51 +0000211 boost::shared_ptr<TTransport> httpSocket(new THttpClient(socket, host, "/service"));
Roger Meierca142b02011-06-07 17:59:07 +0000212 transport = httpSocket;
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100213 } else if (transport_type.compare("framed") == 0) {
Roger Meier611f90c2011-12-11 22:08:51 +0000214 boost::shared_ptr<TFramedTransport> framedSocket(new TFramedTransport(socket));
Mark Sleea3302652006-10-25 19:03:32 +0000215 transport = framedSocket;
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100216 } else {
Roger Meier611f90c2011-12-11 22:08:51 +0000217 boost::shared_ptr<TBufferedTransport> bufferedSocket(new TBufferedTransport(socket));
Mark Sleea3302652006-10-25 19:03:32 +0000218 transport = bufferedSocket;
219 }
220
Roger Meierca142b02011-06-07 17:59:07 +0000221 if (protocol_type.compare("json") == 0) {
Roger Meier611f90c2011-12-11 22:08:51 +0000222 boost::shared_ptr<TProtocol> jsonProtocol(new TJSONProtocol(transport));
Roger Meierca142b02011-06-07 17:59:07 +0000223 protocol = jsonProtocol;
Roger Meier023192f2014-02-12 09:35:12 +0100224 } else if (protocol_type.compare("compact") == 0) {
225 boost::shared_ptr<TProtocol> compactProtocol(new TCompactProtocol(transport));
226 protocol = compactProtocol;
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100227 } else {
Roger Meier611f90c2011-12-11 22:08:51 +0000228 boost::shared_ptr<TBinaryProtocol> binaryProtocol(new TBinaryProtocol(transport));
Roger Meierca142b02011-06-07 17:59:07 +0000229 protocol = binaryProtocol;
230 }
231
232 // Connection info
233 cout << "Connecting (" << transport_type << "/" << protocol_type << ") to: " << domain_socket;
234 if (port != 0) {
235 cout << host << ":" << port;
236 }
237 cout << endl;
238
Roger Meier7e056e72011-07-17 07:28:28 +0000239 if (transport_type.compare("evhttp") == 0) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100240 event_base* base = event_base_new();
Roger Meier7e056e72011-07-17 07:28:28 +0000241 cout << "Libevent Version: " << event_get_version() << endl;
242 cout << "Libevent Method: " << event_base_get_method(base) << endl;
243#if LIBEVENT_VERSION_NUMBER >= 0x02000000
244 cout << "Libevent Features: 0x" << hex << event_base_get_features(base) << endl;
245#endif
246
Roger Meier611f90c2011-12-11 22:08:51 +0000247 boost::shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());
Roger Meier7e056e72011-07-17 07:28:28 +0000248
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100249 boost::shared_ptr<TAsyncChannel> channel(
250 new TEvhttpClientChannel(host.c_str(), "/", host.c_str(), port, base));
Roger Meier7e056e72011-07-17 07:28:28 +0000251 ThriftTestCobClient* client = new ThriftTestCobClient(channel, protocolFactory.get());
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100252 client->testVoid(tcxx::bind(testVoid_clientReturn,
253 host.c_str(),
254 port,
255 base,
256 protocolFactory.get(),
257 tcxx::placeholders::_1));
Jake Farrell5d02b802014-01-07 21:42:01 -0500258
Roger Meier7e056e72011-07-17 07:28:28 +0000259 event_base_loop(base, 0);
260 return 0;
261 }
262
Roger Meierca142b02011-06-07 17:59:07 +0000263 ThriftTestClient testClient(protocol);
Mark Sleed788b2e2006-09-07 01:26:35 +0000264
265 uint64_t time_min = 0;
266 uint64_t time_max = 0;
267 uint64_t time_tot = 0;
David Reiss0c90f6f2008-02-06 22:18:40 +0000268
Roger Meier4fce9602012-05-04 06:22:09 +0000269 int failCount = 0;
Mark Sleee8540632006-05-30 09:24:40 +0000270 int test = 0;
271 for (test = 0; test < numTests; ++test) {
Mark Slee95771002006-06-07 06:53:25 +0000272
Mark Slee95771002006-06-07 06:53:25 +0000273 try {
Mark Sleea3302652006-10-25 19:03:32 +0000274 transport->open();
Mark Slee95771002006-06-07 06:53:25 +0000275 } catch (TTransportException& ttx) {
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000276 printf("Connect failed: %s\n", ttx.what());
Roger Meier5b1e3c72011-12-08 13:20:12 +0000277 return 1;
Mark Sleee8540632006-05-30 09:24:40 +0000278 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000279
Mark Sleed788b2e2006-09-07 01:26:35 +0000280 /**
281 * CONNECT TEST
282 */
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100283 printf("Test #%d, connect %s:%d\n", test + 1, host.c_str(), port);
Mark Slee95771002006-06-07 06:53:25 +0000284
285 uint64_t start = now();
David Reiss0c90f6f2008-02-06 22:18:40 +0000286
Mark Sleee8540632006-05-30 09:24:40 +0000287 /**
288 * VOID TEST
289 */
Mark Sleee129a2d2007-02-21 05:17:48 +0000290 try {
291 printf("testVoid()");
292 testClient.testVoid();
293 printf(" = void\n");
Roger Meier4fce9602012-05-04 06:22:09 +0000294 } catch (TApplicationException& tax) {
Mark Sleee129a2d2007-02-21 05:17:48 +0000295 printf("%s\n", tax.what());
Roger Meier4fce9602012-05-04 06:22:09 +0000296 failCount++;
Mark Sleee129a2d2007-02-21 05:17:48 +0000297 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000298
Mark Sleee8540632006-05-30 09:24:40 +0000299 /**
300 * STRING TEST
301 */
302 printf("testString(\"Test\")");
Mark Slee1921d202007-01-24 19:43:06 +0000303 string s;
304 testClient.testString(s, "Test");
Mark Sleee8540632006-05-30 09:24:40 +0000305 printf(" = \"%s\"\n", s.c_str());
Roger Meier4fce9602012-05-04 06:22:09 +0000306 if (s != "Test")
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100307 failCount++;
David Reiss0c90f6f2008-02-06 22:18:40 +0000308
Mark Sleee8540632006-05-30 09:24:40 +0000309 /**
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900310 * BOOL TEST
311 */
312 printf("testBool(true)");
313 bool bl = testClient.testBool(true);
314 printf(" = %s\n", bl ? "true" : "false");
315 if (bl != true)
316 failCount++;
317
318 printf("testBool(false)");
319 bl = testClient.testBool(false);
320 printf(" = %s\n", bl ? "true" : "false");
321 if (bl != false)
322 failCount++;
323
324 /**
Mark Sleee8540632006-05-30 09:24:40 +0000325 * BYTE TEST
326 */
327 printf("testByte(1)");
328 uint8_t u8 = testClient.testByte(1);
329 printf(" = %d\n", (int)u8);
Roger Meier4fce9602012-05-04 06:22:09 +0000330 if (u8 != 1)
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100331 failCount++;
David Reiss0c90f6f2008-02-06 22:18:40 +0000332
Mark Sleee8540632006-05-30 09:24:40 +0000333 /**
334 * I32 TEST
335 */
336 printf("testI32(-1)");
337 int32_t i32 = testClient.testI32(-1);
338 printf(" = %d\n", i32);
Roger Meier4fce9602012-05-04 06:22:09 +0000339 if (i32 != -1)
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100340 failCount++;
Mark Sleee8540632006-05-30 09:24:40 +0000341
342 /**
Mark Sleee8540632006-05-30 09:24:40 +0000343 * I64 TEST
344 */
345 printf("testI64(-34359738368)");
Marc Slemkobf4fd192006-08-15 21:29:39 +0000346 int64_t i64 = testClient.testI64(-34359738368LL);
Roger Meier0e814802014-01-17 21:07:58 +0100347 printf(" = %" PRId64 "\n", i64);
Roger Meier4fce9602012-05-04 06:22:09 +0000348 if (i64 != -34359738368LL)
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100349 failCount++;
Jens Geyer8bcfdd92014-12-14 03:14:26 +0100350
Mark Sleec98d0502006-09-06 02:42:25 +0000351 /**
352 * DOUBLE TEST
353 */
354 printf("testDouble(-5.2098523)");
355 double dub = testClient.testDouble(-5.2098523);
Roger Meiera8cef6e2011-07-17 18:55:59 +0000356 printf(" = %f\n", dub);
Roger Meier4fce9602012-05-04 06:22:09 +0000357 if ((dub - (-5.2098523)) > 0.001)
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100358 failCount++;
David Reiss0c90f6f2008-02-06 22:18:40 +0000359
Mark Sleee8540632006-05-30 09:24:40 +0000360 /**
Jens Geyer8bcfdd92014-12-14 03:14:26 +0100361 * BINARY TEST
362 */
363 // TODO: add testBinary() call
364
365 /**
Mark Sleee8540632006-05-30 09:24:40 +0000366 * STRUCT TEST
367 */
Mark Slee6e536442006-06-30 18:28:50 +0000368 printf("testStruct({\"Zero\", 1, -3, -5})");
Mark Sleee8540632006-05-30 09:24:40 +0000369 Xtruct out;
370 out.string_thing = "Zero";
371 out.byte_thing = 1;
Mark Sleee8540632006-05-30 09:24:40 +0000372 out.i32_thing = -3;
Mark Sleee8540632006-05-30 09:24:40 +0000373 out.i64_thing = -5;
Mark Slee1921d202007-01-24 19:43:06 +0000374 Xtruct in;
375 testClient.testStruct(in, out);
Roger Meier0e814802014-01-17 21:07:58 +0100376 printf(" = {\"%s\", %d, %d, %" PRId64 "}\n",
Mark Sleee8540632006-05-30 09:24:40 +0000377 in.string_thing.c_str(),
378 (int)in.byte_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000379 in.i32_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000380 in.i64_thing);
Roger Meier4fce9602012-05-04 06:22:09 +0000381 if (in != out)
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100382 failCount++;
David Reiss0c90f6f2008-02-06 22:18:40 +0000383
Mark Sleee8540632006-05-30 09:24:40 +0000384 /**
385 * NESTED STRUCT TEST
386 */
Mark Slee6e536442006-06-30 18:28:50 +0000387 printf("testNest({1, {\"Zero\", 1, -3, -5}), 5}");
Mark Sleee8540632006-05-30 09:24:40 +0000388 Xtruct2 out2;
389 out2.byte_thing = 1;
390 out2.struct_thing = out;
391 out2.i32_thing = 5;
Mark Slee1921d202007-01-24 19:43:06 +0000392 Xtruct2 in2;
393 testClient.testNest(in2, out2);
Mark Sleee8540632006-05-30 09:24:40 +0000394 in = in2.struct_thing;
Roger Meier0e814802014-01-17 21:07:58 +0100395 printf(" = {%d, {\"%s\", %d, %d, %" PRId64 "}, %d}\n",
Mark Sleee8540632006-05-30 09:24:40 +0000396 in2.byte_thing,
397 in.string_thing.c_str(),
398 (int)in.byte_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000399 in.i32_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000400 in.i64_thing,
David Reiss0c90f6f2008-02-06 22:18:40 +0000401 in2.i32_thing);
Roger Meier4fce9602012-05-04 06:22:09 +0000402 if (in2 != out2)
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100403 failCount++;
Mark Sleee8540632006-05-30 09:24:40 +0000404
405 /**
406 * MAP TEST
407 */
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100408 map<int32_t, int32_t> mapout;
Mark Sleee8540632006-05-30 09:24:40 +0000409 for (int32_t i = 0; i < 5; ++i) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100410 mapout.insert(make_pair(i, i - 10));
Mark Sleee8540632006-05-30 09:24:40 +0000411 }
412 printf("testMap({");
413 map<int32_t, int32_t>::const_iterator m_iter;
414 bool first = true;
415 for (m_iter = mapout.begin(); m_iter != mapout.end(); ++m_iter) {
416 if (first) {
417 first = false;
418 } else {
419 printf(", ");
420 }
421 printf("%d => %d", m_iter->first, m_iter->second);
422 }
423 printf("})");
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100424 map<int32_t, int32_t> mapin;
Mark Slee1921d202007-01-24 19:43:06 +0000425 testClient.testMap(mapin, mapout);
Mark Sleee8540632006-05-30 09:24:40 +0000426 printf(" = {");
427 first = true;
428 for (m_iter = mapin.begin(); m_iter != mapin.end(); ++m_iter) {
429 if (first) {
430 first = false;
431 } else {
432 printf(", ");
433 }
434 printf("%d => %d", m_iter->first, m_iter->second);
435 }
436 printf("}\n");
Roger Meier4fce9602012-05-04 06:22:09 +0000437 if (mapin != mapout)
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100438 failCount++;
Roger Meier4fce9602012-05-04 06:22:09 +0000439
440 /**
441 * STRING MAP TEST
442 * missing
443 */
Mark Sleee8540632006-05-30 09:24:40 +0000444
445 /**
446 * SET TEST
447 */
448 set<int32_t> setout;
449 for (int32_t i = -2; i < 3; ++i) {
450 setout.insert(i);
451 }
452 printf("testSet({");
453 set<int32_t>::const_iterator s_iter;
454 first = true;
455 for (s_iter = setout.begin(); s_iter != setout.end(); ++s_iter) {
456 if (first) {
457 first = false;
458 } else {
459 printf(", ");
460 }
461 printf("%d", *s_iter);
462 }
463 printf("})");
Mark Slee1921d202007-01-24 19:43:06 +0000464 set<int32_t> setin;
465 testClient.testSet(setin, setout);
Mark Sleee8540632006-05-30 09:24:40 +0000466 printf(" = {");
467 first = true;
468 for (s_iter = setin.begin(); s_iter != setin.end(); ++s_iter) {
469 if (first) {
470 first = false;
471 } else {
472 printf(", ");
473 }
474 printf("%d", *s_iter);
475 }
476 printf("}\n");
Roger Meier4fce9602012-05-04 06:22:09 +0000477 if (setin != setout)
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100478 failCount++;
Mark Sleee8540632006-05-30 09:24:40 +0000479
480 /**
481 * LIST TEST
482 */
Mark Sleeb9acf982006-10-10 01:57:32 +0000483 vector<int32_t> listout;
Mark Sleee8540632006-05-30 09:24:40 +0000484 for (int32_t i = -2; i < 3; ++i) {
485 listout.push_back(i);
486 }
487 printf("testList({");
Mark Sleeb9acf982006-10-10 01:57:32 +0000488 vector<int32_t>::const_iterator l_iter;
Mark Sleee8540632006-05-30 09:24:40 +0000489 first = true;
490 for (l_iter = listout.begin(); l_iter != listout.end(); ++l_iter) {
491 if (first) {
492 first = false;
493 } else {
494 printf(", ");
495 }
496 printf("%d", *l_iter);
497 }
498 printf("})");
Mark Slee1921d202007-01-24 19:43:06 +0000499 vector<int32_t> listin;
500 testClient.testList(listin, listout);
Mark Sleee8540632006-05-30 09:24:40 +0000501 printf(" = {");
502 first = true;
503 for (l_iter = listin.begin(); l_iter != listin.end(); ++l_iter) {
504 if (first) {
505 first = false;
506 } else {
507 printf(", ");
508 }
509 printf("%d", *l_iter);
510 }
511 printf("}\n");
Roger Meier4fce9602012-05-04 06:22:09 +0000512 if (listin != listout)
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100513 failCount++;
Mark Sleee8540632006-05-30 09:24:40 +0000514
515 /**
516 * ENUM TEST
517 */
518 printf("testEnum(ONE)");
Bryan Duxbury833ae492010-09-27 17:26:02 +0000519 Numberz::type ret = testClient.testEnum(Numberz::ONE);
Mark Sleee8540632006-05-30 09:24:40 +0000520 printf(" = %d\n", ret);
Roger Meier4fce9602012-05-04 06:22:09 +0000521 if (ret != Numberz::ONE)
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100522 failCount++;
Mark Sleee8540632006-05-30 09:24:40 +0000523
524 printf("testEnum(TWO)");
Bryan Duxbury833ae492010-09-27 17:26:02 +0000525 ret = testClient.testEnum(Numberz::TWO);
Mark Sleee8540632006-05-30 09:24:40 +0000526 printf(" = %d\n", ret);
Roger Meier4fce9602012-05-04 06:22:09 +0000527 if (ret != Numberz::TWO)
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100528 failCount++;
Mark Sleee8540632006-05-30 09:24:40 +0000529
530 printf("testEnum(THREE)");
Bryan Duxbury833ae492010-09-27 17:26:02 +0000531 ret = testClient.testEnum(Numberz::THREE);
Mark Sleee8540632006-05-30 09:24:40 +0000532 printf(" = %d\n", ret);
Roger Meier4fce9602012-05-04 06:22:09 +0000533 if (ret != Numberz::THREE)
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100534 failCount++;
Mark Sleee8540632006-05-30 09:24:40 +0000535
536 printf("testEnum(FIVE)");
Bryan Duxbury833ae492010-09-27 17:26:02 +0000537 ret = testClient.testEnum(Numberz::FIVE);
Mark Sleee8540632006-05-30 09:24:40 +0000538 printf(" = %d\n", ret);
Roger Meier4fce9602012-05-04 06:22:09 +0000539 if (ret != Numberz::FIVE)
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100540 failCount++;
Mark Sleee8540632006-05-30 09:24:40 +0000541
542 printf("testEnum(EIGHT)");
Bryan Duxbury833ae492010-09-27 17:26:02 +0000543 ret = testClient.testEnum(Numberz::EIGHT);
Mark Sleee8540632006-05-30 09:24:40 +0000544 printf(" = %d\n", ret);
Roger Meier4fce9602012-05-04 06:22:09 +0000545 if (ret != Numberz::EIGHT)
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100546 failCount++;
Mark Sleee8540632006-05-30 09:24:40 +0000547
548 /**
549 * TYPEDEF TEST
550 */
551 printf("testTypedef(309858235082523)");
Marc Slemkobf4fd192006-08-15 21:29:39 +0000552 UserId uid = testClient.testTypedef(309858235082523LL);
Roger Meier0e814802014-01-17 21:07:58 +0100553 printf(" = %" PRId64 "\n", uid);
Roger Meier4fce9602012-05-04 06:22:09 +0000554 if (uid != 309858235082523LL)
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100555 failCount++;
Mark Sleee8540632006-05-30 09:24:40 +0000556
557 /**
558 * NESTED MAP TEST
559 */
560 printf("testMapMap(1)");
Mark Slee1921d202007-01-24 19:43:06 +0000561 map<int32_t, map<int32_t, int32_t> > mm;
562 testClient.testMapMap(mm, 1);
Mark Sleee8540632006-05-30 09:24:40 +0000563 printf(" = {");
564 map<int32_t, map<int32_t, int32_t> >::const_iterator mi;
565 for (mi = mm.begin(); mi != mm.end(); ++mi) {
566 printf("%d => {", mi->first);
567 map<int32_t, int32_t>::const_iterator mi2;
568 for (mi2 = mi->second.begin(); mi2 != mi->second.end(); ++mi2) {
569 printf("%d => %d, ", mi2->first, mi2->second);
570 }
571 printf("}, ");
572 }
573 printf("}\n");
574
575 /**
576 * INSANITY TEST
577 */
Jens Geyerf4598682014-05-08 23:18:44 +0200578 if (!noinsane) {
579 Insanity insane;
580 insane.userMap.insert(make_pair(Numberz::FIVE, 5000));
581 Xtruct truck;
582 truck.string_thing = "Truck";
583 truck.byte_thing = 8;
584 truck.i32_thing = 8;
585 truck.i64_thing = 8;
586 insane.xtructs.push_back(truck);
587 printf("testInsanity()");
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100588 map<UserId, map<Numberz::type, Insanity> > whoa;
Jens Geyerf4598682014-05-08 23:18:44 +0200589 testClient.testInsanity(whoa, insane);
590 printf(" = {");
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100591 map<UserId, map<Numberz::type, Insanity> >::const_iterator i_iter;
Jens Geyerf4598682014-05-08 23:18:44 +0200592 for (i_iter = whoa.begin(); i_iter != whoa.end(); ++i_iter) {
593 printf("%" PRId64 " => {", i_iter->first);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100594 map<Numberz::type, Insanity>::const_iterator i2_iter;
595 for (i2_iter = i_iter->second.begin(); i2_iter != i_iter->second.end(); ++i2_iter) {
Jens Geyerf4598682014-05-08 23:18:44 +0200596 printf("%d => {", i2_iter->first);
597 map<Numberz::type, UserId> userMap = i2_iter->second.userMap;
598 map<Numberz::type, UserId>::const_iterator um;
599 printf("{");
600 for (um = userMap.begin(); um != userMap.end(); ++um) {
601 printf("%d => %" PRId64 ", ", um->first, um->second);
602 }
603 printf("}, ");
Mark Sleee8540632006-05-30 09:24:40 +0000604
Jens Geyerf4598682014-05-08 23:18:44 +0200605 vector<Xtruct> xtructs = i2_iter->second.xtructs;
606 vector<Xtruct>::const_iterator x;
607 printf("{");
608 for (x = xtructs.begin(); x != xtructs.end(); ++x) {
609 printf("{\"%s\", %d, %d, %" PRId64 "}, ",
610 x->string_thing.c_str(),
611 (int)x->byte_thing,
612 x->i32_thing,
613 x->i64_thing);
614 }
615 printf("}");
Mark Sleee8540632006-05-30 09:24:40 +0000616
Jens Geyerf4598682014-05-08 23:18:44 +0200617 printf("}, ");
618 }
Mark Sleee8540632006-05-30 09:24:40 +0000619 printf("}, ");
620 }
Jens Geyerf4598682014-05-08 23:18:44 +0200621 printf("}\n");
Mark Sleee8540632006-05-30 09:24:40 +0000622 }
Marc Slemko71d4e472006-08-15 22:34:04 +0000623 /* test exception */
Mark Slee95771002006-06-07 06:53:25 +0000624
Marc Slemkobf4fd192006-08-15 21:29:39 +0000625 try {
Marc Slemko71d4e472006-08-15 22:34:04 +0000626 printf("testClient.testException(\"Xception\") =>");
627 testClient.testException("Xception");
628 printf(" void\nFAILURE\n");
Roger Meier4fce9602012-05-04 06:22:09 +0000629 failCount++;
David Reiss0c90f6f2008-02-06 22:18:40 +0000630
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100631 } catch (Xception& e) {
Marc Slemko71d4e472006-08-15 22:34:04 +0000632 printf(" {%u, \"%s\"}\n", e.errorCode, e.message.c_str());
Marc Slemkobf4fd192006-08-15 21:29:39 +0000633 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000634
Marc Slemkobf4fd192006-08-15 21:29:39 +0000635 try {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100636 printf("testClient.testException(\"TException\") =>");
637 testClient.testException("TException");
638 printf(" void\nFAILURE\n");
639 failCount++;
Roger Meierf50df7f2012-05-02 22:49:55 +0000640
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100641 } catch (const TException&) {
642 printf(" Caught TException\n");
643 }
Roger Meierf50df7f2012-05-02 22:49:55 +0000644
645 try {
Marc Slemko71d4e472006-08-15 22:34:04 +0000646 printf("testClient.testException(\"success\") =>");
647 testClient.testException("success");
648 printf(" void\n");
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100649 } catch (...) {
Marc Slemko71d4e472006-08-15 22:34:04 +0000650 printf(" exception\nFAILURE\n");
Roger Meier4fce9602012-05-04 06:22:09 +0000651 failCount++;
Marc Slemko71d4e472006-08-15 22:34:04 +0000652 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000653
Marc Slemko71d4e472006-08-15 22:34:04 +0000654 /* test multi exception */
David Reiss0c90f6f2008-02-06 22:18:40 +0000655
Marc Slemko71d4e472006-08-15 22:34:04 +0000656 try {
657 printf("testClient.testMultiException(\"Xception\", \"test 1\") =>");
Mark Slee1921d202007-01-24 19:43:06 +0000658 Xtruct result;
659 testClient.testMultiException(result, "Xception", "test 1");
Marc Slemko71d4e472006-08-15 22:34:04 +0000660 printf(" result\nFAILURE\n");
Roger Meier4fce9602012-05-04 06:22:09 +0000661 failCount++;
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100662 } catch (Xception& e) {
Marc Slemko71d4e472006-08-15 22:34:04 +0000663 printf(" {%u, \"%s\"}\n", e.errorCode, e.message.c_str());
664 }
665
666 try {
667 printf("testClient.testMultiException(\"Xception2\", \"test 2\") =>");
Mark Slee1921d202007-01-24 19:43:06 +0000668 Xtruct result;
669 testClient.testMultiException(result, "Xception2", "test 2");
Marc Slemko71d4e472006-08-15 22:34:04 +0000670 printf(" result\nFAILURE\n");
Roger Meier4fce9602012-05-04 06:22:09 +0000671 failCount++;
David Reiss0c90f6f2008-02-06 22:18:40 +0000672
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100673 } catch (Xception2& e) {
Marc Slemko71d4e472006-08-15 22:34:04 +0000674 printf(" {%u, {\"%s\"}}\n", e.errorCode, e.struct_thing.string_thing.c_str());
Marc Slemkobf4fd192006-08-15 21:29:39 +0000675 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000676
Marc Slemko71d4e472006-08-15 22:34:04 +0000677 try {
678 printf("testClient.testMultiException(\"success\", \"test 3\") =>");
Mark Slee1921d202007-01-24 19:43:06 +0000679 Xtruct result;
680 testClient.testMultiException(result, "success", "test 3");
Marc Slemko71d4e472006-08-15 22:34:04 +0000681 printf(" {{\"%s\"}}\n", result.string_thing.c_str());
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100682 } catch (...) {
Marc Slemko71d4e472006-08-15 22:34:04 +0000683 printf(" exception\nFAILURE\n");
Roger Meier4fce9602012-05-04 06:22:09 +0000684 failCount++;
Marc Slemko71d4e472006-08-15 22:34:04 +0000685 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000686
David Reissc51986f2009-03-24 20:01:25 +0000687 /* test oneway void */
David Reiss2ab6fe82008-02-18 02:11:44 +0000688 {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100689 printf("testClient.testOneway(1) =>");
690 uint64_t startOneway = now();
691 testClient.testOneway(1);
692 uint64_t elapsed = now() - startOneway;
693 if (elapsed > 200 * 1000) { // 0.2 seconds
694 printf(" FAILURE - took %.2f ms\n", (double)elapsed / 1000.0);
695 failCount++;
696 } else {
697 printf(" success - took %.2f ms\n", (double)elapsed / 1000.0);
698 }
David Reiss2ab6fe82008-02-18 02:11:44 +0000699 }
700
David Reiss2845b522008-02-18 02:11:52 +0000701 /**
David Reissc51986f2009-03-24 20:01:25 +0000702 * redo a simple test after the oneway to make sure we aren't "off by one" --
703 * if the server treated oneway void like normal void, this next test will
David Reiss2845b522008-02-18 02:11:52 +0000704 * fail since it will get the void confirmation rather than the correct
705 * result. In this circumstance, the client will throw the exception:
706 *
707 * TApplicationException: Wrong method namea
708 */
709 /**
710 * I32 TEST
711 */
712 printf("re-test testI32(-1)");
713 i32 = testClient.testI32(-1);
714 printf(" = %d\n", i32);
Roger Meier4fce9602012-05-04 06:22:09 +0000715 if (i32 != -1)
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100716 failCount++;
David Reiss2845b522008-02-18 02:11:52 +0000717
Marc Slemkobf4fd192006-08-15 21:29:39 +0000718 uint64_t stop = now();
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100719 uint64_t tot = stop - start;
Mark Sleed788b2e2006-09-07 01:26:35 +0000720
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100721 printf("Total time: %" PRIu64 " us\n", stop - start);
David Reiss0c90f6f2008-02-06 22:18:40 +0000722
Mark Sleed788b2e2006-09-07 01:26:35 +0000723 time_tot += tot;
724 if (time_min == 0 || tot < time_min) {
725 time_min = tot;
726 }
727 if (tot > time_max) {
728 time_max = tot;
729 }
730
Mark Sleea3302652006-10-25 19:03:32 +0000731 transport->close();
Mark Sleee8540632006-05-30 09:24:40 +0000732 }
733
Mark Sleee8540632006-05-30 09:24:40 +0000734 printf("\nAll tests done.\n");
Mark Sleed788b2e2006-09-07 01:26:35 +0000735
736 uint64_t time_avg = time_tot / numTests;
737
Roger Meier0e814802014-01-17 21:07:58 +0100738 printf("Min time: %" PRIu64 " us\n", time_min);
739 printf("Max time: %" PRIu64 " us\n", time_max);
740 printf("Avg time: %" PRIu64 " us\n", time_avg);
Mark Sleed788b2e2006-09-07 01:26:35 +0000741
Roger Meier4fce9602012-05-04 06:22:09 +0000742 return failCount;
Mark Sleee8540632006-05-30 09:24:40 +0000743}