blob: 795e47fd85e2268dca9e3bcb6f79427a9c10651f [file] [log] [blame]
Roger Meierc1010922010-11-26 10:17:48 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
Roger Meier213a6642010-10-27 12:30:11 +000019
20/* test a C client with a C++ server */
21
22#include <signal.h>
23#include <sys/types.h>
24#include <sys/wait.h>
25#include <protocol/TBinaryProtocol.h>
26#include <protocol/TDebugProtocol.h>
27#include <server/TSimpleServer.h>
28#include <transport/TServerSocket.h>
29#include "ThriftTest.h"
30#include "ThriftTest_types.h"
31
32#include <iostream>
33
34using namespace std;
35using namespace boost;
36
37using namespace apache::thrift;
38using namespace apache::thrift::concurrency;
39using namespace apache::thrift::protocol;
40using namespace apache::thrift::transport;
41using namespace apache::thrift::server;
42
43using namespace thrift::test;
44
45#define TEST_PORT 9980
46
47// Extra functions required for ThriftTest_types to work
48namespace thrift { namespace test {
49
50bool Insanity::operator<(thrift::test::Insanity const& other) const {
51 using apache::thrift::ThriftDebugString;
52 return ThriftDebugString(*this) < ThriftDebugString(other);
53}
54
55}}
56
57class TestHandler : public ThriftTestIf {
58 public:
59 TestHandler() {}
60
61 void testVoid() {
62 printf("[C -> C++] testVoid()\n");
63 }
64
65 void testString(string& out, const string &thing) {
66 printf("[C -> C++] testString(\"%s\")\n", thing.c_str());
67 out = thing;
68 }
69
70 int8_t testByte(const int8_t thing) {
71 printf("[C -> C++] testByte(%d)\n", (int)thing);
72 return thing;
73 }
74 int32_t testI32(const int32_t thing) {
75 printf("[C -> C++] testI32(%d)\n", thing);
76 return thing;
77 }
78
79 int64_t testI64(const int64_t thing) {
80 printf("[C -> C++] testI64(%lld)\n", thing);
81 return thing;
82 }
83
84 double testDouble(const double thing) {
85 printf("[C -> C++] testDouble(%lf)\n", thing);
86 return thing;
87 }
88
89 void testStruct(Xtruct& out, const Xtruct &thing) {
90 printf("[C -> C++] testStruct({\"%s\", %d, %d, %lld})\n", thing.string_thing.c_str(), (int)thing.byte_thing, thing.i32_thing, thing.i64_thing);
91 out = thing;
92 }
93
94 void testNest(Xtruct2& out, const Xtruct2& nest) {
95 const Xtruct &thing = nest.struct_thing;
96 printf("[C -> C++] testNest({%d, {\"%s\", %d, %d, %lld}, %d})\n", (int)nest.byte_thing, thing.string_thing.c_str(), (int)thing.byte_thing, thing.i32_thing, thing.i64_thing, nest.i32_thing);
97 out = nest;
98 }
99
100 void testMap(map<int32_t, int32_t> &out, const map<int32_t, int32_t> &thing) {
101 printf("[C -> C++] testMap({");
102 map<int32_t, int32_t>::const_iterator m_iter;
103 bool first = true;
104 for (m_iter = thing.begin(); m_iter != thing.end(); ++m_iter) {
105 if (first) {
106 first = false;
107 } else {
108 printf(", ");
109 }
110 printf("%d => %d", m_iter->first, m_iter->second);
111 }
112 printf("})\n");
113 out = thing;
114 }
115
Roger Meiera1c416f2011-06-17 19:40:48 +0000116 void testStringMap(map<std::string, std::string> &out, const map<std::string, std::string> &thing) {
117 printf("[C -> C++] testStringMap({");
118 map<std::string, std::string>::const_iterator m_iter;
119 bool first = true;
120 for (m_iter = thing.begin(); m_iter != thing.end(); ++m_iter) {
121 if (first) {
122 first = false;
123 } else {
124 printf(", ");
125 }
126 printf("\"%s\" => \"%s\"", (m_iter->first).c_str(), (m_iter->second).c_str());
127 }
128 printf("})\n");
129 out = thing;
130 }
131
132
Roger Meier213a6642010-10-27 12:30:11 +0000133 void testSet(set<int32_t> &out, const set<int32_t> &thing) {
134 printf("[C -> C++] testSet({");
135 set<int32_t>::const_iterator s_iter;
136 bool first = true;
137 for (s_iter = thing.begin(); s_iter != thing.end(); ++s_iter) {
138 if (first) {
139 first = false;
140 } else {
141 printf(", ");
142 }
143 printf("%d", *s_iter);
144 }
145 printf("})\n");
146 out = thing;
147 }
148
149 void testList(vector<int32_t> &out, const vector<int32_t> &thing) {
150 printf("[C -> C++] testList({");
151 vector<int32_t>::const_iterator l_iter;
152 bool first = true;
153 for (l_iter = thing.begin(); l_iter != thing.end(); ++l_iter) {
154 if (first) {
155 first = false;
156 } else { printf(", ");
157 }
158 printf("%d", *l_iter);
159 }
160 printf("})\n");
161 out = thing;
162 }
163
164 Numberz::type testEnum(const Numberz::type thing) {
165 printf("[C -> C++] testEnum(%d)\n", thing);
166 return thing;
167 }
168
169 UserId testTypedef(const UserId thing) {
170 printf("[C -> C++] testTypedef(%lld)\n", thing);
171 return thing; }
172
173 void testMapMap(map<int32_t, map<int32_t,int32_t> > &mapmap, const int32_t hello) {
174 printf("[C -> C++] testMapMap(%d)\n", hello);
175
176 map<int32_t,int32_t> pos;
177 map<int32_t,int32_t> neg;
178 for (int i = 1; i < 5; i++) {
179 pos.insert(make_pair(i,i));
180 neg.insert(make_pair(-i,-i));
181 }
182
183 mapmap.insert(make_pair(4, pos));
184 mapmap.insert(make_pair(-4, neg));
185
186 }
187
188 void testInsanity(map<UserId, map<Numberz::type,Insanity> > &insane, const Insanity &argument) {
189 printf("[C -> C++] testInsanity()\n");
190
191 Xtruct hello;
192 hello.string_thing = "Hello2";
193 hello.byte_thing = 2;
194 hello.i32_thing = 2;
195 hello.i64_thing = 2;
196
197 Xtruct goodbye;
198 goodbye.string_thing = "Goodbye4";
199 goodbye.byte_thing = 4;
200 goodbye.i32_thing = 4;
201 goodbye.i64_thing = 4;
202
203 Insanity crazy;
204 crazy.userMap.insert(make_pair(Numberz::EIGHT, 8));
205 crazy.xtructs.push_back(goodbye);
206
207 Insanity looney;
208 crazy.userMap.insert(make_pair(Numberz::FIVE, 5));
209 crazy.xtructs.push_back(hello);
210
211 map<Numberz::type, Insanity> first_map;
212 map<Numberz::type, Insanity> second_map;
213
214 first_map.insert(make_pair(Numberz::TWO, crazy));
215 first_map.insert(make_pair(Numberz::THREE, crazy));
216
217 second_map.insert(make_pair(Numberz::SIX, looney));
218
219 insane.insert(make_pair(1, first_map));
220 insane.insert(make_pair(2, second_map));
221
222 printf("return");
223 printf(" = {");
224 map<UserId, map<Numberz::type,Insanity> >::const_iterator i_iter;
225 for (i_iter = insane.begin(); i_iter != insane.end(); ++i_iter) {
226 printf("%lld => {", i_iter->first);
227 map<Numberz::type,Insanity>::const_iterator i2_iter;
228 for (i2_iter = i_iter->second.begin();
229 i2_iter != i_iter->second.end();
230 ++i2_iter) {
231 printf("%d => {", i2_iter->first);
232 map<Numberz::type, UserId> userMap = i2_iter->second.userMap;
233 map<Numberz::type, UserId>::const_iterator um;
234 printf("{");
235 for (um = userMap.begin(); um != userMap.end(); ++um) {
236 printf("%d => %lld, ", um->first, um->second);
237 }
238 printf("}, ");
239
240 vector<Xtruct> xtructs = i2_iter->second.xtructs;
241 vector<Xtruct>::const_iterator x;
242 printf("{");
243 for (x = xtructs.begin(); x != xtructs.end(); ++x) {
244 printf("{\"%s\", %d, %d, %lld}, ", x->string_thing.c_str(), (int)x->byte_thing, x->i32_thing, x->i64_thing);
245 }
246 printf("}");
247
248 printf("}, ");
249 }
250 printf("}, ");
251 }
252 printf("}\n");
253
254
255 }
256
257 void testMulti(Xtruct &hello, const int8_t arg0, const int32_t arg1, const int64_t arg2, const std::map<int16_t, std::string> &arg3, const Numberz::type arg4, const UserId arg5) {
258 printf("[C -> C++] testMulti()\n");
259
260 hello.string_thing = "Hello2";
261 hello.byte_thing = arg0;
262 hello.i32_thing = arg1;
263 hello.i64_thing = (int64_t)arg2;
264 }
265
266 void testException(const std::string &arg)
267 throw(Xception, apache::thrift::TException)
268 {
269 printf("[C -> C++] testException(%s)\n", arg.c_str());
270 if (arg.compare("Xception") == 0) {
271 Xception e;
272 e.errorCode = 1001;
273 e.message = arg;
274 throw e;
275 } else if (arg.compare("ApplicationException") == 0) {
276 apache::thrift::TException e;
277 throw e;
278 } else {
279 Xtruct result;
280 result.string_thing = arg;
281 return;
282 }
283 }
284
285 void testMultiException(Xtruct &result, const std::string &arg0, const std::string &arg1) throw(Xception, Xception2) {
286
287 printf("[C -> C++] testMultiException(%s, %s)\n", arg0.c_str(), arg1.c_str());
288
289 if (arg0.compare("Xception") == 0) {
290 Xception e;
291 e.errorCode = 1001;
292 e.message = "This is an Xception";
293 throw e;
294 } else if (arg0.compare("Xception2") == 0) {
295 Xception2 e;
296 e.errorCode = 2002;
297 e.struct_thing.string_thing = "This is an Xception2";
298 throw e;
299 } else {
300 result.string_thing = arg1;
301 return;
302 }
303 }
304
305 void testOneway(int sleepFor) {
306 printf("testOneway(%d): Sleeping...\n", sleepFor);
307 sleep(sleepFor);
308 printf("testOneway(%d): done sleeping!\n", sleepFor);
309 }
310};
311
312// C CLIENT
313extern "C" {
314
315#include "t_test_thrift_test.h"
316#include "t_test_thrift_test_types.h"
317#include "transport/thrift_socket.h"
318#include "protocol/thrift_protocol.h"
319#include "protocol/thrift_binary_protocol.h"
320
321static void
322test_thrift_client (void)
323{
324 ThriftSocket *tsocket = NULL;
325 ThriftBinaryProtocol *protocol = NULL;
326 TTestThriftTestClient *client = NULL;
327 TTestThriftTestIf *iface = NULL;
328 GError *error = NULL;
329 gchar *string = NULL;
330 gint8 byte = 0;
331 gint16 i16 = 0;
332 gint32 i32 = 0, another_i32 = 56789;
333 gint64 i64 = 0;
334 double dbl = 0.0;
335 TTestXtruct *xtruct_in, *xtruct_out;
336 TTestXtruct2 *xtruct2_in, *xtruct2_out;
337 GHashTable *map_in = NULL, *map_out = NULL;
338 GHashTable *set_in = NULL, *set_out = NULL;
339 GArray *list_in = NULL, *list_out = NULL;
340 TTestNumberz enum_in, enum_out;
341 TTestUserId user_id_in, user_id_out;
342 GHashTable *insanity_in = NULL;
343 TTestXtruct *xtruct1, *xtruct2;
344 TTestInsanity *insanity_out = NULL;
345 TTestXtruct *multi_in = NULL;
346 GHashTable *multi_map_out = NULL;
347 TTestXception *xception = NULL;
348 TTestXception2 *xception2 = NULL;
349
350 // initialize gobject
351 g_type_init ();
352
353 // create a C client
354 tsocket = (ThriftSocket *) g_object_new (THRIFT_TYPE_SOCKET,
355 "hostname", "localhost",
356 "port", TEST_PORT, NULL);
357 protocol = (ThriftBinaryProtocol *) g_object_new (THRIFT_TYPE_BINARY_PROTOCOL,
358 "transport",
359 tsocket, NULL);
360 client = (TTestThriftTestClient *) g_object_new (T_TEST_TYPE_THRIFT_TEST_CLIENT, "input_protocol", protocol, "output_protocol", protocol, NULL);
361 iface = T_TEST_THRIFT_TEST_IF (client);
362
363 // open and send
364 thrift_transport_open (THRIFT_TRANSPORT(tsocket), NULL);
365
366 assert (t_test_thrift_test_client_test_void (iface, &error) == TRUE);
367 assert (error == NULL);
368
369 assert (t_test_thrift_test_client_test_string (iface, &string, "test123", &error) == TRUE);
370 assert (strcmp (string, "test123") == 0);
371 g_free (string);
372 assert (error == NULL);
373
374 assert (t_test_thrift_test_client_test_byte (iface, &byte, (gint8) 5, &error) == TRUE);
375 assert (byte == 5);
376 assert (error == NULL);
377
378 assert (t_test_thrift_test_client_test_i32 (iface, &i32, 123, &error) == TRUE);
379 assert (i32 == 123);
380 assert (error == NULL);
381
382 assert (t_test_thrift_test_client_test_i64 (iface, &i64, 12345, &error) == TRUE);
383 assert (i64 == 12345);
384 assert (error == NULL);
385
386 assert (t_test_thrift_test_client_test_double (iface, &dbl, 5.6, &error) == TRUE);
387 assert (dbl == 5.6);
388 assert (error == NULL);
389
390 xtruct_out = (TTestXtruct *) g_object_new (T_TEST_TYPE_XTRUCT, NULL);
391 xtruct_out->byte_thing = 1;
392 xtruct_out->__isset_byte_thing = TRUE;
393 xtruct_out->i32_thing = 15;
394 xtruct_out->__isset_i32_thing = TRUE;
395 xtruct_out->i64_thing = 151;
396 xtruct_out->__isset_i64_thing = TRUE;
397 xtruct_out->string_thing = g_strdup ("abc123");
398 xtruct_out->__isset_string_thing = TRUE;
399 assert (t_test_thrift_test_client_test_struct (iface, &xtruct_in, xtruct_out, &error) == TRUE);
400 assert (error == NULL);
401
402 xtruct2_out = (TTestXtruct2 *) g_object_new (T_TEST_TYPE_XTRUCT2, NULL);
403 xtruct2_out->byte_thing = 1;
404 xtruct2_out->__isset_byte_thing = TRUE;
405 xtruct2_out->struct_thing = xtruct_out;
406 xtruct2_out->__isset_struct_thing = TRUE;
407 xtruct2_out->i32_thing = 123;
408 xtruct2_out->__isset_i32_thing = TRUE;
409 assert (t_test_thrift_test_client_test_nest (iface, &xtruct2_in, xtruct2_out, &error) == TRUE);
410 assert (error == NULL);
411
412 g_object_unref (xtruct2_out);
413 g_object_unref (xtruct2_in);
414 g_free (xtruct_out->string_thing);
415 g_object_unref (xtruct_out);
416 g_object_unref (xtruct_in);
417
418 map_out = g_hash_table_new (NULL, NULL);
419 map_in = g_hash_table_new (NULL, NULL); g_hash_table_insert (map_out, &i32, &i32);
420 assert (t_test_thrift_test_client_test_map (iface, &map_in, map_out, &error) == TRUE);
421 assert (error == NULL);
422 g_hash_table_destroy (map_out);
423 g_hash_table_destroy (map_in);
424
Roger Meiera1c416f2011-06-17 19:40:48 +0000425 map_out = g_hash_table_new (NULL, NULL);
426 map_in = g_hash_table_new (NULL, NULL);
427 g_hash_table_insert (map_out, g_strdup ("a"), g_strdup ("123"));
428 g_hash_table_insert (map_out, g_strdup ("a b"), g_strdup ("with spaces "));
429 g_hash_table_insert (map_out, g_strdup ("same"), g_strdup ("same"));
430 g_hash_table_insert (map_out, g_strdup ("0"), g_strdup ("numeric key"));
431 assert (t_test_thrift_test_client_test_string_map (iface, &map_in, map_out, &error) == TRUE);
432 assert (error == NULL);
433 g_hash_table_destroy (map_out);
434 g_hash_table_destroy (map_in);
435
Roger Meier213a6642010-10-27 12:30:11 +0000436 set_out = g_hash_table_new (NULL, NULL);
437 set_in = g_hash_table_new (NULL, NULL);
438 g_hash_table_insert (set_out, &i32, &i32);
439 assert (t_test_thrift_test_client_test_set (iface, &set_in, set_out, &error) == TRUE);
440 assert (error == NULL);
441 g_hash_table_destroy (set_out);
442 g_hash_table_destroy (set_in);
443
444 list_out = g_array_new(TRUE, TRUE, sizeof(gint32));
445 list_in = g_array_new(TRUE, TRUE, sizeof(gint32));
446 another_i32 = 456;
447 g_array_append_val (list_out, i32);
448 g_array_append_val (list_out, another_i32);
449 assert (t_test_thrift_test_client_test_list (iface, &list_in, list_out, &error) == TRUE);
450 assert (error == NULL);
451 g_array_free (list_out, TRUE);
452 g_array_free (list_in, TRUE);
453
454 enum_out = T_TEST_NUMBERZ_ONE;
455 assert (t_test_thrift_test_client_test_enum (iface, &enum_in, enum_out, &error) == TRUE);
456 assert (enum_in == enum_out);
457 assert (error == NULL);
458
459 user_id_out = 12345;
460 assert (t_test_thrift_test_client_test_typedef (iface, &user_id_in, user_id_out, &error) == TRUE);
461 assert (user_id_in == user_id_out);
462 assert (error == NULL);
463
464 map_in = g_hash_table_new (NULL, NULL);
465 assert (t_test_thrift_test_client_test_map_map (iface, &map_in, i32, &error) == TRUE);
466 assert (error == NULL);
467 g_hash_table_destroy (map_in);
468
469 // insanity
470 insanity_out = (TTestInsanity *) g_object_new (T_TEST_TYPE_INSANITY, NULL);
471 insanity_out->userMap = g_hash_table_new (NULL, NULL);
472 g_hash_table_insert (insanity_out->userMap, &enum_out, &user_id_out);
473
474 xtruct1 = (TTestXtruct *) g_object_new (T_TEST_TYPE_XTRUCT, NULL);
475 xtruct1->byte_thing = 1;
476 xtruct1->__isset_byte_thing = TRUE;
477 xtruct1->i32_thing = 15;
478 xtruct1->__isset_i32_thing = TRUE;
479 xtruct1->i64_thing = 151;
480 xtruct1->__isset_i64_thing = TRUE;
481 xtruct1->string_thing = g_strdup ("abc123");
482 xtruct1->__isset_string_thing = TRUE;
483 xtruct2 = (TTestXtruct *) g_object_new (T_TEST_TYPE_XTRUCT, NULL);
484 xtruct2->byte_thing = 1;
485 xtruct2->__isset_byte_thing = TRUE;
486 xtruct2->i32_thing = 15;
487 xtruct2->__isset_i32_thing = TRUE;
488 xtruct2->i64_thing = 151;
489 xtruct2->__isset_i64_thing = TRUE;
490 xtruct2->string_thing = g_strdup ("abc123");
491 xtruct2->__isset_string_thing = TRUE;
492
493 insanity_out->xtructs = g_ptr_array_new ();
494 insanity_in = g_hash_table_new (NULL, NULL);
495 g_ptr_array_add (insanity_out->xtructs, xtruct1);
496 g_ptr_array_add (insanity_out->xtructs, xtruct2);
497 assert (t_test_thrift_test_client_test_insanity (iface, &insanity_in, insanity_out, &error) == TRUE);
498
499 g_hash_table_unref (insanity_in);
500 g_ptr_array_free (insanity_out->xtructs, TRUE);
501 g_free (xtruct1->string_thing);
502 g_free (xtruct2->string_thing);
503 g_object_unref (xtruct1);
504 g_object_unref (xtruct2);
505
506 multi_map_out = g_hash_table_new (NULL, NULL);
507 string = g_strdup ("abc123");
508 g_hash_table_insert (multi_map_out, &i16, string);
509 assert (t_test_thrift_test_client_test_multi (iface, &multi_in, byte, i32, i64, multi_map_out, enum_out, user_id_out, &error) == TRUE);
510 assert (multi_in->i32_thing == i32);
511 assert (multi_in->i64_thing == i64);
512 g_object_unref (multi_in);
513 g_hash_table_unref (multi_map_out);
514 g_free (string);
515
516 assert (t_test_thrift_test_client_test_exception (iface, "Xception", &xception, &error) == FALSE);
517 assert (xception->errorCode == 1001);
518 g_error_free (error);
519 error = NULL;
520
521 assert (t_test_thrift_test_client_test_exception (iface, "ApplicationException", &xception, &error) == FALSE);
522 g_error_free (error);
523 error = NULL;
524 g_object_unref (xception);
525 xception = NULL;
526
527 assert (t_test_thrift_test_client_test_exception (iface, "Test", &xception, &error) == TRUE);
528 assert (error == NULL);
529
530 assert (t_test_thrift_test_client_test_multi_exception (iface, &multi_in, "Xception", NULL, &xception, &xception2, &error) == FALSE);
531 assert (xception->errorCode == 1001);
532 g_error_free (error);
533 error = NULL;
534 g_object_unref (xception);
535 xception = NULL;
536 xception2 = NULL;
537
538 assert (t_test_thrift_test_client_test_multi_exception (iface, &multi_in, "Xception2", NULL, &xception, &xception2, &error) == FALSE);
539 assert (xception2->errorCode == 2002);
540 g_error_free (error);
541 error = NULL;
542 g_object_unref (xception2);
543 xception2 = NULL;
544
545 assert (t_test_thrift_test_client_test_multi_exception (iface, &multi_in, NULL , NULL, &xception, &xception2, &error) == TRUE);
546 assert (error == NULL);
547
548 assert (t_test_thrift_test_client_test_oneway (iface, 1, &error) == TRUE);
549 assert (error == NULL);
550
551 /* sleep to let the oneway call go through */
552 sleep (5);
553
554 thrift_transport_close (THRIFT_TRANSPORT(tsocket), NULL);
555 g_object_unref (client);
556 g_object_unref (protocol);
557 g_object_unref (tsocket);
558}
559
560
561} /* extern "C" */
562
563
564static void
565bailout (int signum)
566{
567 exit (1);
568}
569
570int
571main (int argc, char **argv)
572{
573 int status;
574 int pid = fork ();
575 assert (pid >= 0);
576
577 if (pid == 0) /* child */
578 {
579 shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());
580 shared_ptr<TestHandler> testHandler(new TestHandler());
581 shared_ptr<ThriftTestProcessor> testProcessor(new ThriftTestProcessor(testHandler));
582 shared_ptr<TServerSocket> serverSocket(new TServerSocket(TEST_PORT));
583 shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
584 TSimpleServer simpleServer(testProcessor, serverSocket, transportFactory, protocolFactory);
585 signal (SIGALRM, bailout);
586 alarm (60);
587 simpleServer.serve();
588 } else {
589 sleep (1);
590 test_thrift_client ();
591 kill (pid, SIGINT);
592 wait (&status) == pid;
593 }
594
595 return 0;
596}
597