blob: 2a89a2807b02f67d62647db5f42b8b7722ddea08 [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
55// Current time, microseconds since the epoch
Konrad Grochowski16a23a62014-11-13 15:33:38 +010056uint64_t now() {
Roger Meier5f9614c2010-11-21 16:59:05 +000057 int64_t ret;
Mark Slee95771002006-06-07 06:53:25 +000058 struct timeval tv;
David Reiss0c90f6f2008-02-06 22:18:40 +000059
Jake Farrell5d02b802014-01-07 21:42:01 -050060 THRIFT_GETTIMEOFDAY(&tv, NULL);
Mark Slee95771002006-06-07 06:53:25 +000061 ret = tv.tv_sec;
Konrad Grochowski16a23a62014-11-13 15:33:38 +010062 ret = ret * 1000 * 1000 + tv.tv_usec;
Mark Slee95771002006-06-07 06:53:25 +000063 return ret;
64}
65
Konrad Grochowski16a23a62014-11-13 15:33:38 +010066static void testString_clientReturn(const char* host,
67 int port,
68 event_base* base,
69 TProtocolFactory* protocolFactory,
70 ThriftTestCobClient* client) {
71 (void)host;
72 (void)port;
73 (void)protocolFactory;
Roger Meier7e056e72011-07-17 07:28:28 +000074 try {
75 string s;
76 client->recv_testString(s);
77 cout << "testString: " << s << endl;
78 } catch (TException& exn) {
Jake Farrell5d02b802014-01-07 21:42:01 -050079 cout << "Error: " << exn.what() << endl;
Roger Meier7e056e72011-07-17 07:28:28 +000080 }
81
82 event_base_loopbreak(base); // end test
83}
84
Konrad Grochowski16a23a62014-11-13 15:33:38 +010085static void testVoid_clientReturn(const char* host,
86 int port,
87 event_base* base,
88 TProtocolFactory* protocolFactory,
89 ThriftTestCobClient* client) {
Roger Meier7e056e72011-07-17 07:28:28 +000090 try {
91 client->recv_testVoid();
92 cout << "testVoid" << endl;
93
94 // next test
95 delete client;
Roger Meier611f90c2011-12-11 22:08:51 +000096 boost::shared_ptr<TAsyncChannel> channel(new TEvhttpClientChannel(host, "/", host, port, base));
Roger Meier7e056e72011-07-17 07:28:28 +000097 client = new ThriftTestCobClient(channel, protocolFactory);
Konrad Grochowski16a23a62014-11-13 15:33:38 +010098 client->testString(tcxx::bind(testString_clientReturn,
99 host,
100 port,
101 base,
102 protocolFactory,
103 tcxx::placeholders::_1),
104 "Test");
Roger Meier7e056e72011-07-17 07:28:28 +0000105 } catch (TException& exn) {
Jake Farrell5d02b802014-01-07 21:42:01 -0500106 cout << "Error: " << exn.what() << endl;
Roger Meier7e056e72011-07-17 07:28:28 +0000107 }
108}
109
Mark Sleee8540632006-05-30 09:24:40 +0000110int main(int argc, char** argv) {
cdwijayarathnaa07ec0b2014-08-09 17:45:56 +0530111 string file_path = boost::filesystem::system_complete(argv[0]).string();
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100112 string dir_path = file_path.substr(0, file_path.size() - EXECUTABLE_FILE_NAME_LENGTH);
Jake Farrell5d02b802014-01-07 21:42:01 -0500113#if _WIN32
114 transport::TWinsockSingleton::create();
115#endif
Mark Sleee8540632006-05-30 09:24:40 +0000116 string host = "localhost";
117 int port = 9090;
118 int numTests = 1;
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000119 bool ssl = false;
Roger Meierca142b02011-06-07 17:59:07 +0000120 string transport_type = "buffered";
121 string protocol_type = "binary";
122 string domain_socket = "";
Jens Geyerf4598682014-05-08 23:18:44 +0200123 bool noinsane = false;
Mark Sleee8540632006-05-30 09:24:40 +0000124
Jake Farrell5d02b802014-01-07 21:42:01 -0500125 boost::program_options::options_description desc("Allowed options");
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100126 desc.add_options()("help,h",
127 "produce help message")("host",
128 boost::program_options::value<string>(&host)
129 ->default_value(host),
130 "Host to connect")("port",
131 boost::program_options::value<int>(
132 &port)->default_value(port),
133 "Port number to connect")(
134 "domain-socket",
135 boost::program_options::value<string>(&domain_socket)->default_value(domain_socket),
136 "Domain Socket (e.g. /tmp/ThriftTest.thrift), instead of host and port")(
137 "transport",
138 boost::program_options::value<string>(&transport_type)->default_value(transport_type),
139 "Transport: buffered, framed, http, evhttp")(
140 "protocol",
141 boost::program_options::value<string>(&protocol_type)->default_value(protocol_type),
142 "Protocol: binary, compact, json")("ssl", "Encrypted Transport using SSL")(
143 "testloops,n",
144 boost::program_options::value<int>(&numTests)->default_value(numTests),
145 "Number of Tests")("noinsane", "Do not run insanity test");
Roger Meierca142b02011-06-07 17:59:07 +0000146
Jake Farrell5d02b802014-01-07 21:42:01 -0500147 boost::program_options::variables_map vm;
148 boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), vm);
149 boost::program_options::notify(vm);
Roger Meierca142b02011-06-07 17:59:07 +0000150
151 if (vm.count("help")) {
152 cout << desc << "\n";
153 return 1;
Mark Sleee8540632006-05-30 09:24:40 +0000154 }
Mark Sleea3302652006-10-25 19:03:32 +0000155
Jake Farrell5d02b802014-01-07 21:42:01 -0500156 try {
Roger Meierca142b02011-06-07 17:59:07 +0000157 if (!protocol_type.empty()) {
158 if (protocol_type == "binary") {
Roger Meier284101c2014-03-11 21:20:35 +0100159 } else if (protocol_type == "compact") {
Roger Meierca142b02011-06-07 17:59:07 +0000160 } else if (protocol_type == "json") {
161 } else {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100162 throw invalid_argument("Unknown protocol type " + protocol_type);
Roger Meierca142b02011-06-07 17:59:07 +0000163 }
164 }
165
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100166 if (!transport_type.empty()) {
Roger Meierca142b02011-06-07 17:59:07 +0000167 if (transport_type == "buffered") {
168 } else if (transport_type == "framed") {
169 } else if (transport_type == "http") {
Roger Meier7e056e72011-07-17 07:28:28 +0000170 } else if (transport_type == "evhttp") {
Roger Meierca142b02011-06-07 17:59:07 +0000171 } else {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100172 throw invalid_argument("Unknown transport type " + transport_type);
Roger Meierca142b02011-06-07 17:59:07 +0000173 }
174 }
175
176 } catch (std::exception& e) {
177 cerr << e.what() << endl;
178 cout << desc << "\n";
179 return 1;
180 }
181
182 if (vm.count("ssl")) {
183 ssl = true;
184 }
cdwijayarathnaa07ec0b2014-08-09 17:45:56 +0530185
Jens Geyerf4598682014-05-08 23:18:44 +0200186 if (vm.count("noinsane")) {
187 noinsane = true;
188 }
Roger Meierca142b02011-06-07 17:59:07 +0000189
Roger Meier611f90c2011-12-11 22:08:51 +0000190 boost::shared_ptr<TTransport> transport;
191 boost::shared_ptr<TProtocol> protocol;
Roger Meierca142b02011-06-07 17:59:07 +0000192
Roger Meier611f90c2011-12-11 22:08:51 +0000193 boost::shared_ptr<TSocket> socket;
194 boost::shared_ptr<TSSLSocketFactory> factory;
Roger Meierca142b02011-06-07 17:59:07 +0000195
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000196 if (ssl) {
Roger Meier611f90c2011-12-11 22:08:51 +0000197 factory = boost::shared_ptr<TSSLSocketFactory>(new TSSLSocketFactory());
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000198 factory->ciphers("ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
cdwijayarathnaa07ec0b2014-08-09 17:45:56 +0530199 factory->loadTrustedCertificates((dir_path + "../keys/CA.pem").c_str());
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000200 factory->authenticate(true);
201 socket = factory->createSocket(host, port);
202 } else {
Roger Meierca142b02011-06-07 17:59:07 +0000203 if (domain_socket != "") {
Roger Meier611f90c2011-12-11 22:08:51 +0000204 socket = boost::shared_ptr<TSocket>(new TSocket(domain_socket));
Roger Meierca142b02011-06-07 17:59:07 +0000205 port = 0;
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100206 } else {
Roger Meier611f90c2011-12-11 22:08:51 +0000207 socket = boost::shared_ptr<TSocket>(new TSocket(host, port));
Roger Meierca142b02011-06-07 17:59:07 +0000208 }
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000209 }
Mark Sleea3302652006-10-25 19:03:32 +0000210
Roger Meierca142b02011-06-07 17:59:07 +0000211 if (transport_type.compare("http") == 0) {
Roger Meier611f90c2011-12-11 22:08:51 +0000212 boost::shared_ptr<TTransport> httpSocket(new THttpClient(socket, host, "/service"));
Roger Meierca142b02011-06-07 17:59:07 +0000213 transport = httpSocket;
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100214 } else if (transport_type.compare("framed") == 0) {
Roger Meier611f90c2011-12-11 22:08:51 +0000215 boost::shared_ptr<TFramedTransport> framedSocket(new TFramedTransport(socket));
Mark Sleea3302652006-10-25 19:03:32 +0000216 transport = framedSocket;
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100217 } else {
Roger Meier611f90c2011-12-11 22:08:51 +0000218 boost::shared_ptr<TBufferedTransport> bufferedSocket(new TBufferedTransport(socket));
Mark Sleea3302652006-10-25 19:03:32 +0000219 transport = bufferedSocket;
220 }
221
Roger Meierca142b02011-06-07 17:59:07 +0000222 if (protocol_type.compare("json") == 0) {
Roger Meier611f90c2011-12-11 22:08:51 +0000223 boost::shared_ptr<TProtocol> jsonProtocol(new TJSONProtocol(transport));
Roger Meierca142b02011-06-07 17:59:07 +0000224 protocol = jsonProtocol;
Roger Meier023192f2014-02-12 09:35:12 +0100225 } else if (protocol_type.compare("compact") == 0) {
226 boost::shared_ptr<TProtocol> compactProtocol(new TCompactProtocol(transport));
227 protocol = compactProtocol;
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100228 } else {
Roger Meier611f90c2011-12-11 22:08:51 +0000229 boost::shared_ptr<TBinaryProtocol> binaryProtocol(new TBinaryProtocol(transport));
Roger Meierca142b02011-06-07 17:59:07 +0000230 protocol = binaryProtocol;
231 }
232
233 // Connection info
234 cout << "Connecting (" << transport_type << "/" << protocol_type << ") to: " << domain_socket;
235 if (port != 0) {
236 cout << host << ":" << port;
237 }
238 cout << endl;
239
Roger Meier7e056e72011-07-17 07:28:28 +0000240 if (transport_type.compare("evhttp") == 0) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100241 event_base* base = event_base_new();
Roger Meier7e056e72011-07-17 07:28:28 +0000242 cout << "Libevent Version: " << event_get_version() << endl;
243 cout << "Libevent Method: " << event_base_get_method(base) << endl;
244#if LIBEVENT_VERSION_NUMBER >= 0x02000000
245 cout << "Libevent Features: 0x" << hex << event_base_get_features(base) << endl;
246#endif
247
Roger Meier611f90c2011-12-11 22:08:51 +0000248 boost::shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());
Roger Meier7e056e72011-07-17 07:28:28 +0000249
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100250 boost::shared_ptr<TAsyncChannel> channel(
251 new TEvhttpClientChannel(host.c_str(), "/", host.c_str(), port, base));
Roger Meier7e056e72011-07-17 07:28:28 +0000252 ThriftTestCobClient* client = new ThriftTestCobClient(channel, protocolFactory.get());
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100253 client->testVoid(tcxx::bind(testVoid_clientReturn,
254 host.c_str(),
255 port,
256 base,
257 protocolFactory.get(),
258 tcxx::placeholders::_1));
Jake Farrell5d02b802014-01-07 21:42:01 -0500259
Roger Meier7e056e72011-07-17 07:28:28 +0000260 event_base_loop(base, 0);
261 return 0;
262 }
263
Roger Meierca142b02011-06-07 17:59:07 +0000264 ThriftTestClient testClient(protocol);
Mark Sleed788b2e2006-09-07 01:26:35 +0000265
266 uint64_t time_min = 0;
267 uint64_t time_max = 0;
268 uint64_t time_tot = 0;
David Reiss0c90f6f2008-02-06 22:18:40 +0000269
Roger Meier4fce9602012-05-04 06:22:09 +0000270 int failCount = 0;
Mark Sleee8540632006-05-30 09:24:40 +0000271 int test = 0;
272 for (test = 0; test < numTests; ++test) {
Mark Slee95771002006-06-07 06:53:25 +0000273
Mark Slee95771002006-06-07 06:53:25 +0000274 try {
Mark Sleea3302652006-10-25 19:03:32 +0000275 transport->open();
Mark Slee95771002006-06-07 06:53:25 +0000276 } catch (TTransportException& ttx) {
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000277 printf("Connect failed: %s\n", ttx.what());
Roger Meier5b1e3c72011-12-08 13:20:12 +0000278 return 1;
Mark Sleee8540632006-05-30 09:24:40 +0000279 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000280
Mark Sleed788b2e2006-09-07 01:26:35 +0000281 /**
282 * CONNECT TEST
283 */
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100284 printf("Test #%d, connect %s:%d\n", test + 1, host.c_str(), port);
Mark Slee95771002006-06-07 06:53:25 +0000285
286 uint64_t start = now();
David Reiss0c90f6f2008-02-06 22:18:40 +0000287
Mark Sleee8540632006-05-30 09:24:40 +0000288 /**
289 * VOID TEST
290 */
Mark Sleee129a2d2007-02-21 05:17:48 +0000291 try {
292 printf("testVoid()");
293 testClient.testVoid();
294 printf(" = void\n");
Roger Meier4fce9602012-05-04 06:22:09 +0000295 } catch (TApplicationException& tax) {
Mark Sleee129a2d2007-02-21 05:17:48 +0000296 printf("%s\n", tax.what());
Roger Meier4fce9602012-05-04 06:22:09 +0000297 failCount++;
Mark Sleee129a2d2007-02-21 05:17:48 +0000298 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000299
Mark Sleee8540632006-05-30 09:24:40 +0000300 /**
301 * STRING TEST
302 */
303 printf("testString(\"Test\")");
Mark Slee1921d202007-01-24 19:43:06 +0000304 string s;
305 testClient.testString(s, "Test");
Mark Sleee8540632006-05-30 09:24:40 +0000306 printf(" = \"%s\"\n", s.c_str());
Roger Meier4fce9602012-05-04 06:22:09 +0000307 if (s != "Test")
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100308 failCount++;
David Reiss0c90f6f2008-02-06 22:18:40 +0000309
Mark Sleee8540632006-05-30 09:24:40 +0000310 /**
311 * BYTE TEST
312 */
313 printf("testByte(1)");
314 uint8_t u8 = testClient.testByte(1);
315 printf(" = %d\n", (int)u8);
Roger Meier4fce9602012-05-04 06:22:09 +0000316 if (u8 != 1)
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100317 failCount++;
David Reiss0c90f6f2008-02-06 22:18:40 +0000318
Mark Sleee8540632006-05-30 09:24:40 +0000319 /**
320 * I32 TEST
321 */
322 printf("testI32(-1)");
323 int32_t i32 = testClient.testI32(-1);
324 printf(" = %d\n", i32);
Roger Meier4fce9602012-05-04 06:22:09 +0000325 if (i32 != -1)
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100326 failCount++;
Mark Sleee8540632006-05-30 09:24:40 +0000327
328 /**
Mark Sleee8540632006-05-30 09:24:40 +0000329 * I64 TEST
330 */
331 printf("testI64(-34359738368)");
Marc Slemkobf4fd192006-08-15 21:29:39 +0000332 int64_t i64 = testClient.testI64(-34359738368LL);
Roger Meier0e814802014-01-17 21:07:58 +0100333 printf(" = %" PRId64 "\n", i64);
Roger Meier4fce9602012-05-04 06:22:09 +0000334 if (i64 != -34359738368LL)
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100335 failCount++;
Jens Geyer8bcfdd92014-12-14 03:14:26 +0100336
Mark Sleec98d0502006-09-06 02:42:25 +0000337 /**
338 * DOUBLE TEST
339 */
340 printf("testDouble(-5.2098523)");
341 double dub = testClient.testDouble(-5.2098523);
Roger Meiera8cef6e2011-07-17 18:55:59 +0000342 printf(" = %f\n", dub);
Roger Meier4fce9602012-05-04 06:22:09 +0000343 if ((dub - (-5.2098523)) > 0.001)
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100344 failCount++;
David Reiss0c90f6f2008-02-06 22:18:40 +0000345
Mark Sleee8540632006-05-30 09:24:40 +0000346 /**
Jens Geyer8bcfdd92014-12-14 03:14:26 +0100347 * BINARY TEST
348 */
349 // TODO: add testBinary() call
350
351 /**
Mark Sleee8540632006-05-30 09:24:40 +0000352 * STRUCT TEST
353 */
Mark Slee6e536442006-06-30 18:28:50 +0000354 printf("testStruct({\"Zero\", 1, -3, -5})");
Mark Sleee8540632006-05-30 09:24:40 +0000355 Xtruct out;
356 out.string_thing = "Zero";
357 out.byte_thing = 1;
Mark Sleee8540632006-05-30 09:24:40 +0000358 out.i32_thing = -3;
Mark Sleee8540632006-05-30 09:24:40 +0000359 out.i64_thing = -5;
Mark Slee1921d202007-01-24 19:43:06 +0000360 Xtruct in;
361 testClient.testStruct(in, out);
Roger Meier0e814802014-01-17 21:07:58 +0100362 printf(" = {\"%s\", %d, %d, %" PRId64 "}\n",
Mark Sleee8540632006-05-30 09:24:40 +0000363 in.string_thing.c_str(),
364 (int)in.byte_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000365 in.i32_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000366 in.i64_thing);
Roger Meier4fce9602012-05-04 06:22:09 +0000367 if (in != out)
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100368 failCount++;
David Reiss0c90f6f2008-02-06 22:18:40 +0000369
Mark Sleee8540632006-05-30 09:24:40 +0000370 /**
371 * NESTED STRUCT TEST
372 */
Mark Slee6e536442006-06-30 18:28:50 +0000373 printf("testNest({1, {\"Zero\", 1, -3, -5}), 5}");
Mark Sleee8540632006-05-30 09:24:40 +0000374 Xtruct2 out2;
375 out2.byte_thing = 1;
376 out2.struct_thing = out;
377 out2.i32_thing = 5;
Mark Slee1921d202007-01-24 19:43:06 +0000378 Xtruct2 in2;
379 testClient.testNest(in2, out2);
Mark Sleee8540632006-05-30 09:24:40 +0000380 in = in2.struct_thing;
Roger Meier0e814802014-01-17 21:07:58 +0100381 printf(" = {%d, {\"%s\", %d, %d, %" PRId64 "}, %d}\n",
Mark Sleee8540632006-05-30 09:24:40 +0000382 in2.byte_thing,
383 in.string_thing.c_str(),
384 (int)in.byte_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000385 in.i32_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000386 in.i64_thing,
David Reiss0c90f6f2008-02-06 22:18:40 +0000387 in2.i32_thing);
Roger Meier4fce9602012-05-04 06:22:09 +0000388 if (in2 != out2)
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100389 failCount++;
Mark Sleee8540632006-05-30 09:24:40 +0000390
391 /**
392 * MAP TEST
393 */
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100394 map<int32_t, int32_t> mapout;
Mark Sleee8540632006-05-30 09:24:40 +0000395 for (int32_t i = 0; i < 5; ++i) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100396 mapout.insert(make_pair(i, i - 10));
Mark Sleee8540632006-05-30 09:24:40 +0000397 }
398 printf("testMap({");
399 map<int32_t, int32_t>::const_iterator m_iter;
400 bool first = true;
401 for (m_iter = mapout.begin(); m_iter != mapout.end(); ++m_iter) {
402 if (first) {
403 first = false;
404 } else {
405 printf(", ");
406 }
407 printf("%d => %d", m_iter->first, m_iter->second);
408 }
409 printf("})");
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100410 map<int32_t, int32_t> mapin;
Mark Slee1921d202007-01-24 19:43:06 +0000411 testClient.testMap(mapin, mapout);
Mark Sleee8540632006-05-30 09:24:40 +0000412 printf(" = {");
413 first = true;
414 for (m_iter = mapin.begin(); m_iter != mapin.end(); ++m_iter) {
415 if (first) {
416 first = false;
417 } else {
418 printf(", ");
419 }
420 printf("%d => %d", m_iter->first, m_iter->second);
421 }
422 printf("}\n");
Roger Meier4fce9602012-05-04 06:22:09 +0000423 if (mapin != mapout)
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100424 failCount++;
Roger Meier4fce9602012-05-04 06:22:09 +0000425
426 /**
427 * STRING MAP TEST
428 * missing
429 */
Mark Sleee8540632006-05-30 09:24:40 +0000430
431 /**
432 * SET TEST
433 */
434 set<int32_t> setout;
435 for (int32_t i = -2; i < 3; ++i) {
436 setout.insert(i);
437 }
438 printf("testSet({");
439 set<int32_t>::const_iterator s_iter;
440 first = true;
441 for (s_iter = setout.begin(); s_iter != setout.end(); ++s_iter) {
442 if (first) {
443 first = false;
444 } else {
445 printf(", ");
446 }
447 printf("%d", *s_iter);
448 }
449 printf("})");
Mark Slee1921d202007-01-24 19:43:06 +0000450 set<int32_t> setin;
451 testClient.testSet(setin, setout);
Mark Sleee8540632006-05-30 09:24:40 +0000452 printf(" = {");
453 first = true;
454 for (s_iter = setin.begin(); s_iter != setin.end(); ++s_iter) {
455 if (first) {
456 first = false;
457 } else {
458 printf(", ");
459 }
460 printf("%d", *s_iter);
461 }
462 printf("}\n");
Roger Meier4fce9602012-05-04 06:22:09 +0000463 if (setin != setout)
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100464 failCount++;
Mark Sleee8540632006-05-30 09:24:40 +0000465
466 /**
467 * LIST TEST
468 */
Mark Sleeb9acf982006-10-10 01:57:32 +0000469 vector<int32_t> listout;
Mark Sleee8540632006-05-30 09:24:40 +0000470 for (int32_t i = -2; i < 3; ++i) {
471 listout.push_back(i);
472 }
473 printf("testList({");
Mark Sleeb9acf982006-10-10 01:57:32 +0000474 vector<int32_t>::const_iterator l_iter;
Mark Sleee8540632006-05-30 09:24:40 +0000475 first = true;
476 for (l_iter = listout.begin(); l_iter != listout.end(); ++l_iter) {
477 if (first) {
478 first = false;
479 } else {
480 printf(", ");
481 }
482 printf("%d", *l_iter);
483 }
484 printf("})");
Mark Slee1921d202007-01-24 19:43:06 +0000485 vector<int32_t> listin;
486 testClient.testList(listin, listout);
Mark Sleee8540632006-05-30 09:24:40 +0000487 printf(" = {");
488 first = true;
489 for (l_iter = listin.begin(); l_iter != listin.end(); ++l_iter) {
490 if (first) {
491 first = false;
492 } else {
493 printf(", ");
494 }
495 printf("%d", *l_iter);
496 }
497 printf("}\n");
Roger Meier4fce9602012-05-04 06:22:09 +0000498 if (listin != listout)
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100499 failCount++;
Mark Sleee8540632006-05-30 09:24:40 +0000500
501 /**
502 * ENUM TEST
503 */
504 printf("testEnum(ONE)");
Bryan Duxbury833ae492010-09-27 17:26:02 +0000505 Numberz::type ret = testClient.testEnum(Numberz::ONE);
Mark Sleee8540632006-05-30 09:24:40 +0000506 printf(" = %d\n", ret);
Roger Meier4fce9602012-05-04 06:22:09 +0000507 if (ret != Numberz::ONE)
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100508 failCount++;
Mark Sleee8540632006-05-30 09:24:40 +0000509
510 printf("testEnum(TWO)");
Bryan Duxbury833ae492010-09-27 17:26:02 +0000511 ret = testClient.testEnum(Numberz::TWO);
Mark Sleee8540632006-05-30 09:24:40 +0000512 printf(" = %d\n", ret);
Roger Meier4fce9602012-05-04 06:22:09 +0000513 if (ret != Numberz::TWO)
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100514 failCount++;
Mark Sleee8540632006-05-30 09:24:40 +0000515
516 printf("testEnum(THREE)");
Bryan Duxbury833ae492010-09-27 17:26:02 +0000517 ret = testClient.testEnum(Numberz::THREE);
Mark Sleee8540632006-05-30 09:24:40 +0000518 printf(" = %d\n", ret);
Roger Meier4fce9602012-05-04 06:22:09 +0000519 if (ret != Numberz::THREE)
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100520 failCount++;
Mark Sleee8540632006-05-30 09:24:40 +0000521
522 printf("testEnum(FIVE)");
Bryan Duxbury833ae492010-09-27 17:26:02 +0000523 ret = testClient.testEnum(Numberz::FIVE);
Mark Sleee8540632006-05-30 09:24:40 +0000524 printf(" = %d\n", ret);
Roger Meier4fce9602012-05-04 06:22:09 +0000525 if (ret != Numberz::FIVE)
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100526 failCount++;
Mark Sleee8540632006-05-30 09:24:40 +0000527
528 printf("testEnum(EIGHT)");
Bryan Duxbury833ae492010-09-27 17:26:02 +0000529 ret = testClient.testEnum(Numberz::EIGHT);
Mark Sleee8540632006-05-30 09:24:40 +0000530 printf(" = %d\n", ret);
Roger Meier4fce9602012-05-04 06:22:09 +0000531 if (ret != Numberz::EIGHT)
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100532 failCount++;
Mark Sleee8540632006-05-30 09:24:40 +0000533
534 /**
535 * TYPEDEF TEST
536 */
537 printf("testTypedef(309858235082523)");
Marc Slemkobf4fd192006-08-15 21:29:39 +0000538 UserId uid = testClient.testTypedef(309858235082523LL);
Roger Meier0e814802014-01-17 21:07:58 +0100539 printf(" = %" PRId64 "\n", uid);
Roger Meier4fce9602012-05-04 06:22:09 +0000540 if (uid != 309858235082523LL)
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100541 failCount++;
Mark Sleee8540632006-05-30 09:24:40 +0000542
543 /**
544 * NESTED MAP TEST
545 */
546 printf("testMapMap(1)");
Mark Slee1921d202007-01-24 19:43:06 +0000547 map<int32_t, map<int32_t, int32_t> > mm;
548 testClient.testMapMap(mm, 1);
Mark Sleee8540632006-05-30 09:24:40 +0000549 printf(" = {");
550 map<int32_t, map<int32_t, int32_t> >::const_iterator mi;
551 for (mi = mm.begin(); mi != mm.end(); ++mi) {
552 printf("%d => {", mi->first);
553 map<int32_t, int32_t>::const_iterator mi2;
554 for (mi2 = mi->second.begin(); mi2 != mi->second.end(); ++mi2) {
555 printf("%d => %d, ", mi2->first, mi2->second);
556 }
557 printf("}, ");
558 }
559 printf("}\n");
560
561 /**
562 * INSANITY TEST
563 */
Jens Geyerf4598682014-05-08 23:18:44 +0200564 if (!noinsane) {
565 Insanity insane;
566 insane.userMap.insert(make_pair(Numberz::FIVE, 5000));
567 Xtruct truck;
568 truck.string_thing = "Truck";
569 truck.byte_thing = 8;
570 truck.i32_thing = 8;
571 truck.i64_thing = 8;
572 insane.xtructs.push_back(truck);
573 printf("testInsanity()");
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100574 map<UserId, map<Numberz::type, Insanity> > whoa;
Jens Geyerf4598682014-05-08 23:18:44 +0200575 testClient.testInsanity(whoa, insane);
576 printf(" = {");
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100577 map<UserId, map<Numberz::type, Insanity> >::const_iterator i_iter;
Jens Geyerf4598682014-05-08 23:18:44 +0200578 for (i_iter = whoa.begin(); i_iter != whoa.end(); ++i_iter) {
579 printf("%" PRId64 " => {", i_iter->first);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100580 map<Numberz::type, Insanity>::const_iterator i2_iter;
581 for (i2_iter = i_iter->second.begin(); i2_iter != i_iter->second.end(); ++i2_iter) {
Jens Geyerf4598682014-05-08 23:18:44 +0200582 printf("%d => {", i2_iter->first);
583 map<Numberz::type, UserId> userMap = i2_iter->second.userMap;
584 map<Numberz::type, UserId>::const_iterator um;
585 printf("{");
586 for (um = userMap.begin(); um != userMap.end(); ++um) {
587 printf("%d => %" PRId64 ", ", um->first, um->second);
588 }
589 printf("}, ");
Mark Sleee8540632006-05-30 09:24:40 +0000590
Jens Geyerf4598682014-05-08 23:18:44 +0200591 vector<Xtruct> xtructs = i2_iter->second.xtructs;
592 vector<Xtruct>::const_iterator x;
593 printf("{");
594 for (x = xtructs.begin(); x != xtructs.end(); ++x) {
595 printf("{\"%s\", %d, %d, %" PRId64 "}, ",
596 x->string_thing.c_str(),
597 (int)x->byte_thing,
598 x->i32_thing,
599 x->i64_thing);
600 }
601 printf("}");
Mark Sleee8540632006-05-30 09:24:40 +0000602
Jens Geyerf4598682014-05-08 23:18:44 +0200603 printf("}, ");
604 }
Mark Sleee8540632006-05-30 09:24:40 +0000605 printf("}, ");
606 }
Jens Geyerf4598682014-05-08 23:18:44 +0200607 printf("}\n");
Mark Sleee8540632006-05-30 09:24:40 +0000608 }
Marc Slemko71d4e472006-08-15 22:34:04 +0000609 /* test exception */
Mark Slee95771002006-06-07 06:53:25 +0000610
Marc Slemkobf4fd192006-08-15 21:29:39 +0000611 try {
Marc Slemko71d4e472006-08-15 22:34:04 +0000612 printf("testClient.testException(\"Xception\") =>");
613 testClient.testException("Xception");
614 printf(" void\nFAILURE\n");
Roger Meier4fce9602012-05-04 06:22:09 +0000615 failCount++;
David Reiss0c90f6f2008-02-06 22:18:40 +0000616
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100617 } catch (Xception& e) {
Marc Slemko71d4e472006-08-15 22:34:04 +0000618 printf(" {%u, \"%s\"}\n", e.errorCode, e.message.c_str());
Marc Slemkobf4fd192006-08-15 21:29:39 +0000619 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000620
Marc Slemkobf4fd192006-08-15 21:29:39 +0000621 try {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100622 printf("testClient.testException(\"TException\") =>");
623 testClient.testException("TException");
624 printf(" void\nFAILURE\n");
625 failCount++;
Roger Meierf50df7f2012-05-02 22:49:55 +0000626
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100627 } catch (const TException&) {
628 printf(" Caught TException\n");
629 }
Roger Meierf50df7f2012-05-02 22:49:55 +0000630
631 try {
Marc Slemko71d4e472006-08-15 22:34:04 +0000632 printf("testClient.testException(\"success\") =>");
633 testClient.testException("success");
634 printf(" void\n");
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100635 } catch (...) {
Marc Slemko71d4e472006-08-15 22:34:04 +0000636 printf(" exception\nFAILURE\n");
Roger Meier4fce9602012-05-04 06:22:09 +0000637 failCount++;
Marc Slemko71d4e472006-08-15 22:34:04 +0000638 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000639
Marc Slemko71d4e472006-08-15 22:34:04 +0000640 /* test multi exception */
David Reiss0c90f6f2008-02-06 22:18:40 +0000641
Marc Slemko71d4e472006-08-15 22:34:04 +0000642 try {
643 printf("testClient.testMultiException(\"Xception\", \"test 1\") =>");
Mark Slee1921d202007-01-24 19:43:06 +0000644 Xtruct result;
645 testClient.testMultiException(result, "Xception", "test 1");
Marc Slemko71d4e472006-08-15 22:34:04 +0000646 printf(" result\nFAILURE\n");
Roger Meier4fce9602012-05-04 06:22:09 +0000647 failCount++;
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100648 } catch (Xception& e) {
Marc Slemko71d4e472006-08-15 22:34:04 +0000649 printf(" {%u, \"%s\"}\n", e.errorCode, e.message.c_str());
650 }
651
652 try {
653 printf("testClient.testMultiException(\"Xception2\", \"test 2\") =>");
Mark Slee1921d202007-01-24 19:43:06 +0000654 Xtruct result;
655 testClient.testMultiException(result, "Xception2", "test 2");
Marc Slemko71d4e472006-08-15 22:34:04 +0000656 printf(" result\nFAILURE\n");
Roger Meier4fce9602012-05-04 06:22:09 +0000657 failCount++;
David Reiss0c90f6f2008-02-06 22:18:40 +0000658
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100659 } catch (Xception2& e) {
Marc Slemko71d4e472006-08-15 22:34:04 +0000660 printf(" {%u, {\"%s\"}}\n", e.errorCode, e.struct_thing.string_thing.c_str());
Marc Slemkobf4fd192006-08-15 21:29:39 +0000661 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000662
Marc Slemko71d4e472006-08-15 22:34:04 +0000663 try {
664 printf("testClient.testMultiException(\"success\", \"test 3\") =>");
Mark Slee1921d202007-01-24 19:43:06 +0000665 Xtruct result;
666 testClient.testMultiException(result, "success", "test 3");
Marc Slemko71d4e472006-08-15 22:34:04 +0000667 printf(" {{\"%s\"}}\n", result.string_thing.c_str());
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100668 } catch (...) {
Marc Slemko71d4e472006-08-15 22:34:04 +0000669 printf(" exception\nFAILURE\n");
Roger Meier4fce9602012-05-04 06:22:09 +0000670 failCount++;
Marc Slemko71d4e472006-08-15 22:34:04 +0000671 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000672
David Reissc51986f2009-03-24 20:01:25 +0000673 /* test oneway void */
David Reiss2ab6fe82008-02-18 02:11:44 +0000674 {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100675 printf("testClient.testOneway(1) =>");
676 uint64_t startOneway = now();
677 testClient.testOneway(1);
678 uint64_t elapsed = now() - startOneway;
679 if (elapsed > 200 * 1000) { // 0.2 seconds
680 printf(" FAILURE - took %.2f ms\n", (double)elapsed / 1000.0);
681 failCount++;
682 } else {
683 printf(" success - took %.2f ms\n", (double)elapsed / 1000.0);
684 }
David Reiss2ab6fe82008-02-18 02:11:44 +0000685 }
686
David Reiss2845b522008-02-18 02:11:52 +0000687 /**
David Reissc51986f2009-03-24 20:01:25 +0000688 * redo a simple test after the oneway to make sure we aren't "off by one" --
689 * if the server treated oneway void like normal void, this next test will
David Reiss2845b522008-02-18 02:11:52 +0000690 * fail since it will get the void confirmation rather than the correct
691 * result. In this circumstance, the client will throw the exception:
692 *
693 * TApplicationException: Wrong method namea
694 */
695 /**
696 * I32 TEST
697 */
698 printf("re-test testI32(-1)");
699 i32 = testClient.testI32(-1);
700 printf(" = %d\n", i32);
Roger Meier4fce9602012-05-04 06:22:09 +0000701 if (i32 != -1)
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100702 failCount++;
David Reiss2845b522008-02-18 02:11:52 +0000703
Marc Slemkobf4fd192006-08-15 21:29:39 +0000704 uint64_t stop = now();
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100705 uint64_t tot = stop - start;
Mark Sleed788b2e2006-09-07 01:26:35 +0000706
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100707 printf("Total time: %" PRIu64 " us\n", stop - start);
David Reiss0c90f6f2008-02-06 22:18:40 +0000708
Mark Sleed788b2e2006-09-07 01:26:35 +0000709 time_tot += tot;
710 if (time_min == 0 || tot < time_min) {
711 time_min = tot;
712 }
713 if (tot > time_max) {
714 time_max = tot;
715 }
716
Mark Sleea3302652006-10-25 19:03:32 +0000717 transport->close();
Mark Sleee8540632006-05-30 09:24:40 +0000718 }
719
Mark Sleee8540632006-05-30 09:24:40 +0000720 printf("\nAll tests done.\n");
Mark Sleed788b2e2006-09-07 01:26:35 +0000721
722 uint64_t time_avg = time_tot / numTests;
723
Roger Meier0e814802014-01-17 21:07:58 +0100724 printf("Min time: %" PRIu64 " us\n", time_min);
725 printf("Max time: %" PRIu64 " us\n", time_max);
726 printf("Avg time: %" PRIu64 " us\n", time_avg);
Mark Sleed788b2e2006-09-07 01:26:35 +0000727
Roger Meier4fce9602012-05-04 06:22:09 +0000728 return failCount;
Mark Sleee8540632006-05-30 09:24:40 +0000729}