blob: 591d19dadc0e3b0fb0332e30eb8211ca75d46aa1 [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") {
130 } else if (protocol_type == "json") {
131 } else {
132 throw invalid_argument("Unknown protocol type "+protocol_type);
133 }
134 }
135
136 if (!transport_type.empty()) {
137 if (transport_type == "buffered") {
138 } else if (transport_type == "framed") {
139 } else if (transport_type == "http") {
Roger Meier7e056e72011-07-17 07:28:28 +0000140 } else if (transport_type == "evhttp") {
Roger Meierca142b02011-06-07 17:59:07 +0000141 } else {
142 throw invalid_argument("Unknown transport type "+transport_type);
143 }
144 }
145
146 } catch (std::exception& e) {
147 cerr << e.what() << endl;
148 cout << desc << "\n";
149 return 1;
150 }
151
152 if (vm.count("ssl")) {
153 ssl = true;
154 }
155
Roger Meier611f90c2011-12-11 22:08:51 +0000156 boost::shared_ptr<TTransport> transport;
157 boost::shared_ptr<TProtocol> protocol;
Roger Meierca142b02011-06-07 17:59:07 +0000158
Roger Meier611f90c2011-12-11 22:08:51 +0000159 boost::shared_ptr<TSocket> socket;
160 boost::shared_ptr<TSSLSocketFactory> factory;
Roger Meierca142b02011-06-07 17:59:07 +0000161
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000162 if (ssl) {
Roger Meier611f90c2011-12-11 22:08:51 +0000163 factory = boost::shared_ptr<TSSLSocketFactory>(new TSSLSocketFactory());
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000164 factory->ciphers("ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
Roger Meierc94b2932014-02-22 20:07:33 +0100165 factory->loadTrustedCertificates("keys/CA.pem");
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000166 factory->authenticate(true);
167 socket = factory->createSocket(host, port);
168 } else {
Roger Meierca142b02011-06-07 17:59:07 +0000169 if (domain_socket != "") {
Roger Meier611f90c2011-12-11 22:08:51 +0000170 socket = boost::shared_ptr<TSocket>(new TSocket(domain_socket));
Roger Meierca142b02011-06-07 17:59:07 +0000171 port = 0;
172 }
173 else {
Roger Meier611f90c2011-12-11 22:08:51 +0000174 socket = boost::shared_ptr<TSocket>(new TSocket(host, port));
Roger Meierca142b02011-06-07 17:59:07 +0000175 }
Bryan Duxburycd9aea12011-02-22 18:12:06 +0000176 }
Mark Sleea3302652006-10-25 19:03:32 +0000177
Roger Meierca142b02011-06-07 17:59:07 +0000178 if (transport_type.compare("http") == 0) {
Roger Meier611f90c2011-12-11 22:08:51 +0000179 boost::shared_ptr<TTransport> httpSocket(new THttpClient(socket, host, "/service"));
Roger Meierca142b02011-06-07 17:59:07 +0000180 transport = httpSocket;
181 } else if (transport_type.compare("framed") == 0){
Roger Meier611f90c2011-12-11 22:08:51 +0000182 boost::shared_ptr<TFramedTransport> framedSocket(new TFramedTransport(socket));
Mark Sleea3302652006-10-25 19:03:32 +0000183 transport = framedSocket;
Roger Meierca142b02011-06-07 17:59:07 +0000184 } else{
Roger Meier611f90c2011-12-11 22:08:51 +0000185 boost::shared_ptr<TBufferedTransport> bufferedSocket(new TBufferedTransport(socket));
Mark Sleea3302652006-10-25 19:03:32 +0000186 transport = bufferedSocket;
187 }
188
Roger Meierca142b02011-06-07 17:59:07 +0000189 if (protocol_type.compare("json") == 0) {
Roger Meier611f90c2011-12-11 22:08:51 +0000190 boost::shared_ptr<TProtocol> jsonProtocol(new TJSONProtocol(transport));
Roger Meierca142b02011-06-07 17:59:07 +0000191 protocol = jsonProtocol;
Roger Meier023192f2014-02-12 09:35:12 +0100192 } else if (protocol_type.compare("compact") == 0) {
193 boost::shared_ptr<TProtocol> compactProtocol(new TCompactProtocol(transport));
194 protocol = compactProtocol;
Roger Meierca142b02011-06-07 17:59:07 +0000195 } else{
Roger Meier611f90c2011-12-11 22:08:51 +0000196 boost::shared_ptr<TBinaryProtocol> binaryProtocol(new TBinaryProtocol(transport));
Roger Meierca142b02011-06-07 17:59:07 +0000197 protocol = binaryProtocol;
198 }
199
200 // Connection info
201 cout << "Connecting (" << transport_type << "/" << protocol_type << ") to: " << domain_socket;
202 if (port != 0) {
203 cout << host << ":" << port;
204 }
205 cout << endl;
206
Roger Meier7e056e72011-07-17 07:28:28 +0000207 if (transport_type.compare("evhttp") == 0) {
208 event_base *base = event_base_new();
209 cout << "Libevent Version: " << event_get_version() << endl;
210 cout << "Libevent Method: " << event_base_get_method(base) << endl;
211#if LIBEVENT_VERSION_NUMBER >= 0x02000000
212 cout << "Libevent Features: 0x" << hex << event_base_get_features(base) << endl;
213#endif
214
Roger Meier611f90c2011-12-11 22:08:51 +0000215 boost::shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());
Roger Meier7e056e72011-07-17 07:28:28 +0000216
Roger Meier611f90c2011-12-11 22:08:51 +0000217 boost::shared_ptr<TAsyncChannel> channel(new TEvhttpClientChannel(host.c_str(), "/", host.c_str(), port, base));
Roger Meier7e056e72011-07-17 07:28:28 +0000218 ThriftTestCobClient* client = new ThriftTestCobClient(channel, protocolFactory.get());
Roger Meier3faaedf2011-10-02 10:51:45 +0000219 client->testVoid(tr1::bind(testVoid_clientReturn, host.c_str(), port, base, protocolFactory.get(), std::tr1::placeholders::_1));
Jake Farrell5d02b802014-01-07 21:42:01 -0500220
Roger Meier7e056e72011-07-17 07:28:28 +0000221 event_base_loop(base, 0);
222 return 0;
223 }
224
225
Roger Meierca142b02011-06-07 17:59:07 +0000226 ThriftTestClient testClient(protocol);
Mark Sleed788b2e2006-09-07 01:26:35 +0000227
228 uint64_t time_min = 0;
229 uint64_t time_max = 0;
230 uint64_t time_tot = 0;
David Reiss0c90f6f2008-02-06 22:18:40 +0000231
Roger Meier4fce9602012-05-04 06:22:09 +0000232 int failCount = 0;
Mark Sleee8540632006-05-30 09:24:40 +0000233 int test = 0;
234 for (test = 0; test < numTests; ++test) {
Mark Slee95771002006-06-07 06:53:25 +0000235
Mark Slee95771002006-06-07 06:53:25 +0000236 try {
Mark Sleea3302652006-10-25 19:03:32 +0000237 transport->open();
Mark Slee95771002006-06-07 06:53:25 +0000238 } catch (TTransportException& ttx) {
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000239 printf("Connect failed: %s\n", ttx.what());
Roger Meier5b1e3c72011-12-08 13:20:12 +0000240 return 1;
Mark Sleee8540632006-05-30 09:24:40 +0000241 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000242
Mark Sleed788b2e2006-09-07 01:26:35 +0000243 /**
244 * CONNECT TEST
245 */
246 printf("Test #%d, connect %s:%d\n", test+1, host.c_str(), port);
Mark Slee95771002006-06-07 06:53:25 +0000247
248 uint64_t start = now();
David Reiss0c90f6f2008-02-06 22:18:40 +0000249
Mark Sleee8540632006-05-30 09:24:40 +0000250 /**
251 * VOID TEST
252 */
Mark Sleee129a2d2007-02-21 05:17:48 +0000253 try {
254 printf("testVoid()");
255 testClient.testVoid();
256 printf(" = void\n");
Roger Meier4fce9602012-05-04 06:22:09 +0000257 } catch (TApplicationException& tax) {
Mark Sleee129a2d2007-02-21 05:17:48 +0000258 printf("%s\n", tax.what());
Roger Meier4fce9602012-05-04 06:22:09 +0000259 failCount++;
Mark Sleee129a2d2007-02-21 05:17:48 +0000260 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000261
Mark Sleee8540632006-05-30 09:24:40 +0000262 /**
263 * STRING TEST
264 */
265 printf("testString(\"Test\")");
Mark Slee1921d202007-01-24 19:43:06 +0000266 string s;
267 testClient.testString(s, "Test");
Mark Sleee8540632006-05-30 09:24:40 +0000268 printf(" = \"%s\"\n", s.c_str());
Roger Meier4fce9602012-05-04 06:22:09 +0000269 if (s != "Test")
270 failCount++;
David Reiss0c90f6f2008-02-06 22:18:40 +0000271
Mark Sleee8540632006-05-30 09:24:40 +0000272 /**
273 * BYTE TEST
274 */
275 printf("testByte(1)");
276 uint8_t u8 = testClient.testByte(1);
277 printf(" = %d\n", (int)u8);
Roger Meier4fce9602012-05-04 06:22:09 +0000278 if (u8 != 1)
279 failCount++;
David Reiss0c90f6f2008-02-06 22:18:40 +0000280
Mark Sleee8540632006-05-30 09:24:40 +0000281 /**
282 * I32 TEST
283 */
284 printf("testI32(-1)");
285 int32_t i32 = testClient.testI32(-1);
286 printf(" = %d\n", i32);
Roger Meier4fce9602012-05-04 06:22:09 +0000287 if (i32 != -1)
288 failCount++;
Mark Sleee8540632006-05-30 09:24:40 +0000289
290 /**
Mark Sleee8540632006-05-30 09:24:40 +0000291 * I64 TEST
292 */
293 printf("testI64(-34359738368)");
Marc Slemkobf4fd192006-08-15 21:29:39 +0000294 int64_t i64 = testClient.testI64(-34359738368LL);
Roger Meier0e814802014-01-17 21:07:58 +0100295 printf(" = %" PRId64 "\n", i64);
Roger Meier4fce9602012-05-04 06:22:09 +0000296 if (i64 != -34359738368LL)
297 failCount++;
Mark Sleec98d0502006-09-06 02:42:25 +0000298 /**
299 * DOUBLE TEST
300 */
301 printf("testDouble(-5.2098523)");
302 double dub = testClient.testDouble(-5.2098523);
Roger Meiera8cef6e2011-07-17 18:55:59 +0000303 printf(" = %f\n", dub);
Roger Meier4fce9602012-05-04 06:22:09 +0000304 if ((dub - (-5.2098523)) > 0.001)
305 failCount++;
David Reiss0c90f6f2008-02-06 22:18:40 +0000306
Mark Sleee8540632006-05-30 09:24:40 +0000307 /**
308 * STRUCT TEST
309 */
Mark Slee6e536442006-06-30 18:28:50 +0000310 printf("testStruct({\"Zero\", 1, -3, -5})");
Mark Sleee8540632006-05-30 09:24:40 +0000311 Xtruct out;
312 out.string_thing = "Zero";
313 out.byte_thing = 1;
Mark Sleee8540632006-05-30 09:24:40 +0000314 out.i32_thing = -3;
Mark Sleee8540632006-05-30 09:24:40 +0000315 out.i64_thing = -5;
Mark Slee1921d202007-01-24 19:43:06 +0000316 Xtruct in;
317 testClient.testStruct(in, out);
Roger Meier0e814802014-01-17 21:07:58 +0100318 printf(" = {\"%s\", %d, %d, %" PRId64 "}\n",
Mark Sleee8540632006-05-30 09:24:40 +0000319 in.string_thing.c_str(),
320 (int)in.byte_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000321 in.i32_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000322 in.i64_thing);
Roger Meier4fce9602012-05-04 06:22:09 +0000323 if (in != out)
324 failCount++;
David Reiss0c90f6f2008-02-06 22:18:40 +0000325
Mark Sleee8540632006-05-30 09:24:40 +0000326 /**
327 * NESTED STRUCT TEST
328 */
Mark Slee6e536442006-06-30 18:28:50 +0000329 printf("testNest({1, {\"Zero\", 1, -3, -5}), 5}");
Mark Sleee8540632006-05-30 09:24:40 +0000330 Xtruct2 out2;
331 out2.byte_thing = 1;
332 out2.struct_thing = out;
333 out2.i32_thing = 5;
Mark Slee1921d202007-01-24 19:43:06 +0000334 Xtruct2 in2;
335 testClient.testNest(in2, out2);
Mark Sleee8540632006-05-30 09:24:40 +0000336 in = in2.struct_thing;
Roger Meier0e814802014-01-17 21:07:58 +0100337 printf(" = {%d, {\"%s\", %d, %d, %" PRId64 "}, %d}\n",
Mark Sleee8540632006-05-30 09:24:40 +0000338 in2.byte_thing,
339 in.string_thing.c_str(),
340 (int)in.byte_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000341 in.i32_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000342 in.i64_thing,
David Reiss0c90f6f2008-02-06 22:18:40 +0000343 in2.i32_thing);
Roger Meier4fce9602012-05-04 06:22:09 +0000344 if (in2 != out2)
345 failCount++;
Mark Sleee8540632006-05-30 09:24:40 +0000346
347 /**
348 * MAP TEST
349 */
350 map<int32_t,int32_t> mapout;
351 for (int32_t i = 0; i < 5; ++i) {
352 mapout.insert(make_pair(i, i-10));
353 }
354 printf("testMap({");
355 map<int32_t, int32_t>::const_iterator m_iter;
356 bool first = true;
357 for (m_iter = mapout.begin(); m_iter != mapout.end(); ++m_iter) {
358 if (first) {
359 first = false;
360 } else {
361 printf(", ");
362 }
363 printf("%d => %d", m_iter->first, m_iter->second);
364 }
365 printf("})");
Mark Slee1921d202007-01-24 19:43:06 +0000366 map<int32_t,int32_t> mapin;
367 testClient.testMap(mapin, mapout);
Mark Sleee8540632006-05-30 09:24:40 +0000368 printf(" = {");
369 first = true;
370 for (m_iter = mapin.begin(); m_iter != mapin.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("}\n");
Roger Meier4fce9602012-05-04 06:22:09 +0000379 if (mapin != mapout)
380 failCount++;
381
382 /**
383 * STRING MAP TEST
384 * missing
385 */
Mark Sleee8540632006-05-30 09:24:40 +0000386
387 /**
388 * SET TEST
389 */
390 set<int32_t> setout;
391 for (int32_t i = -2; i < 3; ++i) {
392 setout.insert(i);
393 }
394 printf("testSet({");
395 set<int32_t>::const_iterator s_iter;
396 first = true;
397 for (s_iter = setout.begin(); s_iter != setout.end(); ++s_iter) {
398 if (first) {
399 first = false;
400 } else {
401 printf(", ");
402 }
403 printf("%d", *s_iter);
404 }
405 printf("})");
Mark Slee1921d202007-01-24 19:43:06 +0000406 set<int32_t> setin;
407 testClient.testSet(setin, setout);
Mark Sleee8540632006-05-30 09:24:40 +0000408 printf(" = {");
409 first = true;
410 for (s_iter = setin.begin(); s_iter != setin.end(); ++s_iter) {
411 if (first) {
412 first = false;
413 } else {
414 printf(", ");
415 }
416 printf("%d", *s_iter);
417 }
418 printf("}\n");
Roger Meier4fce9602012-05-04 06:22:09 +0000419 if (setin != setout)
420 failCount++;
Mark Sleee8540632006-05-30 09:24:40 +0000421
422 /**
423 * LIST TEST
424 */
Mark Sleeb9acf982006-10-10 01:57:32 +0000425 vector<int32_t> listout;
Mark Sleee8540632006-05-30 09:24:40 +0000426 for (int32_t i = -2; i < 3; ++i) {
427 listout.push_back(i);
428 }
429 printf("testList({");
Mark Sleeb9acf982006-10-10 01:57:32 +0000430 vector<int32_t>::const_iterator l_iter;
Mark Sleee8540632006-05-30 09:24:40 +0000431 first = true;
432 for (l_iter = listout.begin(); l_iter != listout.end(); ++l_iter) {
433 if (first) {
434 first = false;
435 } else {
436 printf(", ");
437 }
438 printf("%d", *l_iter);
439 }
440 printf("})");
Mark Slee1921d202007-01-24 19:43:06 +0000441 vector<int32_t> listin;
442 testClient.testList(listin, listout);
Mark Sleee8540632006-05-30 09:24:40 +0000443 printf(" = {");
444 first = true;
445 for (l_iter = listin.begin(); l_iter != listin.end(); ++l_iter) {
446 if (first) {
447 first = false;
448 } else {
449 printf(", ");
450 }
451 printf("%d", *l_iter);
452 }
453 printf("}\n");
Roger Meier4fce9602012-05-04 06:22:09 +0000454 if (listin != listout)
455 failCount++;
Mark Sleee8540632006-05-30 09:24:40 +0000456
457 /**
458 * ENUM TEST
459 */
460 printf("testEnum(ONE)");
Bryan Duxbury833ae492010-09-27 17:26:02 +0000461 Numberz::type ret = testClient.testEnum(Numberz::ONE);
Mark Sleee8540632006-05-30 09:24:40 +0000462 printf(" = %d\n", ret);
Roger Meier4fce9602012-05-04 06:22:09 +0000463 if (ret != Numberz::ONE)
464 failCount++;
Mark Sleee8540632006-05-30 09:24:40 +0000465
466 printf("testEnum(TWO)");
Bryan Duxbury833ae492010-09-27 17:26:02 +0000467 ret = testClient.testEnum(Numberz::TWO);
Mark Sleee8540632006-05-30 09:24:40 +0000468 printf(" = %d\n", ret);
Roger Meier4fce9602012-05-04 06:22:09 +0000469 if (ret != Numberz::TWO)
470 failCount++;
Mark Sleee8540632006-05-30 09:24:40 +0000471
472 printf("testEnum(THREE)");
Bryan Duxbury833ae492010-09-27 17:26:02 +0000473 ret = testClient.testEnum(Numberz::THREE);
Mark Sleee8540632006-05-30 09:24:40 +0000474 printf(" = %d\n", ret);
Roger Meier4fce9602012-05-04 06:22:09 +0000475 if (ret != Numberz::THREE)
476 failCount++;
Mark Sleee8540632006-05-30 09:24:40 +0000477
478 printf("testEnum(FIVE)");
Bryan Duxbury833ae492010-09-27 17:26:02 +0000479 ret = testClient.testEnum(Numberz::FIVE);
Mark Sleee8540632006-05-30 09:24:40 +0000480 printf(" = %d\n", ret);
Roger Meier4fce9602012-05-04 06:22:09 +0000481 if (ret != Numberz::FIVE)
482 failCount++;
Mark Sleee8540632006-05-30 09:24:40 +0000483
484 printf("testEnum(EIGHT)");
Bryan Duxbury833ae492010-09-27 17:26:02 +0000485 ret = testClient.testEnum(Numberz::EIGHT);
Mark Sleee8540632006-05-30 09:24:40 +0000486 printf(" = %d\n", ret);
Roger Meier4fce9602012-05-04 06:22:09 +0000487 if (ret != Numberz::EIGHT)
488 failCount++;
Mark Sleee8540632006-05-30 09:24:40 +0000489
490 /**
491 * TYPEDEF TEST
492 */
493 printf("testTypedef(309858235082523)");
Marc Slemkobf4fd192006-08-15 21:29:39 +0000494 UserId uid = testClient.testTypedef(309858235082523LL);
Roger Meier0e814802014-01-17 21:07:58 +0100495 printf(" = %" PRId64 "\n", uid);
Roger Meier4fce9602012-05-04 06:22:09 +0000496 if (uid != 309858235082523LL)
497 failCount++;
Mark Sleee8540632006-05-30 09:24:40 +0000498
499 /**
500 * NESTED MAP TEST
501 */
502 printf("testMapMap(1)");
Mark Slee1921d202007-01-24 19:43:06 +0000503 map<int32_t, map<int32_t, int32_t> > mm;
504 testClient.testMapMap(mm, 1);
Mark Sleee8540632006-05-30 09:24:40 +0000505 printf(" = {");
506 map<int32_t, map<int32_t, int32_t> >::const_iterator mi;
507 for (mi = mm.begin(); mi != mm.end(); ++mi) {
508 printf("%d => {", mi->first);
509 map<int32_t, int32_t>::const_iterator mi2;
510 for (mi2 = mi->second.begin(); mi2 != mi->second.end(); ++mi2) {
511 printf("%d => %d, ", mi2->first, mi2->second);
512 }
513 printf("}, ");
514 }
515 printf("}\n");
516
517 /**
518 * INSANITY TEST
519 */
520 Insanity insane;
Bryan Duxbury833ae492010-09-27 17:26:02 +0000521 insane.userMap.insert(make_pair(Numberz::FIVE, 5000));
Mark Sleee8540632006-05-30 09:24:40 +0000522 Xtruct truck;
523 truck.string_thing = "Truck";
524 truck.byte_thing = 8;
Mark Sleee8540632006-05-30 09:24:40 +0000525 truck.i32_thing = 8;
Mark Sleee8540632006-05-30 09:24:40 +0000526 truck.i64_thing = 8;
527 insane.xtructs.push_back(truck);
528 printf("testInsanity()");
Bryan Duxbury833ae492010-09-27 17:26:02 +0000529 map<UserId, map<Numberz::type,Insanity> > whoa;
Mark Slee1921d202007-01-24 19:43:06 +0000530 testClient.testInsanity(whoa, insane);
Mark Sleee8540632006-05-30 09:24:40 +0000531 printf(" = {");
Bryan Duxbury833ae492010-09-27 17:26:02 +0000532 map<UserId, map<Numberz::type,Insanity> >::const_iterator i_iter;
Mark Sleee8540632006-05-30 09:24:40 +0000533 for (i_iter = whoa.begin(); i_iter != whoa.end(); ++i_iter) {
Roger Meier0e814802014-01-17 21:07:58 +0100534 printf("%" PRId64 " => {", i_iter->first);
Bryan Duxbury833ae492010-09-27 17:26:02 +0000535 map<Numberz::type,Insanity>::const_iterator i2_iter;
Mark Sleee8540632006-05-30 09:24:40 +0000536 for (i2_iter = i_iter->second.begin();
537 i2_iter != i_iter->second.end();
538 ++i2_iter) {
539 printf("%d => {", i2_iter->first);
Bryan Duxbury833ae492010-09-27 17:26:02 +0000540 map<Numberz::type, UserId> userMap = i2_iter->second.userMap;
541 map<Numberz::type, UserId>::const_iterator um;
Mark Sleee8540632006-05-30 09:24:40 +0000542 printf("{");
543 for (um = userMap.begin(); um != userMap.end(); ++um) {
Roger Meier0e814802014-01-17 21:07:58 +0100544 printf("%d => %" PRId64 ", ", um->first, um->second);
Mark Sleee8540632006-05-30 09:24:40 +0000545 }
546 printf("}, ");
547
Mark Sleeb9acf982006-10-10 01:57:32 +0000548 vector<Xtruct> xtructs = i2_iter->second.xtructs;
549 vector<Xtruct>::const_iterator x;
Mark Sleee8540632006-05-30 09:24:40 +0000550 printf("{");
551 for (x = xtructs.begin(); x != xtructs.end(); ++x) {
Roger Meier0e814802014-01-17 21:07:58 +0100552 printf("{\"%s\", %d, %d, %" PRId64 "}, ",
Mark Sleee8540632006-05-30 09:24:40 +0000553 x->string_thing.c_str(),
554 (int)x->byte_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000555 x->i32_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000556 x->i64_thing);
557 }
558 printf("}");
559
560 printf("}, ");
561 }
562 printf("}, ");
563 }
564 printf("}\n");
565
Marc Slemko71d4e472006-08-15 22:34:04 +0000566 /* test exception */
Mark Slee95771002006-06-07 06:53:25 +0000567
Marc Slemkobf4fd192006-08-15 21:29:39 +0000568 try {
Marc Slemko71d4e472006-08-15 22:34:04 +0000569 printf("testClient.testException(\"Xception\") =>");
570 testClient.testException("Xception");
571 printf(" void\nFAILURE\n");
Roger Meier4fce9602012-05-04 06:22:09 +0000572 failCount++;
David Reiss0c90f6f2008-02-06 22:18:40 +0000573
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000574 } catch(Xception& e) {
Marc Slemko71d4e472006-08-15 22:34:04 +0000575 printf(" {%u, \"%s\"}\n", e.errorCode, e.message.c_str());
Marc Slemkobf4fd192006-08-15 21:29:39 +0000576 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000577
Marc Slemkobf4fd192006-08-15 21:29:39 +0000578 try {
Roger Meier99b36722012-05-03 21:21:43 +0000579 printf("testClient.testException(\"TException\") =>");
580 testClient.testException("TException");
Roger Meierf50df7f2012-05-02 22:49:55 +0000581 printf(" void\nFAILURE\n");
Roger Meier4fce9602012-05-04 06:22:09 +0000582 failCount++;
Roger Meierf50df7f2012-05-02 22:49:55 +0000583
Jake Farrell5d02b802014-01-07 21:42:01 -0500584 } catch(const TException&) {
Roger Meierf50df7f2012-05-02 22:49:55 +0000585 printf(" Caught TException\n");
586 }
587
588 try {
Marc Slemko71d4e472006-08-15 22:34:04 +0000589 printf("testClient.testException(\"success\") =>");
590 testClient.testException("success");
591 printf(" void\n");
592 } catch(...) {
593 printf(" exception\nFAILURE\n");
Roger Meier4fce9602012-05-04 06:22:09 +0000594 failCount++;
Marc Slemko71d4e472006-08-15 22:34:04 +0000595 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000596
Marc Slemko71d4e472006-08-15 22:34:04 +0000597 /* test multi exception */
David Reiss0c90f6f2008-02-06 22:18:40 +0000598
Marc Slemko71d4e472006-08-15 22:34:04 +0000599 try {
600 printf("testClient.testMultiException(\"Xception\", \"test 1\") =>");
Mark Slee1921d202007-01-24 19:43:06 +0000601 Xtruct result;
602 testClient.testMultiException(result, "Xception", "test 1");
Marc Slemko71d4e472006-08-15 22:34:04 +0000603 printf(" result\nFAILURE\n");
Roger Meier4fce9602012-05-04 06:22:09 +0000604 failCount++;
Mark Sleed3d733a2006-09-01 22:19:06 +0000605 } catch(Xception& e) {
Marc Slemko71d4e472006-08-15 22:34:04 +0000606 printf(" {%u, \"%s\"}\n", e.errorCode, e.message.c_str());
607 }
608
609 try {
610 printf("testClient.testMultiException(\"Xception2\", \"test 2\") =>");
Mark Slee1921d202007-01-24 19:43:06 +0000611 Xtruct result;
612 testClient.testMultiException(result, "Xception2", "test 2");
Marc Slemko71d4e472006-08-15 22:34:04 +0000613 printf(" result\nFAILURE\n");
Roger Meier4fce9602012-05-04 06:22:09 +0000614 failCount++;
David Reiss0c90f6f2008-02-06 22:18:40 +0000615
Mark Sleed3d733a2006-09-01 22:19:06 +0000616 } catch(Xception2& e) {
Marc Slemko71d4e472006-08-15 22:34:04 +0000617 printf(" {%u, {\"%s\"}}\n", e.errorCode, e.struct_thing.string_thing.c_str());
Marc Slemkobf4fd192006-08-15 21:29:39 +0000618 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000619
Marc Slemko71d4e472006-08-15 22:34:04 +0000620 try {
621 printf("testClient.testMultiException(\"success\", \"test 3\") =>");
Mark Slee1921d202007-01-24 19:43:06 +0000622 Xtruct result;
623 testClient.testMultiException(result, "success", "test 3");
Marc Slemko71d4e472006-08-15 22:34:04 +0000624 printf(" {{\"%s\"}}\n", result.string_thing.c_str());
625 } catch(...) {
626 printf(" exception\nFAILURE\n");
Roger Meier4fce9602012-05-04 06:22:09 +0000627 failCount++;
Marc Slemko71d4e472006-08-15 22:34:04 +0000628 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000629
David Reissc51986f2009-03-24 20:01:25 +0000630 /* test oneway void */
David Reiss2ab6fe82008-02-18 02:11:44 +0000631 {
Jake Farrell5d02b802014-01-07 21:42:01 -0500632 printf("testClient.testOneway(1) =>");
David Reiss6ce401d2009-03-24 20:01:58 +0000633 uint64_t startOneway = now();
Jake Farrell5d02b802014-01-07 21:42:01 -0500634 testClient.testOneway(1);
David Reiss6ce401d2009-03-24 20:01:58 +0000635 uint64_t elapsed = now() - startOneway;
David Reiss2ab6fe82008-02-18 02:11:44 +0000636 if (elapsed > 200 * 1000) { // 0.2 seconds
637 printf(" FAILURE - took %.2f ms\n", (double)elapsed/1000.0);
Roger Meier4fce9602012-05-04 06:22:09 +0000638 failCount++;
David Reiss2ab6fe82008-02-18 02:11:44 +0000639 } else {
640 printf(" success - took %.2f ms\n", (double)elapsed/1000.0);
641 }
642 }
643
David Reiss2845b522008-02-18 02:11:52 +0000644 /**
David Reissc51986f2009-03-24 20:01:25 +0000645 * redo a simple test after the oneway to make sure we aren't "off by one" --
646 * if the server treated oneway void like normal void, this next test will
David Reiss2845b522008-02-18 02:11:52 +0000647 * fail since it will get the void confirmation rather than the correct
648 * result. In this circumstance, the client will throw the exception:
649 *
650 * TApplicationException: Wrong method namea
651 */
652 /**
653 * I32 TEST
654 */
655 printf("re-test testI32(-1)");
656 i32 = testClient.testI32(-1);
657 printf(" = %d\n", i32);
Roger Meier4fce9602012-05-04 06:22:09 +0000658 if (i32 != -1)
659 failCount++;
David Reiss2845b522008-02-18 02:11:52 +0000660
661
Marc Slemkobf4fd192006-08-15 21:29:39 +0000662 uint64_t stop = now();
Mark Sleed788b2e2006-09-07 01:26:35 +0000663 uint64_t tot = stop-start;
664
Roger Meier0e814802014-01-17 21:07:58 +0100665 printf("Total time: %" PRIu64 " us\n", stop-start);
David Reiss0c90f6f2008-02-06 22:18:40 +0000666
Mark Sleed788b2e2006-09-07 01:26:35 +0000667 time_tot += tot;
668 if (time_min == 0 || tot < time_min) {
669 time_min = tot;
670 }
671 if (tot > time_max) {
672 time_max = tot;
673 }
674
Mark Sleea3302652006-10-25 19:03:32 +0000675 transport->close();
Mark Sleee8540632006-05-30 09:24:40 +0000676 }
677
Marc Slemko6be374b2006-08-04 03:16:25 +0000678 // printf("\nSocket syscalls: %u", g_socket_syscalls);
Mark Sleee8540632006-05-30 09:24:40 +0000679 printf("\nAll tests done.\n");
Mark Sleed788b2e2006-09-07 01:26:35 +0000680
681 uint64_t time_avg = time_tot / numTests;
682
Roger Meier0e814802014-01-17 21:07:58 +0100683 printf("Min time: %" PRIu64 " us\n", time_min);
684 printf("Max time: %" PRIu64 " us\n", time_max);
685 printf("Avg time: %" PRIu64 " us\n", time_avg);
Mark Sleed788b2e2006-09-07 01:26:35 +0000686
Roger Meier4fce9602012-05-04 06:22:09 +0000687 return failCount;
Mark Sleee8540632006-05-30 09:24:40 +0000688}