blob: 2e2f61bf6fa8c168de37f685dae465e1dc3c9192 [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);
101 printf(" = %lld\n", i64);
Mark Sleee8540632006-05-30 09:24:40 +0000102
103 /**
104 * STRUCT TEST
105 */
Mark Slee6e536442006-06-30 18:28:50 +0000106 printf("testStruct({\"Zero\", 1, -3, -5})");
Mark Sleee8540632006-05-30 09:24:40 +0000107 Xtruct out;
108 out.string_thing = "Zero";
109 out.byte_thing = 1;
Mark Sleee8540632006-05-30 09:24:40 +0000110 out.i32_thing = -3;
Mark Sleee8540632006-05-30 09:24:40 +0000111 out.i64_thing = -5;
112 Xtruct in = testClient.testStruct(out);
Marc Slemkobf4fd192006-08-15 21:29:39 +0000113 printf(" = {\"%s\", %d, %d, %lld}\n",
Mark Sleee8540632006-05-30 09:24:40 +0000114 in.string_thing.c_str(),
115 (int)in.byte_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000116 in.i32_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000117 in.i64_thing);
118
119 /**
120 * NESTED STRUCT TEST
121 */
Mark Slee6e536442006-06-30 18:28:50 +0000122 printf("testNest({1, {\"Zero\", 1, -3, -5}), 5}");
Mark Sleee8540632006-05-30 09:24:40 +0000123 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 Slemkobf4fd192006-08-15 21:29:39 +0000129 printf(" = {%d, {\"%s\", %d, %d, %lld}, %d}\n",
Mark Sleee8540632006-05-30 09:24:40 +0000130 in2.byte_thing,
131 in.string_thing.c_str(),
132 (int)in.byte_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000133 in.i32_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000134 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 Slemkobf4fd192006-08-15 21:29:39 +0000260 UserId uid = testClient.testTypedef(309858235082523LL);
261 printf(" = %lld\n", uid);
Mark Sleee8540632006-05-30 09:24:40 +0000262
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 Sleee8540632006-05-30 09:24:40 +0000288 truck.i32_thing = 8;
Mark Sleee8540632006-05-30 09:24:40 +0000289 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 Slemkobf4fd192006-08-15 21:29:39 +0000296 printf("%lld => {", i_iter->first);
Mark Sleee8540632006-05-30 09:24:40 +0000297 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 Slemkobf4fd192006-08-15 21:29:39 +0000306 printf("%d => %lld, ", um->first, um->second);
Mark Sleee8540632006-05-30 09:24:40 +0000307 }
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 Slemkobf4fd192006-08-15 21:29:39 +0000314 printf("{\"%s\", %d, %d, %lld}, ",
Mark Sleee8540632006-05-30 09:24:40 +0000315 x->string_thing.c_str(),
316 (int)x->byte_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000317 x->i32_thing,
Mark Sleee8540632006-05-30 09:24:40 +0000318 x->i64_thing);
319 }
320 printf("}");
321
322 printf("}, ");
323 }
324 printf("}, ");
325 }
326 printf("}\n");
327
Marc Slemko71d4e472006-08-15 22:34:04 +0000328 /* test exception */
Mark Slee95771002006-06-07 06:53:25 +0000329
Marc Slemkobf4fd192006-08-15 21:29:39 +0000330 try {
Marc Slemko71d4e472006-08-15 22:34:04 +0000331 printf("testClient.testException(\"Xception\") =>");
332 testClient.testException("Xception");
333 printf(" void\nFAILURE\n");
Marc Slemkobf4fd192006-08-15 21:29:39 +0000334
335 } catch(Xception& e) {
Marc Slemko71d4e472006-08-15 22:34:04 +0000336 printf(" {%u, \"%s\"}\n", e.errorCode, e.message.c_str());
Marc Slemkobf4fd192006-08-15 21:29:39 +0000337 }
Marc Slemko71d4e472006-08-15 22:34:04 +0000338
Marc Slemkobf4fd192006-08-15 21:29:39 +0000339 try {
Marc Slemko71d4e472006-08-15 22:34:04 +0000340 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 Slemkobf4fd192006-08-15 21:29:39 +0000359 Xtruct result = testClient.testMultiException("Xception2", "test 2");
Marc Slemko71d4e472006-08-15 22:34:04 +0000360 printf(" result\nFAILURE\n");
Marc Slemkobf4fd192006-08-15 21:29:39 +0000361
362 } catch(Xception2& e) {
Marc Slemko71d4e472006-08-15 22:34:04 +0000363 printf(" {%u, {\"%s\"}}\n", e.errorCode, e.struct_thing.string_thing.c_str());
Marc Slemkobf4fd192006-08-15 21:29:39 +0000364 }
365
Marc Slemko71d4e472006-08-15 22:34:04 +0000366 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 Slemkobf4fd192006-08-15 21:29:39 +0000373
Marc Slemkobf4fd192006-08-15 21:29:39 +0000374 uint64_t stop = now();
375 printf("Total time: %llu us\n", stop-start);
376
Marc Slemko6be374b2006-08-04 03:16:25 +0000377 bufferedSocket->close();
Mark Sleee8540632006-05-30 09:24:40 +0000378 }
379
Marc Slemko6be374b2006-08-04 03:16:25 +0000380 // printf("\nSocket syscalls: %u", g_socket_syscalls);
Mark Sleee8540632006-05-30 09:24:40 +0000381 printf("\nAll tests done.\n");
382 return 0;
383}