blob: faf81db5c74408f61a255c8b63312527fd34d72d [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
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) {
Roger Meier79e971e2015-04-12 13:11:33 +020080 printf("[C -> C++] testI64(%ld)\n", thing);
Roger Meier213a6642010-10-27 12:30:11 +000081 return thing;
82 }
83
84 double testDouble(const double thing) {
85 printf("[C -> C++] testDouble(%lf)\n", thing);
86 return thing;
87 }
88
Jens Geyer8bcfdd92014-12-14 03:14:26 +010089 void testBinary(string& out, const string &thing) {
90 printf("[C -> C++] testBinary(\"%s\")\n", thing.c_str());
91 out = thing;
92 }
93
Roger Meier213a6642010-10-27 12:30:11 +000094 void testStruct(Xtruct& out, const Xtruct &thing) {
Roger Meier79e971e2015-04-12 13:11:33 +020095 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 +000096 out = thing;
97 }
98
99 void testNest(Xtruct2& out, const Xtruct2& nest) {
100 const Xtruct &thing = nest.struct_thing;
Roger Meier79e971e2015-04-12 13:11:33 +0200101 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 +0000102 out = nest;
103 }
104
105 void testMap(map<int32_t, int32_t> &out, const map<int32_t, int32_t> &thing) {
106 printf("[C -> C++] testMap({");
107 map<int32_t, int32_t>::const_iterator m_iter;
108 bool first = true;
109 for (m_iter = thing.begin(); m_iter != thing.end(); ++m_iter) {
110 if (first) {
111 first = false;
112 } else {
113 printf(", ");
114 }
115 printf("%d => %d", m_iter->first, m_iter->second);
116 }
117 printf("})\n");
118 out = thing;
119 }
120
Roger Meiera1c416f2011-06-17 19:40:48 +0000121 void testStringMap(map<std::string, std::string> &out, const map<std::string, std::string> &thing) {
122 printf("[C -> C++] testStringMap({");
123 map<std::string, std::string>::const_iterator m_iter;
124 bool first = true;
125 for (m_iter = thing.begin(); m_iter != thing.end(); ++m_iter) {
126 if (first) {
127 first = false;
128 } else {
129 printf(", ");
130 }
131 printf("\"%s\" => \"%s\"", (m_iter->first).c_str(), (m_iter->second).c_str());
132 }
133 printf("})\n");
134 out = thing;
135 }
136
137
Roger Meier213a6642010-10-27 12:30:11 +0000138 void testSet(set<int32_t> &out, const set<int32_t> &thing) {
139 printf("[C -> C++] testSet({");
140 set<int32_t>::const_iterator s_iter;
141 bool first = true;
142 for (s_iter = thing.begin(); s_iter != thing.end(); ++s_iter) {
143 if (first) {
144 first = false;
145 } else {
146 printf(", ");
147 }
148 printf("%d", *s_iter);
149 }
150 printf("})\n");
151 out = thing;
152 }
153
154 void testList(vector<int32_t> &out, const vector<int32_t> &thing) {
155 printf("[C -> C++] testList({");
156 vector<int32_t>::const_iterator l_iter;
157 bool first = true;
158 for (l_iter = thing.begin(); l_iter != thing.end(); ++l_iter) {
159 if (first) {
160 first = false;
161 } else { printf(", ");
162 }
163 printf("%d", *l_iter);
164 }
165 printf("})\n");
166 out = thing;
167 }
168
169 Numberz::type testEnum(const Numberz::type thing) {
170 printf("[C -> C++] testEnum(%d)\n", thing);
171 return thing;
172 }
173
174 UserId testTypedef(const UserId thing) {
Roger Meier79e971e2015-04-12 13:11:33 +0200175 printf("[C -> C++] testTypedef(%ld)\n", thing);
Roger Meier213a6642010-10-27 12:30:11 +0000176 return thing; }
177
178 void testMapMap(map<int32_t, map<int32_t,int32_t> > &mapmap, const int32_t hello) {
179 printf("[C -> C++] testMapMap(%d)\n", hello);
180
181 map<int32_t,int32_t> pos;
182 map<int32_t,int32_t> neg;
183 for (int i = 1; i < 5; i++) {
184 pos.insert(make_pair(i,i));
185 neg.insert(make_pair(-i,-i));
186 }
187
188 mapmap.insert(make_pair(4, pos));
189 mapmap.insert(make_pair(-4, neg));
190
191 }
192
193 void testInsanity(map<UserId, map<Numberz::type,Insanity> > &insane, const Insanity &argument) {
194 printf("[C -> C++] testInsanity()\n");
195
196 Xtruct hello;
197 hello.string_thing = "Hello2";
198 hello.byte_thing = 2;
199 hello.i32_thing = 2;
200 hello.i64_thing = 2;
201
202 Xtruct goodbye;
203 goodbye.string_thing = "Goodbye4";
204 goodbye.byte_thing = 4;
205 goodbye.i32_thing = 4;
206 goodbye.i64_thing = 4;
207
208 Insanity crazy;
209 crazy.userMap.insert(make_pair(Numberz::EIGHT, 8));
210 crazy.xtructs.push_back(goodbye);
211
212 Insanity looney;
213 crazy.userMap.insert(make_pair(Numberz::FIVE, 5));
214 crazy.xtructs.push_back(hello);
215
216 map<Numberz::type, Insanity> first_map;
217 map<Numberz::type, Insanity> second_map;
218
219 first_map.insert(make_pair(Numberz::TWO, crazy));
220 first_map.insert(make_pair(Numberz::THREE, crazy));
221
222 second_map.insert(make_pair(Numberz::SIX, looney));
223
224 insane.insert(make_pair(1, first_map));
225 insane.insert(make_pair(2, second_map));
226
227 printf("return");
228 printf(" = {");
229 map<UserId, map<Numberz::type,Insanity> >::const_iterator i_iter;
230 for (i_iter = insane.begin(); i_iter != insane.end(); ++i_iter) {
Roger Meier79e971e2015-04-12 13:11:33 +0200231 printf("%ld => {", i_iter->first);
Roger Meier213a6642010-10-27 12:30:11 +0000232 map<Numberz::type,Insanity>::const_iterator i2_iter;
233 for (i2_iter = i_iter->second.begin();
234 i2_iter != i_iter->second.end();
235 ++i2_iter) {
236 printf("%d => {", i2_iter->first);
237 map<Numberz::type, UserId> userMap = i2_iter->second.userMap;
238 map<Numberz::type, UserId>::const_iterator um;
239 printf("{");
240 for (um = userMap.begin(); um != userMap.end(); ++um) {
Roger Meier79e971e2015-04-12 13:11:33 +0200241 printf("%d => %ld, ", um->first, um->second);
Roger Meier213a6642010-10-27 12:30:11 +0000242 }
243 printf("}, ");
244
245 vector<Xtruct> xtructs = i2_iter->second.xtructs;
246 vector<Xtruct>::const_iterator x;
247 printf("{");
248 for (x = xtructs.begin(); x != xtructs.end(); ++x) {
Roger Meier79e971e2015-04-12 13:11:33 +0200249 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 +0000250 }
251 printf("}");
252
253 printf("}, ");
254 }
255 printf("}, ");
256 }
257 printf("}\n");
258
259
260 }
261
262 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) {
263 printf("[C -> C++] testMulti()\n");
264
265 hello.string_thing = "Hello2";
266 hello.byte_thing = arg0;
267 hello.i32_thing = arg1;
268 hello.i64_thing = (int64_t)arg2;
269 }
270
271 void testException(const std::string &arg)
272 throw(Xception, apache::thrift::TException)
273 {
274 printf("[C -> C++] testException(%s)\n", arg.c_str());
275 if (arg.compare("Xception") == 0) {
276 Xception e;
277 e.errorCode = 1001;
278 e.message = arg;
279 throw e;
280 } else if (arg.compare("ApplicationException") == 0) {
281 apache::thrift::TException e;
282 throw e;
283 } else {
284 Xtruct result;
285 result.string_thing = arg;
286 return;
287 }
288 }
289
290 void testMultiException(Xtruct &result, const std::string &arg0, const std::string &arg1) throw(Xception, Xception2) {
291
292 printf("[C -> C++] testMultiException(%s, %s)\n", arg0.c_str(), arg1.c_str());
293
294 if (arg0.compare("Xception") == 0) {
295 Xception e;
296 e.errorCode = 1001;
297 e.message = "This is an Xception";
298 throw e;
299 } else if (arg0.compare("Xception2") == 0) {
300 Xception2 e;
301 e.errorCode = 2002;
302 e.struct_thing.string_thing = "This is an Xception2";
303 throw e;
304 } else {
305 result.string_thing = arg1;
306 return;
307 }
308 }
309
310 void testOneway(int sleepFor) {
311 printf("testOneway(%d): Sleeping...\n", sleepFor);
312 sleep(sleepFor);
313 printf("testOneway(%d): done sleeping!\n", sleepFor);
314 }
315};
316
317// C CLIENT
318extern "C" {
319
Jim King79c99112015-04-30 07:10:08 -0400320#undef THRIFT_SOCKET /* from lib/cpp */
321
Roger Meier213a6642010-10-27 12:30:11 +0000322#include "t_test_thrift_test.h"
323#include "t_test_thrift_test_types.h"
Roger Meiere3da7682013-01-11 11:41:53 +0100324#include <thrift/c_glib/transport/thrift_socket.h>
325#include <thrift/c_glib/protocol/thrift_protocol.h>
326#include <thrift/c_glib/protocol/thrift_binary_protocol.h>
Roger Meier213a6642010-10-27 12:30:11 +0000327
328static void
329test_thrift_client (void)
330{
331 ThriftSocket *tsocket = NULL;
332 ThriftBinaryProtocol *protocol = NULL;
333 TTestThriftTestClient *client = NULL;
334 TTestThriftTestIf *iface = NULL;
335 GError *error = NULL;
336 gchar *string = NULL;
337 gint8 byte = 0;
338 gint16 i16 = 0;
339 gint32 i32 = 0, another_i32 = 56789;
340 gint64 i64 = 0;
341 double dbl = 0.0;
342 TTestXtruct *xtruct_in, *xtruct_out;
343 TTestXtruct2 *xtruct2_in, *xtruct2_out;
344 GHashTable *map_in = NULL, *map_out = NULL;
345 GHashTable *set_in = NULL, *set_out = NULL;
346 GArray *list_in = NULL, *list_out = NULL;
347 TTestNumberz enum_in, enum_out;
348 TTestUserId user_id_in, user_id_out;
349 GHashTable *insanity_in = NULL;
350 TTestXtruct *xtruct1, *xtruct2;
351 TTestInsanity *insanity_out = NULL;
352 TTestXtruct *multi_in = NULL;
353 GHashTable *multi_map_out = NULL;
354 TTestXception *xception = NULL;
355 TTestXception2 *xception2 = NULL;
356
Jens Geyer1c190272015-07-28 23:15:18 +0200357#if (!GLIB_CHECK_VERSION (2, 36, 0))
Roger Meier213a6642010-10-27 12:30:11 +0000358 // initialize gobject
359 g_type_init ();
Jens Geyer1c190272015-07-28 23:15:18 +0200360#endif
Roger Meier213a6642010-10-27 12:30:11 +0000361
362 // create a C client
363 tsocket = (ThriftSocket *) g_object_new (THRIFT_TYPE_SOCKET,
364 "hostname", "localhost",
365 "port", TEST_PORT, NULL);
366 protocol = (ThriftBinaryProtocol *) g_object_new (THRIFT_TYPE_BINARY_PROTOCOL,
367 "transport",
368 tsocket, NULL);
369 client = (TTestThriftTestClient *) g_object_new (T_TEST_TYPE_THRIFT_TEST_CLIENT, "input_protocol", protocol, "output_protocol", protocol, NULL);
370 iface = T_TEST_THRIFT_TEST_IF (client);
371
372 // open and send
373 thrift_transport_open (THRIFT_TRANSPORT(tsocket), NULL);
374
375 assert (t_test_thrift_test_client_test_void (iface, &error) == TRUE);
376 assert (error == NULL);
377
378 assert (t_test_thrift_test_client_test_string (iface, &string, "test123", &error) == TRUE);
379 assert (strcmp (string, "test123") == 0);
380 g_free (string);
381 assert (error == NULL);
382
383 assert (t_test_thrift_test_client_test_byte (iface, &byte, (gint8) 5, &error) == TRUE);
384 assert (byte == 5);
385 assert (error == NULL);
386
387 assert (t_test_thrift_test_client_test_i32 (iface, &i32, 123, &error) == TRUE);
388 assert (i32 == 123);
389 assert (error == NULL);
390
391 assert (t_test_thrift_test_client_test_i64 (iface, &i64, 12345, &error) == TRUE);
392 assert (i64 == 12345);
393 assert (error == NULL);
394
395 assert (t_test_thrift_test_client_test_double (iface, &dbl, 5.6, &error) == TRUE);
396 assert (dbl == 5.6);
397 assert (error == NULL);
398
399 xtruct_out = (TTestXtruct *) g_object_new (T_TEST_TYPE_XTRUCT, NULL);
400 xtruct_out->byte_thing = 1;
401 xtruct_out->__isset_byte_thing = TRUE;
402 xtruct_out->i32_thing = 15;
403 xtruct_out->__isset_i32_thing = TRUE;
404 xtruct_out->i64_thing = 151;
405 xtruct_out->__isset_i64_thing = TRUE;
406 xtruct_out->string_thing = g_strdup ("abc123");
407 xtruct_out->__isset_string_thing = TRUE;
Roger Meierc75797d2012-04-28 11:33:58 +0000408 xtruct_in = (TTestXtruct *) g_object_new(T_TEST_TYPE_XTRUCT, NULL);
Roger Meier213a6642010-10-27 12:30:11 +0000409 assert (t_test_thrift_test_client_test_struct (iface, &xtruct_in, xtruct_out, &error) == TRUE);
410 assert (error == NULL);
411
412 xtruct2_out = (TTestXtruct2 *) g_object_new (T_TEST_TYPE_XTRUCT2, NULL);
413 xtruct2_out->byte_thing = 1;
414 xtruct2_out->__isset_byte_thing = TRUE;
Roger Meierc75797d2012-04-28 11:33:58 +0000415 if (xtruct2_out->struct_thing != NULL)
416 g_object_unref(xtruct2_out->struct_thing);
Roger Meier213a6642010-10-27 12:30:11 +0000417 xtruct2_out->struct_thing = xtruct_out;
418 xtruct2_out->__isset_struct_thing = TRUE;
419 xtruct2_out->i32_thing = 123;
420 xtruct2_out->__isset_i32_thing = TRUE;
Roger Meierc75797d2012-04-28 11:33:58 +0000421 xtruct2_in = (TTestXtruct2 *) g_object_new (T_TEST_TYPE_XTRUCT2, NULL);
Roger Meier213a6642010-10-27 12:30:11 +0000422 assert (t_test_thrift_test_client_test_nest (iface, &xtruct2_in, xtruct2_out, &error) == TRUE);
423 assert (error == NULL);
424
425 g_object_unref (xtruct2_out);
426 g_object_unref (xtruct2_in);
Roger Meier213a6642010-10-27 12:30:11 +0000427 g_object_unref (xtruct_in);
428
429 map_out = g_hash_table_new (NULL, NULL);
430 map_in = g_hash_table_new (NULL, NULL); g_hash_table_insert (map_out, &i32, &i32);
431 assert (t_test_thrift_test_client_test_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 Meiera1c416f2011-06-17 19:40:48 +0000436 map_out = g_hash_table_new (NULL, NULL);
437 map_in = g_hash_table_new (NULL, NULL);
438 g_hash_table_insert (map_out, g_strdup ("a"), g_strdup ("123"));
439 g_hash_table_insert (map_out, g_strdup ("a b"), g_strdup ("with spaces "));
440 g_hash_table_insert (map_out, g_strdup ("same"), g_strdup ("same"));
441 g_hash_table_insert (map_out, g_strdup ("0"), g_strdup ("numeric key"));
442 assert (t_test_thrift_test_client_test_string_map (iface, &map_in, map_out, &error) == TRUE);
443 assert (error == NULL);
444 g_hash_table_destroy (map_out);
445 g_hash_table_destroy (map_in);
446
Roger Meier213a6642010-10-27 12:30:11 +0000447 set_out = g_hash_table_new (NULL, NULL);
448 set_in = g_hash_table_new (NULL, NULL);
449 g_hash_table_insert (set_out, &i32, &i32);
450 assert (t_test_thrift_test_client_test_set (iface, &set_in, set_out, &error) == TRUE);
451 assert (error == NULL);
452 g_hash_table_destroy (set_out);
453 g_hash_table_destroy (set_in);
454
455 list_out = g_array_new(TRUE, TRUE, sizeof(gint32));
456 list_in = g_array_new(TRUE, TRUE, sizeof(gint32));
457 another_i32 = 456;
458 g_array_append_val (list_out, i32);
459 g_array_append_val (list_out, another_i32);
460 assert (t_test_thrift_test_client_test_list (iface, &list_in, list_out, &error) == TRUE);
461 assert (error == NULL);
462 g_array_free (list_out, TRUE);
463 g_array_free (list_in, TRUE);
464
465 enum_out = T_TEST_NUMBERZ_ONE;
466 assert (t_test_thrift_test_client_test_enum (iface, &enum_in, enum_out, &error) == TRUE);
467 assert (enum_in == enum_out);
468 assert (error == NULL);
469
470 user_id_out = 12345;
471 assert (t_test_thrift_test_client_test_typedef (iface, &user_id_in, user_id_out, &error) == TRUE);
472 assert (user_id_in == user_id_out);
473 assert (error == NULL);
474
475 map_in = g_hash_table_new (NULL, NULL);
476 assert (t_test_thrift_test_client_test_map_map (iface, &map_in, i32, &error) == TRUE);
477 assert (error == NULL);
478 g_hash_table_destroy (map_in);
479
480 // insanity
481 insanity_out = (TTestInsanity *) g_object_new (T_TEST_TYPE_INSANITY, NULL);
482 insanity_out->userMap = g_hash_table_new (NULL, NULL);
Roger Meier0cc6d3c2014-09-04 00:24:17 +0200483 g_hash_table_insert (insanity_out->userMap, GINT_TO_POINTER (enum_out), &user_id_out);
Roger Meier213a6642010-10-27 12:30:11 +0000484
485 xtruct1 = (TTestXtruct *) g_object_new (T_TEST_TYPE_XTRUCT, NULL);
486 xtruct1->byte_thing = 1;
487 xtruct1->__isset_byte_thing = TRUE;
488 xtruct1->i32_thing = 15;
489 xtruct1->__isset_i32_thing = TRUE;
490 xtruct1->i64_thing = 151;
491 xtruct1->__isset_i64_thing = TRUE;
492 xtruct1->string_thing = g_strdup ("abc123");
493 xtruct1->__isset_string_thing = TRUE;
494 xtruct2 = (TTestXtruct *) g_object_new (T_TEST_TYPE_XTRUCT, NULL);
495 xtruct2->byte_thing = 1;
496 xtruct2->__isset_byte_thing = TRUE;
497 xtruct2->i32_thing = 15;
498 xtruct2->__isset_i32_thing = TRUE;
499 xtruct2->i64_thing = 151;
500 xtruct2->__isset_i64_thing = TRUE;
501 xtruct2->string_thing = g_strdup ("abc123");
502 xtruct2->__isset_string_thing = TRUE;
503
Roger Meier213a6642010-10-27 12:30:11 +0000504 insanity_in = g_hash_table_new (NULL, NULL);
505 g_ptr_array_add (insanity_out->xtructs, xtruct1);
506 g_ptr_array_add (insanity_out->xtructs, xtruct2);
507 assert (t_test_thrift_test_client_test_insanity (iface, &insanity_in, insanity_out, &error) == TRUE);
508
509 g_hash_table_unref (insanity_in);
510 g_ptr_array_free (insanity_out->xtructs, TRUE);
Roger Meier213a6642010-10-27 12:30:11 +0000511
512 multi_map_out = g_hash_table_new (NULL, NULL);
513 string = g_strdup ("abc123");
514 g_hash_table_insert (multi_map_out, &i16, string);
Roger Meierc75797d2012-04-28 11:33:58 +0000515 multi_in = (TTestXtruct *) g_object_new (T_TEST_TYPE_XTRUCT, NULL);
Roger Meier213a6642010-10-27 12:30:11 +0000516 assert (t_test_thrift_test_client_test_multi (iface, &multi_in, byte, i32, i64, multi_map_out, enum_out, user_id_out, &error) == TRUE);
517 assert (multi_in->i32_thing == i32);
518 assert (multi_in->i64_thing == i64);
519 g_object_unref (multi_in);
520 g_hash_table_unref (multi_map_out);
Roger Meierc75797d2012-04-28 11:33:58 +0000521 g_free (string);
Roger Meier213a6642010-10-27 12:30:11 +0000522
523 assert (t_test_thrift_test_client_test_exception (iface, "Xception", &xception, &error) == FALSE);
524 assert (xception->errorCode == 1001);
525 g_error_free (error);
526 error = NULL;
Roger Meierc75797d2012-04-28 11:33:58 +0000527 g_object_unref (xception);
528 xception = NULL;
Roger Meier213a6642010-10-27 12:30:11 +0000529
530 assert (t_test_thrift_test_client_test_exception (iface, "ApplicationException", &xception, &error) == FALSE);
531 g_error_free (error);
532 error = NULL;
533 g_object_unref (xception);
534 xception = NULL;
535
536 assert (t_test_thrift_test_client_test_exception (iface, "Test", &xception, &error) == TRUE);
537 assert (error == NULL);
538
Roger Meierc75797d2012-04-28 11:33:58 +0000539 multi_in = (TTestXtruct*) g_object_new (T_TEST_TYPE_XTRUCT, NULL);
Roger Meier213a6642010-10-27 12:30:11 +0000540 assert (t_test_thrift_test_client_test_multi_exception (iface, &multi_in, "Xception", NULL, &xception, &xception2, &error) == FALSE);
541 assert (xception->errorCode == 1001);
Roger Meierc75797d2012-04-28 11:33:58 +0000542 assert (xception2 == NULL);
Roger Meier213a6642010-10-27 12:30:11 +0000543 g_error_free (error);
544 error = NULL;
545 g_object_unref (xception);
Roger Meierc75797d2012-04-28 11:33:58 +0000546 g_object_unref (multi_in);
Roger Meier213a6642010-10-27 12:30:11 +0000547 xception = NULL;
Roger Meierc75797d2012-04-28 11:33:58 +0000548 multi_in = NULL;
Roger Meier213a6642010-10-27 12:30:11 +0000549
Roger Meierc75797d2012-04-28 11:33:58 +0000550 multi_in = (TTestXtruct*) g_object_new (T_TEST_TYPE_XTRUCT, NULL);
Roger Meier213a6642010-10-27 12:30:11 +0000551 assert (t_test_thrift_test_client_test_multi_exception (iface, &multi_in, "Xception2", NULL, &xception, &xception2, &error) == FALSE);
552 assert (xception2->errorCode == 2002);
Roger Meierc75797d2012-04-28 11:33:58 +0000553 assert (xception == NULL);
Roger Meier213a6642010-10-27 12:30:11 +0000554 g_error_free (error);
555 error = NULL;
556 g_object_unref (xception2);
Roger Meierc75797d2012-04-28 11:33:58 +0000557 g_object_unref (multi_in);
Roger Meier213a6642010-10-27 12:30:11 +0000558 xception2 = NULL;
Roger Meierc75797d2012-04-28 11:33:58 +0000559 multi_in = NULL;
Roger Meier213a6642010-10-27 12:30:11 +0000560
Roger Meierc75797d2012-04-28 11:33:58 +0000561 multi_in = (TTestXtruct*) g_object_new (T_TEST_TYPE_XTRUCT, NULL);
Roger Meier213a6642010-10-27 12:30:11 +0000562 assert (t_test_thrift_test_client_test_multi_exception (iface, &multi_in, NULL , NULL, &xception, &xception2, &error) == TRUE);
563 assert (error == NULL);
Roger Meierc75797d2012-04-28 11:33:58 +0000564 g_object_unref(multi_in);
565 multi_in = NULL;
Roger Meier213a6642010-10-27 12:30:11 +0000566
567 assert (t_test_thrift_test_client_test_oneway (iface, 1, &error) == TRUE);
568 assert (error == NULL);
569
570 /* sleep to let the oneway call go through */
571 sleep (5);
572
573 thrift_transport_close (THRIFT_TRANSPORT(tsocket), NULL);
574 g_object_unref (client);
575 g_object_unref (protocol);
576 g_object_unref (tsocket);
577}
578
579
580} /* extern "C" */
581
582
583static void
584bailout (int signum)
585{
586 exit (1);
587}
588
589int
590main (int argc, char **argv)
591{
592 int status;
593 int pid = fork ();
594 assert (pid >= 0);
595
596 if (pid == 0) /* child */
597 {
Marco Molteni83494252015-04-16 13:50:20 +0200598 boost::shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());
599 boost::shared_ptr<TestHandler> testHandler(new TestHandler());
600 boost::shared_ptr<ThriftTestProcessor> testProcessor(new ThriftTestProcessor(testHandler));
601 boost::shared_ptr<TServerSocket> serverSocket(new TServerSocket(TEST_PORT));
602 boost::shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
Roger Meier213a6642010-10-27 12:30:11 +0000603 TSimpleServer simpleServer(testProcessor, serverSocket, transportFactory, protocolFactory);
604 signal (SIGALRM, bailout);
605 alarm (60);
606 simpleServer.serve();
607 } else {
608 sleep (1);
609 test_thrift_client ();
610 kill (pid, SIGINT);
611 wait (&status) == pid;
612 }
613
614 return 0;
615}
616