blob: d387396b0751c77196df093042ac9508f79d0a0f [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
357 // initialize gobject
358 g_type_init ();
359
360 // create a C client
361 tsocket = (ThriftSocket *) g_object_new (THRIFT_TYPE_SOCKET,
362 "hostname", "localhost",
363 "port", TEST_PORT, NULL);
364 protocol = (ThriftBinaryProtocol *) g_object_new (THRIFT_TYPE_BINARY_PROTOCOL,
365 "transport",
366 tsocket, NULL);
367 client = (TTestThriftTestClient *) g_object_new (T_TEST_TYPE_THRIFT_TEST_CLIENT, "input_protocol", protocol, "output_protocol", protocol, NULL);
368 iface = T_TEST_THRIFT_TEST_IF (client);
369
370 // open and send
371 thrift_transport_open (THRIFT_TRANSPORT(tsocket), NULL);
372
373 assert (t_test_thrift_test_client_test_void (iface, &error) == TRUE);
374 assert (error == NULL);
375
376 assert (t_test_thrift_test_client_test_string (iface, &string, "test123", &error) == TRUE);
377 assert (strcmp (string, "test123") == 0);
378 g_free (string);
379 assert (error == NULL);
380
381 assert (t_test_thrift_test_client_test_byte (iface, &byte, (gint8) 5, &error) == TRUE);
382 assert (byte == 5);
383 assert (error == NULL);
384
385 assert (t_test_thrift_test_client_test_i32 (iface, &i32, 123, &error) == TRUE);
386 assert (i32 == 123);
387 assert (error == NULL);
388
389 assert (t_test_thrift_test_client_test_i64 (iface, &i64, 12345, &error) == TRUE);
390 assert (i64 == 12345);
391 assert (error == NULL);
392
393 assert (t_test_thrift_test_client_test_double (iface, &dbl, 5.6, &error) == TRUE);
394 assert (dbl == 5.6);
395 assert (error == NULL);
396
397 xtruct_out = (TTestXtruct *) g_object_new (T_TEST_TYPE_XTRUCT, NULL);
398 xtruct_out->byte_thing = 1;
399 xtruct_out->__isset_byte_thing = TRUE;
400 xtruct_out->i32_thing = 15;
401 xtruct_out->__isset_i32_thing = TRUE;
402 xtruct_out->i64_thing = 151;
403 xtruct_out->__isset_i64_thing = TRUE;
404 xtruct_out->string_thing = g_strdup ("abc123");
405 xtruct_out->__isset_string_thing = TRUE;
Roger Meierc75797d2012-04-28 11:33:58 +0000406 xtruct_in = (TTestXtruct *) g_object_new(T_TEST_TYPE_XTRUCT, NULL);
Roger Meier213a6642010-10-27 12:30:11 +0000407 assert (t_test_thrift_test_client_test_struct (iface, &xtruct_in, xtruct_out, &error) == TRUE);
408 assert (error == NULL);
409
410 xtruct2_out = (TTestXtruct2 *) g_object_new (T_TEST_TYPE_XTRUCT2, NULL);
411 xtruct2_out->byte_thing = 1;
412 xtruct2_out->__isset_byte_thing = TRUE;
Roger Meierc75797d2012-04-28 11:33:58 +0000413 if (xtruct2_out->struct_thing != NULL)
414 g_object_unref(xtruct2_out->struct_thing);
Roger Meier213a6642010-10-27 12:30:11 +0000415 xtruct2_out->struct_thing = xtruct_out;
416 xtruct2_out->__isset_struct_thing = TRUE;
417 xtruct2_out->i32_thing = 123;
418 xtruct2_out->__isset_i32_thing = TRUE;
Roger Meierc75797d2012-04-28 11:33:58 +0000419 xtruct2_in = (TTestXtruct2 *) g_object_new (T_TEST_TYPE_XTRUCT2, NULL);
Roger Meier213a6642010-10-27 12:30:11 +0000420 assert (t_test_thrift_test_client_test_nest (iface, &xtruct2_in, xtruct2_out, &error) == TRUE);
421 assert (error == NULL);
422
423 g_object_unref (xtruct2_out);
424 g_object_unref (xtruct2_in);
Roger Meier213a6642010-10-27 12:30:11 +0000425 g_object_unref (xtruct_in);
426
427 map_out = g_hash_table_new (NULL, NULL);
428 map_in = g_hash_table_new (NULL, NULL); g_hash_table_insert (map_out, &i32, &i32);
429 assert (t_test_thrift_test_client_test_map (iface, &map_in, map_out, &error) == TRUE);
430 assert (error == NULL);
431 g_hash_table_destroy (map_out);
432 g_hash_table_destroy (map_in);
433
Roger Meiera1c416f2011-06-17 19:40:48 +0000434 map_out = g_hash_table_new (NULL, NULL);
435 map_in = g_hash_table_new (NULL, NULL);
436 g_hash_table_insert (map_out, g_strdup ("a"), g_strdup ("123"));
437 g_hash_table_insert (map_out, g_strdup ("a b"), g_strdup ("with spaces "));
438 g_hash_table_insert (map_out, g_strdup ("same"), g_strdup ("same"));
439 g_hash_table_insert (map_out, g_strdup ("0"), g_strdup ("numeric key"));
440 assert (t_test_thrift_test_client_test_string_map (iface, &map_in, map_out, &error) == TRUE);
441 assert (error == NULL);
442 g_hash_table_destroy (map_out);
443 g_hash_table_destroy (map_in);
444
Roger Meier213a6642010-10-27 12:30:11 +0000445 set_out = g_hash_table_new (NULL, NULL);
446 set_in = g_hash_table_new (NULL, NULL);
447 g_hash_table_insert (set_out, &i32, &i32);
448 assert (t_test_thrift_test_client_test_set (iface, &set_in, set_out, &error) == TRUE);
449 assert (error == NULL);
450 g_hash_table_destroy (set_out);
451 g_hash_table_destroy (set_in);
452
453 list_out = g_array_new(TRUE, TRUE, sizeof(gint32));
454 list_in = g_array_new(TRUE, TRUE, sizeof(gint32));
455 another_i32 = 456;
456 g_array_append_val (list_out, i32);
457 g_array_append_val (list_out, another_i32);
458 assert (t_test_thrift_test_client_test_list (iface, &list_in, list_out, &error) == TRUE);
459 assert (error == NULL);
460 g_array_free (list_out, TRUE);
461 g_array_free (list_in, TRUE);
462
463 enum_out = T_TEST_NUMBERZ_ONE;
464 assert (t_test_thrift_test_client_test_enum (iface, &enum_in, enum_out, &error) == TRUE);
465 assert (enum_in == enum_out);
466 assert (error == NULL);
467
468 user_id_out = 12345;
469 assert (t_test_thrift_test_client_test_typedef (iface, &user_id_in, user_id_out, &error) == TRUE);
470 assert (user_id_in == user_id_out);
471 assert (error == NULL);
472
473 map_in = g_hash_table_new (NULL, NULL);
474 assert (t_test_thrift_test_client_test_map_map (iface, &map_in, i32, &error) == TRUE);
475 assert (error == NULL);
476 g_hash_table_destroy (map_in);
477
478 // insanity
479 insanity_out = (TTestInsanity *) g_object_new (T_TEST_TYPE_INSANITY, NULL);
480 insanity_out->userMap = g_hash_table_new (NULL, NULL);
Roger Meier0cc6d3c2014-09-04 00:24:17 +0200481 g_hash_table_insert (insanity_out->userMap, GINT_TO_POINTER (enum_out), &user_id_out);
Roger Meier213a6642010-10-27 12:30:11 +0000482
483 xtruct1 = (TTestXtruct *) g_object_new (T_TEST_TYPE_XTRUCT, NULL);
484 xtruct1->byte_thing = 1;
485 xtruct1->__isset_byte_thing = TRUE;
486 xtruct1->i32_thing = 15;
487 xtruct1->__isset_i32_thing = TRUE;
488 xtruct1->i64_thing = 151;
489 xtruct1->__isset_i64_thing = TRUE;
490 xtruct1->string_thing = g_strdup ("abc123");
491 xtruct1->__isset_string_thing = TRUE;
492 xtruct2 = (TTestXtruct *) g_object_new (T_TEST_TYPE_XTRUCT, NULL);
493 xtruct2->byte_thing = 1;
494 xtruct2->__isset_byte_thing = TRUE;
495 xtruct2->i32_thing = 15;
496 xtruct2->__isset_i32_thing = TRUE;
497 xtruct2->i64_thing = 151;
498 xtruct2->__isset_i64_thing = TRUE;
499 xtruct2->string_thing = g_strdup ("abc123");
500 xtruct2->__isset_string_thing = TRUE;
501
Roger Meier213a6642010-10-27 12:30:11 +0000502 insanity_in = g_hash_table_new (NULL, NULL);
503 g_ptr_array_add (insanity_out->xtructs, xtruct1);
504 g_ptr_array_add (insanity_out->xtructs, xtruct2);
505 assert (t_test_thrift_test_client_test_insanity (iface, &insanity_in, insanity_out, &error) == TRUE);
506
507 g_hash_table_unref (insanity_in);
508 g_ptr_array_free (insanity_out->xtructs, TRUE);
Roger Meier213a6642010-10-27 12:30:11 +0000509
510 multi_map_out = g_hash_table_new (NULL, NULL);
511 string = g_strdup ("abc123");
512 g_hash_table_insert (multi_map_out, &i16, string);
Roger Meierc75797d2012-04-28 11:33:58 +0000513 multi_in = (TTestXtruct *) g_object_new (T_TEST_TYPE_XTRUCT, NULL);
Roger Meier213a6642010-10-27 12:30:11 +0000514 assert (t_test_thrift_test_client_test_multi (iface, &multi_in, byte, i32, i64, multi_map_out, enum_out, user_id_out, &error) == TRUE);
515 assert (multi_in->i32_thing == i32);
516 assert (multi_in->i64_thing == i64);
517 g_object_unref (multi_in);
518 g_hash_table_unref (multi_map_out);
Roger Meierc75797d2012-04-28 11:33:58 +0000519 g_free (string);
Roger Meier213a6642010-10-27 12:30:11 +0000520
521 assert (t_test_thrift_test_client_test_exception (iface, "Xception", &xception, &error) == FALSE);
522 assert (xception->errorCode == 1001);
523 g_error_free (error);
524 error = NULL;
Roger Meierc75797d2012-04-28 11:33:58 +0000525 g_object_unref (xception);
526 xception = NULL;
Roger Meier213a6642010-10-27 12:30:11 +0000527
528 assert (t_test_thrift_test_client_test_exception (iface, "ApplicationException", &xception, &error) == FALSE);
529 g_error_free (error);
530 error = NULL;
531 g_object_unref (xception);
532 xception = NULL;
533
534 assert (t_test_thrift_test_client_test_exception (iface, "Test", &xception, &error) == TRUE);
535 assert (error == NULL);
536
Roger Meierc75797d2012-04-28 11:33:58 +0000537 multi_in = (TTestXtruct*) g_object_new (T_TEST_TYPE_XTRUCT, NULL);
Roger Meier213a6642010-10-27 12:30:11 +0000538 assert (t_test_thrift_test_client_test_multi_exception (iface, &multi_in, "Xception", NULL, &xception, &xception2, &error) == FALSE);
539 assert (xception->errorCode == 1001);
Roger Meierc75797d2012-04-28 11:33:58 +0000540 assert (xception2 == NULL);
Roger Meier213a6642010-10-27 12:30:11 +0000541 g_error_free (error);
542 error = NULL;
543 g_object_unref (xception);
Roger Meierc75797d2012-04-28 11:33:58 +0000544 g_object_unref (multi_in);
Roger Meier213a6642010-10-27 12:30:11 +0000545 xception = NULL;
Roger Meierc75797d2012-04-28 11:33:58 +0000546 multi_in = NULL;
Roger Meier213a6642010-10-27 12:30:11 +0000547
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, "Xception2", NULL, &xception, &xception2, &error) == FALSE);
550 assert (xception2->errorCode == 2002);
Roger Meierc75797d2012-04-28 11:33:58 +0000551 assert (xception == NULL);
Roger Meier213a6642010-10-27 12:30:11 +0000552 g_error_free (error);
553 error = NULL;
554 g_object_unref (xception2);
Roger Meierc75797d2012-04-28 11:33:58 +0000555 g_object_unref (multi_in);
Roger Meier213a6642010-10-27 12:30:11 +0000556 xception2 = 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, NULL , NULL, &xception, &xception2, &error) == TRUE);
561 assert (error == NULL);
Roger Meierc75797d2012-04-28 11:33:58 +0000562 g_object_unref(multi_in);
563 multi_in = NULL;
Roger Meier213a6642010-10-27 12:30:11 +0000564
565 assert (t_test_thrift_test_client_test_oneway (iface, 1, &error) == TRUE);
566 assert (error == NULL);
567
568 /* sleep to let the oneway call go through */
569 sleep (5);
570
571 thrift_transport_close (THRIFT_TRANSPORT(tsocket), NULL);
572 g_object_unref (client);
573 g_object_unref (protocol);
574 g_object_unref (tsocket);
575}
576
577
578} /* extern "C" */
579
580
581static void
582bailout (int signum)
583{
584 exit (1);
585}
586
587int
588main (int argc, char **argv)
589{
590 int status;
591 int pid = fork ();
592 assert (pid >= 0);
593
594 if (pid == 0) /* child */
595 {
Marco Molteni83494252015-04-16 13:50:20 +0200596 boost::shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());
597 boost::shared_ptr<TestHandler> testHandler(new TestHandler());
598 boost::shared_ptr<ThriftTestProcessor> testProcessor(new ThriftTestProcessor(testHandler));
599 boost::shared_ptr<TServerSocket> serverSocket(new TServerSocket(TEST_PORT));
600 boost::shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
Roger Meier213a6642010-10-27 12:30:11 +0000601 TSimpleServer simpleServer(testProcessor, serverSocket, transportFactory, protocolFactory);
602 signal (SIGALRM, bailout);
603 alarm (60);
604 simpleServer.serve();
605 } else {
606 sleep (1);
607 test_thrift_client ();
608 kill (pid, SIGINT);
609 wait (&status) == pid;
610 }
611
612 return 0;
613}
614