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