blob: e618fe9381ad87ff97ff2130fe246e2c0d700805 [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>
Roger Meier49ff8b12012-04-13 09:12:31 +000025#include <thrift/protocol/TBinaryProtocol.h>
26#include <thrift/protocol/TDebugProtocol.h>
27#include <thrift/server/TSimpleServer.h>
28#include <thrift/transport/TServerSocket.h>
Roger Meier213a6642010-10-27 12:30:11 +000029#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
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +090070 bool testBool(const bool thing) {
71 printf("[C -> C++] testBool(%s)\n", thing ? "true" : "false");
72 return thing;
73 }
Roger Meier213a6642010-10-27 12:30:11 +000074 int8_t testByte(const int8_t thing) {
75 printf("[C -> C++] testByte(%d)\n", (int)thing);
76 return thing;
77 }
78 int32_t testI32(const int32_t thing) {
79 printf("[C -> C++] testI32(%d)\n", thing);
80 return thing;
81 }
82
83 int64_t testI64(const int64_t thing) {
Roger Meier79e971e2015-04-12 13:11:33 +020084 printf("[C -> C++] testI64(%ld)\n", thing);
Roger Meier213a6642010-10-27 12:30:11 +000085 return thing;
86 }
87
88 double testDouble(const double thing) {
89 printf("[C -> C++] testDouble(%lf)\n", thing);
90 return thing;
91 }
92
Jens Geyer8bcfdd92014-12-14 03:14:26 +010093 void testBinary(string& out, const string &thing) {
94 printf("[C -> C++] testBinary(\"%s\")\n", thing.c_str());
95 out = thing;
96 }
97
Roger Meier213a6642010-10-27 12:30:11 +000098 void testStruct(Xtruct& out, const Xtruct &thing) {
Roger Meier79e971e2015-04-12 13:11:33 +020099 printf("[C -> C++] testStruct({\"%s\", %d, %d, %ld})\n", thing.string_thing.c_str(), (int)thing.byte_thing, thing.i32_thing, thing.i64_thing);
Roger Meier213a6642010-10-27 12:30:11 +0000100 out = thing;
101 }
102
103 void testNest(Xtruct2& out, const Xtruct2& nest) {
104 const Xtruct &thing = nest.struct_thing;
Roger Meier79e971e2015-04-12 13:11:33 +0200105 printf("[C -> C++] testNest({%d, {\"%s\", %d, %d, %ld}, %d})\n", (int)nest.byte_thing, thing.string_thing.c_str(), (int)thing.byte_thing, thing.i32_thing, thing.i64_thing, nest.i32_thing);
Roger Meier213a6642010-10-27 12:30:11 +0000106 out = nest;
107 }
108
109 void testMap(map<int32_t, int32_t> &out, const map<int32_t, int32_t> &thing) {
110 printf("[C -> C++] testMap({");
111 map<int32_t, int32_t>::const_iterator m_iter;
112 bool first = true;
113 for (m_iter = thing.begin(); m_iter != thing.end(); ++m_iter) {
114 if (first) {
115 first = false;
116 } else {
117 printf(", ");
118 }
119 printf("%d => %d", m_iter->first, m_iter->second);
120 }
121 printf("})\n");
122 out = thing;
123 }
124
Roger Meiera1c416f2011-06-17 19:40:48 +0000125 void testStringMap(map<std::string, std::string> &out, const map<std::string, std::string> &thing) {
126 printf("[C -> C++] testStringMap({");
127 map<std::string, std::string>::const_iterator m_iter;
128 bool first = true;
129 for (m_iter = thing.begin(); m_iter != thing.end(); ++m_iter) {
130 if (first) {
131 first = false;
132 } else {
133 printf(", ");
134 }
135 printf("\"%s\" => \"%s\"", (m_iter->first).c_str(), (m_iter->second).c_str());
136 }
137 printf("})\n");
138 out = thing;
139 }
140
141
Roger Meier213a6642010-10-27 12:30:11 +0000142 void testSet(set<int32_t> &out, const set<int32_t> &thing) {
143 printf("[C -> C++] testSet({");
144 set<int32_t>::const_iterator s_iter;
145 bool first = true;
146 for (s_iter = thing.begin(); s_iter != thing.end(); ++s_iter) {
147 if (first) {
148 first = false;
149 } else {
150 printf(", ");
151 }
152 printf("%d", *s_iter);
153 }
154 printf("})\n");
155 out = thing;
156 }
157
158 void testList(vector<int32_t> &out, const vector<int32_t> &thing) {
159 printf("[C -> C++] testList({");
160 vector<int32_t>::const_iterator l_iter;
161 bool first = true;
162 for (l_iter = thing.begin(); l_iter != thing.end(); ++l_iter) {
163 if (first) {
164 first = false;
165 } else { printf(", ");
166 }
167 printf("%d", *l_iter);
168 }
169 printf("})\n");
170 out = thing;
171 }
172
173 Numberz::type testEnum(const Numberz::type thing) {
174 printf("[C -> C++] testEnum(%d)\n", thing);
175 return thing;
176 }
177
178 UserId testTypedef(const UserId thing) {
Roger Meier79e971e2015-04-12 13:11:33 +0200179 printf("[C -> C++] testTypedef(%ld)\n", thing);
Roger Meier213a6642010-10-27 12:30:11 +0000180 return thing; }
181
182 void testMapMap(map<int32_t, map<int32_t,int32_t> > &mapmap, const int32_t hello) {
183 printf("[C -> C++] testMapMap(%d)\n", hello);
184
185 map<int32_t,int32_t> pos;
186 map<int32_t,int32_t> neg;
187 for (int i = 1; i < 5; i++) {
188 pos.insert(make_pair(i,i));
189 neg.insert(make_pair(-i,-i));
190 }
191
192 mapmap.insert(make_pair(4, pos));
193 mapmap.insert(make_pair(-4, neg));
194
195 }
196
197 void testInsanity(map<UserId, map<Numberz::type,Insanity> > &insane, const Insanity &argument) {
Simon South38e71552015-08-03 10:51:16 +0000198 THRIFT_UNUSED_VARIABLE (argument);
199
Roger Meier213a6642010-10-27 12:30:11 +0000200 printf("[C -> C++] testInsanity()\n");
201
202 Xtruct hello;
203 hello.string_thing = "Hello2";
204 hello.byte_thing = 2;
205 hello.i32_thing = 2;
206 hello.i64_thing = 2;
207
208 Xtruct goodbye;
209 goodbye.string_thing = "Goodbye4";
210 goodbye.byte_thing = 4;
211 goodbye.i32_thing = 4;
212 goodbye.i64_thing = 4;
213
214 Insanity crazy;
215 crazy.userMap.insert(make_pair(Numberz::EIGHT, 8));
216 crazy.xtructs.push_back(goodbye);
217
218 Insanity looney;
219 crazy.userMap.insert(make_pair(Numberz::FIVE, 5));
220 crazy.xtructs.push_back(hello);
221
222 map<Numberz::type, Insanity> first_map;
223 map<Numberz::type, Insanity> second_map;
224
225 first_map.insert(make_pair(Numberz::TWO, crazy));
226 first_map.insert(make_pair(Numberz::THREE, crazy));
227
228 second_map.insert(make_pair(Numberz::SIX, looney));
229
230 insane.insert(make_pair(1, first_map));
231 insane.insert(make_pair(2, second_map));
232
233 printf("return");
234 printf(" = {");
235 map<UserId, map<Numberz::type,Insanity> >::const_iterator i_iter;
236 for (i_iter = insane.begin(); i_iter != insane.end(); ++i_iter) {
Roger Meier79e971e2015-04-12 13:11:33 +0200237 printf("%ld => {", i_iter->first);
Roger Meier213a6642010-10-27 12:30:11 +0000238 map<Numberz::type,Insanity>::const_iterator i2_iter;
239 for (i2_iter = i_iter->second.begin();
240 i2_iter != i_iter->second.end();
241 ++i2_iter) {
242 printf("%d => {", i2_iter->first);
243 map<Numberz::type, UserId> userMap = i2_iter->second.userMap;
244 map<Numberz::type, UserId>::const_iterator um;
245 printf("{");
246 for (um = userMap.begin(); um != userMap.end(); ++um) {
Roger Meier79e971e2015-04-12 13:11:33 +0200247 printf("%d => %ld, ", um->first, um->second);
Roger Meier213a6642010-10-27 12:30:11 +0000248 }
249 printf("}, ");
250
251 vector<Xtruct> xtructs = i2_iter->second.xtructs;
252 vector<Xtruct>::const_iterator x;
253 printf("{");
254 for (x = xtructs.begin(); x != xtructs.end(); ++x) {
Roger Meier79e971e2015-04-12 13:11:33 +0200255 printf("{\"%s\", %d, %d, %ld}, ", x->string_thing.c_str(), (int)x->byte_thing, x->i32_thing, x->i64_thing);
Roger Meier213a6642010-10-27 12:30:11 +0000256 }
257 printf("}");
258
259 printf("}, ");
260 }
261 printf("}, ");
262 }
263 printf("}\n");
264
265
266 }
267
268 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) {
Simon South38e71552015-08-03 10:51:16 +0000269 THRIFT_UNUSED_VARIABLE (arg3);
270 THRIFT_UNUSED_VARIABLE (arg4);
271 THRIFT_UNUSED_VARIABLE (arg5);
272
Roger Meier213a6642010-10-27 12:30:11 +0000273 printf("[C -> C++] testMulti()\n");
274
275 hello.string_thing = "Hello2";
276 hello.byte_thing = arg0;
277 hello.i32_thing = arg1;
278 hello.i64_thing = (int64_t)arg2;
279 }
280
281 void testException(const std::string &arg)
282 throw(Xception, apache::thrift::TException)
283 {
284 printf("[C -> C++] testException(%s)\n", arg.c_str());
285 if (arg.compare("Xception") == 0) {
286 Xception e;
287 e.errorCode = 1001;
288 e.message = arg;
289 throw e;
290 } else if (arg.compare("ApplicationException") == 0) {
291 apache::thrift::TException e;
292 throw e;
293 } else {
294 Xtruct result;
295 result.string_thing = arg;
296 return;
297 }
298 }
299
300 void testMultiException(Xtruct &result, const std::string &arg0, const std::string &arg1) throw(Xception, Xception2) {
301
302 printf("[C -> C++] testMultiException(%s, %s)\n", arg0.c_str(), arg1.c_str());
303
304 if (arg0.compare("Xception") == 0) {
305 Xception e;
306 e.errorCode = 1001;
307 e.message = "This is an Xception";
308 throw e;
309 } else if (arg0.compare("Xception2") == 0) {
310 Xception2 e;
311 e.errorCode = 2002;
312 e.struct_thing.string_thing = "This is an Xception2";
313 throw e;
314 } else {
315 result.string_thing = arg1;
316 return;
317 }
318 }
319
320 void testOneway(int sleepFor) {
321 printf("testOneway(%d): Sleeping...\n", sleepFor);
322 sleep(sleepFor);
323 printf("testOneway(%d): done sleeping!\n", sleepFor);
324 }
325};
326
327// C CLIENT
328extern "C" {
329
Jim King79c99112015-04-30 07:10:08 -0400330#undef THRIFT_SOCKET /* from lib/cpp */
331
Roger Meier213a6642010-10-27 12:30:11 +0000332#include "t_test_thrift_test.h"
333#include "t_test_thrift_test_types.h"
Roger Meiere3da7682013-01-11 11:41:53 +0100334#include <thrift/c_glib/transport/thrift_socket.h>
335#include <thrift/c_glib/protocol/thrift_protocol.h>
336#include <thrift/c_glib/protocol/thrift_binary_protocol.h>
Roger Meier213a6642010-10-27 12:30:11 +0000337
338static void
339test_thrift_client (void)
340{
341 ThriftSocket *tsocket = NULL;
342 ThriftBinaryProtocol *protocol = NULL;
343 TTestThriftTestClient *client = NULL;
344 TTestThriftTestIf *iface = NULL;
345 GError *error = NULL;
346 gchar *string = NULL;
347 gint8 byte = 0;
348 gint16 i16 = 0;
349 gint32 i32 = 0, another_i32 = 56789;
350 gint64 i64 = 0;
351 double dbl = 0.0;
352 TTestXtruct *xtruct_in, *xtruct_out;
353 TTestXtruct2 *xtruct2_in, *xtruct2_out;
354 GHashTable *map_in = NULL, *map_out = NULL;
355 GHashTable *set_in = NULL, *set_out = NULL;
356 GArray *list_in = NULL, *list_out = NULL;
357 TTestNumberz enum_in, enum_out;
358 TTestUserId user_id_in, user_id_out;
359 GHashTable *insanity_in = NULL;
360 TTestXtruct *xtruct1, *xtruct2;
361 TTestInsanity *insanity_out = NULL;
362 TTestXtruct *multi_in = NULL;
363 GHashTable *multi_map_out = NULL;
364 TTestXception *xception = NULL;
365 TTestXception2 *xception2 = NULL;
366
Jens Geyer1c190272015-07-28 23:15:18 +0200367#if (!GLIB_CHECK_VERSION (2, 36, 0))
Roger Meier213a6642010-10-27 12:30:11 +0000368 // initialize gobject
369 g_type_init ();
Jens Geyer1c190272015-07-28 23:15:18 +0200370#endif
Roger Meier213a6642010-10-27 12:30:11 +0000371
372 // create a C client
373 tsocket = (ThriftSocket *) g_object_new (THRIFT_TYPE_SOCKET,
374 "hostname", "localhost",
375 "port", TEST_PORT, NULL);
376 protocol = (ThriftBinaryProtocol *) g_object_new (THRIFT_TYPE_BINARY_PROTOCOL,
377 "transport",
378 tsocket, NULL);
379 client = (TTestThriftTestClient *) g_object_new (T_TEST_TYPE_THRIFT_TEST_CLIENT, "input_protocol", protocol, "output_protocol", protocol, NULL);
380 iface = T_TEST_THRIFT_TEST_IF (client);
381
382 // open and send
383 thrift_transport_open (THRIFT_TRANSPORT(tsocket), NULL);
384
385 assert (t_test_thrift_test_client_test_void (iface, &error) == TRUE);
386 assert (error == NULL);
387
388 assert (t_test_thrift_test_client_test_string (iface, &string, "test123", &error) == TRUE);
389 assert (strcmp (string, "test123") == 0);
390 g_free (string);
391 assert (error == NULL);
392
393 assert (t_test_thrift_test_client_test_byte (iface, &byte, (gint8) 5, &error) == TRUE);
394 assert (byte == 5);
395 assert (error == NULL);
396
397 assert (t_test_thrift_test_client_test_i32 (iface, &i32, 123, &error) == TRUE);
398 assert (i32 == 123);
399 assert (error == NULL);
400
401 assert (t_test_thrift_test_client_test_i64 (iface, &i64, 12345, &error) == TRUE);
402 assert (i64 == 12345);
403 assert (error == NULL);
404
405 assert (t_test_thrift_test_client_test_double (iface, &dbl, 5.6, &error) == TRUE);
406 assert (dbl == 5.6);
407 assert (error == NULL);
408
409 xtruct_out = (TTestXtruct *) g_object_new (T_TEST_TYPE_XTRUCT, NULL);
410 xtruct_out->byte_thing = 1;
411 xtruct_out->__isset_byte_thing = TRUE;
412 xtruct_out->i32_thing = 15;
413 xtruct_out->__isset_i32_thing = TRUE;
414 xtruct_out->i64_thing = 151;
415 xtruct_out->__isset_i64_thing = TRUE;
416 xtruct_out->string_thing = g_strdup ("abc123");
417 xtruct_out->__isset_string_thing = TRUE;
Roger Meierc75797d2012-04-28 11:33:58 +0000418 xtruct_in = (TTestXtruct *) g_object_new(T_TEST_TYPE_XTRUCT, NULL);
Roger Meier213a6642010-10-27 12:30:11 +0000419 assert (t_test_thrift_test_client_test_struct (iface, &xtruct_in, xtruct_out, &error) == TRUE);
420 assert (error == NULL);
421
422 xtruct2_out = (TTestXtruct2 *) g_object_new (T_TEST_TYPE_XTRUCT2, NULL);
423 xtruct2_out->byte_thing = 1;
424 xtruct2_out->__isset_byte_thing = TRUE;
Roger Meierc75797d2012-04-28 11:33:58 +0000425 if (xtruct2_out->struct_thing != NULL)
426 g_object_unref(xtruct2_out->struct_thing);
Roger Meier213a6642010-10-27 12:30:11 +0000427 xtruct2_out->struct_thing = xtruct_out;
428 xtruct2_out->__isset_struct_thing = TRUE;
429 xtruct2_out->i32_thing = 123;
430 xtruct2_out->__isset_i32_thing = TRUE;
Roger Meierc75797d2012-04-28 11:33:58 +0000431 xtruct2_in = (TTestXtruct2 *) g_object_new (T_TEST_TYPE_XTRUCT2, NULL);
Roger Meier213a6642010-10-27 12:30:11 +0000432 assert (t_test_thrift_test_client_test_nest (iface, &xtruct2_in, xtruct2_out, &error) == TRUE);
433 assert (error == NULL);
434
435 g_object_unref (xtruct2_out);
436 g_object_unref (xtruct2_in);
Roger Meier213a6642010-10-27 12:30:11 +0000437 g_object_unref (xtruct_in);
438
439 map_out = g_hash_table_new (NULL, NULL);
440 map_in = g_hash_table_new (NULL, NULL); g_hash_table_insert (map_out, &i32, &i32);
441 assert (t_test_thrift_test_client_test_map (iface, &map_in, map_out, &error) == TRUE);
442 assert (error == NULL);
443 g_hash_table_destroy (map_out);
444 g_hash_table_destroy (map_in);
445
Roger Meiera1c416f2011-06-17 19:40:48 +0000446 map_out = g_hash_table_new (NULL, NULL);
447 map_in = g_hash_table_new (NULL, NULL);
448 g_hash_table_insert (map_out, g_strdup ("a"), g_strdup ("123"));
449 g_hash_table_insert (map_out, g_strdup ("a b"), g_strdup ("with spaces "));
450 g_hash_table_insert (map_out, g_strdup ("same"), g_strdup ("same"));
451 g_hash_table_insert (map_out, g_strdup ("0"), g_strdup ("numeric key"));
452 assert (t_test_thrift_test_client_test_string_map (iface, &map_in, map_out, &error) == TRUE);
453 assert (error == NULL);
454 g_hash_table_destroy (map_out);
455 g_hash_table_destroy (map_in);
456
Roger Meier213a6642010-10-27 12:30:11 +0000457 set_out = g_hash_table_new (NULL, NULL);
458 set_in = g_hash_table_new (NULL, NULL);
459 g_hash_table_insert (set_out, &i32, &i32);
460 assert (t_test_thrift_test_client_test_set (iface, &set_in, set_out, &error) == TRUE);
461 assert (error == NULL);
462 g_hash_table_destroy (set_out);
463 g_hash_table_destroy (set_in);
464
465 list_out = g_array_new(TRUE, TRUE, sizeof(gint32));
466 list_in = g_array_new(TRUE, TRUE, sizeof(gint32));
467 another_i32 = 456;
468 g_array_append_val (list_out, i32);
469 g_array_append_val (list_out, another_i32);
470 assert (t_test_thrift_test_client_test_list (iface, &list_in, list_out, &error) == TRUE);
471 assert (error == NULL);
472 g_array_free (list_out, TRUE);
473 g_array_free (list_in, TRUE);
474
475 enum_out = T_TEST_NUMBERZ_ONE;
476 assert (t_test_thrift_test_client_test_enum (iface, &enum_in, enum_out, &error) == TRUE);
477 assert (enum_in == enum_out);
478 assert (error == NULL);
479
480 user_id_out = 12345;
481 assert (t_test_thrift_test_client_test_typedef (iface, &user_id_in, user_id_out, &error) == TRUE);
482 assert (user_id_in == user_id_out);
483 assert (error == NULL);
484
485 map_in = g_hash_table_new (NULL, NULL);
486 assert (t_test_thrift_test_client_test_map_map (iface, &map_in, i32, &error) == TRUE);
487 assert (error == NULL);
488 g_hash_table_destroy (map_in);
489
490 // insanity
491 insanity_out = (TTestInsanity *) g_object_new (T_TEST_TYPE_INSANITY, NULL);
492 insanity_out->userMap = g_hash_table_new (NULL, NULL);
Roger Meier0cc6d3c2014-09-04 00:24:17 +0200493 g_hash_table_insert (insanity_out->userMap, GINT_TO_POINTER (enum_out), &user_id_out);
Roger Meier213a6642010-10-27 12:30:11 +0000494
495 xtruct1 = (TTestXtruct *) g_object_new (T_TEST_TYPE_XTRUCT, NULL);
496 xtruct1->byte_thing = 1;
497 xtruct1->__isset_byte_thing = TRUE;
498 xtruct1->i32_thing = 15;
499 xtruct1->__isset_i32_thing = TRUE;
500 xtruct1->i64_thing = 151;
501 xtruct1->__isset_i64_thing = TRUE;
502 xtruct1->string_thing = g_strdup ("abc123");
503 xtruct1->__isset_string_thing = TRUE;
504 xtruct2 = (TTestXtruct *) g_object_new (T_TEST_TYPE_XTRUCT, NULL);
505 xtruct2->byte_thing = 1;
506 xtruct2->__isset_byte_thing = TRUE;
507 xtruct2->i32_thing = 15;
508 xtruct2->__isset_i32_thing = TRUE;
509 xtruct2->i64_thing = 151;
510 xtruct2->__isset_i64_thing = TRUE;
511 xtruct2->string_thing = g_strdup ("abc123");
512 xtruct2->__isset_string_thing = TRUE;
513
Roger Meier213a6642010-10-27 12:30:11 +0000514 insanity_in = g_hash_table_new (NULL, NULL);
515 g_ptr_array_add (insanity_out->xtructs, xtruct1);
516 g_ptr_array_add (insanity_out->xtructs, xtruct2);
517 assert (t_test_thrift_test_client_test_insanity (iface, &insanity_in, insanity_out, &error) == TRUE);
518
519 g_hash_table_unref (insanity_in);
520 g_ptr_array_free (insanity_out->xtructs, TRUE);
Roger Meier213a6642010-10-27 12:30:11 +0000521
522 multi_map_out = g_hash_table_new (NULL, NULL);
523 string = g_strdup ("abc123");
524 g_hash_table_insert (multi_map_out, &i16, string);
Roger Meierc75797d2012-04-28 11:33:58 +0000525 multi_in = (TTestXtruct *) g_object_new (T_TEST_TYPE_XTRUCT, NULL);
Roger Meier213a6642010-10-27 12:30:11 +0000526 assert (t_test_thrift_test_client_test_multi (iface, &multi_in, byte, i32, i64, multi_map_out, enum_out, user_id_out, &error) == TRUE);
527 assert (multi_in->i32_thing == i32);
528 assert (multi_in->i64_thing == i64);
529 g_object_unref (multi_in);
530 g_hash_table_unref (multi_map_out);
Roger Meierc75797d2012-04-28 11:33:58 +0000531 g_free (string);
Roger Meier213a6642010-10-27 12:30:11 +0000532
533 assert (t_test_thrift_test_client_test_exception (iface, "Xception", &xception, &error) == FALSE);
534 assert (xception->errorCode == 1001);
535 g_error_free (error);
536 error = NULL;
Roger Meierc75797d2012-04-28 11:33:58 +0000537 g_object_unref (xception);
538 xception = NULL;
Roger Meier213a6642010-10-27 12:30:11 +0000539
540 assert (t_test_thrift_test_client_test_exception (iface, "ApplicationException", &xception, &error) == FALSE);
541 g_error_free (error);
542 error = NULL;
Jens Geyer482da722015-07-28 23:46:02 +0200543 assert (xception == NULL);
Roger Meier213a6642010-10-27 12:30:11 +0000544
545 assert (t_test_thrift_test_client_test_exception (iface, "Test", &xception, &error) == TRUE);
546 assert (error == NULL);
547
Roger Meierc75797d2012-04-28 11:33:58 +0000548 multi_in = (TTestXtruct*) g_object_new (T_TEST_TYPE_XTRUCT, NULL);
Roger Meier213a6642010-10-27 12:30:11 +0000549 assert (t_test_thrift_test_client_test_multi_exception (iface, &multi_in, "Xception", NULL, &xception, &xception2, &error) == FALSE);
550 assert (xception->errorCode == 1001);
Roger Meierc75797d2012-04-28 11:33:58 +0000551 assert (xception2 == NULL);
Roger Meier213a6642010-10-27 12:30:11 +0000552 g_error_free (error);
553 error = NULL;
554 g_object_unref (xception);
Roger Meierc75797d2012-04-28 11:33:58 +0000555 g_object_unref (multi_in);
Roger Meier213a6642010-10-27 12:30:11 +0000556 xception = NULL;
Roger Meierc75797d2012-04-28 11:33:58 +0000557 multi_in = NULL;
Roger Meier213a6642010-10-27 12:30:11 +0000558
Roger Meierc75797d2012-04-28 11:33:58 +0000559 multi_in = (TTestXtruct*) g_object_new (T_TEST_TYPE_XTRUCT, NULL);
Roger Meier213a6642010-10-27 12:30:11 +0000560 assert (t_test_thrift_test_client_test_multi_exception (iface, &multi_in, "Xception2", NULL, &xception, &xception2, &error) == FALSE);
561 assert (xception2->errorCode == 2002);
Roger Meierc75797d2012-04-28 11:33:58 +0000562 assert (xception == NULL);
Roger Meier213a6642010-10-27 12:30:11 +0000563 g_error_free (error);
564 error = NULL;
565 g_object_unref (xception2);
Roger Meierc75797d2012-04-28 11:33:58 +0000566 g_object_unref (multi_in);
Roger Meier213a6642010-10-27 12:30:11 +0000567 xception2 = NULL;
Roger Meierc75797d2012-04-28 11:33:58 +0000568 multi_in = NULL;
Roger Meier213a6642010-10-27 12:30:11 +0000569
Roger Meierc75797d2012-04-28 11:33:58 +0000570 multi_in = (TTestXtruct*) g_object_new (T_TEST_TYPE_XTRUCT, NULL);
Roger Meier213a6642010-10-27 12:30:11 +0000571 assert (t_test_thrift_test_client_test_multi_exception (iface, &multi_in, NULL , NULL, &xception, &xception2, &error) == TRUE);
572 assert (error == NULL);
Roger Meierc75797d2012-04-28 11:33:58 +0000573 g_object_unref(multi_in);
574 multi_in = NULL;
Roger Meier213a6642010-10-27 12:30:11 +0000575
576 assert (t_test_thrift_test_client_test_oneway (iface, 1, &error) == TRUE);
577 assert (error == NULL);
578
579 /* sleep to let the oneway call go through */
580 sleep (5);
581
582 thrift_transport_close (THRIFT_TRANSPORT(tsocket), NULL);
583 g_object_unref (client);
584 g_object_unref (protocol);
585 g_object_unref (tsocket);
586}
587
588
589} /* extern "C" */
590
591
592static void
593bailout (int signum)
594{
Simon South38e71552015-08-03 10:51:16 +0000595 THRIFT_UNUSED_VARIABLE (signum);
596
Roger Meier213a6642010-10-27 12:30:11 +0000597 exit (1);
598}
599
600int
Simon South38e71552015-08-03 10:51:16 +0000601main (void)
Roger Meier213a6642010-10-27 12:30:11 +0000602{
603 int status;
604 int pid = fork ();
605 assert (pid >= 0);
606
607 if (pid == 0) /* child */
608 {
Marco Molteni83494252015-04-16 13:50:20 +0200609 boost::shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());
610 boost::shared_ptr<TestHandler> testHandler(new TestHandler());
611 boost::shared_ptr<ThriftTestProcessor> testProcessor(new ThriftTestProcessor(testHandler));
612 boost::shared_ptr<TServerSocket> serverSocket(new TServerSocket(TEST_PORT));
613 boost::shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
Roger Meier213a6642010-10-27 12:30:11 +0000614 TSimpleServer simpleServer(testProcessor, serverSocket, transportFactory, protocolFactory);
615 signal (SIGALRM, bailout);
616 alarm (60);
617 simpleServer.serve();
618 } else {
619 sleep (1);
620 test_thrift_client ();
621 kill (pid, SIGINT);
Simon South38e71552015-08-03 10:51:16 +0000622 assert (wait (&status) == pid);
Roger Meier213a6642010-10-27 12:30:11 +0000623 }
624
625 return 0;
626}
627