Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <unistd.h> |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 3 | #include <sys/time.h> |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 4 | #include <protocol/TBinaryProtocol.h> |
| 5 | #include <transport/TBufferedTransport.h> |
| 6 | #include <transport/TSocket.h> |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 7 | |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 8 | #include <boost/shared_ptr.hpp> |
| 9 | #include "ThriftTest.h" |
| 10 | |
| 11 | using namespace boost; |
| 12 | using namespace std; |
| 13 | using namespace facebook::thrift; |
| 14 | using namespace facebook::thrift::protocol; |
| 15 | using namespace facebook::thrift::transport; |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 16 | using namespace thrift::test; |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 17 | |
| 18 | //extern uint32_t g_socket_syscalls; |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 19 | |
| 20 | // Current time, microseconds since the epoch |
| 21 | uint64_t now() |
| 22 | { |
| 23 | long long ret; |
| 24 | struct timeval tv; |
| 25 | |
| 26 | gettimeofday(&tv, NULL); |
| 27 | ret = tv.tv_sec; |
| 28 | ret = ret*1000*1000 + tv.tv_usec; |
| 29 | return ret; |
| 30 | } |
| 31 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 32 | int main(int argc, char** argv) { |
| 33 | string host = "localhost"; |
| 34 | int port = 9090; |
| 35 | int numTests = 1; |
| 36 | |
| 37 | if (argc > 1) { |
| 38 | host = argv[1]; |
| 39 | } |
| 40 | if (argc > 2) { |
| 41 | port = atoi(argv[2]); |
| 42 | } |
| 43 | if (argc > 3) { |
| 44 | numTests = atoi(argv[3]); |
| 45 | } |
| 46 | |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 47 | shared_ptr<TSocket> socket(new TSocket(host, port)); |
| 48 | shared_ptr<TBufferedTransport> bufferedSocket(new TBufferedTransport(socket, 2048)); |
| 49 | shared_ptr<TBinaryProtocol> binaryProtocol(new TBinaryProtocol()); |
| 50 | ThriftTestClient testClient(bufferedSocket, binaryProtocol); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 51 | |
| 52 | int test = 0; |
| 53 | for (test = 0; test < numTests; ++test) { |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 54 | |
| 55 | /** |
| 56 | * CONNECT TEST |
| 57 | */ |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 58 | 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] | 59 | try { |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 60 | bufferedSocket->open(); |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 61 | } catch (TTransportException& ttx) { |
| 62 | printf("Connect failed: %s\n", ttx.getMessage().c_str()); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 63 | continue; |
| 64 | } |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 65 | |
| 66 | uint64_t start = now(); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 67 | |
| 68 | /** |
| 69 | * VOID TEST |
| 70 | */ |
| 71 | printf("testVoid()"); |
| 72 | testClient.testVoid(); |
| 73 | printf(" = void\n"); |
| 74 | |
| 75 | /** |
| 76 | * STRING TEST |
| 77 | */ |
| 78 | printf("testString(\"Test\")"); |
| 79 | string s = testClient.testString("Test"); |
| 80 | printf(" = \"%s\"\n", s.c_str()); |
| 81 | |
| 82 | /** |
| 83 | * BYTE TEST |
| 84 | */ |
| 85 | printf("testByte(1)"); |
| 86 | uint8_t u8 = testClient.testByte(1); |
| 87 | printf(" = %d\n", (int)u8); |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 88 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 89 | /** |
| 90 | * I32 TEST |
| 91 | */ |
| 92 | printf("testI32(-1)"); |
| 93 | int32_t i32 = testClient.testI32(-1); |
| 94 | printf(" = %d\n", i32); |
| 95 | |
| 96 | /** |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 97 | * I64 TEST |
| 98 | */ |
| 99 | printf("testI64(-34359738368)"); |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 100 | int64_t i64 = testClient.testI64(-34359738368LL); |
| 101 | printf(" = %lld\n", i64); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 102 | |
| 103 | /** |
| 104 | * STRUCT TEST |
| 105 | */ |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 106 | printf("testStruct({\"Zero\", 1, -3, -5})"); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 107 | Xtruct out; |
| 108 | out.string_thing = "Zero"; |
| 109 | out.byte_thing = 1; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 110 | out.i32_thing = -3; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 111 | out.i64_thing = -5; |
| 112 | Xtruct in = testClient.testStruct(out); |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 113 | printf(" = {\"%s\", %d, %d, %lld}\n", |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 114 | in.string_thing.c_str(), |
| 115 | (int)in.byte_thing, |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 116 | in.i32_thing, |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 117 | in.i64_thing); |
| 118 | |
| 119 | /** |
| 120 | * NESTED STRUCT TEST |
| 121 | */ |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 122 | printf("testNest({1, {\"Zero\", 1, -3, -5}), 5}"); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 123 | Xtruct2 out2; |
| 124 | out2.byte_thing = 1; |
| 125 | out2.struct_thing = out; |
| 126 | out2.i32_thing = 5; |
| 127 | Xtruct2 in2 = testClient.testNest(out2); |
| 128 | in = in2.struct_thing; |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 129 | printf(" = {%d, {\"%s\", %d, %d, %lld}, %d}\n", |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 130 | in2.byte_thing, |
| 131 | in.string_thing.c_str(), |
| 132 | (int)in.byte_thing, |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 133 | in.i32_thing, |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 134 | in.i64_thing, |
| 135 | in2.i32_thing); |
| 136 | |
| 137 | /** |
| 138 | * MAP TEST |
| 139 | */ |
| 140 | map<int32_t,int32_t> mapout; |
| 141 | for (int32_t i = 0; i < 5; ++i) { |
| 142 | mapout.insert(make_pair(i, i-10)); |
| 143 | } |
| 144 | printf("testMap({"); |
| 145 | map<int32_t, int32_t>::const_iterator m_iter; |
| 146 | bool first = true; |
| 147 | for (m_iter = mapout.begin(); m_iter != mapout.end(); ++m_iter) { |
| 148 | if (first) { |
| 149 | first = false; |
| 150 | } else { |
| 151 | printf(", "); |
| 152 | } |
| 153 | printf("%d => %d", m_iter->first, m_iter->second); |
| 154 | } |
| 155 | printf("})"); |
| 156 | map<int32_t,int32_t> mapin = testClient.testMap(mapout); |
| 157 | printf(" = {"); |
| 158 | first = true; |
| 159 | for (m_iter = mapin.begin(); m_iter != mapin.end(); ++m_iter) { |
| 160 | if (first) { |
| 161 | first = false; |
| 162 | } else { |
| 163 | printf(", "); |
| 164 | } |
| 165 | printf("%d => %d", m_iter->first, m_iter->second); |
| 166 | } |
| 167 | printf("}\n"); |
| 168 | |
| 169 | /** |
| 170 | * SET TEST |
| 171 | */ |
| 172 | set<int32_t> setout; |
| 173 | for (int32_t i = -2; i < 3; ++i) { |
| 174 | setout.insert(i); |
| 175 | } |
| 176 | printf("testSet({"); |
| 177 | set<int32_t>::const_iterator s_iter; |
| 178 | first = true; |
| 179 | for (s_iter = setout.begin(); s_iter != setout.end(); ++s_iter) { |
| 180 | if (first) { |
| 181 | first = false; |
| 182 | } else { |
| 183 | printf(", "); |
| 184 | } |
| 185 | printf("%d", *s_iter); |
| 186 | } |
| 187 | printf("})"); |
| 188 | set<int32_t> setin = testClient.testSet(setout); |
| 189 | printf(" = {"); |
| 190 | first = true; |
| 191 | for (s_iter = setin.begin(); s_iter != setin.end(); ++s_iter) { |
| 192 | if (first) { |
| 193 | first = false; |
| 194 | } else { |
| 195 | printf(", "); |
| 196 | } |
| 197 | printf("%d", *s_iter); |
| 198 | } |
| 199 | printf("}\n"); |
| 200 | |
| 201 | /** |
| 202 | * LIST TEST |
| 203 | */ |
| 204 | list<int32_t> listout; |
| 205 | for (int32_t i = -2; i < 3; ++i) { |
| 206 | listout.push_back(i); |
| 207 | } |
| 208 | printf("testList({"); |
| 209 | list<int32_t>::const_iterator l_iter; |
| 210 | first = true; |
| 211 | for (l_iter = listout.begin(); l_iter != listout.end(); ++l_iter) { |
| 212 | if (first) { |
| 213 | first = false; |
| 214 | } else { |
| 215 | printf(", "); |
| 216 | } |
| 217 | printf("%d", *l_iter); |
| 218 | } |
| 219 | printf("})"); |
| 220 | list<int32_t> listin = testClient.testList(listout); |
| 221 | printf(" = {"); |
| 222 | first = true; |
| 223 | for (l_iter = listin.begin(); l_iter != listin.end(); ++l_iter) { |
| 224 | if (first) { |
| 225 | first = false; |
| 226 | } else { |
| 227 | printf(", "); |
| 228 | } |
| 229 | printf("%d", *l_iter); |
| 230 | } |
| 231 | printf("}\n"); |
| 232 | |
| 233 | /** |
| 234 | * ENUM TEST |
| 235 | */ |
| 236 | printf("testEnum(ONE)"); |
| 237 | Numberz ret = testClient.testEnum(ONE); |
| 238 | printf(" = %d\n", ret); |
| 239 | |
| 240 | printf("testEnum(TWO)"); |
| 241 | ret = testClient.testEnum(TWO); |
| 242 | printf(" = %d\n", ret); |
| 243 | |
| 244 | printf("testEnum(THREE)"); |
| 245 | ret = testClient.testEnum(THREE); |
| 246 | printf(" = %d\n", ret); |
| 247 | |
| 248 | printf("testEnum(FIVE)"); |
| 249 | ret = testClient.testEnum(FIVE); |
| 250 | printf(" = %d\n", ret); |
| 251 | |
| 252 | printf("testEnum(EIGHT)"); |
| 253 | ret = testClient.testEnum(EIGHT); |
| 254 | printf(" = %d\n", ret); |
| 255 | |
| 256 | /** |
| 257 | * TYPEDEF TEST |
| 258 | */ |
| 259 | printf("testTypedef(309858235082523)"); |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 260 | UserId uid = testClient.testTypedef(309858235082523LL); |
| 261 | printf(" = %lld\n", uid); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 262 | |
| 263 | /** |
| 264 | * NESTED MAP TEST |
| 265 | */ |
| 266 | printf("testMapMap(1)"); |
| 267 | map<int32_t, map<int32_t, int32_t> > mm = testClient.testMapMap(1); |
| 268 | printf(" = {"); |
| 269 | map<int32_t, map<int32_t, int32_t> >::const_iterator mi; |
| 270 | for (mi = mm.begin(); mi != mm.end(); ++mi) { |
| 271 | printf("%d => {", mi->first); |
| 272 | map<int32_t, int32_t>::const_iterator mi2; |
| 273 | for (mi2 = mi->second.begin(); mi2 != mi->second.end(); ++mi2) { |
| 274 | printf("%d => %d, ", mi2->first, mi2->second); |
| 275 | } |
| 276 | printf("}, "); |
| 277 | } |
| 278 | printf("}\n"); |
| 279 | |
| 280 | /** |
| 281 | * INSANITY TEST |
| 282 | */ |
| 283 | Insanity insane; |
| 284 | insane.userMap.insert(make_pair(FIVE, 5000)); |
| 285 | Xtruct truck; |
| 286 | truck.string_thing = "Truck"; |
| 287 | truck.byte_thing = 8; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 288 | truck.i32_thing = 8; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 289 | truck.i64_thing = 8; |
| 290 | insane.xtructs.push_back(truck); |
| 291 | printf("testInsanity()"); |
| 292 | map<UserId, map<Numberz,Insanity> > whoa = testClient.testInsanity(insane); |
| 293 | printf(" = {"); |
| 294 | map<UserId, map<Numberz,Insanity> >::const_iterator i_iter; |
| 295 | for (i_iter = whoa.begin(); i_iter != whoa.end(); ++i_iter) { |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 296 | printf("%lld => {", i_iter->first); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 297 | map<Numberz,Insanity>::const_iterator i2_iter; |
| 298 | for (i2_iter = i_iter->second.begin(); |
| 299 | i2_iter != i_iter->second.end(); |
| 300 | ++i2_iter) { |
| 301 | printf("%d => {", i2_iter->first); |
| 302 | map<Numberz, UserId> userMap = i2_iter->second.userMap; |
| 303 | map<Numberz, UserId>::const_iterator um; |
| 304 | printf("{"); |
| 305 | for (um = userMap.begin(); um != userMap.end(); ++um) { |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 306 | printf("%d => %lld, ", um->first, um->second); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 307 | } |
| 308 | printf("}, "); |
| 309 | |
| 310 | list<Xtruct> xtructs = i2_iter->second.xtructs; |
| 311 | list<Xtruct>::const_iterator x; |
| 312 | printf("{"); |
| 313 | for (x = xtructs.begin(); x != xtructs.end(); ++x) { |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 314 | printf("{\"%s\", %d, %d, %lld}, ", |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 315 | x->string_thing.c_str(), |
| 316 | (int)x->byte_thing, |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 317 | x->i32_thing, |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 318 | x->i64_thing); |
| 319 | } |
| 320 | printf("}"); |
| 321 | |
| 322 | printf("}, "); |
| 323 | } |
| 324 | printf("}, "); |
| 325 | } |
| 326 | printf("}\n"); |
| 327 | |
Marc Slemko | 71d4e47 | 2006-08-15 22:34:04 +0000 | [diff] [blame] | 328 | /* test exception */ |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 329 | |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 330 | try { |
Marc Slemko | 71d4e47 | 2006-08-15 22:34:04 +0000 | [diff] [blame] | 331 | printf("testClient.testException(\"Xception\") =>"); |
| 332 | testClient.testException("Xception"); |
| 333 | printf(" void\nFAILURE\n"); |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 334 | |
| 335 | } catch(Xception& e) { |
Marc Slemko | 71d4e47 | 2006-08-15 22:34:04 +0000 | [diff] [blame] | 336 | printf(" {%u, \"%s\"}\n", e.errorCode, e.message.c_str()); |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 337 | } |
Marc Slemko | 71d4e47 | 2006-08-15 22:34:04 +0000 | [diff] [blame] | 338 | |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 339 | try { |
Marc Slemko | 71d4e47 | 2006-08-15 22:34:04 +0000 | [diff] [blame] | 340 | printf("testClient.testException(\"success\") =>"); |
| 341 | testClient.testException("success"); |
| 342 | printf(" void\n"); |
| 343 | } catch(...) { |
| 344 | printf(" exception\nFAILURE\n"); |
| 345 | } |
| 346 | |
| 347 | /* test multi exception */ |
| 348 | |
| 349 | try { |
| 350 | printf("testClient.testMultiException(\"Xception\", \"test 1\") =>"); |
| 351 | Xtruct result = testClient.testMultiException("Xception", "test 1"); |
| 352 | printf(" result\nFAILURE\n"); |
| 353 | } catch(Xception& e) { |
| 354 | printf(" {%u, \"%s\"}\n", e.errorCode, e.message.c_str()); |
| 355 | } |
| 356 | |
| 357 | try { |
| 358 | printf("testClient.testMultiException(\"Xception2\", \"test 2\") =>"); |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 359 | Xtruct result = testClient.testMultiException("Xception2", "test 2"); |
Marc Slemko | 71d4e47 | 2006-08-15 22:34:04 +0000 | [diff] [blame] | 360 | printf(" result\nFAILURE\n"); |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 361 | |
| 362 | } catch(Xception2& e) { |
Marc Slemko | 71d4e47 | 2006-08-15 22:34:04 +0000 | [diff] [blame] | 363 | 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] | 364 | } |
| 365 | |
Marc Slemko | 71d4e47 | 2006-08-15 22:34:04 +0000 | [diff] [blame] | 366 | try { |
| 367 | printf("testClient.testMultiException(\"success\", \"test 3\") =>"); |
| 368 | Xtruct result = testClient.testMultiException("success", "test 3"); |
| 369 | printf(" {{\"%s\"}}\n", result.string_thing.c_str()); |
| 370 | } catch(...) { |
| 371 | printf(" exception\nFAILURE\n"); |
| 372 | } |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 373 | |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 374 | uint64_t stop = now(); |
| 375 | printf("Total time: %llu us\n", stop-start); |
| 376 | |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 377 | bufferedSocket->close(); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 378 | } |
| 379 | |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 380 | // printf("\nSocket syscalls: %u", g_socket_syscalls); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 381 | printf("\nAll tests done.\n"); |
| 382 | return 0; |
| 383 | } |