blob: 01951392c5924a788e5b89a380b13303917f5354 [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 Meier3faaedf2011-10-02 10:51:45 +000088 client->testString(tr1::bind(testString_clientReturn, host, port, base, protocolFactory, std::tr1::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 = "";
Mark Sleee8540632006-05-30 09:24:40 +0000105
Jake Farrell5d02b802014-01-07 21:42:01 -0500106 boost::program_options::options_description desc("Allowed options");
Roger Meierca142b02011-06-07 17:59:07 +0000107 desc.add_options()
108 ("help,h", "produce help message")
Jake Farrell5d02b802014-01-07 21:42:01 -0500109 ("host", boost::program_options::value<string>(&host)->default_value(host), "Host to connect")
110 ("port", boost::program_options::value<int>(&port)->default_value(port), "Port number to connect")
111 ("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")
112 ("transport", boost::program_options::value<string>(&transport_type)->default_value(transport_type), "Transport: buffered, framed, http, evhttp")
Roger Meier023192f2014-02-12 09:35:12 +0100113 ("protocol", boost::program_options::value<string>(&protocol_type)->default_value(protocol_type), "Protocol: binary, compact, json")
Roger Meierca142b02011-06-07 17:59:07 +0000114 ("ssl", "Encrypted Transport using SSL")
Jake Farrell5d02b802014-01-07 21:42:01 -0500115 ("testloops,n", boost::program_options::value<int>(&numTests)->default_value(numTests), "Number of Tests")
Roger Meierca142b02011-06-07 17:59:07 +0000116 ;
117
Jake Farrell5d02b802014-01-07 21:42:01 -0500118 boost::program_options::variables_map vm;
119 boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), vm);
120 boost::program_options::notify(vm);
Roger Meierca142b02011-06-07 17:59:07 +0000121
122 if (vm.count("help")) {
123 cout << desc << "\n";
124 return 1;
Mark Sleee8540632006-05-30 09:24:40 +0000125 }
Mark Sleea3302652006-10-25 19:03:32 +0000126
Jake Farrell5d02b802014-01-07 21:42:01 -0500127 try {
Roger Meierca142b02011-06-07 17:59:07 +0000128 if (!protocol_type.empty()) {
129 if (protocol_type == "binary") {
Roger Meier284101c2014-03-11 21:20:35 +0100130 } else if (protocol_type == "compact") {
Roger Meierca142b02011-06-07 17:59:07 +0000131 } else if (protocol_type == "json") {
132 } else {
133 throw invalid_argument("Unknown protocol type "+protocol_type);
134 }
135 }
136
137 if (!transport_type.empty()) {
138 if (transport_type == "buffered") {
139 } else if (transport_type == "framed") {
140 } else if (transport_type == "http") {
Roger Meier7e056e72011-07-17 07:28:28 +0000141 } else if (transport_type == "evhttp") {
Roger Meierca142b02011-06-07 17:59:07 +0000142 } else {
143 throw invalid_argument("Unknown transport type "+transport_type);
144 }
145 }
146
147 } catch (std::exception& e) {
148 cerr << e.what() << endl;
149 cout << desc << "\n";
150 return 1;
151 }
152
153 if (vm.count("ssl")) {
154 ssl = true;
155 }
156
Roger Meier611f90c2011-12-11 22:08:51 +0000157 boost::shared_ptr<TTransport> transport;
158 boost::shared_ptr<TProtocol> protocol;
Roger Meierca142b02011-06-07 17:59:07 +0000159
Roger Meier611f90c2011-12-11 22:08:51 +0000160 boost::shared_ptr<TSocket> socket;
161 boost::shared_ptr<TSSLSocketFactory> factory;
Roger Meierca142b02011-06-07 17:59:07 +0000162
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000163 if (ssl) {
Roger Meier611f90c2011-12-11 22:08:51 +0000164 factory = boost::shared_ptr<TSSLSocketFactory>(new TSSLSocketFactory());
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000165 factory->ciphers("ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
Roger Meierc94b2932014-02-22 20:07:33 +0100166 factory->loadTrustedCertificates("keys/CA.pem");
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000167 factory->authenticate(true);
168 socket = factory->createSocket(host, port);
169 } else {
Roger Meierca142b02011-06-07 17:59:07 +0000170 if (domain_socket != "") {
Roger Meier611f90c2011-12-11 22:08:51 +0000171 socket = boost::shared_ptr<TSocket>(new TSocket(domain_socket));
Roger Meierca142b02011-06-07 17:59:07 +0000172 port = 0;
173 }
174 else {
Roger Meier611f90c2011-12-11 22:08:51 +0000175 socket = boost::shared_ptr<TSocket>(new TSocket(host, port));
Roger Meierca142b02011-06-07 17:59:07 +0000176 }
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000177 }
Mark Sleea3302652006-10-25 19:03:32 +0000178
Roger Meierca142b02011-06-07 17:59:07 +0000179 if (transport_type.compare("http") == 0) {
Roger Meier611f90c2011-12-11 22:08:51 +0000180 boost::shared_ptr<TTransport> httpSocket(new THttpClient(socket, host, "/service"));
Roger Meierca142b02011-06-07 17:59:07 +0000181 transport = httpSocket;
182 } else if (transport_type.compare("framed") == 0){
Roger Meier611f90c2011-12-11 22:08:51 +0000183 boost::shared_ptr<TFramedTransport> framedSocket(new TFramedTransport(socket));
Mark Sleea3302652006-10-25 19:03:32 +0000184 transport = framedSocket;
Roger Meierca142b02011-06-07 17:59:07 +0000185 } else{
Roger Meier611f90c2011-12-11 22:08:51 +0000186 boost::shared_ptr<TBufferedTransport> bufferedSocket(new TBufferedTransport(socket));
Mark Sleea3302652006-10-25 19:03:32 +0000187 transport = bufferedSocket;
188 }
189
Roger Meierca142b02011-06-07 17:59:07 +0000190 if (protocol_type.compare("json") == 0) {
Roger Meier611f90c2011-12-11 22:08:51 +0000191 boost::shared_ptr<TProtocol> jsonProtocol(new TJSONProtocol(transport));
Roger Meierca142b02011-06-07 17:59:07 +0000192 protocol = jsonProtocol;
Roger Meier023192f2014-02-12 09:35:12 +0100193 } else if (protocol_type.compare("compact") == 0) {
194 boost::shared_ptr<TProtocol> compactProtocol(new TCompactProtocol(transport));
195 protocol = compactProtocol;
Roger Meierca142b02011-06-07 17:59:07 +0000196 } else{
Roger Meier611f90c2011-12-11 22:08:51 +0000197 boost::shared_ptr<TBinaryProtocol> binaryProtocol(new TBinaryProtocol(transport));
Roger Meierca142b02011-06-07 17:59:07 +0000198 protocol = binaryProtocol;
199 }
200
201 // Connection info
202 cout << "Connecting (" << transport_type << "/" << protocol_type << ") to: " << domain_socket;
203 if (port != 0) {
204 cout << host << ":" << port;
205 }
206 cout << endl;
207
Roger Meier7e056e72011-07-17 07:28:28 +0000208 if (transport_type.compare("evhttp") == 0) {
209 event_base *base = event_base_new();
210 cout << "Libevent Version: " << event_get_version() << endl;
211 cout << "Libevent Method: " << event_base_get_method(base) << endl;
212#if LIBEVENT_VERSION_NUMBER >= 0x02000000
213 cout << "Libevent Features: 0x" << hex << event_base_get_features(base) << endl;
214#endif
215
Roger Meier611f90c2011-12-11 22:08:51 +0000216 boost::shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());
Roger Meier7e056e72011-07-17 07:28:28 +0000217
Roger Meier611f90c2011-12-11 22:08:51 +0000218 boost::shared_ptr<TAsyncChannel> channel(new TEvhttpClientChannel(host.c_str(), "/", host.c_str(), port, base));
Roger Meier7e056e72011-07-17 07:28:28 +0000219 ThriftTestCobClient* client = new ThriftTestCobClient(channel, protocolFactory.get());
Roger Meier3faaedf2011-10-02 10:51:45 +0000220 client->testVoid(tr1::bind(testVoid_clientReturn, host.c_str(), port, base, protocolFactory.get(), std::tr1::placeholders::_1));
Jake Farrell5d02b802014-01-07 21:42:01 -0500221
Roger Meier7e056e72011-07-17 07:28:28 +0000222 event_base_loop(base, 0);
223 return 0;
224 }
225
226
Roger Meierca142b02011-06-07 17:59:07 +0000227 ThriftTestClient testClient(protocol);
Mark Sleed788b2e2006-09-07 01:26:35 +0000228
229 uint64_t time_min = 0;
230 uint64_t time_max = 0;
231 uint64_t time_tot = 0;
David Reiss0c90f6f2008-02-06 22:18:40 +0000232
Roger Meier4fce9602012-05-04 06:22:09 +0000233 int failCount = 0;
Mark Sleee8540632006-05-30 09:24:40 +0000234 int test = 0;
235 for (test = 0; test < numTests; ++test) {
Mark Slee95771002006-06-07 06:53:25 +0000236
Mark Slee95771002006-06-07 06:53:25 +0000237 try {
Mark Sleea3302652006-10-25 19:03:32 +0000238 transport->open();
Mark Slee95771002006-06-07 06:53:25 +0000239 } catch (TTransportException& ttx) {
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000240 printf("Connect failed: %s\n", ttx.what());
Roger Meier5b1e3c72011-12-08 13:20:12 +0000241 return 1;
Mark Sleee8540632006-05-30 09:24:40 +0000242 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000243
Mark Sleed788b2e2006-09-07 01:26:35 +0000244 /**
245 * CONNECT TEST
246 */
247 printf("Test #%d, connect %s:%d\n", test+1, host.c_str(), port);
Mark Slee95771002006-06-07 06:53:25 +0000248
249 uint64_t start = now();
David Reiss0c90f6f2008-02-06 22:18:40 +0000250
Mark Sleee8540632006-05-30 09:24:40 +0000251 /**
252 * VOID TEST
253 */
Mark Sleee129a2d2007-02-21 05:17:48 +0000254 try {
255 printf("testVoid()");
256 testClient.testVoid();
257 printf(" = void\n");
Roger Meier4fce9602012-05-04 06:22:09 +0000258 } catch (TApplicationException& tax) {
Mark Sleee129a2d2007-02-21 05:17:48 +0000259 printf("%s\n", tax.what());
Roger Meier4fce9602012-05-04 06:22:09 +0000260 failCount++;
Mark Sleee129a2d2007-02-21 05:17:48 +0000261 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000262
Mark Sleee8540632006-05-30 09:24:40 +0000263 /**
264 * STRING TEST
265 */
266 printf("testString(\"Test\")");
Mark Slee1921d202007-01-24 19:43:06 +0000267 string s;
268 testClient.testString(s, "Test");
Mark Sleee8540632006-05-30 09:24:40 +0000269 printf(" = \"%s\"\n", s.c_str());
Roger Meier4fce9602012-05-04 06:22:09 +0000270 if (s != "Test")
271 failCount++;
David Reiss0c90f6f2008-02-06 22:18:40 +0000272
Mark Sleee8540632006-05-30 09:24:40 +0000273 /**
274 * BYTE TEST
275 */
276 printf("testByte(1)");
277 uint8_t u8 = testClient.testByte(1);
278 printf(" = %d\n", (int)u8);
Roger Meier4fce9602012-05-04 06:22:09 +0000279 if (u8 != 1)
280 failCount++;
David Reiss0c90f6f2008-02-06 22:18:40 +0000281
Mark Sleee8540632006-05-30 09:24:40 +0000282 /**
283 * I32 TEST
284 */
285 printf("testI32(-1)");
286 int32_t i32 = testClient.testI32(-1);
287 printf(" = %d\n", i32);
Roger Meier4fce9602012-05-04 06:22:09 +0000288 if (i32 != -1)
289 failCount++;
Mark Sleee8540632006-05-30 09:24:40 +0000290
291 /**
Mark Sleee8540632006-05-30 09:24:40 +0000292 * I64 TEST
293 */
294 printf("testI64(-34359738368)");
Marc Slemkobf4fd192006-08-15 21:29:39 +0000295 int64_t i64 = testClient.testI64(-34359738368LL);
Roger Meier0e814802014-01-17 21:07:58 +0100296 printf(" = %" PRId64 "\n", i64);
Roger Meier4fce9602012-05-04 06:22:09 +0000297 if (i64 != -34359738368LL)
298 failCount++;
Mark Sleec98d0502006-09-06 02:42:25 +0000299 /**
300 * DOUBLE TEST
301 */
302 printf("testDouble(-5.2098523)");
303 double dub = testClient.testDouble(-5.2098523);
Roger Meiera8cef6e2011-07-17 18:55:59 +0000304 printf(" = %f\n", dub);
Roger Meier4fce9602012-05-04 06:22:09 +0000305 if ((dub - (-5.2098523)) > 0.001)
306 failCount++;
David Reiss0c90f6f2008-02-06 22:18:40 +0000307
Mark Sleee8540632006-05-30 09:24:40 +0000308 /**
309 * STRUCT TEST
310 */
Mark Slee6e536442006-06-30 18:28:50 +0000311 printf("testStruct({\"Zero\", 1, -3, -5})");
Mark Sleee8540632006-05-30 09:24:40 +0000312 Xtruct out;
313 out.string_thing = "Zero";
314 out.byte_thing = 1;
Mark Sleee8540632006-05-30 09:24:40 +0000315 out.i32_thing = -3;
Mark Sleee8540632006-05-30 09:24:40 +0000316 out.i64_thing = -5;
Mark Slee1921d202007-01-24 19:43:06 +0000317 Xtruct in;
318 testClient.testStruct(in, out);
Roger Meier0e814802014-01-17 21:07:58 +0100319 printf(" = {\"%s\", %d, %d, %" PRId64 "}\n",
Mark Sleee8540632006-05-30 09:24:40 +0000320 in.string_thing.c_str(),
321 (int)in.byte_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000322 in.i32_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000323 in.i64_thing);
Roger Meier4fce9602012-05-04 06:22:09 +0000324 if (in != out)
325 failCount++;
David Reiss0c90f6f2008-02-06 22:18:40 +0000326
Mark Sleee8540632006-05-30 09:24:40 +0000327 /**
328 * NESTED STRUCT TEST
329 */
Mark Slee6e536442006-06-30 18:28:50 +0000330 printf("testNest({1, {\"Zero\", 1, -3, -5}), 5}");
Mark Sleee8540632006-05-30 09:24:40 +0000331 Xtruct2 out2;
332 out2.byte_thing = 1;
333 out2.struct_thing = out;
334 out2.i32_thing = 5;
Mark Slee1921d202007-01-24 19:43:06 +0000335 Xtruct2 in2;
336 testClient.testNest(in2, out2);
Mark Sleee8540632006-05-30 09:24:40 +0000337 in = in2.struct_thing;
Roger Meier0e814802014-01-17 21:07:58 +0100338 printf(" = {%d, {\"%s\", %d, %d, %" PRId64 "}, %d}\n",
Mark Sleee8540632006-05-30 09:24:40 +0000339 in2.byte_thing,
340 in.string_thing.c_str(),
341 (int)in.byte_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000342 in.i32_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000343 in.i64_thing,
David Reiss0c90f6f2008-02-06 22:18:40 +0000344 in2.i32_thing);
Roger Meier4fce9602012-05-04 06:22:09 +0000345 if (in2 != out2)
346 failCount++;
Mark Sleee8540632006-05-30 09:24:40 +0000347
348 /**
349 * MAP TEST
350 */
351 map<int32_t,int32_t> mapout;
352 for (int32_t i = 0; i < 5; ++i) {
353 mapout.insert(make_pair(i, i-10));
354 }
355 printf("testMap({");
356 map<int32_t, int32_t>::const_iterator m_iter;
357 bool first = true;
358 for (m_iter = mapout.begin(); m_iter != mapout.end(); ++m_iter) {
359 if (first) {
360 first = false;
361 } else {
362 printf(", ");
363 }
364 printf("%d => %d", m_iter->first, m_iter->second);
365 }
366 printf("})");
Mark Slee1921d202007-01-24 19:43:06 +0000367 map<int32_t,int32_t> mapin;
368 testClient.testMap(mapin, mapout);
Mark Sleee8540632006-05-30 09:24:40 +0000369 printf(" = {");
370 first = true;
371 for (m_iter = mapin.begin(); m_iter != mapin.end(); ++m_iter) {
372 if (first) {
373 first = false;
374 } else {
375 printf(", ");
376 }
377 printf("%d => %d", m_iter->first, m_iter->second);
378 }
379 printf("}\n");
Roger Meier4fce9602012-05-04 06:22:09 +0000380 if (mapin != mapout)
381 failCount++;
382
383 /**
384 * STRING MAP TEST
385 * missing
386 */
Mark Sleee8540632006-05-30 09:24:40 +0000387
388 /**
389 * SET TEST
390 */
391 set<int32_t> setout;
392 for (int32_t i = -2; i < 3; ++i) {
393 setout.insert(i);
394 }
395 printf("testSet({");
396 set<int32_t>::const_iterator s_iter;
397 first = true;
398 for (s_iter = setout.begin(); s_iter != setout.end(); ++s_iter) {
399 if (first) {
400 first = false;
401 } else {
402 printf(", ");
403 }
404 printf("%d", *s_iter);
405 }
406 printf("})");
Mark Slee1921d202007-01-24 19:43:06 +0000407 set<int32_t> setin;
408 testClient.testSet(setin, setout);
Mark Sleee8540632006-05-30 09:24:40 +0000409 printf(" = {");
410 first = true;
411 for (s_iter = setin.begin(); s_iter != setin.end(); ++s_iter) {
412 if (first) {
413 first = false;
414 } else {
415 printf(", ");
416 }
417 printf("%d", *s_iter);
418 }
419 printf("}\n");
Roger Meier4fce9602012-05-04 06:22:09 +0000420 if (setin != setout)
421 failCount++;
Mark Sleee8540632006-05-30 09:24:40 +0000422
423 /**
424 * LIST TEST
425 */
Mark Sleeb9acf982006-10-10 01:57:32 +0000426 vector<int32_t> listout;
Mark Sleee8540632006-05-30 09:24:40 +0000427 for (int32_t i = -2; i < 3; ++i) {
428 listout.push_back(i);
429 }
430 printf("testList({");
Mark Sleeb9acf982006-10-10 01:57:32 +0000431 vector<int32_t>::const_iterator l_iter;
Mark Sleee8540632006-05-30 09:24:40 +0000432 first = true;
433 for (l_iter = listout.begin(); l_iter != listout.end(); ++l_iter) {
434 if (first) {
435 first = false;
436 } else {
437 printf(", ");
438 }
439 printf("%d", *l_iter);
440 }
441 printf("})");
Mark Slee1921d202007-01-24 19:43:06 +0000442 vector<int32_t> listin;
443 testClient.testList(listin, listout);
Mark Sleee8540632006-05-30 09:24:40 +0000444 printf(" = {");
445 first = true;
446 for (l_iter = listin.begin(); l_iter != listin.end(); ++l_iter) {
447 if (first) {
448 first = false;
449 } else {
450 printf(", ");
451 }
452 printf("%d", *l_iter);
453 }
454 printf("}\n");
Roger Meier4fce9602012-05-04 06:22:09 +0000455 if (listin != listout)
456 failCount++;
Mark Sleee8540632006-05-30 09:24:40 +0000457
458 /**
459 * ENUM TEST
460 */
461 printf("testEnum(ONE)");
Bryan Duxbury833ae492010-09-27 17:26:02 +0000462 Numberz::type ret = testClient.testEnum(Numberz::ONE);
Mark Sleee8540632006-05-30 09:24:40 +0000463 printf(" = %d\n", ret);
Roger Meier4fce9602012-05-04 06:22:09 +0000464 if (ret != Numberz::ONE)
465 failCount++;
Mark Sleee8540632006-05-30 09:24:40 +0000466
467 printf("testEnum(TWO)");
Bryan Duxbury833ae492010-09-27 17:26:02 +0000468 ret = testClient.testEnum(Numberz::TWO);
Mark Sleee8540632006-05-30 09:24:40 +0000469 printf(" = %d\n", ret);
Roger Meier4fce9602012-05-04 06:22:09 +0000470 if (ret != Numberz::TWO)
471 failCount++;
Mark Sleee8540632006-05-30 09:24:40 +0000472
473 printf("testEnum(THREE)");
Bryan Duxbury833ae492010-09-27 17:26:02 +0000474 ret = testClient.testEnum(Numberz::THREE);
Mark Sleee8540632006-05-30 09:24:40 +0000475 printf(" = %d\n", ret);
Roger Meier4fce9602012-05-04 06:22:09 +0000476 if (ret != Numberz::THREE)
477 failCount++;
Mark Sleee8540632006-05-30 09:24:40 +0000478
479 printf("testEnum(FIVE)");
Bryan Duxbury833ae492010-09-27 17:26:02 +0000480 ret = testClient.testEnum(Numberz::FIVE);
Mark Sleee8540632006-05-30 09:24:40 +0000481 printf(" = %d\n", ret);
Roger Meier4fce9602012-05-04 06:22:09 +0000482 if (ret != Numberz::FIVE)
483 failCount++;
Mark Sleee8540632006-05-30 09:24:40 +0000484
485 printf("testEnum(EIGHT)");
Bryan Duxbury833ae492010-09-27 17:26:02 +0000486 ret = testClient.testEnum(Numberz::EIGHT);
Mark Sleee8540632006-05-30 09:24:40 +0000487 printf(" = %d\n", ret);
Roger Meier4fce9602012-05-04 06:22:09 +0000488 if (ret != Numberz::EIGHT)
489 failCount++;
Mark Sleee8540632006-05-30 09:24:40 +0000490
491 /**
492 * TYPEDEF TEST
493 */
494 printf("testTypedef(309858235082523)");
Marc Slemkobf4fd192006-08-15 21:29:39 +0000495 UserId uid = testClient.testTypedef(309858235082523LL);
Roger Meier0e814802014-01-17 21:07:58 +0100496 printf(" = %" PRId64 "\n", uid);
Roger Meier4fce9602012-05-04 06:22:09 +0000497 if (uid != 309858235082523LL)
498 failCount++;
Mark Sleee8540632006-05-30 09:24:40 +0000499
500 /**
501 * NESTED MAP TEST
502 */
503 printf("testMapMap(1)");
Mark Slee1921d202007-01-24 19:43:06 +0000504 map<int32_t, map<int32_t, int32_t> > mm;
505 testClient.testMapMap(mm, 1);
Mark Sleee8540632006-05-30 09:24:40 +0000506 printf(" = {");
507 map<int32_t, map<int32_t, int32_t> >::const_iterator mi;
508 for (mi = mm.begin(); mi != mm.end(); ++mi) {
509 printf("%d => {", mi->first);
510 map<int32_t, int32_t>::const_iterator mi2;
511 for (mi2 = mi->second.begin(); mi2 != mi->second.end(); ++mi2) {
512 printf("%d => %d, ", mi2->first, mi2->second);
513 }
514 printf("}, ");
515 }
516 printf("}\n");
517
518 /**
519 * INSANITY TEST
520 */
521 Insanity insane;
Bryan Duxbury833ae492010-09-27 17:26:02 +0000522 insane.userMap.insert(make_pair(Numberz::FIVE, 5000));
Mark Sleee8540632006-05-30 09:24:40 +0000523 Xtruct truck;
524 truck.string_thing = "Truck";
525 truck.byte_thing = 8;
Mark Sleee8540632006-05-30 09:24:40 +0000526 truck.i32_thing = 8;
Mark Sleee8540632006-05-30 09:24:40 +0000527 truck.i64_thing = 8;
528 insane.xtructs.push_back(truck);
529 printf("testInsanity()");
Bryan Duxbury833ae492010-09-27 17:26:02 +0000530 map<UserId, map<Numberz::type,Insanity> > whoa;
Mark Slee1921d202007-01-24 19:43:06 +0000531 testClient.testInsanity(whoa, insane);
Mark Sleee8540632006-05-30 09:24:40 +0000532 printf(" = {");
Bryan Duxbury833ae492010-09-27 17:26:02 +0000533 map<UserId, map<Numberz::type,Insanity> >::const_iterator i_iter;
Mark Sleee8540632006-05-30 09:24:40 +0000534 for (i_iter = whoa.begin(); i_iter != whoa.end(); ++i_iter) {
Roger Meier0e814802014-01-17 21:07:58 +0100535 printf("%" PRId64 " => {", i_iter->first);
Bryan Duxbury833ae492010-09-27 17:26:02 +0000536 map<Numberz::type,Insanity>::const_iterator i2_iter;
Mark Sleee8540632006-05-30 09:24:40 +0000537 for (i2_iter = i_iter->second.begin();
538 i2_iter != i_iter->second.end();
539 ++i2_iter) {
540 printf("%d => {", i2_iter->first);
Bryan Duxbury833ae492010-09-27 17:26:02 +0000541 map<Numberz::type, UserId> userMap = i2_iter->second.userMap;
542 map<Numberz::type, UserId>::const_iterator um;
Mark Sleee8540632006-05-30 09:24:40 +0000543 printf("{");
544 for (um = userMap.begin(); um != userMap.end(); ++um) {
Roger Meier0e814802014-01-17 21:07:58 +0100545 printf("%d => %" PRId64 ", ", um->first, um->second);
Mark Sleee8540632006-05-30 09:24:40 +0000546 }
547 printf("}, ");
548
Mark Sleeb9acf982006-10-10 01:57:32 +0000549 vector<Xtruct> xtructs = i2_iter->second.xtructs;
550 vector<Xtruct>::const_iterator x;
Mark Sleee8540632006-05-30 09:24:40 +0000551 printf("{");
552 for (x = xtructs.begin(); x != xtructs.end(); ++x) {
Roger Meier0e814802014-01-17 21:07:58 +0100553 printf("{\"%s\", %d, %d, %" PRId64 "}, ",
Mark Sleee8540632006-05-30 09:24:40 +0000554 x->string_thing.c_str(),
555 (int)x->byte_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000556 x->i32_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000557 x->i64_thing);
558 }
559 printf("}");
560
561 printf("}, ");
562 }
563 printf("}, ");
564 }
565 printf("}\n");
566
Marc Slemko71d4e472006-08-15 22:34:04 +0000567 /* test exception */
Mark Slee95771002006-06-07 06:53:25 +0000568
Marc Slemkobf4fd192006-08-15 21:29:39 +0000569 try {
Marc Slemko71d4e472006-08-15 22:34:04 +0000570 printf("testClient.testException(\"Xception\") =>");
571 testClient.testException("Xception");
572 printf(" void\nFAILURE\n");
Roger Meier4fce9602012-05-04 06:22:09 +0000573 failCount++;
David Reiss0c90f6f2008-02-06 22:18:40 +0000574
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000575 } catch(Xception& e) {
Marc Slemko71d4e472006-08-15 22:34:04 +0000576 printf(" {%u, \"%s\"}\n", e.errorCode, e.message.c_str());
Marc Slemkobf4fd192006-08-15 21:29:39 +0000577 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000578
Marc Slemkobf4fd192006-08-15 21:29:39 +0000579 try {
Roger Meier99b36722012-05-03 21:21:43 +0000580 printf("testClient.testException(\"TException\") =>");
581 testClient.testException("TException");
Roger Meierf50df7f2012-05-02 22:49:55 +0000582 printf(" void\nFAILURE\n");
Roger Meier4fce9602012-05-04 06:22:09 +0000583 failCount++;
Roger Meierf50df7f2012-05-02 22:49:55 +0000584
Jake Farrell5d02b802014-01-07 21:42:01 -0500585 } catch(const TException&) {
Roger Meierf50df7f2012-05-02 22:49:55 +0000586 printf(" Caught TException\n");
587 }
588
589 try {
Marc Slemko71d4e472006-08-15 22:34:04 +0000590 printf("testClient.testException(\"success\") =>");
591 testClient.testException("success");
592 printf(" void\n");
593 } catch(...) {
594 printf(" exception\nFAILURE\n");
Roger Meier4fce9602012-05-04 06:22:09 +0000595 failCount++;
Marc Slemko71d4e472006-08-15 22:34:04 +0000596 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000597
Marc Slemko71d4e472006-08-15 22:34:04 +0000598 /* test multi exception */
David Reiss0c90f6f2008-02-06 22:18:40 +0000599
Marc Slemko71d4e472006-08-15 22:34:04 +0000600 try {
601 printf("testClient.testMultiException(\"Xception\", \"test 1\") =>");
Mark Slee1921d202007-01-24 19:43:06 +0000602 Xtruct result;
603 testClient.testMultiException(result, "Xception", "test 1");
Marc Slemko71d4e472006-08-15 22:34:04 +0000604 printf(" result\nFAILURE\n");
Roger Meier4fce9602012-05-04 06:22:09 +0000605 failCount++;
Mark Sleed3d733a2006-09-01 22:19:06 +0000606 } catch(Xception& e) {
Marc Slemko71d4e472006-08-15 22:34:04 +0000607 printf(" {%u, \"%s\"}\n", e.errorCode, e.message.c_str());
608 }
609
610 try {
611 printf("testClient.testMultiException(\"Xception2\", \"test 2\") =>");
Mark Slee1921d202007-01-24 19:43:06 +0000612 Xtruct result;
613 testClient.testMultiException(result, "Xception2", "test 2");
Marc Slemko71d4e472006-08-15 22:34:04 +0000614 printf(" result\nFAILURE\n");
Roger Meier4fce9602012-05-04 06:22:09 +0000615 failCount++;
David Reiss0c90f6f2008-02-06 22:18:40 +0000616
Mark Sleed3d733a2006-09-01 22:19:06 +0000617 } catch(Xception2& e) {
Marc Slemko71d4e472006-08-15 22:34:04 +0000618 printf(" {%u, {\"%s\"}}\n", e.errorCode, e.struct_thing.string_thing.c_str());
Marc Slemkobf4fd192006-08-15 21:29:39 +0000619 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000620
Marc Slemko71d4e472006-08-15 22:34:04 +0000621 try {
622 printf("testClient.testMultiException(\"success\", \"test 3\") =>");
Mark Slee1921d202007-01-24 19:43:06 +0000623 Xtruct result;
624 testClient.testMultiException(result, "success", "test 3");
Marc Slemko71d4e472006-08-15 22:34:04 +0000625 printf(" {{\"%s\"}}\n", result.string_thing.c_str());
626 } catch(...) {
627 printf(" exception\nFAILURE\n");
Roger Meier4fce9602012-05-04 06:22:09 +0000628 failCount++;
Marc Slemko71d4e472006-08-15 22:34:04 +0000629 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000630
David Reissc51986f2009-03-24 20:01:25 +0000631 /* test oneway void */
David Reiss2ab6fe82008-02-18 02:11:44 +0000632 {
Jake Farrell5d02b802014-01-07 21:42:01 -0500633 printf("testClient.testOneway(1) =>");
David Reiss6ce401d2009-03-24 20:01:58 +0000634 uint64_t startOneway = now();
Jake Farrell5d02b802014-01-07 21:42:01 -0500635 testClient.testOneway(1);
David Reiss6ce401d2009-03-24 20:01:58 +0000636 uint64_t elapsed = now() - startOneway;
David Reiss2ab6fe82008-02-18 02:11:44 +0000637 if (elapsed > 200 * 1000) { // 0.2 seconds
638 printf(" FAILURE - took %.2f ms\n", (double)elapsed/1000.0);
Roger Meier4fce9602012-05-04 06:22:09 +0000639 failCount++;
David Reiss2ab6fe82008-02-18 02:11:44 +0000640 } else {
641 printf(" success - took %.2f ms\n", (double)elapsed/1000.0);
642 }
643 }
644
David Reiss2845b522008-02-18 02:11:52 +0000645 /**
David Reissc51986f2009-03-24 20:01:25 +0000646 * redo a simple test after the oneway to make sure we aren't "off by one" --
647 * if the server treated oneway void like normal void, this next test will
David Reiss2845b522008-02-18 02:11:52 +0000648 * fail since it will get the void confirmation rather than the correct
649 * result. In this circumstance, the client will throw the exception:
650 *
651 * TApplicationException: Wrong method namea
652 */
653 /**
654 * I32 TEST
655 */
656 printf("re-test testI32(-1)");
657 i32 = testClient.testI32(-1);
658 printf(" = %d\n", i32);
Roger Meier4fce9602012-05-04 06:22:09 +0000659 if (i32 != -1)
660 failCount++;
David Reiss2845b522008-02-18 02:11:52 +0000661
662
Marc Slemkobf4fd192006-08-15 21:29:39 +0000663 uint64_t stop = now();
Mark Sleed788b2e2006-09-07 01:26:35 +0000664 uint64_t tot = stop-start;
665
Roger Meier0e814802014-01-17 21:07:58 +0100666 printf("Total time: %" PRIu64 " us\n", stop-start);
David Reiss0c90f6f2008-02-06 22:18:40 +0000667
Mark Sleed788b2e2006-09-07 01:26:35 +0000668 time_tot += tot;
669 if (time_min == 0 || tot < time_min) {
670 time_min = tot;
671 }
672 if (tot > time_max) {
673 time_max = tot;
674 }
675
Mark Sleea3302652006-10-25 19:03:32 +0000676 transport->close();
Mark Sleee8540632006-05-30 09:24:40 +0000677 }
678
Marc Slemko6be374b2006-08-04 03:16:25 +0000679 // printf("\nSocket syscalls: %u", g_socket_syscalls);
Mark Sleee8540632006-05-30 09:24:40 +0000680 printf("\nAll tests done.\n");
Mark Sleed788b2e2006-09-07 01:26:35 +0000681
682 uint64_t time_avg = time_tot / numTests;
683
Roger Meier0e814802014-01-17 21:07:58 +0100684 printf("Min time: %" PRIu64 " us\n", time_min);
685 printf("Max time: %" PRIu64 " us\n", time_max);
686 printf("Avg time: %" PRIu64 " us\n", time_avg);
Mark Sleed788b2e2006-09-07 01:26:35 +0000687
Roger Meier4fce9602012-05-04 06:22:09 +0000688 return failCount;
Mark Sleee8540632006-05-30 09:24:40 +0000689}