blob: 8c97dc0c51526be2ad4e4151259f08f48f908d08 [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
39 #include <thrift/windows/TWinsockSingleton.h>
40#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
Marc Slemko6be374b2006-08-04 03:16:25 +000054//extern uint32_t g_socket_syscalls;
Mark Slee95771002006-06-07 06:53:25 +000055
56// Current time, microseconds since the epoch
57uint64_t now()
58{
Roger Meier5f9614c2010-11-21 16:59:05 +000059 int64_t ret;
Mark Slee95771002006-06-07 06:53:25 +000060 struct timeval tv;
David Reiss0c90f6f2008-02-06 22:18:40 +000061
Jake Farrell5d02b802014-01-07 21:42:01 -050062 THRIFT_GETTIMEOFDAY(&tv, NULL);
Mark Slee95771002006-06-07 06:53:25 +000063 ret = tv.tv_sec;
64 ret = ret*1000*1000 + tv.tv_usec;
65 return ret;
66}
67
Roger Meier7e056e72011-07-17 07:28:28 +000068static void testString_clientReturn(const char* host, int port, event_base *base, TProtocolFactory* protocolFactory, ThriftTestCobClient* client) {
Roger Meiera8cef6e2011-07-17 18:55:59 +000069 (void) host;
70 (void) port;
71 (void) protocolFactory;
Roger Meier7e056e72011-07-17 07:28:28 +000072 try {
73 string s;
74 client->recv_testString(s);
75 cout << "testString: " << s << endl;
76 } catch (TException& exn) {
Jake Farrell5d02b802014-01-07 21:42:01 -050077 cout << "Error: " << exn.what() << endl;
Roger Meier7e056e72011-07-17 07:28:28 +000078 }
79
80 event_base_loopbreak(base); // end test
81}
82
83static void testVoid_clientReturn(const char* host, int port, event_base *base, TProtocolFactory* protocolFactory, ThriftTestCobClient* client) {
84 try {
85 client->recv_testVoid();
86 cout << "testVoid" << endl;
87
88 // next test
89 delete client;
Roger Meier611f90c2011-12-11 22:08:51 +000090 boost::shared_ptr<TAsyncChannel> channel(new TEvhttpClientChannel(host, "/", host, port, base));
Roger Meier7e056e72011-07-17 07:28:28 +000091 client = new ThriftTestCobClient(channel, protocolFactory);
Roger Meier0a7c69c2014-05-02 21:15:45 +020092 client->testString(tcxx::bind(testString_clientReturn, host, port, base, protocolFactory, tcxx::placeholders::_1), "Test");
Roger Meier7e056e72011-07-17 07:28:28 +000093 } catch (TException& exn) {
Jake Farrell5d02b802014-01-07 21:42:01 -050094 cout << "Error: " << exn.what() << endl;
Roger Meier7e056e72011-07-17 07:28:28 +000095 }
96}
97
Mark Sleee8540632006-05-30 09:24:40 +000098int main(int argc, char** argv) {
cdwijayarathnaa07ec0b2014-08-09 17:45:56 +053099 string file_path = boost::filesystem::system_complete(argv[0]).string();
100 string dir_path = file_path.substr(0, file_path.size()-EXECUTABLE_FILE_NAME_LENGTH);
Jake Farrell5d02b802014-01-07 21:42:01 -0500101#if _WIN32
102 transport::TWinsockSingleton::create();
103#endif
Mark Sleee8540632006-05-30 09:24:40 +0000104 string host = "localhost";
105 int port = 9090;
106 int numTests = 1;
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000107 bool ssl = false;
Roger Meierca142b02011-06-07 17:59:07 +0000108 string transport_type = "buffered";
109 string protocol_type = "binary";
110 string domain_socket = "";
Jens Geyerf4598682014-05-08 23:18:44 +0200111 bool noinsane = false;
Mark Sleee8540632006-05-30 09:24:40 +0000112
Jake Farrell5d02b802014-01-07 21:42:01 -0500113 boost::program_options::options_description desc("Allowed options");
Roger Meierca142b02011-06-07 17:59:07 +0000114 desc.add_options()
115 ("help,h", "produce help message")
Jake Farrell5d02b802014-01-07 21:42:01 -0500116 ("host", boost::program_options::value<string>(&host)->default_value(host), "Host to connect")
117 ("port", boost::program_options::value<int>(&port)->default_value(port), "Port number to connect")
118 ("domain-socket", boost::program_options::value<string>(&domain_socket)->default_value(domain_socket), "Domain Socket (e.g. /tmp/ThriftTest.thrift), instead of host and port")
119 ("transport", boost::program_options::value<string>(&transport_type)->default_value(transport_type), "Transport: buffered, framed, http, evhttp")
Roger Meier023192f2014-02-12 09:35:12 +0100120 ("protocol", boost::program_options::value<string>(&protocol_type)->default_value(protocol_type), "Protocol: binary, compact, json")
Roger Meierca142b02011-06-07 17:59:07 +0000121 ("ssl", "Encrypted Transport using SSL")
Jake Farrell5d02b802014-01-07 21:42:01 -0500122 ("testloops,n", boost::program_options::value<int>(&numTests)->default_value(numTests), "Number of Tests")
Jens Geyerf4598682014-05-08 23:18:44 +0200123 ("noinsane", "Do not run insanity test")
Roger Meierca142b02011-06-07 17:59:07 +0000124 ;
125
Jake Farrell5d02b802014-01-07 21:42:01 -0500126 boost::program_options::variables_map vm;
127 boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), vm);
128 boost::program_options::notify(vm);
Roger Meierca142b02011-06-07 17:59:07 +0000129
130 if (vm.count("help")) {
131 cout << desc << "\n";
132 return 1;
Mark Sleee8540632006-05-30 09:24:40 +0000133 }
Mark Sleea3302652006-10-25 19:03:32 +0000134
Jake Farrell5d02b802014-01-07 21:42:01 -0500135 try {
Roger Meierca142b02011-06-07 17:59:07 +0000136 if (!protocol_type.empty()) {
137 if (protocol_type == "binary") {
Roger Meier284101c2014-03-11 21:20:35 +0100138 } else if (protocol_type == "compact") {
Roger Meierca142b02011-06-07 17:59:07 +0000139 } else if (protocol_type == "json") {
140 } else {
141 throw invalid_argument("Unknown protocol type "+protocol_type);
142 }
143 }
144
145 if (!transport_type.empty()) {
146 if (transport_type == "buffered") {
147 } else if (transport_type == "framed") {
148 } else if (transport_type == "http") {
Roger Meier7e056e72011-07-17 07:28:28 +0000149 } else if (transport_type == "evhttp") {
Roger Meierca142b02011-06-07 17:59:07 +0000150 } else {
151 throw invalid_argument("Unknown transport type "+transport_type);
152 }
153 }
154
155 } catch (std::exception& e) {
156 cerr << e.what() << endl;
157 cout << desc << "\n";
158 return 1;
159 }
160
161 if (vm.count("ssl")) {
162 ssl = true;
163 }
cdwijayarathnaa07ec0b2014-08-09 17:45:56 +0530164
Jens Geyerf4598682014-05-08 23:18:44 +0200165 if (vm.count("noinsane")) {
166 noinsane = true;
167 }
Roger Meierca142b02011-06-07 17:59:07 +0000168
Roger Meier611f90c2011-12-11 22:08:51 +0000169 boost::shared_ptr<TTransport> transport;
170 boost::shared_ptr<TProtocol> protocol;
Roger Meierca142b02011-06-07 17:59:07 +0000171
Roger Meier611f90c2011-12-11 22:08:51 +0000172 boost::shared_ptr<TSocket> socket;
173 boost::shared_ptr<TSSLSocketFactory> factory;
Roger Meierca142b02011-06-07 17:59:07 +0000174
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000175 if (ssl) {
Roger Meier611f90c2011-12-11 22:08:51 +0000176 factory = boost::shared_ptr<TSSLSocketFactory>(new TSSLSocketFactory());
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000177 factory->ciphers("ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
cdwijayarathnaa07ec0b2014-08-09 17:45:56 +0530178 factory->loadTrustedCertificates((dir_path + "../keys/CA.pem").c_str());
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000179 factory->authenticate(true);
180 socket = factory->createSocket(host, port);
181 } else {
Roger Meierca142b02011-06-07 17:59:07 +0000182 if (domain_socket != "") {
Roger Meier611f90c2011-12-11 22:08:51 +0000183 socket = boost::shared_ptr<TSocket>(new TSocket(domain_socket));
Roger Meierca142b02011-06-07 17:59:07 +0000184 port = 0;
185 }
186 else {
Roger Meier611f90c2011-12-11 22:08:51 +0000187 socket = boost::shared_ptr<TSocket>(new TSocket(host, port));
Roger Meierca142b02011-06-07 17:59:07 +0000188 }
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000189 }
Mark Sleea3302652006-10-25 19:03:32 +0000190
Roger Meierca142b02011-06-07 17:59:07 +0000191 if (transport_type.compare("http") == 0) {
Roger Meier611f90c2011-12-11 22:08:51 +0000192 boost::shared_ptr<TTransport> httpSocket(new THttpClient(socket, host, "/service"));
Roger Meierca142b02011-06-07 17:59:07 +0000193 transport = httpSocket;
194 } else if (transport_type.compare("framed") == 0){
Roger Meier611f90c2011-12-11 22:08:51 +0000195 boost::shared_ptr<TFramedTransport> framedSocket(new TFramedTransport(socket));
Mark Sleea3302652006-10-25 19:03:32 +0000196 transport = framedSocket;
Roger Meierca142b02011-06-07 17:59:07 +0000197 } else{
Roger Meier611f90c2011-12-11 22:08:51 +0000198 boost::shared_ptr<TBufferedTransport> bufferedSocket(new TBufferedTransport(socket));
Mark Sleea3302652006-10-25 19:03:32 +0000199 transport = bufferedSocket;
200 }
201
Roger Meierca142b02011-06-07 17:59:07 +0000202 if (protocol_type.compare("json") == 0) {
Roger Meier611f90c2011-12-11 22:08:51 +0000203 boost::shared_ptr<TProtocol> jsonProtocol(new TJSONProtocol(transport));
Roger Meierca142b02011-06-07 17:59:07 +0000204 protocol = jsonProtocol;
Roger Meier023192f2014-02-12 09:35:12 +0100205 } else if (protocol_type.compare("compact") == 0) {
206 boost::shared_ptr<TProtocol> compactProtocol(new TCompactProtocol(transport));
207 protocol = compactProtocol;
Roger Meierca142b02011-06-07 17:59:07 +0000208 } else{
Roger Meier611f90c2011-12-11 22:08:51 +0000209 boost::shared_ptr<TBinaryProtocol> binaryProtocol(new TBinaryProtocol(transport));
Roger Meierca142b02011-06-07 17:59:07 +0000210 protocol = binaryProtocol;
211 }
212
213 // Connection info
214 cout << "Connecting (" << transport_type << "/" << protocol_type << ") to: " << domain_socket;
215 if (port != 0) {
216 cout << host << ":" << port;
217 }
218 cout << endl;
219
Roger Meier7e056e72011-07-17 07:28:28 +0000220 if (transport_type.compare("evhttp") == 0) {
221 event_base *base = event_base_new();
222 cout << "Libevent Version: " << event_get_version() << endl;
223 cout << "Libevent Method: " << event_base_get_method(base) << endl;
224#if LIBEVENT_VERSION_NUMBER >= 0x02000000
225 cout << "Libevent Features: 0x" << hex << event_base_get_features(base) << endl;
226#endif
227
Roger Meier611f90c2011-12-11 22:08:51 +0000228 boost::shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());
Roger Meier7e056e72011-07-17 07:28:28 +0000229
Roger Meier611f90c2011-12-11 22:08:51 +0000230 boost::shared_ptr<TAsyncChannel> channel(new TEvhttpClientChannel(host.c_str(), "/", host.c_str(), port, base));
Roger Meier7e056e72011-07-17 07:28:28 +0000231 ThriftTestCobClient* client = new ThriftTestCobClient(channel, protocolFactory.get());
Roger Meier0a7c69c2014-05-02 21:15:45 +0200232 client->testVoid(tcxx::bind(testVoid_clientReturn, host.c_str(), port, base, protocolFactory.get(), tcxx::placeholders::_1));
Jake Farrell5d02b802014-01-07 21:42:01 -0500233
Roger Meier7e056e72011-07-17 07:28:28 +0000234 event_base_loop(base, 0);
235 return 0;
236 }
237
238
Roger Meierca142b02011-06-07 17:59:07 +0000239 ThriftTestClient testClient(protocol);
Mark Sleed788b2e2006-09-07 01:26:35 +0000240
241 uint64_t time_min = 0;
242 uint64_t time_max = 0;
243 uint64_t time_tot = 0;
David Reiss0c90f6f2008-02-06 22:18:40 +0000244
Roger Meier4fce9602012-05-04 06:22:09 +0000245 int failCount = 0;
Mark Sleee8540632006-05-30 09:24:40 +0000246 int test = 0;
247 for (test = 0; test < numTests; ++test) {
Mark Slee95771002006-06-07 06:53:25 +0000248
Mark Slee95771002006-06-07 06:53:25 +0000249 try {
Mark Sleea3302652006-10-25 19:03:32 +0000250 transport->open();
Mark Slee95771002006-06-07 06:53:25 +0000251 } catch (TTransportException& ttx) {
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000252 printf("Connect failed: %s\n", ttx.what());
Roger Meier5b1e3c72011-12-08 13:20:12 +0000253 return 1;
Mark Sleee8540632006-05-30 09:24:40 +0000254 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000255
Mark Sleed788b2e2006-09-07 01:26:35 +0000256 /**
257 * CONNECT TEST
258 */
259 printf("Test #%d, connect %s:%d\n", test+1, host.c_str(), port);
Mark Slee95771002006-06-07 06:53:25 +0000260
261 uint64_t start = now();
David Reiss0c90f6f2008-02-06 22:18:40 +0000262
Mark Sleee8540632006-05-30 09:24:40 +0000263 /**
264 * VOID TEST
265 */
Mark Sleee129a2d2007-02-21 05:17:48 +0000266 try {
267 printf("testVoid()");
268 testClient.testVoid();
269 printf(" = void\n");
Roger Meier4fce9602012-05-04 06:22:09 +0000270 } catch (TApplicationException& tax) {
Mark Sleee129a2d2007-02-21 05:17:48 +0000271 printf("%s\n", tax.what());
Roger Meier4fce9602012-05-04 06:22:09 +0000272 failCount++;
Mark Sleee129a2d2007-02-21 05:17:48 +0000273 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000274
Mark Sleee8540632006-05-30 09:24:40 +0000275 /**
276 * STRING TEST
277 */
278 printf("testString(\"Test\")");
Mark Slee1921d202007-01-24 19:43:06 +0000279 string s;
280 testClient.testString(s, "Test");
Mark Sleee8540632006-05-30 09:24:40 +0000281 printf(" = \"%s\"\n", s.c_str());
Roger Meier4fce9602012-05-04 06:22:09 +0000282 if (s != "Test")
283 failCount++;
David Reiss0c90f6f2008-02-06 22:18:40 +0000284
Mark Sleee8540632006-05-30 09:24:40 +0000285 /**
286 * BYTE TEST
287 */
288 printf("testByte(1)");
289 uint8_t u8 = testClient.testByte(1);
290 printf(" = %d\n", (int)u8);
Roger Meier4fce9602012-05-04 06:22:09 +0000291 if (u8 != 1)
292 failCount++;
David Reiss0c90f6f2008-02-06 22:18:40 +0000293
Mark Sleee8540632006-05-30 09:24:40 +0000294 /**
295 * I32 TEST
296 */
297 printf("testI32(-1)");
298 int32_t i32 = testClient.testI32(-1);
299 printf(" = %d\n", i32);
Roger Meier4fce9602012-05-04 06:22:09 +0000300 if (i32 != -1)
301 failCount++;
Mark Sleee8540632006-05-30 09:24:40 +0000302
303 /**
Mark Sleee8540632006-05-30 09:24:40 +0000304 * I64 TEST
305 */
306 printf("testI64(-34359738368)");
Marc Slemkobf4fd192006-08-15 21:29:39 +0000307 int64_t i64 = testClient.testI64(-34359738368LL);
Roger Meier0e814802014-01-17 21:07:58 +0100308 printf(" = %" PRId64 "\n", i64);
Roger Meier4fce9602012-05-04 06:22:09 +0000309 if (i64 != -34359738368LL)
310 failCount++;
Mark Sleec98d0502006-09-06 02:42:25 +0000311 /**
312 * DOUBLE TEST
313 */
314 printf("testDouble(-5.2098523)");
315 double dub = testClient.testDouble(-5.2098523);
Roger Meiera8cef6e2011-07-17 18:55:59 +0000316 printf(" = %f\n", dub);
Roger Meier4fce9602012-05-04 06:22:09 +0000317 if ((dub - (-5.2098523)) > 0.001)
318 failCount++;
David Reiss0c90f6f2008-02-06 22:18:40 +0000319
Mark Sleee8540632006-05-30 09:24:40 +0000320 /**
321 * STRUCT TEST
322 */
Mark Slee6e536442006-06-30 18:28:50 +0000323 printf("testStruct({\"Zero\", 1, -3, -5})");
Mark Sleee8540632006-05-30 09:24:40 +0000324 Xtruct out;
325 out.string_thing = "Zero";
326 out.byte_thing = 1;
Mark Sleee8540632006-05-30 09:24:40 +0000327 out.i32_thing = -3;
Mark Sleee8540632006-05-30 09:24:40 +0000328 out.i64_thing = -5;
Mark Slee1921d202007-01-24 19:43:06 +0000329 Xtruct in;
330 testClient.testStruct(in, out);
Roger Meier0e814802014-01-17 21:07:58 +0100331 printf(" = {\"%s\", %d, %d, %" PRId64 "}\n",
Mark Sleee8540632006-05-30 09:24:40 +0000332 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);
Roger Meier4fce9602012-05-04 06:22:09 +0000336 if (in != out)
337 failCount++;
David Reiss0c90f6f2008-02-06 22:18:40 +0000338
Mark Sleee8540632006-05-30 09:24:40 +0000339 /**
340 * NESTED STRUCT TEST
341 */
Mark Slee6e536442006-06-30 18:28:50 +0000342 printf("testNest({1, {\"Zero\", 1, -3, -5}), 5}");
Mark Sleee8540632006-05-30 09:24:40 +0000343 Xtruct2 out2;
344 out2.byte_thing = 1;
345 out2.struct_thing = out;
346 out2.i32_thing = 5;
Mark Slee1921d202007-01-24 19:43:06 +0000347 Xtruct2 in2;
348 testClient.testNest(in2, out2);
Mark Sleee8540632006-05-30 09:24:40 +0000349 in = in2.struct_thing;
Roger Meier0e814802014-01-17 21:07:58 +0100350 printf(" = {%d, {\"%s\", %d, %d, %" PRId64 "}, %d}\n",
Mark Sleee8540632006-05-30 09:24:40 +0000351 in2.byte_thing,
352 in.string_thing.c_str(),
353 (int)in.byte_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000354 in.i32_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000355 in.i64_thing,
David Reiss0c90f6f2008-02-06 22:18:40 +0000356 in2.i32_thing);
Roger Meier4fce9602012-05-04 06:22:09 +0000357 if (in2 != out2)
358 failCount++;
Mark Sleee8540632006-05-30 09:24:40 +0000359
360 /**
361 * MAP TEST
362 */
363 map<int32_t,int32_t> mapout;
364 for (int32_t i = 0; i < 5; ++i) {
365 mapout.insert(make_pair(i, i-10));
366 }
367 printf("testMap({");
368 map<int32_t, int32_t>::const_iterator m_iter;
369 bool first = true;
370 for (m_iter = mapout.begin(); m_iter != mapout.end(); ++m_iter) {
371 if (first) {
372 first = false;
373 } else {
374 printf(", ");
375 }
376 printf("%d => %d", m_iter->first, m_iter->second);
377 }
378 printf("})");
Mark Slee1921d202007-01-24 19:43:06 +0000379 map<int32_t,int32_t> mapin;
380 testClient.testMap(mapin, mapout);
Mark Sleee8540632006-05-30 09:24:40 +0000381 printf(" = {");
382 first = true;
383 for (m_iter = mapin.begin(); m_iter != mapin.end(); ++m_iter) {
384 if (first) {
385 first = false;
386 } else {
387 printf(", ");
388 }
389 printf("%d => %d", m_iter->first, m_iter->second);
390 }
391 printf("}\n");
Roger Meier4fce9602012-05-04 06:22:09 +0000392 if (mapin != mapout)
393 failCount++;
394
395 /**
396 * STRING MAP TEST
397 * missing
398 */
Mark Sleee8540632006-05-30 09:24:40 +0000399
400 /**
401 * SET TEST
402 */
403 set<int32_t> setout;
404 for (int32_t i = -2; i < 3; ++i) {
405 setout.insert(i);
406 }
407 printf("testSet({");
408 set<int32_t>::const_iterator s_iter;
409 first = true;
410 for (s_iter = setout.begin(); s_iter != setout.end(); ++s_iter) {
411 if (first) {
412 first = false;
413 } else {
414 printf(", ");
415 }
416 printf("%d", *s_iter);
417 }
418 printf("})");
Mark Slee1921d202007-01-24 19:43:06 +0000419 set<int32_t> setin;
420 testClient.testSet(setin, setout);
Mark Sleee8540632006-05-30 09:24:40 +0000421 printf(" = {");
422 first = true;
423 for (s_iter = setin.begin(); s_iter != setin.end(); ++s_iter) {
424 if (first) {
425 first = false;
426 } else {
427 printf(", ");
428 }
429 printf("%d", *s_iter);
430 }
431 printf("}\n");
Roger Meier4fce9602012-05-04 06:22:09 +0000432 if (setin != setout)
433 failCount++;
Mark Sleee8540632006-05-30 09:24:40 +0000434
435 /**
436 * LIST TEST
437 */
Mark Sleeb9acf982006-10-10 01:57:32 +0000438 vector<int32_t> listout;
Mark Sleee8540632006-05-30 09:24:40 +0000439 for (int32_t i = -2; i < 3; ++i) {
440 listout.push_back(i);
441 }
442 printf("testList({");
Mark Sleeb9acf982006-10-10 01:57:32 +0000443 vector<int32_t>::const_iterator l_iter;
Mark Sleee8540632006-05-30 09:24:40 +0000444 first = true;
445 for (l_iter = listout.begin(); l_iter != listout.end(); ++l_iter) {
446 if (first) {
447 first = false;
448 } else {
449 printf(", ");
450 }
451 printf("%d", *l_iter);
452 }
453 printf("})");
Mark Slee1921d202007-01-24 19:43:06 +0000454 vector<int32_t> listin;
455 testClient.testList(listin, listout);
Mark Sleee8540632006-05-30 09:24:40 +0000456 printf(" = {");
457 first = true;
458 for (l_iter = listin.begin(); l_iter != listin.end(); ++l_iter) {
459 if (first) {
460 first = false;
461 } else {
462 printf(", ");
463 }
464 printf("%d", *l_iter);
465 }
466 printf("}\n");
Roger Meier4fce9602012-05-04 06:22:09 +0000467 if (listin != listout)
468 failCount++;
Mark Sleee8540632006-05-30 09:24:40 +0000469
470 /**
471 * ENUM TEST
472 */
473 printf("testEnum(ONE)");
Bryan Duxbury833ae492010-09-27 17:26:02 +0000474 Numberz::type ret = testClient.testEnum(Numberz::ONE);
Mark Sleee8540632006-05-30 09:24:40 +0000475 printf(" = %d\n", ret);
Roger Meier4fce9602012-05-04 06:22:09 +0000476 if (ret != Numberz::ONE)
477 failCount++;
Mark Sleee8540632006-05-30 09:24:40 +0000478
479 printf("testEnum(TWO)");
Bryan Duxbury833ae492010-09-27 17:26:02 +0000480 ret = testClient.testEnum(Numberz::TWO);
Mark Sleee8540632006-05-30 09:24:40 +0000481 printf(" = %d\n", ret);
Roger Meier4fce9602012-05-04 06:22:09 +0000482 if (ret != Numberz::TWO)
483 failCount++;
Mark Sleee8540632006-05-30 09:24:40 +0000484
485 printf("testEnum(THREE)");
Bryan Duxbury833ae492010-09-27 17:26:02 +0000486 ret = testClient.testEnum(Numberz::THREE);
Mark Sleee8540632006-05-30 09:24:40 +0000487 printf(" = %d\n", ret);
Roger Meier4fce9602012-05-04 06:22:09 +0000488 if (ret != Numberz::THREE)
489 failCount++;
Mark Sleee8540632006-05-30 09:24:40 +0000490
491 printf("testEnum(FIVE)");
Bryan Duxbury833ae492010-09-27 17:26:02 +0000492 ret = testClient.testEnum(Numberz::FIVE);
Mark Sleee8540632006-05-30 09:24:40 +0000493 printf(" = %d\n", ret);
Roger Meier4fce9602012-05-04 06:22:09 +0000494 if (ret != Numberz::FIVE)
495 failCount++;
Mark Sleee8540632006-05-30 09:24:40 +0000496
497 printf("testEnum(EIGHT)");
Bryan Duxbury833ae492010-09-27 17:26:02 +0000498 ret = testClient.testEnum(Numberz::EIGHT);
Mark Sleee8540632006-05-30 09:24:40 +0000499 printf(" = %d\n", ret);
Roger Meier4fce9602012-05-04 06:22:09 +0000500 if (ret != Numberz::EIGHT)
501 failCount++;
Mark Sleee8540632006-05-30 09:24:40 +0000502
503 /**
504 * TYPEDEF TEST
505 */
506 printf("testTypedef(309858235082523)");
Marc Slemkobf4fd192006-08-15 21:29:39 +0000507 UserId uid = testClient.testTypedef(309858235082523LL);
Roger Meier0e814802014-01-17 21:07:58 +0100508 printf(" = %" PRId64 "\n", uid);
Roger Meier4fce9602012-05-04 06:22:09 +0000509 if (uid != 309858235082523LL)
510 failCount++;
Mark Sleee8540632006-05-30 09:24:40 +0000511
512 /**
513 * NESTED MAP TEST
514 */
515 printf("testMapMap(1)");
Mark Slee1921d202007-01-24 19:43:06 +0000516 map<int32_t, map<int32_t, int32_t> > mm;
517 testClient.testMapMap(mm, 1);
Mark Sleee8540632006-05-30 09:24:40 +0000518 printf(" = {");
519 map<int32_t, map<int32_t, int32_t> >::const_iterator mi;
520 for (mi = mm.begin(); mi != mm.end(); ++mi) {
521 printf("%d => {", mi->first);
522 map<int32_t, int32_t>::const_iterator mi2;
523 for (mi2 = mi->second.begin(); mi2 != mi->second.end(); ++mi2) {
524 printf("%d => %d, ", mi2->first, mi2->second);
525 }
526 printf("}, ");
527 }
528 printf("}\n");
529
530 /**
531 * INSANITY TEST
532 */
Jens Geyerf4598682014-05-08 23:18:44 +0200533 if (!noinsane) {
534 Insanity insane;
535 insane.userMap.insert(make_pair(Numberz::FIVE, 5000));
536 Xtruct truck;
537 truck.string_thing = "Truck";
538 truck.byte_thing = 8;
539 truck.i32_thing = 8;
540 truck.i64_thing = 8;
541 insane.xtructs.push_back(truck);
542 printf("testInsanity()");
543 map<UserId, map<Numberz::type,Insanity> > whoa;
544 testClient.testInsanity(whoa, insane);
545 printf(" = {");
546 map<UserId, map<Numberz::type,Insanity> >::const_iterator i_iter;
547 for (i_iter = whoa.begin(); i_iter != whoa.end(); ++i_iter) {
548 printf("%" PRId64 " => {", i_iter->first);
549 map<Numberz::type,Insanity>::const_iterator i2_iter;
550 for (i2_iter = i_iter->second.begin();
551 i2_iter != i_iter->second.end();
552 ++i2_iter) {
553 printf("%d => {", i2_iter->first);
554 map<Numberz::type, UserId> userMap = i2_iter->second.userMap;
555 map<Numberz::type, UserId>::const_iterator um;
556 printf("{");
557 for (um = userMap.begin(); um != userMap.end(); ++um) {
558 printf("%d => %" PRId64 ", ", um->first, um->second);
559 }
560 printf("}, ");
Mark Sleee8540632006-05-30 09:24:40 +0000561
Jens Geyerf4598682014-05-08 23:18:44 +0200562 vector<Xtruct> xtructs = i2_iter->second.xtructs;
563 vector<Xtruct>::const_iterator x;
564 printf("{");
565 for (x = xtructs.begin(); x != xtructs.end(); ++x) {
566 printf("{\"%s\", %d, %d, %" PRId64 "}, ",
567 x->string_thing.c_str(),
568 (int)x->byte_thing,
569 x->i32_thing,
570 x->i64_thing);
571 }
572 printf("}");
Mark Sleee8540632006-05-30 09:24:40 +0000573
Jens Geyerf4598682014-05-08 23:18:44 +0200574 printf("}, ");
575 }
Mark Sleee8540632006-05-30 09:24:40 +0000576 printf("}, ");
577 }
Jens Geyerf4598682014-05-08 23:18:44 +0200578 printf("}\n");
Mark Sleee8540632006-05-30 09:24:40 +0000579 }
Marc Slemko71d4e472006-08-15 22:34:04 +0000580 /* test exception */
Mark Slee95771002006-06-07 06:53:25 +0000581
Marc Slemkobf4fd192006-08-15 21:29:39 +0000582 try {
Marc Slemko71d4e472006-08-15 22:34:04 +0000583 printf("testClient.testException(\"Xception\") =>");
584 testClient.testException("Xception");
585 printf(" void\nFAILURE\n");
Roger Meier4fce9602012-05-04 06:22:09 +0000586 failCount++;
David Reiss0c90f6f2008-02-06 22:18:40 +0000587
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000588 } catch(Xception& e) {
Marc Slemko71d4e472006-08-15 22:34:04 +0000589 printf(" {%u, \"%s\"}\n", e.errorCode, e.message.c_str());
Marc Slemkobf4fd192006-08-15 21:29:39 +0000590 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000591
Marc Slemkobf4fd192006-08-15 21:29:39 +0000592 try {
Roger Meier99b36722012-05-03 21:21:43 +0000593 printf("testClient.testException(\"TException\") =>");
594 testClient.testException("TException");
Roger Meierf50df7f2012-05-02 22:49:55 +0000595 printf(" void\nFAILURE\n");
Roger Meier4fce9602012-05-04 06:22:09 +0000596 failCount++;
Roger Meierf50df7f2012-05-02 22:49:55 +0000597
Jake Farrell5d02b802014-01-07 21:42:01 -0500598 } catch(const TException&) {
Roger Meierf50df7f2012-05-02 22:49:55 +0000599 printf(" Caught TException\n");
600 }
601
602 try {
Marc Slemko71d4e472006-08-15 22:34:04 +0000603 printf("testClient.testException(\"success\") =>");
604 testClient.testException("success");
605 printf(" void\n");
606 } catch(...) {
607 printf(" exception\nFAILURE\n");
Roger Meier4fce9602012-05-04 06:22:09 +0000608 failCount++;
Marc Slemko71d4e472006-08-15 22:34:04 +0000609 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000610
Marc Slemko71d4e472006-08-15 22:34:04 +0000611 /* test multi exception */
David Reiss0c90f6f2008-02-06 22:18:40 +0000612
Marc Slemko71d4e472006-08-15 22:34:04 +0000613 try {
614 printf("testClient.testMultiException(\"Xception\", \"test 1\") =>");
Mark Slee1921d202007-01-24 19:43:06 +0000615 Xtruct result;
616 testClient.testMultiException(result, "Xception", "test 1");
Marc Slemko71d4e472006-08-15 22:34:04 +0000617 printf(" result\nFAILURE\n");
Roger Meier4fce9602012-05-04 06:22:09 +0000618 failCount++;
Mark Sleed3d733a2006-09-01 22:19:06 +0000619 } catch(Xception& e) {
Marc Slemko71d4e472006-08-15 22:34:04 +0000620 printf(" {%u, \"%s\"}\n", e.errorCode, e.message.c_str());
621 }
622
623 try {
624 printf("testClient.testMultiException(\"Xception2\", \"test 2\") =>");
Mark Slee1921d202007-01-24 19:43:06 +0000625 Xtruct result;
626 testClient.testMultiException(result, "Xception2", "test 2");
Marc Slemko71d4e472006-08-15 22:34:04 +0000627 printf(" result\nFAILURE\n");
Roger Meier4fce9602012-05-04 06:22:09 +0000628 failCount++;
David Reiss0c90f6f2008-02-06 22:18:40 +0000629
Mark Sleed3d733a2006-09-01 22:19:06 +0000630 } catch(Xception2& e) {
Marc Slemko71d4e472006-08-15 22:34:04 +0000631 printf(" {%u, {\"%s\"}}\n", e.errorCode, e.struct_thing.string_thing.c_str());
Marc Slemkobf4fd192006-08-15 21:29:39 +0000632 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000633
Marc Slemko71d4e472006-08-15 22:34:04 +0000634 try {
635 printf("testClient.testMultiException(\"success\", \"test 3\") =>");
Mark Slee1921d202007-01-24 19:43:06 +0000636 Xtruct result;
637 testClient.testMultiException(result, "success", "test 3");
Marc Slemko71d4e472006-08-15 22:34:04 +0000638 printf(" {{\"%s\"}}\n", result.string_thing.c_str());
639 } catch(...) {
640 printf(" exception\nFAILURE\n");
Roger Meier4fce9602012-05-04 06:22:09 +0000641 failCount++;
Marc Slemko71d4e472006-08-15 22:34:04 +0000642 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000643
David Reissc51986f2009-03-24 20:01:25 +0000644 /* test oneway void */
David Reiss2ab6fe82008-02-18 02:11:44 +0000645 {
Jake Farrell5d02b802014-01-07 21:42:01 -0500646 printf("testClient.testOneway(1) =>");
David Reiss6ce401d2009-03-24 20:01:58 +0000647 uint64_t startOneway = now();
Jake Farrell5d02b802014-01-07 21:42:01 -0500648 testClient.testOneway(1);
David Reiss6ce401d2009-03-24 20:01:58 +0000649 uint64_t elapsed = now() - startOneway;
David Reiss2ab6fe82008-02-18 02:11:44 +0000650 if (elapsed > 200 * 1000) { // 0.2 seconds
651 printf(" FAILURE - took %.2f ms\n", (double)elapsed/1000.0);
Roger Meier4fce9602012-05-04 06:22:09 +0000652 failCount++;
David Reiss2ab6fe82008-02-18 02:11:44 +0000653 } else {
654 printf(" success - took %.2f ms\n", (double)elapsed/1000.0);
655 }
656 }
657
David Reiss2845b522008-02-18 02:11:52 +0000658 /**
David Reissc51986f2009-03-24 20:01:25 +0000659 * redo a simple test after the oneway to make sure we aren't "off by one" --
660 * if the server treated oneway void like normal void, this next test will
David Reiss2845b522008-02-18 02:11:52 +0000661 * fail since it will get the void confirmation rather than the correct
662 * result. In this circumstance, the client will throw the exception:
663 *
664 * TApplicationException: Wrong method namea
665 */
666 /**
667 * I32 TEST
668 */
669 printf("re-test testI32(-1)");
670 i32 = testClient.testI32(-1);
671 printf(" = %d\n", i32);
Roger Meier4fce9602012-05-04 06:22:09 +0000672 if (i32 != -1)
673 failCount++;
David Reiss2845b522008-02-18 02:11:52 +0000674
675
Marc Slemkobf4fd192006-08-15 21:29:39 +0000676 uint64_t stop = now();
Mark Sleed788b2e2006-09-07 01:26:35 +0000677 uint64_t tot = stop-start;
678
Roger Meier0e814802014-01-17 21:07:58 +0100679 printf("Total time: %" PRIu64 " us\n", stop-start);
David Reiss0c90f6f2008-02-06 22:18:40 +0000680
Mark Sleed788b2e2006-09-07 01:26:35 +0000681 time_tot += tot;
682 if (time_min == 0 || tot < time_min) {
683 time_min = tot;
684 }
685 if (tot > time_max) {
686 time_max = tot;
687 }
688
Mark Sleea3302652006-10-25 19:03:32 +0000689 transport->close();
Mark Sleee8540632006-05-30 09:24:40 +0000690 }
691
Marc Slemko6be374b2006-08-04 03:16:25 +0000692 // printf("\nSocket syscalls: %u", g_socket_syscalls);
Mark Sleee8540632006-05-30 09:24:40 +0000693 printf("\nAll tests done.\n");
Mark Sleed788b2e2006-09-07 01:26:35 +0000694
695 uint64_t time_avg = time_tot / numTests;
696
Roger Meier0e814802014-01-17 21:07:58 +0100697 printf("Min time: %" PRIu64 " us\n", time_min);
698 printf("Max time: %" PRIu64 " us\n", time_max);
699 printf("Avg time: %" PRIu64 " us\n", time_avg);
Mark Sleed788b2e2006-09-07 01:26:35 +0000700
Roger Meier4fce9602012-05-04 06:22:09 +0000701 return failCount;
Mark Sleee8540632006-05-30 09:24:40 +0000702}