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