David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 1 | /* |
| 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 Meier | d3b9dca | 2011-06-24 14:01:10 +0000 | [diff] [blame] | 20 | #define __STDC_FORMAT_MACROS |
| 21 | #include <inttypes.h> |
| 22 | |
Roger Meier | ca142b0 | 2011-06-07 17:59:07 +0000 | [diff] [blame] | 23 | #include <iostream> |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 24 | #include <unistd.h> |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 25 | #include <sys/time.h> |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 26 | #include <protocol/TBinaryProtocol.h> |
Roger Meier | ca142b0 | 2011-06-07 17:59:07 +0000 | [diff] [blame] | 27 | #include <protocol/TJSONProtocol.h> |
| 28 | #include <transport/THttpClient.h> |
Mark Slee | a330265 | 2006-10-25 19:03:32 +0000 | [diff] [blame] | 29 | #include <transport/TTransportUtils.h> |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 30 | #include <transport/TSocket.h> |
Bryan Duxbury | cd9aea1 | 2011-02-22 18:12:06 +0000 | [diff] [blame] | 31 | #include <transport/TSSLSocket.h> |
Roger Meier | 7e056e7 | 2011-07-17 07:28:28 +0000 | [diff] [blame^] | 32 | #include <async/TEvhttpClientChannel.h> |
| 33 | #include <server/TNonblockingServer.h> // <event.h> |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 34 | |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 35 | #include <boost/shared_ptr.hpp> |
Roger Meier | ca142b0 | 2011-06-07 17:59:07 +0000 | [diff] [blame] | 36 | #include <boost/program_options.hpp> |
Roger Meier | 7e056e7 | 2011-07-17 07:28:28 +0000 | [diff] [blame^] | 37 | #include <tr1/functional> |
Roger Meier | ca142b0 | 2011-06-07 17:59:07 +0000 | [diff] [blame] | 38 | |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 39 | #include "ThriftTest.h" |
| 40 | |
| 41 | using namespace boost; |
| 42 | using namespace std; |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 43 | using namespace apache::thrift; |
| 44 | using namespace apache::thrift::protocol; |
| 45 | using namespace apache::thrift::transport; |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 46 | using namespace thrift::test; |
Roger Meier | 7e056e7 | 2011-07-17 07:28:28 +0000 | [diff] [blame^] | 47 | using namespace apache::thrift::async; |
| 48 | |
| 49 | using std::tr1::placeholders::_1; |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 50 | |
| 51 | //extern uint32_t g_socket_syscalls; |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 52 | |
| 53 | // Current time, microseconds since the epoch |
| 54 | uint64_t now() |
| 55 | { |
Roger Meier | 5f9614c | 2010-11-21 16:59:05 +0000 | [diff] [blame] | 56 | int64_t ret; |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 57 | struct timeval tv; |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 58 | |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 59 | gettimeofday(&tv, NULL); |
| 60 | ret = tv.tv_sec; |
| 61 | ret = ret*1000*1000 + tv.tv_usec; |
| 62 | return ret; |
| 63 | } |
| 64 | |
Roger Meier | 7e056e7 | 2011-07-17 07:28:28 +0000 | [diff] [blame^] | 65 | static void testString_clientReturn(const char* host, int port, event_base *base, TProtocolFactory* protocolFactory, ThriftTestCobClient* client) { |
| 66 | try { |
| 67 | string s; |
| 68 | client->recv_testString(s); |
| 69 | cout << "testString: " << s << endl; |
| 70 | } catch (TException& exn) { |
| 71 | cout << "Error: " << exn.what() << endl; |
| 72 | } |
| 73 | |
| 74 | event_base_loopbreak(base); // end test |
| 75 | } |
| 76 | |
| 77 | static void testVoid_clientReturn(const char* host, int port, event_base *base, TProtocolFactory* protocolFactory, ThriftTestCobClient* client) { |
| 78 | try { |
| 79 | client->recv_testVoid(); |
| 80 | cout << "testVoid" << endl; |
| 81 | |
| 82 | // next test |
| 83 | delete client; |
| 84 | shared_ptr<TAsyncChannel> channel(new TEvhttpClientChannel(host, "/", host, port, base)); |
| 85 | client = new ThriftTestCobClient(channel, protocolFactory); |
| 86 | client->testString(tr1::bind(testString_clientReturn, host, port, base, protocolFactory, _1), "Test"); |
| 87 | } catch (TException& exn) { |
| 88 | cout << "Error: " << exn.what() << endl; |
| 89 | } |
| 90 | } |
| 91 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 92 | int main(int argc, char** argv) { |
| 93 | string host = "localhost"; |
| 94 | int port = 9090; |
| 95 | int numTests = 1; |
Bryan Duxbury | cd9aea1 | 2011-02-22 18:12:06 +0000 | [diff] [blame] | 96 | bool ssl = false; |
Roger Meier | ca142b0 | 2011-06-07 17:59:07 +0000 | [diff] [blame] | 97 | string transport_type = "buffered"; |
| 98 | string protocol_type = "binary"; |
| 99 | string domain_socket = ""; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 100 | |
Roger Meier | ca142b0 | 2011-06-07 17:59:07 +0000 | [diff] [blame] | 101 | program_options::options_description desc("Allowed options"); |
| 102 | desc.add_options() |
| 103 | ("help,h", "produce help message") |
| 104 | ("host", program_options::value<string>(&host)->default_value(host), "Host to connect") |
| 105 | ("port", program_options::value<int>(&port)->default_value(port), "Port number to connect") |
| 106 | ("domain-socket", program_options::value<string>(&domain_socket)->default_value(domain_socket), "Domain Socket (e.g. /tmp/ThriftTest.thrift), instead of host and port") |
Roger Meier | 7e056e7 | 2011-07-17 07:28:28 +0000 | [diff] [blame^] | 107 | ("transport", program_options::value<string>(&transport_type)->default_value(transport_type), "Transport: buffered, framed, http, evhttp") |
Roger Meier | ca142b0 | 2011-06-07 17:59:07 +0000 | [diff] [blame] | 108 | ("protocol", program_options::value<string>(&protocol_type)->default_value(protocol_type), "Protocol: binary, json") |
| 109 | ("ssl", "Encrypted Transport using SSL") |
| 110 | ("testloops,n", program_options::value<int>(&numTests)->default_value(numTests), "Number of Tests") |
| 111 | ; |
| 112 | |
| 113 | program_options::variables_map vm; |
| 114 | program_options::store(program_options::parse_command_line(argc, argv, desc), vm); |
| 115 | program_options::notify(vm); |
| 116 | |
| 117 | if (vm.count("help")) { |
| 118 | cout << desc << "\n"; |
| 119 | return 1; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 120 | } |
Mark Slee | a330265 | 2006-10-25 19:03:32 +0000 | [diff] [blame] | 121 | |
Roger Meier | ca142b0 | 2011-06-07 17:59:07 +0000 | [diff] [blame] | 122 | try { |
| 123 | if (!protocol_type.empty()) { |
| 124 | if (protocol_type == "binary") { |
| 125 | } else if (protocol_type == "json") { |
| 126 | } else { |
| 127 | throw invalid_argument("Unknown protocol type "+protocol_type); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | if (!transport_type.empty()) { |
| 132 | if (transport_type == "buffered") { |
| 133 | } else if (transport_type == "framed") { |
| 134 | } else if (transport_type == "http") { |
Roger Meier | 7e056e7 | 2011-07-17 07:28:28 +0000 | [diff] [blame^] | 135 | } else if (transport_type == "evhttp") { |
Roger Meier | ca142b0 | 2011-06-07 17:59:07 +0000 | [diff] [blame] | 136 | } else { |
| 137 | throw invalid_argument("Unknown transport type "+transport_type); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | } catch (std::exception& e) { |
| 142 | cerr << e.what() << endl; |
| 143 | cout << desc << "\n"; |
| 144 | return 1; |
| 145 | } |
| 146 | |
| 147 | if (vm.count("ssl")) { |
| 148 | ssl = true; |
| 149 | } |
| 150 | |
| 151 | shared_ptr<TTransport> transport; |
| 152 | shared_ptr<TProtocol> protocol; |
| 153 | |
Bryan Duxbury | cd9aea1 | 2011-02-22 18:12:06 +0000 | [diff] [blame] | 154 | shared_ptr<TSocket> socket; |
| 155 | shared_ptr<TSSLSocketFactory> factory; |
Roger Meier | ca142b0 | 2011-06-07 17:59:07 +0000 | [diff] [blame] | 156 | |
Bryan Duxbury | cd9aea1 | 2011-02-22 18:12:06 +0000 | [diff] [blame] | 157 | if (ssl) { |
| 158 | factory = shared_ptr<TSSLSocketFactory>(new TSSLSocketFactory()); |
| 159 | factory->ciphers("ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH"); |
| 160 | factory->loadTrustedCertificates("./trusted-ca-certificate.pem"); |
| 161 | factory->authenticate(true); |
| 162 | socket = factory->createSocket(host, port); |
| 163 | } else { |
Roger Meier | ca142b0 | 2011-06-07 17:59:07 +0000 | [diff] [blame] | 164 | if (domain_socket != "") { |
| 165 | socket = shared_ptr<TSocket>(new TSocket(domain_socket)); |
| 166 | port = 0; |
| 167 | } |
| 168 | else { |
| 169 | socket = shared_ptr<TSocket>(new TSocket(host, port)); |
| 170 | } |
Bryan Duxbury | cd9aea1 | 2011-02-22 18:12:06 +0000 | [diff] [blame] | 171 | } |
Mark Slee | a330265 | 2006-10-25 19:03:32 +0000 | [diff] [blame] | 172 | |
Roger Meier | ca142b0 | 2011-06-07 17:59:07 +0000 | [diff] [blame] | 173 | if (transport_type.compare("http") == 0) { |
| 174 | shared_ptr<TTransport> httpSocket(new THttpClient(socket, host, "/service")); |
| 175 | transport = httpSocket; |
| 176 | } else if (transport_type.compare("framed") == 0){ |
Mark Slee | a330265 | 2006-10-25 19:03:32 +0000 | [diff] [blame] | 177 | shared_ptr<TFramedTransport> framedSocket(new TFramedTransport(socket)); |
Mark Slee | a330265 | 2006-10-25 19:03:32 +0000 | [diff] [blame] | 178 | transport = framedSocket; |
Roger Meier | ca142b0 | 2011-06-07 17:59:07 +0000 | [diff] [blame] | 179 | } else{ |
Mark Slee | a330265 | 2006-10-25 19:03:32 +0000 | [diff] [blame] | 180 | shared_ptr<TBufferedTransport> bufferedSocket(new TBufferedTransport(socket)); |
| 181 | transport = bufferedSocket; |
| 182 | } |
| 183 | |
Roger Meier | ca142b0 | 2011-06-07 17:59:07 +0000 | [diff] [blame] | 184 | if (protocol_type.compare("json") == 0) { |
| 185 | shared_ptr<TProtocol> jsonProtocol(new TJSONProtocol(transport)); |
| 186 | protocol = jsonProtocol; |
| 187 | } else{ |
| 188 | shared_ptr<TBinaryProtocol> binaryProtocol(new TBinaryProtocol(transport)); |
| 189 | protocol = binaryProtocol; |
| 190 | } |
| 191 | |
| 192 | // Connection info |
| 193 | cout << "Connecting (" << transport_type << "/" << protocol_type << ") to: " << domain_socket; |
| 194 | if (port != 0) { |
| 195 | cout << host << ":" << port; |
| 196 | } |
| 197 | cout << endl; |
| 198 | |
Roger Meier | 7e056e7 | 2011-07-17 07:28:28 +0000 | [diff] [blame^] | 199 | if (transport_type.compare("evhttp") == 0) { |
| 200 | event_base *base = event_base_new(); |
| 201 | cout << "Libevent Version: " << event_get_version() << endl; |
| 202 | cout << "Libevent Method: " << event_base_get_method(base) << endl; |
| 203 | #if LIBEVENT_VERSION_NUMBER >= 0x02000000 |
| 204 | cout << "Libevent Features: 0x" << hex << event_base_get_features(base) << endl; |
| 205 | #endif |
| 206 | |
| 207 | shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory()); |
| 208 | |
| 209 | shared_ptr<TAsyncChannel> channel(new TEvhttpClientChannel(host.c_str(), "/", host.c_str(), port, base)); |
| 210 | ThriftTestCobClient* client = new ThriftTestCobClient(channel, protocolFactory.get()); |
| 211 | client->testVoid(tr1::bind(testVoid_clientReturn, host.c_str(), port, base, protocolFactory.get(), _1)); |
| 212 | |
| 213 | event_base_loop(base, 0); |
| 214 | return 0; |
| 215 | } |
| 216 | |
| 217 | |
Roger Meier | ca142b0 | 2011-06-07 17:59:07 +0000 | [diff] [blame] | 218 | ThriftTestClient testClient(protocol); |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 219 | |
| 220 | uint64_t time_min = 0; |
| 221 | uint64_t time_max = 0; |
| 222 | uint64_t time_tot = 0; |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 223 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 224 | int test = 0; |
| 225 | for (test = 0; test < numTests; ++test) { |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 226 | |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 227 | try { |
Mark Slee | a330265 | 2006-10-25 19:03:32 +0000 | [diff] [blame] | 228 | transport->open(); |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 229 | } catch (TTransportException& ttx) { |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 230 | printf("Connect failed: %s\n", ttx.what()); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 231 | continue; |
| 232 | } |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 233 | |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 234 | /** |
| 235 | * CONNECT TEST |
| 236 | */ |
| 237 | printf("Test #%d, connect %s:%d\n", test+1, host.c_str(), port); |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 238 | |
| 239 | uint64_t start = now(); |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 240 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 241 | /** |
| 242 | * VOID TEST |
| 243 | */ |
Mark Slee | e129a2d | 2007-02-21 05:17:48 +0000 | [diff] [blame] | 244 | try { |
| 245 | printf("testVoid()"); |
| 246 | testClient.testVoid(); |
| 247 | printf(" = void\n"); |
| 248 | } catch (TApplicationException tax) { |
| 249 | printf("%s\n", tax.what()); |
| 250 | } |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 251 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 252 | /** |
| 253 | * STRING TEST |
| 254 | */ |
| 255 | printf("testString(\"Test\")"); |
Mark Slee | 1921d20 | 2007-01-24 19:43:06 +0000 | [diff] [blame] | 256 | string s; |
| 257 | testClient.testString(s, "Test"); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 258 | printf(" = \"%s\"\n", s.c_str()); |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 259 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 260 | /** |
| 261 | * BYTE TEST |
| 262 | */ |
| 263 | printf("testByte(1)"); |
| 264 | uint8_t u8 = testClient.testByte(1); |
| 265 | printf(" = %d\n", (int)u8); |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 266 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 267 | /** |
| 268 | * I32 TEST |
| 269 | */ |
| 270 | printf("testI32(-1)"); |
| 271 | int32_t i32 = testClient.testI32(-1); |
| 272 | printf(" = %d\n", i32); |
| 273 | |
| 274 | /** |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 275 | * I64 TEST |
| 276 | */ |
| 277 | printf("testI64(-34359738368)"); |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 278 | int64_t i64 = testClient.testI64(-34359738368LL); |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 279 | printf(" = %"PRId64"\n", i64); |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 280 | |
| 281 | /** |
| 282 | * DOUBLE TEST |
| 283 | */ |
| 284 | printf("testDouble(-5.2098523)"); |
| 285 | double dub = testClient.testDouble(-5.2098523); |
| 286 | printf(" = %lf\n", dub); |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 287 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 288 | /** |
| 289 | * STRUCT TEST |
| 290 | */ |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 291 | printf("testStruct({\"Zero\", 1, -3, -5})"); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 292 | Xtruct out; |
| 293 | out.string_thing = "Zero"; |
| 294 | out.byte_thing = 1; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 295 | out.i32_thing = -3; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 296 | out.i64_thing = -5; |
Mark Slee | 1921d20 | 2007-01-24 19:43:06 +0000 | [diff] [blame] | 297 | Xtruct in; |
| 298 | testClient.testStruct(in, out); |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 299 | printf(" = {\"%s\", %d, %d, %"PRId64"}\n", |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 300 | in.string_thing.c_str(), |
| 301 | (int)in.byte_thing, |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 302 | in.i32_thing, |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 303 | in.i64_thing); |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 304 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 305 | /** |
| 306 | * NESTED STRUCT TEST |
| 307 | */ |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 308 | printf("testNest({1, {\"Zero\", 1, -3, -5}), 5}"); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 309 | Xtruct2 out2; |
| 310 | out2.byte_thing = 1; |
| 311 | out2.struct_thing = out; |
| 312 | out2.i32_thing = 5; |
Mark Slee | 1921d20 | 2007-01-24 19:43:06 +0000 | [diff] [blame] | 313 | Xtruct2 in2; |
| 314 | testClient.testNest(in2, out2); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 315 | in = in2.struct_thing; |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 316 | printf(" = {%d, {\"%s\", %d, %d, %"PRId64"}, %d}\n", |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 317 | in2.byte_thing, |
| 318 | in.string_thing.c_str(), |
| 319 | (int)in.byte_thing, |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 320 | in.i32_thing, |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 321 | in.i64_thing, |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 322 | in2.i32_thing); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 323 | |
| 324 | /** |
| 325 | * MAP TEST |
| 326 | */ |
| 327 | map<int32_t,int32_t> mapout; |
| 328 | for (int32_t i = 0; i < 5; ++i) { |
| 329 | mapout.insert(make_pair(i, i-10)); |
| 330 | } |
| 331 | printf("testMap({"); |
| 332 | map<int32_t, int32_t>::const_iterator m_iter; |
| 333 | bool first = true; |
| 334 | for (m_iter = mapout.begin(); m_iter != mapout.end(); ++m_iter) { |
| 335 | if (first) { |
| 336 | first = false; |
| 337 | } else { |
| 338 | printf(", "); |
| 339 | } |
| 340 | printf("%d => %d", m_iter->first, m_iter->second); |
| 341 | } |
| 342 | printf("})"); |
Mark Slee | 1921d20 | 2007-01-24 19:43:06 +0000 | [diff] [blame] | 343 | map<int32_t,int32_t> mapin; |
| 344 | testClient.testMap(mapin, mapout); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 345 | printf(" = {"); |
| 346 | first = true; |
| 347 | for (m_iter = mapin.begin(); m_iter != mapin.end(); ++m_iter) { |
| 348 | if (first) { |
| 349 | first = false; |
| 350 | } else { |
| 351 | printf(", "); |
| 352 | } |
| 353 | printf("%d => %d", m_iter->first, m_iter->second); |
| 354 | } |
| 355 | printf("}\n"); |
| 356 | |
| 357 | /** |
| 358 | * SET TEST |
| 359 | */ |
| 360 | set<int32_t> setout; |
| 361 | for (int32_t i = -2; i < 3; ++i) { |
| 362 | setout.insert(i); |
| 363 | } |
| 364 | printf("testSet({"); |
| 365 | set<int32_t>::const_iterator s_iter; |
| 366 | first = true; |
| 367 | for (s_iter = setout.begin(); s_iter != setout.end(); ++s_iter) { |
| 368 | if (first) { |
| 369 | first = false; |
| 370 | } else { |
| 371 | printf(", "); |
| 372 | } |
| 373 | printf("%d", *s_iter); |
| 374 | } |
| 375 | printf("})"); |
Mark Slee | 1921d20 | 2007-01-24 19:43:06 +0000 | [diff] [blame] | 376 | set<int32_t> setin; |
| 377 | testClient.testSet(setin, setout); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 378 | printf(" = {"); |
| 379 | first = true; |
| 380 | for (s_iter = setin.begin(); s_iter != setin.end(); ++s_iter) { |
| 381 | if (first) { |
| 382 | first = false; |
| 383 | } else { |
| 384 | printf(", "); |
| 385 | } |
| 386 | printf("%d", *s_iter); |
| 387 | } |
| 388 | printf("}\n"); |
| 389 | |
| 390 | /** |
| 391 | * LIST TEST |
| 392 | */ |
Mark Slee | b9acf98 | 2006-10-10 01:57:32 +0000 | [diff] [blame] | 393 | vector<int32_t> listout; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 394 | for (int32_t i = -2; i < 3; ++i) { |
| 395 | listout.push_back(i); |
| 396 | } |
| 397 | printf("testList({"); |
Mark Slee | b9acf98 | 2006-10-10 01:57:32 +0000 | [diff] [blame] | 398 | vector<int32_t>::const_iterator l_iter; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 399 | first = true; |
| 400 | for (l_iter = listout.begin(); l_iter != listout.end(); ++l_iter) { |
| 401 | if (first) { |
| 402 | first = false; |
| 403 | } else { |
| 404 | printf(", "); |
| 405 | } |
| 406 | printf("%d", *l_iter); |
| 407 | } |
| 408 | printf("})"); |
Mark Slee | 1921d20 | 2007-01-24 19:43:06 +0000 | [diff] [blame] | 409 | vector<int32_t> listin; |
| 410 | testClient.testList(listin, listout); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 411 | printf(" = {"); |
| 412 | first = true; |
| 413 | for (l_iter = listin.begin(); l_iter != listin.end(); ++l_iter) { |
| 414 | if (first) { |
| 415 | first = false; |
| 416 | } else { |
| 417 | printf(", "); |
| 418 | } |
| 419 | printf("%d", *l_iter); |
| 420 | } |
| 421 | printf("}\n"); |
| 422 | |
| 423 | /** |
| 424 | * ENUM TEST |
| 425 | */ |
| 426 | printf("testEnum(ONE)"); |
Bryan Duxbury | 833ae49 | 2010-09-27 17:26:02 +0000 | [diff] [blame] | 427 | Numberz::type ret = testClient.testEnum(Numberz::ONE); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 428 | printf(" = %d\n", ret); |
| 429 | |
| 430 | printf("testEnum(TWO)"); |
Bryan Duxbury | 833ae49 | 2010-09-27 17:26:02 +0000 | [diff] [blame] | 431 | ret = testClient.testEnum(Numberz::TWO); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 432 | printf(" = %d\n", ret); |
| 433 | |
| 434 | printf("testEnum(THREE)"); |
Bryan Duxbury | 833ae49 | 2010-09-27 17:26:02 +0000 | [diff] [blame] | 435 | ret = testClient.testEnum(Numberz::THREE); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 436 | printf(" = %d\n", ret); |
| 437 | |
| 438 | printf("testEnum(FIVE)"); |
Bryan Duxbury | 833ae49 | 2010-09-27 17:26:02 +0000 | [diff] [blame] | 439 | ret = testClient.testEnum(Numberz::FIVE); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 440 | printf(" = %d\n", ret); |
| 441 | |
| 442 | printf("testEnum(EIGHT)"); |
Bryan Duxbury | 833ae49 | 2010-09-27 17:26:02 +0000 | [diff] [blame] | 443 | ret = testClient.testEnum(Numberz::EIGHT); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 444 | printf(" = %d\n", ret); |
| 445 | |
| 446 | /** |
| 447 | * TYPEDEF TEST |
| 448 | */ |
| 449 | printf("testTypedef(309858235082523)"); |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 450 | UserId uid = testClient.testTypedef(309858235082523LL); |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 451 | printf(" = %"PRId64"\n", uid); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 452 | |
| 453 | /** |
| 454 | * NESTED MAP TEST |
| 455 | */ |
| 456 | printf("testMapMap(1)"); |
Mark Slee | 1921d20 | 2007-01-24 19:43:06 +0000 | [diff] [blame] | 457 | map<int32_t, map<int32_t, int32_t> > mm; |
| 458 | testClient.testMapMap(mm, 1); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 459 | printf(" = {"); |
| 460 | map<int32_t, map<int32_t, int32_t> >::const_iterator mi; |
| 461 | for (mi = mm.begin(); mi != mm.end(); ++mi) { |
| 462 | printf("%d => {", mi->first); |
| 463 | map<int32_t, int32_t>::const_iterator mi2; |
| 464 | for (mi2 = mi->second.begin(); mi2 != mi->second.end(); ++mi2) { |
| 465 | printf("%d => %d, ", mi2->first, mi2->second); |
| 466 | } |
| 467 | printf("}, "); |
| 468 | } |
| 469 | printf("}\n"); |
| 470 | |
| 471 | /** |
| 472 | * INSANITY TEST |
| 473 | */ |
| 474 | Insanity insane; |
Bryan Duxbury | 833ae49 | 2010-09-27 17:26:02 +0000 | [diff] [blame] | 475 | insane.userMap.insert(make_pair(Numberz::FIVE, 5000)); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 476 | Xtruct truck; |
| 477 | truck.string_thing = "Truck"; |
| 478 | truck.byte_thing = 8; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 479 | truck.i32_thing = 8; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 480 | truck.i64_thing = 8; |
| 481 | insane.xtructs.push_back(truck); |
| 482 | printf("testInsanity()"); |
Bryan Duxbury | 833ae49 | 2010-09-27 17:26:02 +0000 | [diff] [blame] | 483 | map<UserId, map<Numberz::type,Insanity> > whoa; |
Mark Slee | 1921d20 | 2007-01-24 19:43:06 +0000 | [diff] [blame] | 484 | testClient.testInsanity(whoa, insane); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 485 | printf(" = {"); |
Bryan Duxbury | 833ae49 | 2010-09-27 17:26:02 +0000 | [diff] [blame] | 486 | map<UserId, map<Numberz::type,Insanity> >::const_iterator i_iter; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 487 | for (i_iter = whoa.begin(); i_iter != whoa.end(); ++i_iter) { |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 488 | printf("%"PRId64" => {", i_iter->first); |
Bryan Duxbury | 833ae49 | 2010-09-27 17:26:02 +0000 | [diff] [blame] | 489 | map<Numberz::type,Insanity>::const_iterator i2_iter; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 490 | for (i2_iter = i_iter->second.begin(); |
| 491 | i2_iter != i_iter->second.end(); |
| 492 | ++i2_iter) { |
| 493 | printf("%d => {", i2_iter->first); |
Bryan Duxbury | 833ae49 | 2010-09-27 17:26:02 +0000 | [diff] [blame] | 494 | map<Numberz::type, UserId> userMap = i2_iter->second.userMap; |
| 495 | map<Numberz::type, UserId>::const_iterator um; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 496 | printf("{"); |
| 497 | for (um = userMap.begin(); um != userMap.end(); ++um) { |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 498 | printf("%d => %"PRId64", ", um->first, um->second); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 499 | } |
| 500 | printf("}, "); |
| 501 | |
Mark Slee | b9acf98 | 2006-10-10 01:57:32 +0000 | [diff] [blame] | 502 | vector<Xtruct> xtructs = i2_iter->second.xtructs; |
| 503 | vector<Xtruct>::const_iterator x; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 504 | printf("{"); |
| 505 | for (x = xtructs.begin(); x != xtructs.end(); ++x) { |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 506 | printf("{\"%s\", %d, %d, %"PRId64"}, ", |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 507 | x->string_thing.c_str(), |
| 508 | (int)x->byte_thing, |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 509 | x->i32_thing, |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 510 | x->i64_thing); |
| 511 | } |
| 512 | printf("}"); |
| 513 | |
| 514 | printf("}, "); |
| 515 | } |
| 516 | printf("}, "); |
| 517 | } |
| 518 | printf("}\n"); |
| 519 | |
Marc Slemko | 71d4e47 | 2006-08-15 22:34:04 +0000 | [diff] [blame] | 520 | /* test exception */ |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 521 | |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 522 | try { |
Marc Slemko | 71d4e47 | 2006-08-15 22:34:04 +0000 | [diff] [blame] | 523 | printf("testClient.testException(\"Xception\") =>"); |
| 524 | testClient.testException("Xception"); |
| 525 | printf(" void\nFAILURE\n"); |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 526 | |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 527 | } catch(Xception& e) { |
Marc Slemko | 71d4e47 | 2006-08-15 22:34:04 +0000 | [diff] [blame] | 528 | printf(" {%u, \"%s\"}\n", e.errorCode, e.message.c_str()); |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 529 | } |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 530 | |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 531 | try { |
Marc Slemko | 71d4e47 | 2006-08-15 22:34:04 +0000 | [diff] [blame] | 532 | printf("testClient.testException(\"success\") =>"); |
| 533 | testClient.testException("success"); |
| 534 | printf(" void\n"); |
| 535 | } catch(...) { |
| 536 | printf(" exception\nFAILURE\n"); |
| 537 | } |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 538 | |
Marc Slemko | 71d4e47 | 2006-08-15 22:34:04 +0000 | [diff] [blame] | 539 | /* test multi exception */ |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 540 | |
Marc Slemko | 71d4e47 | 2006-08-15 22:34:04 +0000 | [diff] [blame] | 541 | try { |
| 542 | printf("testClient.testMultiException(\"Xception\", \"test 1\") =>"); |
Mark Slee | 1921d20 | 2007-01-24 19:43:06 +0000 | [diff] [blame] | 543 | Xtruct result; |
| 544 | testClient.testMultiException(result, "Xception", "test 1"); |
Marc Slemko | 71d4e47 | 2006-08-15 22:34:04 +0000 | [diff] [blame] | 545 | printf(" result\nFAILURE\n"); |
Mark Slee | d3d733a | 2006-09-01 22:19:06 +0000 | [diff] [blame] | 546 | } catch(Xception& e) { |
Marc Slemko | 71d4e47 | 2006-08-15 22:34:04 +0000 | [diff] [blame] | 547 | printf(" {%u, \"%s\"}\n", e.errorCode, e.message.c_str()); |
| 548 | } |
| 549 | |
| 550 | try { |
| 551 | printf("testClient.testMultiException(\"Xception2\", \"test 2\") =>"); |
Mark Slee | 1921d20 | 2007-01-24 19:43:06 +0000 | [diff] [blame] | 552 | Xtruct result; |
| 553 | testClient.testMultiException(result, "Xception2", "test 2"); |
Marc Slemko | 71d4e47 | 2006-08-15 22:34:04 +0000 | [diff] [blame] | 554 | printf(" result\nFAILURE\n"); |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 555 | |
Mark Slee | d3d733a | 2006-09-01 22:19:06 +0000 | [diff] [blame] | 556 | } catch(Xception2& e) { |
Marc Slemko | 71d4e47 | 2006-08-15 22:34:04 +0000 | [diff] [blame] | 557 | printf(" {%u, {\"%s\"}}\n", e.errorCode, e.struct_thing.string_thing.c_str()); |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 558 | } |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 559 | |
Marc Slemko | 71d4e47 | 2006-08-15 22:34:04 +0000 | [diff] [blame] | 560 | try { |
| 561 | printf("testClient.testMultiException(\"success\", \"test 3\") =>"); |
Mark Slee | 1921d20 | 2007-01-24 19:43:06 +0000 | [diff] [blame] | 562 | Xtruct result; |
| 563 | testClient.testMultiException(result, "success", "test 3"); |
Marc Slemko | 71d4e47 | 2006-08-15 22:34:04 +0000 | [diff] [blame] | 564 | printf(" {{\"%s\"}}\n", result.string_thing.c_str()); |
| 565 | } catch(...) { |
| 566 | printf(" exception\nFAILURE\n"); |
| 567 | } |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 568 | |
David Reiss | c51986f | 2009-03-24 20:01:25 +0000 | [diff] [blame] | 569 | /* test oneway void */ |
David Reiss | 2ab6fe8 | 2008-02-18 02:11:44 +0000 | [diff] [blame] | 570 | { |
David Reiss | 6ce401d | 2009-03-24 20:01:58 +0000 | [diff] [blame] | 571 | printf("testClient.testOneway(3) =>"); |
| 572 | uint64_t startOneway = now(); |
| 573 | testClient.testOneway(3); |
| 574 | uint64_t elapsed = now() - startOneway; |
David Reiss | 2ab6fe8 | 2008-02-18 02:11:44 +0000 | [diff] [blame] | 575 | if (elapsed > 200 * 1000) { // 0.2 seconds |
| 576 | printf(" FAILURE - took %.2f ms\n", (double)elapsed/1000.0); |
| 577 | } else { |
| 578 | printf(" success - took %.2f ms\n", (double)elapsed/1000.0); |
| 579 | } |
| 580 | } |
| 581 | |
David Reiss | 2845b52 | 2008-02-18 02:11:52 +0000 | [diff] [blame] | 582 | /** |
David Reiss | c51986f | 2009-03-24 20:01:25 +0000 | [diff] [blame] | 583 | * redo a simple test after the oneway to make sure we aren't "off by one" -- |
| 584 | * if the server treated oneway void like normal void, this next test will |
David Reiss | 2845b52 | 2008-02-18 02:11:52 +0000 | [diff] [blame] | 585 | * fail since it will get the void confirmation rather than the correct |
| 586 | * result. In this circumstance, the client will throw the exception: |
| 587 | * |
| 588 | * TApplicationException: Wrong method namea |
| 589 | */ |
| 590 | /** |
| 591 | * I32 TEST |
| 592 | */ |
| 593 | printf("re-test testI32(-1)"); |
| 594 | i32 = testClient.testI32(-1); |
| 595 | printf(" = %d\n", i32); |
| 596 | |
| 597 | |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 598 | uint64_t stop = now(); |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 599 | uint64_t tot = stop-start; |
| 600 | |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 601 | printf("Total time: %"PRIu64" us\n", stop-start); |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 602 | |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 603 | time_tot += tot; |
| 604 | if (time_min == 0 || tot < time_min) { |
| 605 | time_min = tot; |
| 606 | } |
| 607 | if (tot > time_max) { |
| 608 | time_max = tot; |
| 609 | } |
| 610 | |
Mark Slee | a330265 | 2006-10-25 19:03:32 +0000 | [diff] [blame] | 611 | transport->close(); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 612 | } |
| 613 | |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 614 | // printf("\nSocket syscalls: %u", g_socket_syscalls); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 615 | printf("\nAll tests done.\n"); |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 616 | |
| 617 | uint64_t time_avg = time_tot / numTests; |
| 618 | |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 619 | printf("Min time: %"PRIu64" us\n", time_min); |
| 620 | printf("Max time: %"PRIu64" us\n", time_max); |
| 621 | printf("Avg time: %"PRIu64" us\n", time_avg); |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 622 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 623 | return 0; |
| 624 | } |