blob: e12b65b7925e97dc14282830c75de512e59c1fdd [file] [log] [blame]
Mark Sleee8540632006-05-30 09:24:40 +00001#include <stdio.h>
2#include <unistd.h>
Mark Slee95771002006-06-07 06:53:25 +00003#include <sys/time.h>
Marc Slemko6be374b2006-08-04 03:16:25 +00004#include <protocol/TBinaryProtocol.h>
5#include <transport/TBufferedTransport.h>
6#include <transport/TSocket.h>
Mark Sleee8540632006-05-30 09:24:40 +00007
Marc Slemko6be374b2006-08-04 03:16:25 +00008#include <boost/shared_ptr.hpp>
9#include "ThriftTest.h"
10
11using namespace boost;
12using namespace std;
13using namespace facebook::thrift;
14using namespace facebook::thrift::protocol;
15using namespace facebook::thrift::transport;
Marc Slemkobf4fd192006-08-15 21:29:39 +000016using namespace thrift::test;
Marc Slemko6be374b2006-08-04 03:16:25 +000017
18//extern uint32_t g_socket_syscalls;
Mark Slee95771002006-06-07 06:53:25 +000019
20// Current time, microseconds since the epoch
21uint64_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 Sleee8540632006-05-30 09:24:40 +000032int 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 Slemko6be374b2006-08-04 03:16:25 +000047 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 Sleee8540632006-05-30 09:24:40 +000051
52 int test = 0;
53 for (test = 0; test < numTests; ++test) {
Mark Slee95771002006-06-07 06:53:25 +000054
55 /**
56 * CONNECT TEST
57 */
Mark Sleee8540632006-05-30 09:24:40 +000058 printf("Test #%d, connect %s:%d\n", test+1, host.c_str(), port);
Mark Slee95771002006-06-07 06:53:25 +000059 try {
Marc Slemko6be374b2006-08-04 03:16:25 +000060 bufferedSocket->open();
Mark Slee95771002006-06-07 06:53:25 +000061 } catch (TTransportException& ttx) {
62 printf("Connect failed: %s\n", ttx.getMessage().c_str());
Mark Sleee8540632006-05-30 09:24:40 +000063 continue;
64 }
Mark Slee95771002006-06-07 06:53:25 +000065
66 uint64_t start = now();
Mark Sleee8540632006-05-30 09:24:40 +000067
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 Slee6e536442006-06-30 18:28:50 +000088
Mark Sleee8540632006-05-30 09:24:40 +000089 /**
90 * I32 TEST
91 */
92 printf("testI32(-1)");
93 int32_t i32 = testClient.testI32(-1);
94 printf(" = %d\n", i32);
95
96 /**
Mark Sleee8540632006-05-30 09:24:40 +000097 * I64 TEST
98 */
99 printf("testI64(-34359738368)");
Marc Slemkobf4fd192006-08-15 21:29:39 +0000100 int64_t i64 = testClient.testI64(-34359738368LL);
Mark Sleed3d733a2006-09-01 22:19:06 +0000101 printf(" = %ld\n", i64);
Mark Sleec98d0502006-09-06 02:42:25 +0000102
103 /**
104 * DOUBLE TEST
105 */
106 printf("testDouble(-5.2098523)");
107 double dub = testClient.testDouble(-5.2098523);
108 printf(" = %lf\n", dub);
Mark Sleee8540632006-05-30 09:24:40 +0000109
110 /**
111 * STRUCT TEST
112 */
Mark Slee6e536442006-06-30 18:28:50 +0000113 printf("testStruct({\"Zero\", 1, -3, -5})");
Mark Sleee8540632006-05-30 09:24:40 +0000114 Xtruct out;
115 out.string_thing = "Zero";
116 out.byte_thing = 1;
Mark Sleee8540632006-05-30 09:24:40 +0000117 out.i32_thing = -3;
Mark Sleee8540632006-05-30 09:24:40 +0000118 out.i64_thing = -5;
119 Xtruct in = testClient.testStruct(out);
Mark Sleed3d733a2006-09-01 22:19:06 +0000120 printf(" = {\"%s\", %d, %d, %ld}\n",
Mark Sleee8540632006-05-30 09:24:40 +0000121 in.string_thing.c_str(),
122 (int)in.byte_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000123 in.i32_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000124 in.i64_thing);
125
126 /**
127 * NESTED STRUCT TEST
128 */
Mark Slee6e536442006-06-30 18:28:50 +0000129 printf("testNest({1, {\"Zero\", 1, -3, -5}), 5}");
Mark Sleee8540632006-05-30 09:24:40 +0000130 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 Sleed3d733a2006-09-01 22:19:06 +0000136 printf(" = {%d, {\"%s\", %d, %d, %ld}, %d}\n",
Mark Sleee8540632006-05-30 09:24:40 +0000137 in2.byte_thing,
138 in.string_thing.c_str(),
139 (int)in.byte_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000140 in.i32_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000141 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 Slemkobf4fd192006-08-15 21:29:39 +0000267 UserId uid = testClient.testTypedef(309858235082523LL);
Mark Sleed3d733a2006-09-01 22:19:06 +0000268 printf(" = %ld\n", uid);
Mark Sleee8540632006-05-30 09:24:40 +0000269
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 Sleee8540632006-05-30 09:24:40 +0000295 truck.i32_thing = 8;
Mark Sleee8540632006-05-30 09:24:40 +0000296 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 Sleed3d733a2006-09-01 22:19:06 +0000303 printf("%ld => {", i_iter->first);
Mark Sleee8540632006-05-30 09:24:40 +0000304 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 Sleed3d733a2006-09-01 22:19:06 +0000313 printf("%d => %ld, ", um->first, um->second);
Mark Sleee8540632006-05-30 09:24:40 +0000314 }
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 Sleed3d733a2006-09-01 22:19:06 +0000321 printf("{\"%s\", %d, %d, %ld}, ",
Mark Sleee8540632006-05-30 09:24:40 +0000322 x->string_thing.c_str(),
323 (int)x->byte_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000324 x->i32_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000325 x->i64_thing);
326 }
327 printf("}");
328
329 printf("}, ");
330 }
331 printf("}, ");
332 }
333 printf("}\n");
334
Marc Slemko71d4e472006-08-15 22:34:04 +0000335 /* test exception */
Mark Slee95771002006-06-07 06:53:25 +0000336
Marc Slemkobf4fd192006-08-15 21:29:39 +0000337 try {
Marc Slemko71d4e472006-08-15 22:34:04 +0000338 printf("testClient.testException(\"Xception\") =>");
339 testClient.testException("Xception");
340 printf(" void\nFAILURE\n");
Marc Slemkobf4fd192006-08-15 21:29:39 +0000341
342 } catch(Xception& e) {
Marc Slemko71d4e472006-08-15 22:34:04 +0000343 printf(" {%u, \"%s\"}\n", e.errorCode, e.message.c_str());
Marc Slemkobf4fd192006-08-15 21:29:39 +0000344 }
Marc Slemko71d4e472006-08-15 22:34:04 +0000345
Marc Slemkobf4fd192006-08-15 21:29:39 +0000346 try {
Marc Slemko71d4e472006-08-15 22:34:04 +0000347 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 Sleed3d733a2006-09-01 22:19:06 +0000360 } catch(Xception& e) {
Marc Slemko71d4e472006-08-15 22:34:04 +0000361 printf(" {%u, \"%s\"}\n", e.errorCode, e.message.c_str());
362 }
363
364 try {
365 printf("testClient.testMultiException(\"Xception2\", \"test 2\") =>");
Marc Slemkobf4fd192006-08-15 21:29:39 +0000366 Xtruct result = testClient.testMultiException("Xception2", "test 2");
Marc Slemko71d4e472006-08-15 22:34:04 +0000367 printf(" result\nFAILURE\n");
Marc Slemkobf4fd192006-08-15 21:29:39 +0000368
Mark Sleed3d733a2006-09-01 22:19:06 +0000369 } catch(Xception2& e) {
Marc Slemko71d4e472006-08-15 22:34:04 +0000370 printf(" {%u, {\"%s\"}}\n", e.errorCode, e.struct_thing.string_thing.c_str());
Marc Slemkobf4fd192006-08-15 21:29:39 +0000371 }
372
Marc Slemko71d4e472006-08-15 22:34:04 +0000373 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 Slemkobf4fd192006-08-15 21:29:39 +0000380
Marc Slemkobf4fd192006-08-15 21:29:39 +0000381 uint64_t stop = now();
Mark Sleed3d733a2006-09-01 22:19:06 +0000382 printf("Total time: %lu us\n", stop-start);
Marc Slemkobf4fd192006-08-15 21:29:39 +0000383
Marc Slemko6be374b2006-08-04 03:16:25 +0000384 bufferedSocket->close();
Mark Sleee8540632006-05-30 09:24:40 +0000385 }
386
Marc Slemko6be374b2006-08-04 03:16:25 +0000387 // printf("\nSocket syscalls: %u", g_socket_syscalls);
Mark Sleee8540632006-05-30 09:24:40 +0000388 printf("\nAll tests done.\n");
389 return 0;
390}