blob: 7a783d221d887db007d646e65c3abfac16dadcc4 [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>
Jake Farrell5d02b802014-01-07 21:42:01 -050036#include <thrift/cxxfunctional.h>
37#if _WIN32
38 #include <thrift/windows/TWinsockSingleton.h>
39#endif
Roger Meierca142b02011-06-07 17:59:07 +000040
Marc Slemko6be374b2006-08-04 03:16:25 +000041#include "ThriftTest.h"
42
Marc Slemko6be374b2006-08-04 03:16:25 +000043using namespace std;
T Jake Lucianib5e62212009-01-31 22:36:20 +000044using namespace apache::thrift;
45using namespace apache::thrift::protocol;
46using namespace apache::thrift::transport;
Marc Slemkobf4fd192006-08-15 21:29:39 +000047using namespace thrift::test;
Roger Meier7e056e72011-07-17 07:28:28 +000048using namespace apache::thrift::async;
49
Marc Slemko6be374b2006-08-04 03:16:25 +000050//extern uint32_t g_socket_syscalls;
Mark Slee95771002006-06-07 06:53:25 +000051
52// Current time, microseconds since the epoch
53uint64_t now()
54{
Roger Meier5f9614c2010-11-21 16:59:05 +000055 int64_t ret;
Mark Slee95771002006-06-07 06:53:25 +000056 struct timeval tv;
David Reiss0c90f6f2008-02-06 22:18:40 +000057
Jake Farrell5d02b802014-01-07 21:42:01 -050058 THRIFT_GETTIMEOFDAY(&tv, NULL);
Mark Slee95771002006-06-07 06:53:25 +000059 ret = tv.tv_sec;
60 ret = ret*1000*1000 + tv.tv_usec;
61 return ret;
62}
63
Roger Meier7e056e72011-07-17 07:28:28 +000064static void testString_clientReturn(const char* host, int port, event_base *base, TProtocolFactory* protocolFactory, ThriftTestCobClient* client) {
Roger Meiera8cef6e2011-07-17 18:55:59 +000065 (void) host;
66 (void) port;
67 (void) protocolFactory;
Roger Meier7e056e72011-07-17 07:28:28 +000068 try {
69 string s;
70 client->recv_testString(s);
71 cout << "testString: " << s << endl;
72 } catch (TException& exn) {
Jake Farrell5d02b802014-01-07 21:42:01 -050073 cout << "Error: " << exn.what() << endl;
Roger Meier7e056e72011-07-17 07:28:28 +000074 }
75
76 event_base_loopbreak(base); // end test
77}
78
79static void testVoid_clientReturn(const char* host, int port, event_base *base, TProtocolFactory* protocolFactory, ThriftTestCobClient* client) {
80 try {
81 client->recv_testVoid();
82 cout << "testVoid" << endl;
83
84 // next test
85 delete client;
Roger Meier611f90c2011-12-11 22:08:51 +000086 boost::shared_ptr<TAsyncChannel> channel(new TEvhttpClientChannel(host, "/", host, port, base));
Roger Meier7e056e72011-07-17 07:28:28 +000087 client = new ThriftTestCobClient(channel, protocolFactory);
Roger Meier0a7c69c2014-05-02 21:15:45 +020088 client->testString(tcxx::bind(testString_clientReturn, host, port, base, protocolFactory, tcxx::placeholders::_1), "Test");
Roger Meier7e056e72011-07-17 07:28:28 +000089 } catch (TException& exn) {
Jake Farrell5d02b802014-01-07 21:42:01 -050090 cout << "Error: " << exn.what() << endl;
Roger Meier7e056e72011-07-17 07:28:28 +000091 }
92}
93
Mark Sleee8540632006-05-30 09:24:40 +000094int main(int argc, char** argv) {
Jake Farrell5d02b802014-01-07 21:42:01 -050095#if _WIN32
96 transport::TWinsockSingleton::create();
97#endif
Mark Sleee8540632006-05-30 09:24:40 +000098 string host = "localhost";
99 int port = 9090;
100 int numTests = 1;
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000101 bool ssl = false;
Roger Meierca142b02011-06-07 17:59:07 +0000102 string transport_type = "buffered";
103 string protocol_type = "binary";
104 string domain_socket = "";
Jens Geyerf4598682014-05-08 23:18:44 +0200105 bool noinsane = false;
Mark Sleee8540632006-05-30 09:24:40 +0000106
Jake Farrell5d02b802014-01-07 21:42:01 -0500107 boost::program_options::options_description desc("Allowed options");
Roger Meierca142b02011-06-07 17:59:07 +0000108 desc.add_options()
109 ("help,h", "produce help message")
Jake Farrell5d02b802014-01-07 21:42:01 -0500110 ("host", boost::program_options::value<string>(&host)->default_value(host), "Host to connect")
111 ("port", boost::program_options::value<int>(&port)->default_value(port), "Port number to connect")
112 ("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")
113 ("transport", boost::program_options::value<string>(&transport_type)->default_value(transport_type), "Transport: buffered, framed, http, evhttp")
Roger Meier023192f2014-02-12 09:35:12 +0100114 ("protocol", boost::program_options::value<string>(&protocol_type)->default_value(protocol_type), "Protocol: binary, compact, json")
Roger Meierca142b02011-06-07 17:59:07 +0000115 ("ssl", "Encrypted Transport using SSL")
Jake Farrell5d02b802014-01-07 21:42:01 -0500116 ("testloops,n", boost::program_options::value<int>(&numTests)->default_value(numTests), "Number of Tests")
Jens Geyerf4598682014-05-08 23:18:44 +0200117 ("noinsane", "Do not run insanity test")
Roger Meierca142b02011-06-07 17:59:07 +0000118 ;
119
Jake Farrell5d02b802014-01-07 21:42:01 -0500120 boost::program_options::variables_map vm;
121 boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), vm);
122 boost::program_options::notify(vm);
Roger Meierca142b02011-06-07 17:59:07 +0000123
124 if (vm.count("help")) {
125 cout << desc << "\n";
126 return 1;
Mark Sleee8540632006-05-30 09:24:40 +0000127 }
Mark Sleea3302652006-10-25 19:03:32 +0000128
Jake Farrell5d02b802014-01-07 21:42:01 -0500129 try {
Roger Meierca142b02011-06-07 17:59:07 +0000130 if (!protocol_type.empty()) {
131 if (protocol_type == "binary") {
Roger Meier284101c2014-03-11 21:20:35 +0100132 } else if (protocol_type == "compact") {
Roger Meierca142b02011-06-07 17:59:07 +0000133 } else if (protocol_type == "json") {
134 } else {
135 throw invalid_argument("Unknown protocol type "+protocol_type);
136 }
137 }
138
139 if (!transport_type.empty()) {
140 if (transport_type == "buffered") {
141 } else if (transport_type == "framed") {
142 } else if (transport_type == "http") {
Roger Meier7e056e72011-07-17 07:28:28 +0000143 } else if (transport_type == "evhttp") {
Roger Meierca142b02011-06-07 17:59:07 +0000144 } else {
145 throw invalid_argument("Unknown transport type "+transport_type);
146 }
147 }
148
149 } catch (std::exception& e) {
150 cerr << e.what() << endl;
151 cout << desc << "\n";
152 return 1;
153 }
154
155 if (vm.count("ssl")) {
156 ssl = true;
157 }
Jens Geyerf4598682014-05-08 23:18:44 +0200158
159 if (vm.count("noinsane")) {
160 noinsane = true;
161 }
Roger Meierca142b02011-06-07 17:59:07 +0000162
Roger Meier611f90c2011-12-11 22:08:51 +0000163 boost::shared_ptr<TTransport> transport;
164 boost::shared_ptr<TProtocol> protocol;
Roger Meierca142b02011-06-07 17:59:07 +0000165
Roger Meier611f90c2011-12-11 22:08:51 +0000166 boost::shared_ptr<TSocket> socket;
167 boost::shared_ptr<TSSLSocketFactory> factory;
Roger Meierca142b02011-06-07 17:59:07 +0000168
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000169 if (ssl) {
Roger Meier611f90c2011-12-11 22:08:51 +0000170 factory = boost::shared_ptr<TSSLSocketFactory>(new TSSLSocketFactory());
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000171 factory->ciphers("ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
Roger Meierc94b2932014-02-22 20:07:33 +0100172 factory->loadTrustedCertificates("keys/CA.pem");
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000173 factory->authenticate(true);
174 socket = factory->createSocket(host, port);
175 } else {
Roger Meierca142b02011-06-07 17:59:07 +0000176 if (domain_socket != "") {
Roger Meier611f90c2011-12-11 22:08:51 +0000177 socket = boost::shared_ptr<TSocket>(new TSocket(domain_socket));
Roger Meierca142b02011-06-07 17:59:07 +0000178 port = 0;
179 }
180 else {
Roger Meier611f90c2011-12-11 22:08:51 +0000181 socket = boost::shared_ptr<TSocket>(new TSocket(host, port));
Roger Meierca142b02011-06-07 17:59:07 +0000182 }
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000183 }
Mark Sleea3302652006-10-25 19:03:32 +0000184
Roger Meierca142b02011-06-07 17:59:07 +0000185 if (transport_type.compare("http") == 0) {
Roger Meier611f90c2011-12-11 22:08:51 +0000186 boost::shared_ptr<TTransport> httpSocket(new THttpClient(socket, host, "/service"));
Roger Meierca142b02011-06-07 17:59:07 +0000187 transport = httpSocket;
188 } else if (transport_type.compare("framed") == 0){
Roger Meier611f90c2011-12-11 22:08:51 +0000189 boost::shared_ptr<TFramedTransport> framedSocket(new TFramedTransport(socket));
Mark Sleea3302652006-10-25 19:03:32 +0000190 transport = framedSocket;
Roger Meierca142b02011-06-07 17:59:07 +0000191 } else{
Roger Meier611f90c2011-12-11 22:08:51 +0000192 boost::shared_ptr<TBufferedTransport> bufferedSocket(new TBufferedTransport(socket));
Mark Sleea3302652006-10-25 19:03:32 +0000193 transport = bufferedSocket;
194 }
195
Roger Meierca142b02011-06-07 17:59:07 +0000196 if (protocol_type.compare("json") == 0) {
Roger Meier611f90c2011-12-11 22:08:51 +0000197 boost::shared_ptr<TProtocol> jsonProtocol(new TJSONProtocol(transport));
Roger Meierca142b02011-06-07 17:59:07 +0000198 protocol = jsonProtocol;
Roger Meier023192f2014-02-12 09:35:12 +0100199 } else if (protocol_type.compare("compact") == 0) {
200 boost::shared_ptr<TProtocol> compactProtocol(new TCompactProtocol(transport));
201 protocol = compactProtocol;
Roger Meierca142b02011-06-07 17:59:07 +0000202 } else{
Roger Meier611f90c2011-12-11 22:08:51 +0000203 boost::shared_ptr<TBinaryProtocol> binaryProtocol(new TBinaryProtocol(transport));
Roger Meierca142b02011-06-07 17:59:07 +0000204 protocol = binaryProtocol;
205 }
206
207 // Connection info
208 cout << "Connecting (" << transport_type << "/" << protocol_type << ") to: " << domain_socket;
209 if (port != 0) {
210 cout << host << ":" << port;
211 }
212 cout << endl;
213
Roger Meier7e056e72011-07-17 07:28:28 +0000214 if (transport_type.compare("evhttp") == 0) {
215 event_base *base = event_base_new();
216 cout << "Libevent Version: " << event_get_version() << endl;
217 cout << "Libevent Method: " << event_base_get_method(base) << endl;
218#if LIBEVENT_VERSION_NUMBER >= 0x02000000
219 cout << "Libevent Features: 0x" << hex << event_base_get_features(base) << endl;
220#endif
221
Roger Meier611f90c2011-12-11 22:08:51 +0000222 boost::shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());
Roger Meier7e056e72011-07-17 07:28:28 +0000223
Roger Meier611f90c2011-12-11 22:08:51 +0000224 boost::shared_ptr<TAsyncChannel> channel(new TEvhttpClientChannel(host.c_str(), "/", host.c_str(), port, base));
Roger Meier7e056e72011-07-17 07:28:28 +0000225 ThriftTestCobClient* client = new ThriftTestCobClient(channel, protocolFactory.get());
Roger Meier0a7c69c2014-05-02 21:15:45 +0200226 client->testVoid(tcxx::bind(testVoid_clientReturn, host.c_str(), port, base, protocolFactory.get(), tcxx::placeholders::_1));
Jake Farrell5d02b802014-01-07 21:42:01 -0500227
Roger Meier7e056e72011-07-17 07:28:28 +0000228 event_base_loop(base, 0);
229 return 0;
230 }
231
232
Roger Meierca142b02011-06-07 17:59:07 +0000233 ThriftTestClient testClient(protocol);
Mark Sleed788b2e2006-09-07 01:26:35 +0000234
235 uint64_t time_min = 0;
236 uint64_t time_max = 0;
237 uint64_t time_tot = 0;
David Reiss0c90f6f2008-02-06 22:18:40 +0000238
Roger Meier4fce9602012-05-04 06:22:09 +0000239 int failCount = 0;
Mark Sleee8540632006-05-30 09:24:40 +0000240 int test = 0;
241 for (test = 0; test < numTests; ++test) {
Mark Slee95771002006-06-07 06:53:25 +0000242
Mark Slee95771002006-06-07 06:53:25 +0000243 try {
Mark Sleea3302652006-10-25 19:03:32 +0000244 transport->open();
Mark Slee95771002006-06-07 06:53:25 +0000245 } catch (TTransportException& ttx) {
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000246 printf("Connect failed: %s\n", ttx.what());
Roger Meier5b1e3c72011-12-08 13:20:12 +0000247 return 1;
Mark Sleee8540632006-05-30 09:24:40 +0000248 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000249
Mark Sleed788b2e2006-09-07 01:26:35 +0000250 /**
251 * CONNECT TEST
252 */
253 printf("Test #%d, connect %s:%d\n", test+1, host.c_str(), port);
Mark Slee95771002006-06-07 06:53:25 +0000254
255 uint64_t start = now();
David Reiss0c90f6f2008-02-06 22:18:40 +0000256
Mark Sleee8540632006-05-30 09:24:40 +0000257 /**
258 * VOID TEST
259 */
Mark Sleee129a2d2007-02-21 05:17:48 +0000260 try {
261 printf("testVoid()");
262 testClient.testVoid();
263 printf(" = void\n");
Roger Meier4fce9602012-05-04 06:22:09 +0000264 } catch (TApplicationException& tax) {
Mark Sleee129a2d2007-02-21 05:17:48 +0000265 printf("%s\n", tax.what());
Roger Meier4fce9602012-05-04 06:22:09 +0000266 failCount++;
Mark Sleee129a2d2007-02-21 05:17:48 +0000267 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000268
Mark Sleee8540632006-05-30 09:24:40 +0000269 /**
270 * STRING TEST
271 */
272 printf("testString(\"Test\")");
Mark Slee1921d202007-01-24 19:43:06 +0000273 string s;
274 testClient.testString(s, "Test");
Mark Sleee8540632006-05-30 09:24:40 +0000275 printf(" = \"%s\"\n", s.c_str());
Roger Meier4fce9602012-05-04 06:22:09 +0000276 if (s != "Test")
277 failCount++;
David Reiss0c90f6f2008-02-06 22:18:40 +0000278
Mark Sleee8540632006-05-30 09:24:40 +0000279 /**
280 * BYTE TEST
281 */
282 printf("testByte(1)");
283 uint8_t u8 = testClient.testByte(1);
284 printf(" = %d\n", (int)u8);
Roger Meier4fce9602012-05-04 06:22:09 +0000285 if (u8 != 1)
286 failCount++;
David Reiss0c90f6f2008-02-06 22:18:40 +0000287
Mark Sleee8540632006-05-30 09:24:40 +0000288 /**
289 * I32 TEST
290 */
291 printf("testI32(-1)");
292 int32_t i32 = testClient.testI32(-1);
293 printf(" = %d\n", i32);
Roger Meier4fce9602012-05-04 06:22:09 +0000294 if (i32 != -1)
295 failCount++;
Mark Sleee8540632006-05-30 09:24:40 +0000296
297 /**
Mark Sleee8540632006-05-30 09:24:40 +0000298 * I64 TEST
299 */
300 printf("testI64(-34359738368)");
Marc Slemkobf4fd192006-08-15 21:29:39 +0000301 int64_t i64 = testClient.testI64(-34359738368LL);
Roger Meier0e814802014-01-17 21:07:58 +0100302 printf(" = %" PRId64 "\n", i64);
Roger Meier4fce9602012-05-04 06:22:09 +0000303 if (i64 != -34359738368LL)
304 failCount++;
Mark Sleec98d0502006-09-06 02:42:25 +0000305 /**
306 * DOUBLE TEST
307 */
308 printf("testDouble(-5.2098523)");
309 double dub = testClient.testDouble(-5.2098523);
Roger Meiera8cef6e2011-07-17 18:55:59 +0000310 printf(" = %f\n", dub);
Roger Meier4fce9602012-05-04 06:22:09 +0000311 if ((dub - (-5.2098523)) > 0.001)
312 failCount++;
David Reiss0c90f6f2008-02-06 22:18:40 +0000313
Mark Sleee8540632006-05-30 09:24:40 +0000314 /**
315 * STRUCT TEST
316 */
Mark Slee6e536442006-06-30 18:28:50 +0000317 printf("testStruct({\"Zero\", 1, -3, -5})");
Mark Sleee8540632006-05-30 09:24:40 +0000318 Xtruct out;
319 out.string_thing = "Zero";
320 out.byte_thing = 1;
Mark Sleee8540632006-05-30 09:24:40 +0000321 out.i32_thing = -3;
Mark Sleee8540632006-05-30 09:24:40 +0000322 out.i64_thing = -5;
Mark Slee1921d202007-01-24 19:43:06 +0000323 Xtruct in;
324 testClient.testStruct(in, out);
Roger Meier0e814802014-01-17 21:07:58 +0100325 printf(" = {\"%s\", %d, %d, %" PRId64 "}\n",
Mark Sleee8540632006-05-30 09:24:40 +0000326 in.string_thing.c_str(),
327 (int)in.byte_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000328 in.i32_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000329 in.i64_thing);
Roger Meier4fce9602012-05-04 06:22:09 +0000330 if (in != out)
331 failCount++;
David Reiss0c90f6f2008-02-06 22:18:40 +0000332
Mark Sleee8540632006-05-30 09:24:40 +0000333 /**
334 * NESTED STRUCT TEST
335 */
Mark Slee6e536442006-06-30 18:28:50 +0000336 printf("testNest({1, {\"Zero\", 1, -3, -5}), 5}");
Mark Sleee8540632006-05-30 09:24:40 +0000337 Xtruct2 out2;
338 out2.byte_thing = 1;
339 out2.struct_thing = out;
340 out2.i32_thing = 5;
Mark Slee1921d202007-01-24 19:43:06 +0000341 Xtruct2 in2;
342 testClient.testNest(in2, out2);
Mark Sleee8540632006-05-30 09:24:40 +0000343 in = in2.struct_thing;
Roger Meier0e814802014-01-17 21:07:58 +0100344 printf(" = {%d, {\"%s\", %d, %d, %" PRId64 "}, %d}\n",
Mark Sleee8540632006-05-30 09:24:40 +0000345 in2.byte_thing,
346 in.string_thing.c_str(),
347 (int)in.byte_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000348 in.i32_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000349 in.i64_thing,
David Reiss0c90f6f2008-02-06 22:18:40 +0000350 in2.i32_thing);
Roger Meier4fce9602012-05-04 06:22:09 +0000351 if (in2 != out2)
352 failCount++;
Mark Sleee8540632006-05-30 09:24:40 +0000353
354 /**
355 * MAP TEST
356 */
357 map<int32_t,int32_t> mapout;
358 for (int32_t i = 0; i < 5; ++i) {
359 mapout.insert(make_pair(i, i-10));
360 }
361 printf("testMap({");
362 map<int32_t, int32_t>::const_iterator m_iter;
363 bool first = true;
364 for (m_iter = mapout.begin(); m_iter != mapout.end(); ++m_iter) {
365 if (first) {
366 first = false;
367 } else {
368 printf(", ");
369 }
370 printf("%d => %d", m_iter->first, m_iter->second);
371 }
372 printf("})");
Mark Slee1921d202007-01-24 19:43:06 +0000373 map<int32_t,int32_t> mapin;
374 testClient.testMap(mapin, mapout);
Mark Sleee8540632006-05-30 09:24:40 +0000375 printf(" = {");
376 first = true;
377 for (m_iter = mapin.begin(); m_iter != mapin.end(); ++m_iter) {
378 if (first) {
379 first = false;
380 } else {
381 printf(", ");
382 }
383 printf("%d => %d", m_iter->first, m_iter->second);
384 }
385 printf("}\n");
Roger Meier4fce9602012-05-04 06:22:09 +0000386 if (mapin != mapout)
387 failCount++;
388
389 /**
390 * STRING MAP TEST
391 * missing
392 */
Mark Sleee8540632006-05-30 09:24:40 +0000393
394 /**
395 * SET TEST
396 */
397 set<int32_t> setout;
398 for (int32_t i = -2; i < 3; ++i) {
399 setout.insert(i);
400 }
401 printf("testSet({");
402 set<int32_t>::const_iterator s_iter;
403 first = true;
404 for (s_iter = setout.begin(); s_iter != setout.end(); ++s_iter) {
405 if (first) {
406 first = false;
407 } else {
408 printf(", ");
409 }
410 printf("%d", *s_iter);
411 }
412 printf("})");
Mark Slee1921d202007-01-24 19:43:06 +0000413 set<int32_t> setin;
414 testClient.testSet(setin, setout);
Mark Sleee8540632006-05-30 09:24:40 +0000415 printf(" = {");
416 first = true;
417 for (s_iter = setin.begin(); s_iter != setin.end(); ++s_iter) {
418 if (first) {
419 first = false;
420 } else {
421 printf(", ");
422 }
423 printf("%d", *s_iter);
424 }
425 printf("}\n");
Roger Meier4fce9602012-05-04 06:22:09 +0000426 if (setin != setout)
427 failCount++;
Mark Sleee8540632006-05-30 09:24:40 +0000428
429 /**
430 * LIST TEST
431 */
Mark Sleeb9acf982006-10-10 01:57:32 +0000432 vector<int32_t> listout;
Mark Sleee8540632006-05-30 09:24:40 +0000433 for (int32_t i = -2; i < 3; ++i) {
434 listout.push_back(i);
435 }
436 printf("testList({");
Mark Sleeb9acf982006-10-10 01:57:32 +0000437 vector<int32_t>::const_iterator l_iter;
Mark Sleee8540632006-05-30 09:24:40 +0000438 first = true;
439 for (l_iter = listout.begin(); l_iter != listout.end(); ++l_iter) {
440 if (first) {
441 first = false;
442 } else {
443 printf(", ");
444 }
445 printf("%d", *l_iter);
446 }
447 printf("})");
Mark Slee1921d202007-01-24 19:43:06 +0000448 vector<int32_t> listin;
449 testClient.testList(listin, listout);
Mark Sleee8540632006-05-30 09:24:40 +0000450 printf(" = {");
451 first = true;
452 for (l_iter = listin.begin(); l_iter != listin.end(); ++l_iter) {
453 if (first) {
454 first = false;
455 } else {
456 printf(", ");
457 }
458 printf("%d", *l_iter);
459 }
460 printf("}\n");
Roger Meier4fce9602012-05-04 06:22:09 +0000461 if (listin != listout)
462 failCount++;
Mark Sleee8540632006-05-30 09:24:40 +0000463
464 /**
465 * ENUM TEST
466 */
467 printf("testEnum(ONE)");
Bryan Duxbury833ae492010-09-27 17:26:02 +0000468 Numberz::type ret = testClient.testEnum(Numberz::ONE);
Mark Sleee8540632006-05-30 09:24:40 +0000469 printf(" = %d\n", ret);
Roger Meier4fce9602012-05-04 06:22:09 +0000470 if (ret != Numberz::ONE)
471 failCount++;
Mark Sleee8540632006-05-30 09:24:40 +0000472
473 printf("testEnum(TWO)");
Bryan Duxbury833ae492010-09-27 17:26:02 +0000474 ret = testClient.testEnum(Numberz::TWO);
Mark Sleee8540632006-05-30 09:24:40 +0000475 printf(" = %d\n", ret);
Roger Meier4fce9602012-05-04 06:22:09 +0000476 if (ret != Numberz::TWO)
477 failCount++;
Mark Sleee8540632006-05-30 09:24:40 +0000478
479 printf("testEnum(THREE)");
Bryan Duxbury833ae492010-09-27 17:26:02 +0000480 ret = testClient.testEnum(Numberz::THREE);
Mark Sleee8540632006-05-30 09:24:40 +0000481 printf(" = %d\n", ret);
Roger Meier4fce9602012-05-04 06:22:09 +0000482 if (ret != Numberz::THREE)
483 failCount++;
Mark Sleee8540632006-05-30 09:24:40 +0000484
485 printf("testEnum(FIVE)");
Bryan Duxbury833ae492010-09-27 17:26:02 +0000486 ret = testClient.testEnum(Numberz::FIVE);
Mark Sleee8540632006-05-30 09:24:40 +0000487 printf(" = %d\n", ret);
Roger Meier4fce9602012-05-04 06:22:09 +0000488 if (ret != Numberz::FIVE)
489 failCount++;
Mark Sleee8540632006-05-30 09:24:40 +0000490
491 printf("testEnum(EIGHT)");
Bryan Duxbury833ae492010-09-27 17:26:02 +0000492 ret = testClient.testEnum(Numberz::EIGHT);
Mark Sleee8540632006-05-30 09:24:40 +0000493 printf(" = %d\n", ret);
Roger Meier4fce9602012-05-04 06:22:09 +0000494 if (ret != Numberz::EIGHT)
495 failCount++;
Mark Sleee8540632006-05-30 09:24:40 +0000496
497 /**
498 * TYPEDEF TEST
499 */
500 printf("testTypedef(309858235082523)");
Marc Slemkobf4fd192006-08-15 21:29:39 +0000501 UserId uid = testClient.testTypedef(309858235082523LL);
Roger Meier0e814802014-01-17 21:07:58 +0100502 printf(" = %" PRId64 "\n", uid);
Roger Meier4fce9602012-05-04 06:22:09 +0000503 if (uid != 309858235082523LL)
504 failCount++;
Mark Sleee8540632006-05-30 09:24:40 +0000505
506 /**
507 * NESTED MAP TEST
508 */
509 printf("testMapMap(1)");
Mark Slee1921d202007-01-24 19:43:06 +0000510 map<int32_t, map<int32_t, int32_t> > mm;
511 testClient.testMapMap(mm, 1);
Mark Sleee8540632006-05-30 09:24:40 +0000512 printf(" = {");
513 map<int32_t, map<int32_t, int32_t> >::const_iterator mi;
514 for (mi = mm.begin(); mi != mm.end(); ++mi) {
515 printf("%d => {", mi->first);
516 map<int32_t, int32_t>::const_iterator mi2;
517 for (mi2 = mi->second.begin(); mi2 != mi->second.end(); ++mi2) {
518 printf("%d => %d, ", mi2->first, mi2->second);
519 }
520 printf("}, ");
521 }
522 printf("}\n");
523
524 /**
525 * INSANITY TEST
526 */
Jens Geyerf4598682014-05-08 23:18:44 +0200527 if (!noinsane) {
528 Insanity insane;
529 insane.userMap.insert(make_pair(Numberz::FIVE, 5000));
530 Xtruct truck;
531 truck.string_thing = "Truck";
532 truck.byte_thing = 8;
533 truck.i32_thing = 8;
534 truck.i64_thing = 8;
535 insane.xtructs.push_back(truck);
536 printf("testInsanity()");
537 map<UserId, map<Numberz::type,Insanity> > whoa;
538 testClient.testInsanity(whoa, insane);
539 printf(" = {");
540 map<UserId, map<Numberz::type,Insanity> >::const_iterator i_iter;
541 for (i_iter = whoa.begin(); i_iter != whoa.end(); ++i_iter) {
542 printf("%" PRId64 " => {", i_iter->first);
543 map<Numberz::type,Insanity>::const_iterator i2_iter;
544 for (i2_iter = i_iter->second.begin();
545 i2_iter != i_iter->second.end();
546 ++i2_iter) {
547 printf("%d => {", i2_iter->first);
548 map<Numberz::type, UserId> userMap = i2_iter->second.userMap;
549 map<Numberz::type, UserId>::const_iterator um;
550 printf("{");
551 for (um = userMap.begin(); um != userMap.end(); ++um) {
552 printf("%d => %" PRId64 ", ", um->first, um->second);
553 }
554 printf("}, ");
Mark Sleee8540632006-05-30 09:24:40 +0000555
Jens Geyerf4598682014-05-08 23:18:44 +0200556 vector<Xtruct> xtructs = i2_iter->second.xtructs;
557 vector<Xtruct>::const_iterator x;
558 printf("{");
559 for (x = xtructs.begin(); x != xtructs.end(); ++x) {
560 printf("{\"%s\", %d, %d, %" PRId64 "}, ",
561 x->string_thing.c_str(),
562 (int)x->byte_thing,
563 x->i32_thing,
564 x->i64_thing);
565 }
566 printf("}");
Mark Sleee8540632006-05-30 09:24:40 +0000567
Jens Geyerf4598682014-05-08 23:18:44 +0200568 printf("}, ");
569 }
Mark Sleee8540632006-05-30 09:24:40 +0000570 printf("}, ");
571 }
Jens Geyerf4598682014-05-08 23:18:44 +0200572 printf("}\n");
Mark Sleee8540632006-05-30 09:24:40 +0000573 }
Marc Slemko71d4e472006-08-15 22:34:04 +0000574 /* test exception */
Mark Slee95771002006-06-07 06:53:25 +0000575
Marc Slemkobf4fd192006-08-15 21:29:39 +0000576 try {
Marc Slemko71d4e472006-08-15 22:34:04 +0000577 printf("testClient.testException(\"Xception\") =>");
578 testClient.testException("Xception");
579 printf(" void\nFAILURE\n");
Roger Meier4fce9602012-05-04 06:22:09 +0000580 failCount++;
David Reiss0c90f6f2008-02-06 22:18:40 +0000581
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000582 } catch(Xception& e) {
Marc Slemko71d4e472006-08-15 22:34:04 +0000583 printf(" {%u, \"%s\"}\n", e.errorCode, e.message.c_str());
Marc Slemkobf4fd192006-08-15 21:29:39 +0000584 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000585
Marc Slemkobf4fd192006-08-15 21:29:39 +0000586 try {
Roger Meier99b36722012-05-03 21:21:43 +0000587 printf("testClient.testException(\"TException\") =>");
588 testClient.testException("TException");
Roger Meierf50df7f2012-05-02 22:49:55 +0000589 printf(" void\nFAILURE\n");
Roger Meier4fce9602012-05-04 06:22:09 +0000590 failCount++;
Roger Meierf50df7f2012-05-02 22:49:55 +0000591
Jake Farrell5d02b802014-01-07 21:42:01 -0500592 } catch(const TException&) {
Roger Meierf50df7f2012-05-02 22:49:55 +0000593 printf(" Caught TException\n");
594 }
595
596 try {
Marc Slemko71d4e472006-08-15 22:34:04 +0000597 printf("testClient.testException(\"success\") =>");
598 testClient.testException("success");
599 printf(" void\n");
600 } catch(...) {
601 printf(" exception\nFAILURE\n");
Roger Meier4fce9602012-05-04 06:22:09 +0000602 failCount++;
Marc Slemko71d4e472006-08-15 22:34:04 +0000603 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000604
Marc Slemko71d4e472006-08-15 22:34:04 +0000605 /* test multi exception */
David Reiss0c90f6f2008-02-06 22:18:40 +0000606
Marc Slemko71d4e472006-08-15 22:34:04 +0000607 try {
608 printf("testClient.testMultiException(\"Xception\", \"test 1\") =>");
Mark Slee1921d202007-01-24 19:43:06 +0000609 Xtruct result;
610 testClient.testMultiException(result, "Xception", "test 1");
Marc Slemko71d4e472006-08-15 22:34:04 +0000611 printf(" result\nFAILURE\n");
Roger Meier4fce9602012-05-04 06:22:09 +0000612 failCount++;
Mark Sleed3d733a2006-09-01 22:19:06 +0000613 } catch(Xception& e) {
Marc Slemko71d4e472006-08-15 22:34:04 +0000614 printf(" {%u, \"%s\"}\n", e.errorCode, e.message.c_str());
615 }
616
617 try {
618 printf("testClient.testMultiException(\"Xception2\", \"test 2\") =>");
Mark Slee1921d202007-01-24 19:43:06 +0000619 Xtruct result;
620 testClient.testMultiException(result, "Xception2", "test 2");
Marc Slemko71d4e472006-08-15 22:34:04 +0000621 printf(" result\nFAILURE\n");
Roger Meier4fce9602012-05-04 06:22:09 +0000622 failCount++;
David Reiss0c90f6f2008-02-06 22:18:40 +0000623
Mark Sleed3d733a2006-09-01 22:19:06 +0000624 } catch(Xception2& e) {
Marc Slemko71d4e472006-08-15 22:34:04 +0000625 printf(" {%u, {\"%s\"}}\n", e.errorCode, e.struct_thing.string_thing.c_str());
Marc Slemkobf4fd192006-08-15 21:29:39 +0000626 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000627
Marc Slemko71d4e472006-08-15 22:34:04 +0000628 try {
629 printf("testClient.testMultiException(\"success\", \"test 3\") =>");
Mark Slee1921d202007-01-24 19:43:06 +0000630 Xtruct result;
631 testClient.testMultiException(result, "success", "test 3");
Marc Slemko71d4e472006-08-15 22:34:04 +0000632 printf(" {{\"%s\"}}\n", result.string_thing.c_str());
633 } catch(...) {
634 printf(" exception\nFAILURE\n");
Roger Meier4fce9602012-05-04 06:22:09 +0000635 failCount++;
Marc Slemko71d4e472006-08-15 22:34:04 +0000636 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000637
David Reissc51986f2009-03-24 20:01:25 +0000638 /* test oneway void */
David Reiss2ab6fe82008-02-18 02:11:44 +0000639 {
Jake Farrell5d02b802014-01-07 21:42:01 -0500640 printf("testClient.testOneway(1) =>");
David Reiss6ce401d2009-03-24 20:01:58 +0000641 uint64_t startOneway = now();
Jake Farrell5d02b802014-01-07 21:42:01 -0500642 testClient.testOneway(1);
David Reiss6ce401d2009-03-24 20:01:58 +0000643 uint64_t elapsed = now() - startOneway;
David Reiss2ab6fe82008-02-18 02:11:44 +0000644 if (elapsed > 200 * 1000) { // 0.2 seconds
645 printf(" FAILURE - took %.2f ms\n", (double)elapsed/1000.0);
Roger Meier4fce9602012-05-04 06:22:09 +0000646 failCount++;
David Reiss2ab6fe82008-02-18 02:11:44 +0000647 } else {
648 printf(" success - took %.2f ms\n", (double)elapsed/1000.0);
649 }
650 }
651
David Reiss2845b522008-02-18 02:11:52 +0000652 /**
David Reissc51986f2009-03-24 20:01:25 +0000653 * redo a simple test after the oneway to make sure we aren't "off by one" --
654 * if the server treated oneway void like normal void, this next test will
David Reiss2845b522008-02-18 02:11:52 +0000655 * fail since it will get the void confirmation rather than the correct
656 * result. In this circumstance, the client will throw the exception:
657 *
658 * TApplicationException: Wrong method namea
659 */
660 /**
661 * I32 TEST
662 */
663 printf("re-test testI32(-1)");
664 i32 = testClient.testI32(-1);
665 printf(" = %d\n", i32);
Roger Meier4fce9602012-05-04 06:22:09 +0000666 if (i32 != -1)
667 failCount++;
David Reiss2845b522008-02-18 02:11:52 +0000668
669
Marc Slemkobf4fd192006-08-15 21:29:39 +0000670 uint64_t stop = now();
Mark Sleed788b2e2006-09-07 01:26:35 +0000671 uint64_t tot = stop-start;
672
Roger Meier0e814802014-01-17 21:07:58 +0100673 printf("Total time: %" PRIu64 " us\n", stop-start);
David Reiss0c90f6f2008-02-06 22:18:40 +0000674
Mark Sleed788b2e2006-09-07 01:26:35 +0000675 time_tot += tot;
676 if (time_min == 0 || tot < time_min) {
677 time_min = tot;
678 }
679 if (tot > time_max) {
680 time_max = tot;
681 }
682
Mark Sleea3302652006-10-25 19:03:32 +0000683 transport->close();
Mark Sleee8540632006-05-30 09:24:40 +0000684 }
685
Marc Slemko6be374b2006-08-04 03:16:25 +0000686 // printf("\nSocket syscalls: %u", g_socket_syscalls);
Mark Sleee8540632006-05-30 09:24:40 +0000687 printf("\nAll tests done.\n");
Mark Sleed788b2e2006-09-07 01:26:35 +0000688
689 uint64_t time_avg = time_tot / numTests;
690
Roger Meier0e814802014-01-17 21:07:58 +0100691 printf("Min time: %" PRIu64 " us\n", time_min);
692 printf("Max time: %" PRIu64 " us\n", time_max);
693 printf("Avg time: %" PRIu64 " us\n", time_avg);
Mark Sleed788b2e2006-09-07 01:26:35 +0000694
Roger Meier4fce9602012-05-04 06:22:09 +0000695 return failCount;
Mark Sleee8540632006-05-30 09:24:40 +0000696}