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); |
Mark Slee | d3d733a | 2006-09-01 22:19:06 +0000 | [diff] [blame] | 101 | printf(" = %ld\n", i64); |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame^] | 102 | |
| 103 | /** |
| 104 | * DOUBLE TEST |
| 105 | */ |
| 106 | printf("testDouble(-5.2098523)"); |
| 107 | double dub = testClient.testDouble(-5.2098523); |
| 108 | printf(" = %lf\n", dub); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 109 | |
| 110 | /** |
| 111 | * STRUCT TEST |
| 112 | */ |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 113 | printf("testStruct({\"Zero\", 1, -3, -5})"); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 114 | Xtruct out; |
| 115 | out.string_thing = "Zero"; |
| 116 | out.byte_thing = 1; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 117 | out.i32_thing = -3; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 118 | out.i64_thing = -5; |
| 119 | Xtruct in = testClient.testStruct(out); |
Mark Slee | d3d733a | 2006-09-01 22:19:06 +0000 | [diff] [blame] | 120 | printf(" = {\"%s\", %d, %d, %ld}\n", |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 121 | in.string_thing.c_str(), |
| 122 | (int)in.byte_thing, |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 123 | in.i32_thing, |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 124 | in.i64_thing); |
| 125 | |
| 126 | /** |
| 127 | * NESTED STRUCT TEST |
| 128 | */ |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 129 | printf("testNest({1, {\"Zero\", 1, -3, -5}), 5}"); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 130 | Xtruct2 out2; |
| 131 | out2.byte_thing = 1; |
| 132 | out2.struct_thing = out; |
| 133 | out2.i32_thing = 5; |
| 134 | Xtruct2 in2 = testClient.testNest(out2); |
| 135 | in = in2.struct_thing; |
Mark Slee | d3d733a | 2006-09-01 22:19:06 +0000 | [diff] [blame] | 136 | printf(" = {%d, {\"%s\", %d, %d, %ld}, %d}\n", |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 137 | in2.byte_thing, |
| 138 | in.string_thing.c_str(), |
| 139 | (int)in.byte_thing, |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 140 | in.i32_thing, |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 141 | in.i64_thing, |
| 142 | in2.i32_thing); |
| 143 | |
| 144 | /** |
| 145 | * MAP TEST |
| 146 | */ |
| 147 | map<int32_t,int32_t> mapout; |
| 148 | for (int32_t i = 0; i < 5; ++i) { |
| 149 | mapout.insert(make_pair(i, i-10)); |
| 150 | } |
| 151 | printf("testMap({"); |
| 152 | map<int32_t, int32_t>::const_iterator m_iter; |
| 153 | bool first = true; |
| 154 | for (m_iter = mapout.begin(); m_iter != mapout.end(); ++m_iter) { |
| 155 | if (first) { |
| 156 | first = false; |
| 157 | } else { |
| 158 | printf(", "); |
| 159 | } |
| 160 | printf("%d => %d", m_iter->first, m_iter->second); |
| 161 | } |
| 162 | printf("})"); |
| 163 | map<int32_t,int32_t> mapin = testClient.testMap(mapout); |
| 164 | printf(" = {"); |
| 165 | first = true; |
| 166 | for (m_iter = mapin.begin(); m_iter != mapin.end(); ++m_iter) { |
| 167 | if (first) { |
| 168 | first = false; |
| 169 | } else { |
| 170 | printf(", "); |
| 171 | } |
| 172 | printf("%d => %d", m_iter->first, m_iter->second); |
| 173 | } |
| 174 | printf("}\n"); |
| 175 | |
| 176 | /** |
| 177 | * SET TEST |
| 178 | */ |
| 179 | set<int32_t> setout; |
| 180 | for (int32_t i = -2; i < 3; ++i) { |
| 181 | setout.insert(i); |
| 182 | } |
| 183 | printf("testSet({"); |
| 184 | set<int32_t>::const_iterator s_iter; |
| 185 | first = true; |
| 186 | for (s_iter = setout.begin(); s_iter != setout.end(); ++s_iter) { |
| 187 | if (first) { |
| 188 | first = false; |
| 189 | } else { |
| 190 | printf(", "); |
| 191 | } |
| 192 | printf("%d", *s_iter); |
| 193 | } |
| 194 | printf("})"); |
| 195 | set<int32_t> setin = testClient.testSet(setout); |
| 196 | printf(" = {"); |
| 197 | first = true; |
| 198 | for (s_iter = setin.begin(); s_iter != setin.end(); ++s_iter) { |
| 199 | if (first) { |
| 200 | first = false; |
| 201 | } else { |
| 202 | printf(", "); |
| 203 | } |
| 204 | printf("%d", *s_iter); |
| 205 | } |
| 206 | printf("}\n"); |
| 207 | |
| 208 | /** |
| 209 | * LIST TEST |
| 210 | */ |
| 211 | list<int32_t> listout; |
| 212 | for (int32_t i = -2; i < 3; ++i) { |
| 213 | listout.push_back(i); |
| 214 | } |
| 215 | printf("testList({"); |
| 216 | list<int32_t>::const_iterator l_iter; |
| 217 | first = true; |
| 218 | for (l_iter = listout.begin(); l_iter != listout.end(); ++l_iter) { |
| 219 | if (first) { |
| 220 | first = false; |
| 221 | } else { |
| 222 | printf(", "); |
| 223 | } |
| 224 | printf("%d", *l_iter); |
| 225 | } |
| 226 | printf("})"); |
| 227 | list<int32_t> listin = testClient.testList(listout); |
| 228 | printf(" = {"); |
| 229 | first = true; |
| 230 | for (l_iter = listin.begin(); l_iter != listin.end(); ++l_iter) { |
| 231 | if (first) { |
| 232 | first = false; |
| 233 | } else { |
| 234 | printf(", "); |
| 235 | } |
| 236 | printf("%d", *l_iter); |
| 237 | } |
| 238 | printf("}\n"); |
| 239 | |
| 240 | /** |
| 241 | * ENUM TEST |
| 242 | */ |
| 243 | printf("testEnum(ONE)"); |
| 244 | Numberz ret = testClient.testEnum(ONE); |
| 245 | printf(" = %d\n", ret); |
| 246 | |
| 247 | printf("testEnum(TWO)"); |
| 248 | ret = testClient.testEnum(TWO); |
| 249 | printf(" = %d\n", ret); |
| 250 | |
| 251 | printf("testEnum(THREE)"); |
| 252 | ret = testClient.testEnum(THREE); |
| 253 | printf(" = %d\n", ret); |
| 254 | |
| 255 | printf("testEnum(FIVE)"); |
| 256 | ret = testClient.testEnum(FIVE); |
| 257 | printf(" = %d\n", ret); |
| 258 | |
| 259 | printf("testEnum(EIGHT)"); |
| 260 | ret = testClient.testEnum(EIGHT); |
| 261 | printf(" = %d\n", ret); |
| 262 | |
| 263 | /** |
| 264 | * TYPEDEF TEST |
| 265 | */ |
| 266 | printf("testTypedef(309858235082523)"); |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 267 | UserId uid = testClient.testTypedef(309858235082523LL); |
Mark Slee | d3d733a | 2006-09-01 22:19:06 +0000 | [diff] [blame] | 268 | printf(" = %ld\n", uid); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 269 | |
| 270 | /** |
| 271 | * NESTED MAP TEST |
| 272 | */ |
| 273 | printf("testMapMap(1)"); |
| 274 | map<int32_t, map<int32_t, int32_t> > mm = testClient.testMapMap(1); |
| 275 | printf(" = {"); |
| 276 | map<int32_t, map<int32_t, int32_t> >::const_iterator mi; |
| 277 | for (mi = mm.begin(); mi != mm.end(); ++mi) { |
| 278 | printf("%d => {", mi->first); |
| 279 | map<int32_t, int32_t>::const_iterator mi2; |
| 280 | for (mi2 = mi->second.begin(); mi2 != mi->second.end(); ++mi2) { |
| 281 | printf("%d => %d, ", mi2->first, mi2->second); |
| 282 | } |
| 283 | printf("}, "); |
| 284 | } |
| 285 | printf("}\n"); |
| 286 | |
| 287 | /** |
| 288 | * INSANITY TEST |
| 289 | */ |
| 290 | Insanity insane; |
| 291 | insane.userMap.insert(make_pair(FIVE, 5000)); |
| 292 | Xtruct truck; |
| 293 | truck.string_thing = "Truck"; |
| 294 | truck.byte_thing = 8; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 295 | truck.i32_thing = 8; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 296 | truck.i64_thing = 8; |
| 297 | insane.xtructs.push_back(truck); |
| 298 | printf("testInsanity()"); |
| 299 | map<UserId, map<Numberz,Insanity> > whoa = testClient.testInsanity(insane); |
| 300 | printf(" = {"); |
| 301 | map<UserId, map<Numberz,Insanity> >::const_iterator i_iter; |
| 302 | for (i_iter = whoa.begin(); i_iter != whoa.end(); ++i_iter) { |
Mark Slee | d3d733a | 2006-09-01 22:19:06 +0000 | [diff] [blame] | 303 | printf("%ld => {", i_iter->first); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 304 | map<Numberz,Insanity>::const_iterator i2_iter; |
| 305 | for (i2_iter = i_iter->second.begin(); |
| 306 | i2_iter != i_iter->second.end(); |
| 307 | ++i2_iter) { |
| 308 | printf("%d => {", i2_iter->first); |
| 309 | map<Numberz, UserId> userMap = i2_iter->second.userMap; |
| 310 | map<Numberz, UserId>::const_iterator um; |
| 311 | printf("{"); |
| 312 | for (um = userMap.begin(); um != userMap.end(); ++um) { |
Mark Slee | d3d733a | 2006-09-01 22:19:06 +0000 | [diff] [blame] | 313 | printf("%d => %ld, ", um->first, um->second); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 314 | } |
| 315 | printf("}, "); |
| 316 | |
| 317 | list<Xtruct> xtructs = i2_iter->second.xtructs; |
| 318 | list<Xtruct>::const_iterator x; |
| 319 | printf("{"); |
| 320 | for (x = xtructs.begin(); x != xtructs.end(); ++x) { |
Mark Slee | d3d733a | 2006-09-01 22:19:06 +0000 | [diff] [blame] | 321 | printf("{\"%s\", %d, %d, %ld}, ", |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 322 | x->string_thing.c_str(), |
| 323 | (int)x->byte_thing, |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 324 | x->i32_thing, |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 325 | x->i64_thing); |
| 326 | } |
| 327 | printf("}"); |
| 328 | |
| 329 | printf("}, "); |
| 330 | } |
| 331 | printf("}, "); |
| 332 | } |
| 333 | printf("}\n"); |
| 334 | |
Marc Slemko | 71d4e47 | 2006-08-15 22:34:04 +0000 | [diff] [blame] | 335 | /* test exception */ |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 336 | |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 337 | try { |
Marc Slemko | 71d4e47 | 2006-08-15 22:34:04 +0000 | [diff] [blame] | 338 | printf("testClient.testException(\"Xception\") =>"); |
| 339 | testClient.testException("Xception"); |
| 340 | printf(" void\nFAILURE\n"); |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 341 | |
| 342 | } catch(Xception& e) { |
Marc Slemko | 71d4e47 | 2006-08-15 22:34:04 +0000 | [diff] [blame] | 343 | printf(" {%u, \"%s\"}\n", e.errorCode, e.message.c_str()); |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 344 | } |
Marc Slemko | 71d4e47 | 2006-08-15 22:34:04 +0000 | [diff] [blame] | 345 | |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 346 | try { |
Marc Slemko | 71d4e47 | 2006-08-15 22:34:04 +0000 | [diff] [blame] | 347 | printf("testClient.testException(\"success\") =>"); |
| 348 | testClient.testException("success"); |
| 349 | printf(" void\n"); |
| 350 | } catch(...) { |
| 351 | printf(" exception\nFAILURE\n"); |
| 352 | } |
| 353 | |
| 354 | /* test multi exception */ |
| 355 | |
| 356 | try { |
| 357 | printf("testClient.testMultiException(\"Xception\", \"test 1\") =>"); |
| 358 | Xtruct result = testClient.testMultiException("Xception", "test 1"); |
| 359 | printf(" result\nFAILURE\n"); |
Mark Slee | d3d733a | 2006-09-01 22:19:06 +0000 | [diff] [blame] | 360 | } catch(Xception& e) { |
Marc Slemko | 71d4e47 | 2006-08-15 22:34:04 +0000 | [diff] [blame] | 361 | printf(" {%u, \"%s\"}\n", e.errorCode, e.message.c_str()); |
| 362 | } |
| 363 | |
| 364 | try { |
| 365 | printf("testClient.testMultiException(\"Xception2\", \"test 2\") =>"); |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 366 | Xtruct result = testClient.testMultiException("Xception2", "test 2"); |
Marc Slemko | 71d4e47 | 2006-08-15 22:34:04 +0000 | [diff] [blame] | 367 | printf(" result\nFAILURE\n"); |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 368 | |
Mark Slee | d3d733a | 2006-09-01 22:19:06 +0000 | [diff] [blame] | 369 | } catch(Xception2& e) { |
Marc Slemko | 71d4e47 | 2006-08-15 22:34:04 +0000 | [diff] [blame] | 370 | 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] | 371 | } |
| 372 | |
Marc Slemko | 71d4e47 | 2006-08-15 22:34:04 +0000 | [diff] [blame] | 373 | try { |
| 374 | printf("testClient.testMultiException(\"success\", \"test 3\") =>"); |
| 375 | Xtruct result = testClient.testMultiException("success", "test 3"); |
| 376 | printf(" {{\"%s\"}}\n", result.string_thing.c_str()); |
| 377 | } catch(...) { |
| 378 | printf(" exception\nFAILURE\n"); |
| 379 | } |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 380 | |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 381 | uint64_t stop = now(); |
Mark Slee | d3d733a | 2006-09-01 22:19:06 +0000 | [diff] [blame] | 382 | printf("Total time: %lu us\n", stop-start); |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 383 | |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 384 | bufferedSocket->close(); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 385 | } |
| 386 | |
Marc Slemko | 6be374b | 2006-08-04 03:16:25 +0000 | [diff] [blame] | 387 | // printf("\nSocket syscalls: %u", g_socket_syscalls); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 388 | printf("\nAll tests done.\n"); |
| 389 | return 0; |
| 390 | } |