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 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 20 | #include <stdio.h> |
| 21 | #include <unistd.h> |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 22 | #include <sys/time.h> |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 23 | #include <protocol/TBinaryProtocol.h> |
Mark Slee | a330265 | 2006-10-25 19:03:32 +0000 | [diff] [blame] | 24 | #include <transport/TTransportUtils.h> |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 25 | #include <transport/TSocket.h> |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 26 | |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 27 | #include <boost/shared_ptr.hpp> |
| 28 | #include "ThriftTest.h" |
| 29 | |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 30 | #define __STDC_FORMAT_MACROS |
| 31 | #include <inttypes.h> |
| 32 | |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 33 | using namespace boost; |
| 34 | using namespace std; |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 35 | using namespace apache::thrift; |
| 36 | using namespace apache::thrift::protocol; |
| 37 | using namespace apache::thrift::transport; |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 38 | using namespace thrift::test; |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 39 | |
| 40 | //extern uint32_t g_socket_syscalls; |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 41 | |
| 42 | // Current time, microseconds since the epoch |
| 43 | uint64_t now() |
| 44 | { |
Roger Meier | 5f9614c | 2010-11-21 16:59:05 +0000 | [diff] [blame] | 45 | int64_t ret; |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 46 | struct timeval tv; |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 47 | |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 48 | gettimeofday(&tv, NULL); |
| 49 | ret = tv.tv_sec; |
| 50 | ret = ret*1000*1000 + tv.tv_usec; |
| 51 | return ret; |
| 52 | } |
| 53 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 54 | int main(int argc, char** argv) { |
| 55 | string host = "localhost"; |
| 56 | int port = 9090; |
| 57 | int numTests = 1; |
Mark Slee | a330265 | 2006-10-25 19:03:32 +0000 | [diff] [blame] | 58 | bool framed = false; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 59 | |
Mark Slee | a330265 | 2006-10-25 19:03:32 +0000 | [diff] [blame] | 60 | for (int i = 0; i < argc; ++i) { |
| 61 | if (strcmp(argv[i], "-h") == 0) { |
| 62 | char* pch = strtok(argv[++i], ":"); |
| 63 | if (pch != NULL) { |
| 64 | host = string(pch); |
| 65 | } |
| 66 | pch = strtok(NULL, ":"); |
| 67 | if (pch != NULL) { |
| 68 | port = atoi(pch); |
| 69 | } |
| 70 | } else if (strcmp(argv[i], "-n") == 0) { |
| 71 | numTests = atoi(argv[++i]); |
| 72 | } else if (strcmp(argv[i], "-f") == 0) { |
| 73 | framed = true; |
Mark Slee | a330265 | 2006-10-25 19:03:32 +0000 | [diff] [blame] | 74 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 75 | } |
Mark Slee | a330265 | 2006-10-25 19:03:32 +0000 | [diff] [blame] | 76 | |
| 77 | |
David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 78 | shared_ptr<TBufferBase> transport; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 79 | |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 80 | shared_ptr<TSocket> socket(new TSocket(host, port)); |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 81 | |
Mark Slee | a330265 | 2006-10-25 19:03:32 +0000 | [diff] [blame] | 82 | if (framed) { |
| 83 | shared_ptr<TFramedTransport> framedSocket(new TFramedTransport(socket)); |
Mark Slee | a330265 | 2006-10-25 19:03:32 +0000 | [diff] [blame] | 84 | transport = framedSocket; |
Mark Slee | a330265 | 2006-10-25 19:03:32 +0000 | [diff] [blame] | 85 | } else { |
| 86 | shared_ptr<TBufferedTransport> bufferedSocket(new TBufferedTransport(socket)); |
| 87 | transport = bufferedSocket; |
| 88 | } |
| 89 | |
David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 90 | shared_ptr< TBinaryProtocolT<TBufferBase> > protocol( |
| 91 | new TBinaryProtocolT<TBufferBase>(transport)); |
David Reiss | b7762a0 | 2010-10-06 17:10:00 +0000 | [diff] [blame] | 92 | ThriftTestClientT< TBinaryProtocolT<TBufferBase> > testClient(protocol); |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 93 | |
| 94 | uint64_t time_min = 0; |
| 95 | uint64_t time_max = 0; |
| 96 | uint64_t time_tot = 0; |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 97 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 98 | int test = 0; |
| 99 | for (test = 0; test < numTests; ++test) { |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 100 | |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 101 | try { |
Mark Slee | a330265 | 2006-10-25 19:03:32 +0000 | [diff] [blame] | 102 | transport->open(); |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 103 | } catch (TTransportException& ttx) { |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 104 | printf("Connect failed: %s\n", ttx.what()); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 105 | continue; |
| 106 | } |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 107 | |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 108 | /** |
| 109 | * CONNECT TEST |
| 110 | */ |
| 111 | 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] | 112 | |
| 113 | uint64_t start = now(); |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 114 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 115 | /** |
| 116 | * VOID TEST |
| 117 | */ |
Mark Slee | e129a2d | 2007-02-21 05:17:48 +0000 | [diff] [blame] | 118 | try { |
| 119 | printf("testVoid()"); |
| 120 | testClient.testVoid(); |
| 121 | printf(" = void\n"); |
| 122 | } catch (TApplicationException tax) { |
| 123 | printf("%s\n", tax.what()); |
| 124 | } |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 125 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 126 | /** |
| 127 | * STRING TEST |
| 128 | */ |
| 129 | printf("testString(\"Test\")"); |
Mark Slee | 1921d20 | 2007-01-24 19:43:06 +0000 | [diff] [blame] | 130 | string s; |
| 131 | testClient.testString(s, "Test"); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 132 | printf(" = \"%s\"\n", s.c_str()); |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 133 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 134 | /** |
| 135 | * BYTE TEST |
| 136 | */ |
| 137 | printf("testByte(1)"); |
| 138 | uint8_t u8 = testClient.testByte(1); |
| 139 | printf(" = %d\n", (int)u8); |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 140 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 141 | /** |
| 142 | * I32 TEST |
| 143 | */ |
| 144 | printf("testI32(-1)"); |
| 145 | int32_t i32 = testClient.testI32(-1); |
| 146 | printf(" = %d\n", i32); |
| 147 | |
| 148 | /** |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 149 | * I64 TEST |
| 150 | */ |
| 151 | printf("testI64(-34359738368)"); |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 152 | int64_t i64 = testClient.testI64(-34359738368LL); |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 153 | printf(" = %"PRId64"\n", i64); |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 154 | |
| 155 | /** |
| 156 | * DOUBLE TEST |
| 157 | */ |
| 158 | printf("testDouble(-5.2098523)"); |
| 159 | double dub = testClient.testDouble(-5.2098523); |
| 160 | printf(" = %lf\n", dub); |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 161 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 162 | /** |
| 163 | * STRUCT TEST |
| 164 | */ |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 165 | printf("testStruct({\"Zero\", 1, -3, -5})"); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 166 | Xtruct out; |
| 167 | out.string_thing = "Zero"; |
| 168 | out.byte_thing = 1; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 169 | out.i32_thing = -3; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 170 | out.i64_thing = -5; |
Mark Slee | 1921d20 | 2007-01-24 19:43:06 +0000 | [diff] [blame] | 171 | Xtruct in; |
| 172 | testClient.testStruct(in, out); |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 173 | printf(" = {\"%s\", %d, %d, %"PRId64"}\n", |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 174 | in.string_thing.c_str(), |
| 175 | (int)in.byte_thing, |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 176 | in.i32_thing, |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 177 | in.i64_thing); |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 178 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 179 | /** |
| 180 | * NESTED STRUCT TEST |
| 181 | */ |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 182 | printf("testNest({1, {\"Zero\", 1, -3, -5}), 5}"); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 183 | Xtruct2 out2; |
| 184 | out2.byte_thing = 1; |
| 185 | out2.struct_thing = out; |
| 186 | out2.i32_thing = 5; |
Mark Slee | 1921d20 | 2007-01-24 19:43:06 +0000 | [diff] [blame] | 187 | Xtruct2 in2; |
| 188 | testClient.testNest(in2, out2); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 189 | in = in2.struct_thing; |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 190 | printf(" = {%d, {\"%s\", %d, %d, %"PRId64"}, %d}\n", |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 191 | in2.byte_thing, |
| 192 | in.string_thing.c_str(), |
| 193 | (int)in.byte_thing, |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 194 | in.i32_thing, |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 195 | in.i64_thing, |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 196 | in2.i32_thing); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 197 | |
| 198 | /** |
| 199 | * MAP TEST |
| 200 | */ |
| 201 | map<int32_t,int32_t> mapout; |
| 202 | for (int32_t i = 0; i < 5; ++i) { |
| 203 | mapout.insert(make_pair(i, i-10)); |
| 204 | } |
| 205 | printf("testMap({"); |
| 206 | map<int32_t, int32_t>::const_iterator m_iter; |
| 207 | bool first = true; |
| 208 | for (m_iter = mapout.begin(); m_iter != mapout.end(); ++m_iter) { |
| 209 | if (first) { |
| 210 | first = false; |
| 211 | } else { |
| 212 | printf(", "); |
| 213 | } |
| 214 | printf("%d => %d", m_iter->first, m_iter->second); |
| 215 | } |
| 216 | printf("})"); |
Mark Slee | 1921d20 | 2007-01-24 19:43:06 +0000 | [diff] [blame] | 217 | map<int32_t,int32_t> mapin; |
| 218 | testClient.testMap(mapin, mapout); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 219 | printf(" = {"); |
| 220 | first = true; |
| 221 | for (m_iter = mapin.begin(); m_iter != mapin.end(); ++m_iter) { |
| 222 | if (first) { |
| 223 | first = false; |
| 224 | } else { |
| 225 | printf(", "); |
| 226 | } |
| 227 | printf("%d => %d", m_iter->first, m_iter->second); |
| 228 | } |
| 229 | printf("}\n"); |
| 230 | |
| 231 | /** |
| 232 | * SET TEST |
| 233 | */ |
| 234 | set<int32_t> setout; |
| 235 | for (int32_t i = -2; i < 3; ++i) { |
| 236 | setout.insert(i); |
| 237 | } |
| 238 | printf("testSet({"); |
| 239 | set<int32_t>::const_iterator s_iter; |
| 240 | first = true; |
| 241 | for (s_iter = setout.begin(); s_iter != setout.end(); ++s_iter) { |
| 242 | if (first) { |
| 243 | first = false; |
| 244 | } else { |
| 245 | printf(", "); |
| 246 | } |
| 247 | printf("%d", *s_iter); |
| 248 | } |
| 249 | printf("})"); |
Mark Slee | 1921d20 | 2007-01-24 19:43:06 +0000 | [diff] [blame] | 250 | set<int32_t> setin; |
| 251 | testClient.testSet(setin, setout); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 252 | printf(" = {"); |
| 253 | first = true; |
| 254 | for (s_iter = setin.begin(); s_iter != setin.end(); ++s_iter) { |
| 255 | if (first) { |
| 256 | first = false; |
| 257 | } else { |
| 258 | printf(", "); |
| 259 | } |
| 260 | printf("%d", *s_iter); |
| 261 | } |
| 262 | printf("}\n"); |
| 263 | |
| 264 | /** |
| 265 | * LIST TEST |
| 266 | */ |
Mark Slee | b9acf98 | 2006-10-10 01:57:32 +0000 | [diff] [blame] | 267 | vector<int32_t> listout; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 268 | for (int32_t i = -2; i < 3; ++i) { |
| 269 | listout.push_back(i); |
| 270 | } |
| 271 | printf("testList({"); |
Mark Slee | b9acf98 | 2006-10-10 01:57:32 +0000 | [diff] [blame] | 272 | vector<int32_t>::const_iterator l_iter; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 273 | first = true; |
| 274 | for (l_iter = listout.begin(); l_iter != listout.end(); ++l_iter) { |
| 275 | if (first) { |
| 276 | first = false; |
| 277 | } else { |
| 278 | printf(", "); |
| 279 | } |
| 280 | printf("%d", *l_iter); |
| 281 | } |
| 282 | printf("})"); |
Mark Slee | 1921d20 | 2007-01-24 19:43:06 +0000 | [diff] [blame] | 283 | vector<int32_t> listin; |
| 284 | testClient.testList(listin, listout); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 285 | printf(" = {"); |
| 286 | first = true; |
| 287 | for (l_iter = listin.begin(); l_iter != listin.end(); ++l_iter) { |
| 288 | if (first) { |
| 289 | first = false; |
| 290 | } else { |
| 291 | printf(", "); |
| 292 | } |
| 293 | printf("%d", *l_iter); |
| 294 | } |
| 295 | printf("}\n"); |
| 296 | |
| 297 | /** |
| 298 | * ENUM TEST |
| 299 | */ |
| 300 | printf("testEnum(ONE)"); |
Bryan Duxbury | 833ae49 | 2010-09-27 17:26:02 +0000 | [diff] [blame] | 301 | Numberz::type ret = testClient.testEnum(Numberz::ONE); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 302 | printf(" = %d\n", ret); |
| 303 | |
| 304 | printf("testEnum(TWO)"); |
Bryan Duxbury | 833ae49 | 2010-09-27 17:26:02 +0000 | [diff] [blame] | 305 | ret = testClient.testEnum(Numberz::TWO); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 306 | printf(" = %d\n", ret); |
| 307 | |
| 308 | printf("testEnum(THREE)"); |
Bryan Duxbury | 833ae49 | 2010-09-27 17:26:02 +0000 | [diff] [blame] | 309 | ret = testClient.testEnum(Numberz::THREE); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 310 | printf(" = %d\n", ret); |
| 311 | |
| 312 | printf("testEnum(FIVE)"); |
Bryan Duxbury | 833ae49 | 2010-09-27 17:26:02 +0000 | [diff] [blame] | 313 | ret = testClient.testEnum(Numberz::FIVE); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 314 | printf(" = %d\n", ret); |
| 315 | |
| 316 | printf("testEnum(EIGHT)"); |
Bryan Duxbury | 833ae49 | 2010-09-27 17:26:02 +0000 | [diff] [blame] | 317 | ret = testClient.testEnum(Numberz::EIGHT); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 318 | printf(" = %d\n", ret); |
| 319 | |
| 320 | /** |
| 321 | * TYPEDEF TEST |
| 322 | */ |
| 323 | printf("testTypedef(309858235082523)"); |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 324 | UserId uid = testClient.testTypedef(309858235082523LL); |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 325 | printf(" = %"PRId64"\n", uid); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 326 | |
| 327 | /** |
| 328 | * NESTED MAP TEST |
| 329 | */ |
| 330 | printf("testMapMap(1)"); |
Mark Slee | 1921d20 | 2007-01-24 19:43:06 +0000 | [diff] [blame] | 331 | map<int32_t, map<int32_t, int32_t> > mm; |
| 332 | testClient.testMapMap(mm, 1); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 333 | printf(" = {"); |
| 334 | map<int32_t, map<int32_t, int32_t> >::const_iterator mi; |
| 335 | for (mi = mm.begin(); mi != mm.end(); ++mi) { |
| 336 | printf("%d => {", mi->first); |
| 337 | map<int32_t, int32_t>::const_iterator mi2; |
| 338 | for (mi2 = mi->second.begin(); mi2 != mi->second.end(); ++mi2) { |
| 339 | printf("%d => %d, ", mi2->first, mi2->second); |
| 340 | } |
| 341 | printf("}, "); |
| 342 | } |
| 343 | printf("}\n"); |
| 344 | |
| 345 | /** |
| 346 | * INSANITY TEST |
| 347 | */ |
| 348 | Insanity insane; |
Bryan Duxbury | 833ae49 | 2010-09-27 17:26:02 +0000 | [diff] [blame] | 349 | insane.userMap.insert(make_pair(Numberz::FIVE, 5000)); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 350 | Xtruct truck; |
| 351 | truck.string_thing = "Truck"; |
| 352 | truck.byte_thing = 8; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 353 | truck.i32_thing = 8; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 354 | truck.i64_thing = 8; |
| 355 | insane.xtructs.push_back(truck); |
| 356 | printf("testInsanity()"); |
Bryan Duxbury | 833ae49 | 2010-09-27 17:26:02 +0000 | [diff] [blame] | 357 | map<UserId, map<Numberz::type,Insanity> > whoa; |
Mark Slee | 1921d20 | 2007-01-24 19:43:06 +0000 | [diff] [blame] | 358 | testClient.testInsanity(whoa, insane); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 359 | printf(" = {"); |
Bryan Duxbury | 833ae49 | 2010-09-27 17:26:02 +0000 | [diff] [blame] | 360 | map<UserId, map<Numberz::type,Insanity> >::const_iterator i_iter; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 361 | for (i_iter = whoa.begin(); i_iter != whoa.end(); ++i_iter) { |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 362 | printf("%"PRId64" => {", i_iter->first); |
Bryan Duxbury | 833ae49 | 2010-09-27 17:26:02 +0000 | [diff] [blame] | 363 | map<Numberz::type,Insanity>::const_iterator i2_iter; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 364 | for (i2_iter = i_iter->second.begin(); |
| 365 | i2_iter != i_iter->second.end(); |
| 366 | ++i2_iter) { |
| 367 | printf("%d => {", i2_iter->first); |
Bryan Duxbury | 833ae49 | 2010-09-27 17:26:02 +0000 | [diff] [blame] | 368 | map<Numberz::type, UserId> userMap = i2_iter->second.userMap; |
| 369 | map<Numberz::type, UserId>::const_iterator um; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 370 | printf("{"); |
| 371 | for (um = userMap.begin(); um != userMap.end(); ++um) { |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 372 | printf("%d => %"PRId64", ", um->first, um->second); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 373 | } |
| 374 | printf("}, "); |
| 375 | |
Mark Slee | b9acf98 | 2006-10-10 01:57:32 +0000 | [diff] [blame] | 376 | vector<Xtruct> xtructs = i2_iter->second.xtructs; |
| 377 | vector<Xtruct>::const_iterator x; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 378 | printf("{"); |
| 379 | for (x = xtructs.begin(); x != xtructs.end(); ++x) { |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 380 | printf("{\"%s\", %d, %d, %"PRId64"}, ", |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 381 | x->string_thing.c_str(), |
| 382 | (int)x->byte_thing, |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 383 | x->i32_thing, |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 384 | x->i64_thing); |
| 385 | } |
| 386 | printf("}"); |
| 387 | |
| 388 | printf("}, "); |
| 389 | } |
| 390 | printf("}, "); |
| 391 | } |
| 392 | printf("}\n"); |
| 393 | |
Marc Slemko | 71d4e47 | 2006-08-15 22:34:04 +0000 | [diff] [blame] | 394 | /* test exception */ |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 395 | |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 396 | try { |
Marc Slemko | 71d4e47 | 2006-08-15 22:34:04 +0000 | [diff] [blame] | 397 | printf("testClient.testException(\"Xception\") =>"); |
| 398 | testClient.testException("Xception"); |
| 399 | printf(" void\nFAILURE\n"); |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 400 | |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 401 | } catch(Xception& e) { |
Marc Slemko | 71d4e47 | 2006-08-15 22:34:04 +0000 | [diff] [blame] | 402 | printf(" {%u, \"%s\"}\n", e.errorCode, e.message.c_str()); |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 403 | } |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 404 | |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 405 | try { |
Marc Slemko | 71d4e47 | 2006-08-15 22:34:04 +0000 | [diff] [blame] | 406 | printf("testClient.testException(\"success\") =>"); |
| 407 | testClient.testException("success"); |
| 408 | printf(" void\n"); |
| 409 | } catch(...) { |
| 410 | printf(" exception\nFAILURE\n"); |
| 411 | } |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 412 | |
Marc Slemko | 71d4e47 | 2006-08-15 22:34:04 +0000 | [diff] [blame] | 413 | /* test multi exception */ |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 414 | |
Marc Slemko | 71d4e47 | 2006-08-15 22:34:04 +0000 | [diff] [blame] | 415 | try { |
| 416 | printf("testClient.testMultiException(\"Xception\", \"test 1\") =>"); |
Mark Slee | 1921d20 | 2007-01-24 19:43:06 +0000 | [diff] [blame] | 417 | Xtruct result; |
| 418 | testClient.testMultiException(result, "Xception", "test 1"); |
Marc Slemko | 71d4e47 | 2006-08-15 22:34:04 +0000 | [diff] [blame] | 419 | printf(" result\nFAILURE\n"); |
Mark Slee | d3d733a | 2006-09-01 22:19:06 +0000 | [diff] [blame] | 420 | } catch(Xception& e) { |
Marc Slemko | 71d4e47 | 2006-08-15 22:34:04 +0000 | [diff] [blame] | 421 | printf(" {%u, \"%s\"}\n", e.errorCode, e.message.c_str()); |
| 422 | } |
| 423 | |
| 424 | try { |
| 425 | printf("testClient.testMultiException(\"Xception2\", \"test 2\") =>"); |
Mark Slee | 1921d20 | 2007-01-24 19:43:06 +0000 | [diff] [blame] | 426 | Xtruct result; |
| 427 | testClient.testMultiException(result, "Xception2", "test 2"); |
Marc Slemko | 71d4e47 | 2006-08-15 22:34:04 +0000 | [diff] [blame] | 428 | printf(" result\nFAILURE\n"); |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 429 | |
Mark Slee | d3d733a | 2006-09-01 22:19:06 +0000 | [diff] [blame] | 430 | } catch(Xception2& e) { |
Marc Slemko | 71d4e47 | 2006-08-15 22:34:04 +0000 | [diff] [blame] | 431 | 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] | 432 | } |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 433 | |
Marc Slemko | 71d4e47 | 2006-08-15 22:34:04 +0000 | [diff] [blame] | 434 | try { |
| 435 | printf("testClient.testMultiException(\"success\", \"test 3\") =>"); |
Mark Slee | 1921d20 | 2007-01-24 19:43:06 +0000 | [diff] [blame] | 436 | Xtruct result; |
| 437 | testClient.testMultiException(result, "success", "test 3"); |
Marc Slemko | 71d4e47 | 2006-08-15 22:34:04 +0000 | [diff] [blame] | 438 | printf(" {{\"%s\"}}\n", result.string_thing.c_str()); |
| 439 | } catch(...) { |
| 440 | printf(" exception\nFAILURE\n"); |
| 441 | } |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 442 | |
David Reiss | c51986f | 2009-03-24 20:01:25 +0000 | [diff] [blame] | 443 | /* test oneway void */ |
David Reiss | 2ab6fe8 | 2008-02-18 02:11:44 +0000 | [diff] [blame] | 444 | { |
David Reiss | 6ce401d | 2009-03-24 20:01:58 +0000 | [diff] [blame] | 445 | printf("testClient.testOneway(3) =>"); |
| 446 | uint64_t startOneway = now(); |
| 447 | testClient.testOneway(3); |
| 448 | uint64_t elapsed = now() - startOneway; |
David Reiss | 2ab6fe8 | 2008-02-18 02:11:44 +0000 | [diff] [blame] | 449 | if (elapsed > 200 * 1000) { // 0.2 seconds |
| 450 | printf(" FAILURE - took %.2f ms\n", (double)elapsed/1000.0); |
| 451 | } else { |
| 452 | printf(" success - took %.2f ms\n", (double)elapsed/1000.0); |
| 453 | } |
| 454 | } |
| 455 | |
David Reiss | 2845b52 | 2008-02-18 02:11:52 +0000 | [diff] [blame] | 456 | /** |
David Reiss | c51986f | 2009-03-24 20:01:25 +0000 | [diff] [blame] | 457 | * redo a simple test after the oneway to make sure we aren't "off by one" -- |
| 458 | * 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] | 459 | * fail since it will get the void confirmation rather than the correct |
| 460 | * result. In this circumstance, the client will throw the exception: |
| 461 | * |
| 462 | * TApplicationException: Wrong method namea |
| 463 | */ |
| 464 | /** |
| 465 | * I32 TEST |
| 466 | */ |
| 467 | printf("re-test testI32(-1)"); |
| 468 | i32 = testClient.testI32(-1); |
| 469 | printf(" = %d\n", i32); |
| 470 | |
| 471 | |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 472 | uint64_t stop = now(); |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 473 | uint64_t tot = stop-start; |
| 474 | |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 475 | printf("Total time: %"PRIu64" us\n", stop-start); |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 476 | |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 477 | time_tot += tot; |
| 478 | if (time_min == 0 || tot < time_min) { |
| 479 | time_min = tot; |
| 480 | } |
| 481 | if (tot > time_max) { |
| 482 | time_max = tot; |
| 483 | } |
| 484 | |
Mark Slee | a330265 | 2006-10-25 19:03:32 +0000 | [diff] [blame] | 485 | transport->close(); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 486 | } |
| 487 | |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 488 | // printf("\nSocket syscalls: %u", g_socket_syscalls); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 489 | printf("\nAll tests done.\n"); |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 490 | |
| 491 | uint64_t time_avg = time_tot / numTests; |
| 492 | |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 493 | printf("Min time: %"PRIu64" us\n", time_min); |
| 494 | printf("Max time: %"PRIu64" us\n", time_max); |
| 495 | printf("Avg time: %"PRIu64" us\n", time_avg); |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 496 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 497 | return 0; |
| 498 | } |