blob: 307fd4f5f01485f883a01c53a506f4eb497c3b0c [file] [log] [blame]
zeshuai007037753e2020-11-30 11:16:10 +08001/*
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 */
19
20/* test a C client with a C++ server (that makes sense...) */
21
22#include <signal.h>
23#include <sys/types.h>
24#include <sys/wait.h>
25#include <thrift/protocol/TBinaryProtocol.h>
26#include <thrift/protocol/TDebugProtocol.h>
27#include <thrift/server/TSimpleServer.h>
28#include <memory>
29#include <thrift/transport/TServerSocket.h>
30#include <thrift/transport/TZlibTransport.h>
31#include "ThriftTest.h"
32#include "ThriftTest_types.h"
33
34#include <iostream>
35#include <map>
36#include <set>
37#include <string>
38#include <vector>
39
40using namespace apache::thrift;
41using namespace apache::thrift::concurrency;
42using namespace apache::thrift::protocol;
43using namespace apache::thrift::server;
44using namespace apache::thrift::transport;
45
46using namespace thrift::test;
47
48using std::cout;
zeshuai007037753e2020-11-30 11:16:10 +080049using std::fixed;
50using std::make_pair;
51using std::map;
52using std::set;
53using std::string;
54using std::vector;
55
56#define TEST_PORT 9980
57
58// Extra functions required for ThriftTest_types to work
59namespace thrift { namespace test {
60
61bool Insanity::operator<(thrift::test::Insanity const& other) const {
62 using apache::thrift::ThriftDebugString;
63 return ThriftDebugString(*this) < ThriftDebugString(other);
64}
65
66}}
67
68class TestHandler : public ThriftTestIf {
69 public:
70 TestHandler() = default;
71
72 void testVoid() override {
CJCombrink4a280d52024-03-14 19:57:41 +010073 cout << "[C -> C++] testVoid()" << '\n';
zeshuai007037753e2020-11-30 11:16:10 +080074 }
75
76 void testString(string& out, const string &thing) override {
CJCombrink4a280d52024-03-14 19:57:41 +010077 cout << "[C -> C++] testString(\"" << thing << "\")" << '\n';
zeshuai007037753e2020-11-30 11:16:10 +080078 out = thing;
79 }
80
81 bool testBool(const bool thing) override {
CJCombrink4a280d52024-03-14 19:57:41 +010082 cout << "[C -> C++] testBool(" << (thing ? "true" : "false") << ")" << '\n';
zeshuai007037753e2020-11-30 11:16:10 +080083 return thing;
84 }
85 int8_t testByte(const int8_t thing) override {
CJCombrink4a280d52024-03-14 19:57:41 +010086 cout << "[C -> C++] testByte(" << (int)thing << ")" << '\n';
zeshuai007037753e2020-11-30 11:16:10 +080087 return thing;
88 }
89 int32_t testI32(const int32_t thing) override {
CJCombrink4a280d52024-03-14 19:57:41 +010090 cout << "[C -> C++] testI32(" << thing << ")" << '\n';
zeshuai007037753e2020-11-30 11:16:10 +080091 return thing;
92 }
93
94 int64_t testI64(const int64_t thing) override {
CJCombrink4a280d52024-03-14 19:57:41 +010095 cout << "[C -> C++] testI64(" << thing << ")" << '\n';
zeshuai007037753e2020-11-30 11:16:10 +080096 return thing;
97 }
98
99 double testDouble(const double thing) override {
100 cout.precision(6);
CJCombrink4a280d52024-03-14 19:57:41 +0100101 cout << "[C -> C++] testDouble(" << fixed << thing << ")" << '\n';
zeshuai007037753e2020-11-30 11:16:10 +0800102 return thing;
103 }
104
105 void testBinary(string& out, const string &thing) override {
CJCombrink4a280d52024-03-14 19:57:41 +0100106 cout << "[C -> C++] testBinary(\"" << thing << "\")" << '\n';
zeshuai007037753e2020-11-30 11:16:10 +0800107 out = thing;
108 }
109
110 void testStruct(Xtruct& out, const Xtruct &thing) override {
CJCombrink4a280d52024-03-14 19:57:41 +0100111 cout << "[C -> C++] testStruct({\"" << thing.string_thing << "\", " << (int)thing.byte_thing << ", " << thing.i32_thing << ", " << thing.i64_thing << "})" << '\n';
zeshuai007037753e2020-11-30 11:16:10 +0800112 out = thing;
113 }
114
115 void testNest(Xtruct2& out, const Xtruct2& nest) override {
116 const Xtruct &thing = nest.struct_thing;
CJCombrink4a280d52024-03-14 19:57:41 +0100117 cout << "[C -> C++] testNest({" << (int)nest.byte_thing << ", {\"" << thing.string_thing << "\", " << (int)thing.byte_thing << ", " << thing.i32_thing << ", " << thing.i64_thing << "}, " << nest.i32_thing << "})" << '\n';
zeshuai007037753e2020-11-30 11:16:10 +0800118 out = nest;
119 }
120
121 void testMap(map<int32_t, int32_t> &out, const map<int32_t, int32_t> &thing) override {
122 cout << "[C -> C++] testMap({";
123 map<int32_t, int32_t>::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 cout << ", ";
130 }
131 cout << m_iter->first << " => " << m_iter->second;
132 }
CJCombrink4a280d52024-03-14 19:57:41 +0100133 cout << "})" << '\n';
zeshuai007037753e2020-11-30 11:16:10 +0800134 out = thing;
135 }
136
137 void testStringMap(map<std::string, std::string> &out, const map<std::string, std::string> &thing) override {
138 cout << "[C -> C++] testStringMap({";
139 map<std::string, std::string>::const_iterator m_iter;
140 bool first = true;
141 for (m_iter = thing.begin(); m_iter != thing.end(); ++m_iter) {
142 if (first) {
143 first = false;
144 } else {
145 cout << ", ";
146 }
147 cout << "\"" << m_iter->first << "\" => \"" << m_iter->second << "\"";
148 }
CJCombrink4a280d52024-03-14 19:57:41 +0100149 cout << "})" << '\n';
zeshuai007037753e2020-11-30 11:16:10 +0800150 out = thing;
151 }
152
153
154 void testSet(set<int32_t> &out, const set<int32_t> &thing) override {
155 cout << "[C -> C++] testSet({";
156 set<int32_t>::const_iterator s_iter;
157 bool first = true;
158 for (s_iter = thing.begin(); s_iter != thing.end(); ++s_iter) {
159 if (first) {
160 first = false;
161 } else {
162 cout << ", ";
163 }
164 cout << *s_iter;
165 }
CJCombrink4a280d52024-03-14 19:57:41 +0100166 cout << "})" << '\n';
zeshuai007037753e2020-11-30 11:16:10 +0800167 out = thing;
168 }
169
170 void testList(vector<int32_t> &out, const vector<int32_t> &thing) override {
171 cout << "[C -> C++] testList({";
172 vector<int32_t>::const_iterator l_iter;
173 bool first = true;
174 for (l_iter = thing.begin(); l_iter != thing.end(); ++l_iter) {
175 if (first) {
176 first = false;
177 } else {
178 cout << ", ";
179 }
180 cout << *l_iter;
181 }
CJCombrink4a280d52024-03-14 19:57:41 +0100182 cout << "})" << '\n';
zeshuai007037753e2020-11-30 11:16:10 +0800183 out = thing;
184 }
185
186 Numberz::type testEnum(const Numberz::type thing) override {
CJCombrink4a280d52024-03-14 19:57:41 +0100187 cout << "[C -> C++] testEnum(" << thing << ")" << '\n';
zeshuai007037753e2020-11-30 11:16:10 +0800188 return thing;
189 }
190
191 UserId testTypedef(const UserId thing) override {
CJCombrink4a280d52024-03-14 19:57:41 +0100192 cout << "[C -> C++] testTypedef(" << thing << ")" << '\n';
zeshuai007037753e2020-11-30 11:16:10 +0800193 return thing; }
194
195 void testMapMap(map<int32_t, map<int32_t,int32_t> > &mapmap, const int32_t hello) override {
CJCombrink4a280d52024-03-14 19:57:41 +0100196 cout << "[C -> C++] testMapMap(" << hello << ")" << '\n';
zeshuai007037753e2020-11-30 11:16:10 +0800197
198 map<int32_t,int32_t> pos;
199 map<int32_t,int32_t> neg;
200 for (int i = 1; i < 5; i++) {
201 pos.insert(make_pair(i,i));
202 neg.insert(make_pair(-i,-i));
203 }
204
205 mapmap.insert(make_pair(4, pos));
206 mapmap.insert(make_pair(-4, neg));
207
208 }
209
210 void testInsanity(map<UserId, map<Numberz::type,Insanity> > &insane, const Insanity &argument) override {
211 THRIFT_UNUSED_VARIABLE (argument);
212
CJCombrink4a280d52024-03-14 19:57:41 +0100213 cout << "[C -> C++] testInsanity()" << '\n';
zeshuai007037753e2020-11-30 11:16:10 +0800214
215 Xtruct hello;
216 hello.string_thing = "Hello2";
217 hello.byte_thing = 2;
218 hello.i32_thing = 2;
219 hello.i64_thing = 2;
220
221 Xtruct goodbye;
222 goodbye.string_thing = "Goodbye4";
223 goodbye.byte_thing = 4;
224 goodbye.i32_thing = 4;
225 goodbye.i64_thing = 4;
226
227 Insanity crazy;
228 crazy.userMap.insert(make_pair(Numberz::EIGHT, 8));
229 crazy.xtructs.push_back(goodbye);
230
231 Insanity looney;
232 crazy.userMap.insert(make_pair(Numberz::FIVE, 5));
233 crazy.xtructs.push_back(hello);
234
235 map<Numberz::type, Insanity> first_map;
236 map<Numberz::type, Insanity> second_map;
237
238 first_map.insert(make_pair(Numberz::TWO, crazy));
239 first_map.insert(make_pair(Numberz::THREE, crazy));
240
241 second_map.insert(make_pair(Numberz::SIX, looney));
242
243 insane.insert(make_pair(1, first_map));
244 insane.insert(make_pair(2, second_map));
245
246 cout << "return = {";
247 map<UserId, map<Numberz::type,Insanity> >::const_iterator i_iter;
248 for (i_iter = insane.begin(); i_iter != insane.end(); ++i_iter) {
249 cout << i_iter->first << " => {";
250 map<Numberz::type,Insanity>::const_iterator i2_iter;
251 for (i2_iter = i_iter->second.begin();
252 i2_iter != i_iter->second.end();
253 ++i2_iter) {
254 cout << i2_iter->first << " => {";
255 map<Numberz::type, UserId> userMap = i2_iter->second.userMap;
256 map<Numberz::type, UserId>::const_iterator um;
257 cout << "{";
258 for (um = userMap.begin(); um != userMap.end(); ++um) {
259 cout << um->first << " => " << um->second << ", ";
260 }
261 cout << "}, ";
262
263 vector<Xtruct> xtructs = i2_iter->second.xtructs;
264 vector<Xtruct>::const_iterator x;
265 cout << "{";
266 for (x = xtructs.begin(); x != xtructs.end(); ++x) {
267 cout << "{\"" << x->string_thing << "\", " << (int)x->byte_thing << ", " << x->i32_thing << ", " << x->i64_thing << "}, ";
268 }
269 cout << "}";
270
271 cout << "}, ";
272 }
273 cout << "}, ";
274 }
CJCombrink4a280d52024-03-14 19:57:41 +0100275 cout << "}" << '\n';
zeshuai007037753e2020-11-30 11:16:10 +0800276
277
278 }
279
280 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) override {
281 THRIFT_UNUSED_VARIABLE (arg3);
282 THRIFT_UNUSED_VARIABLE (arg4);
283 THRIFT_UNUSED_VARIABLE (arg5);
284
CJCombrink4a280d52024-03-14 19:57:41 +0100285 cout << "[C -> C++] testMulti()" << '\n';
zeshuai007037753e2020-11-30 11:16:10 +0800286
287 hello.string_thing = "Hello2";
288 hello.byte_thing = arg0;
289 hello.i32_thing = arg1;
290 hello.i64_thing = (int64_t)arg2;
291 }
292
293 void testException(const std::string &arg)
294 throw(Xception, apache::thrift::TException) override
295 {
CJCombrink4a280d52024-03-14 19:57:41 +0100296 cout << "[C -> C++] testException(" << arg << ")" << '\n';
zeshuai007037753e2020-11-30 11:16:10 +0800297 if (arg.compare("Xception") == 0) {
298 Xception e;
299 e.errorCode = 1001;
300 e.message = arg;
301 throw e;
302 } else if (arg.compare("ApplicationException") == 0) {
303 apache::thrift::TException e;
304 throw e;
305 } else {
306 Xtruct result;
307 result.string_thing = arg;
308 return;
309 }
310 }
311
312 void testMultiException(Xtruct &result, const std::string &arg0, const std::string &arg1) throw(Xception, Xception2) override {
313
CJCombrink4a280d52024-03-14 19:57:41 +0100314 cout << "[C -> C++] testMultiException(" << arg0 << ", " << arg1 << ")" << '\n';
zeshuai007037753e2020-11-30 11:16:10 +0800315
316 if (arg0.compare("Xception") == 0) {
317 Xception e;
318 e.errorCode = 1001;
319 e.message = "This is an Xception";
320 throw e;
321 } else if (arg0.compare("Xception2") == 0) {
322 Xception2 e;
323 e.errorCode = 2002;
324 e.struct_thing.string_thing = "This is an Xception2";
325 throw e;
326 } else {
327 result.string_thing = arg1;
328 return;
329 }
330 }
331
332 void testOneway(int sleepFor) override {
CJCombrink4a280d52024-03-14 19:57:41 +0100333 cout << "testOneway(" << sleepFor << "): Sleeping..." << '\n';
zeshuai007037753e2020-11-30 11:16:10 +0800334 sleep(sleepFor);
CJCombrink4a280d52024-03-14 19:57:41 +0100335 cout << "testOneway(" << sleepFor << "): done sleeping!" << '\n';
zeshuai007037753e2020-11-30 11:16:10 +0800336 }
337};
338
339// C CLIENT
340extern "C" {
341
342#undef THRIFT_SOCKET /* from lib/cpp */
343
344#include "t_test_thrift_test.h"
345#include "t_test_thrift_test_types.h"
346#include <thrift/c_glib/transport/thrift_socket.h>
347#include <thrift/c_glib/transport/thrift_zlib_transport.h>
348#include <thrift/c_glib/protocol/thrift_protocol.h>
349#include <thrift/c_glib/protocol/thrift_binary_protocol.h>
350
351static void
352test_thrift_client (void)
353{
354 ThriftSocket *tsocket = nullptr;
355 ThriftBinaryProtocol *protocol = nullptr;
356 ThriftZlibTransport *transport = nullptr;
357 TTestThriftTestClient *client = nullptr;
358 TTestThriftTestIf *iface = nullptr;
359 GError *error = nullptr;
360 gchar *string = nullptr;
361 gint8 byte = 0;
362 gint16 i16 = 0;
363 gint32 i32 = 0, another_i32 = 56789;
364 gint64 i64 = 0;
365 double dbl = 0.0;
366 TTestXtruct *xtruct_in, *xtruct_out;
367 TTestXtruct2 *xtruct2_in, *xtruct2_out;
368 GHashTable *map_in = nullptr, *map_out = nullptr;
369 GHashTable *set_in = nullptr, *set_out = nullptr;
370 GArray *list_in = nullptr, *list_out = nullptr;
371 TTestNumberz enum_in, enum_out;
372 TTestUserId user_id_in, user_id_out;
373 GHashTable *insanity_in = nullptr;
374 TTestXtruct *xtruct1, *xtruct2;
375 TTestInsanity *insanity_out = nullptr;
376 TTestXtruct *multi_in = nullptr;
377 GHashTable *multi_map_out = nullptr;
378 TTestXception *xception = nullptr;
379 TTestXception2 *xception2 = nullptr;
380
381#if (!GLIB_CHECK_VERSION (2, 36, 0))
382 // initialize gobject
383 g_type_init ();
384#endif
385
386 // create a C client
387 tsocket = (ThriftSocket *) g_object_new (THRIFT_TYPE_SOCKET,
388 "hostname", "localhost",
389 "port", TEST_PORT, nullptr);
390 transport = (ThriftZlibTransport *) g_object_new (THRIFT_TYPE_ZLIB_TRANSPORT,
391 "transport", tsocket, nullptr);
392 protocol = (ThriftBinaryProtocol *) g_object_new (THRIFT_TYPE_BINARY_PROTOCOL,
393 "transport",
394 transport, nullptr);
395 client = (TTestThriftTestClient *) g_object_new (T_TEST_TYPE_THRIFT_TEST_CLIENT, "input_protocol", protocol, "output_protocol", protocol, nullptr);
396 iface = T_TEST_THRIFT_TEST_IF (client);
397
398 // open and send
399 thrift_transport_open (THRIFT_TRANSPORT(transport), nullptr);
400
401 assert (t_test_thrift_test_client_test_void (iface, &error) == TRUE);
402 assert (error == nullptr);
403
404 assert (t_test_thrift_test_client_test_string (iface, &string, "test123", &error) == TRUE);
405 assert (strcmp (string, "test123") == 0);
406 g_free (string);
407 assert (error == nullptr);
408
409 assert (t_test_thrift_test_client_test_byte (iface, &byte, (gint8) 5, &error) == TRUE);
410 assert (byte == 5);
411 assert (error == nullptr);
412
413 assert (t_test_thrift_test_client_test_i32 (iface, &i32, 123, &error) == TRUE);
414 assert (i32 == 123);
415 assert (error == nullptr);
416
417 assert (t_test_thrift_test_client_test_i64 (iface, &i64, 12345, &error) == TRUE);
418 assert (i64 == 12345);
419 assert (error == nullptr);
420
421 assert (t_test_thrift_test_client_test_double (iface, &dbl, 5.6, &error) == TRUE);
422 assert (dbl == 5.6);
423 assert (error == nullptr);
424
425 xtruct_out = (TTestXtruct *) g_object_new (T_TEST_TYPE_XTRUCT, nullptr);
426 xtruct_out->byte_thing = 1;
427 xtruct_out->__isset_byte_thing = TRUE;
428 xtruct_out->i32_thing = 15;
429 xtruct_out->__isset_i32_thing = TRUE;
430 xtruct_out->i64_thing = 151;
431 xtruct_out->__isset_i64_thing = TRUE;
432 xtruct_out->string_thing = g_strdup ("abc123");
433 xtruct_out->__isset_string_thing = TRUE;
434 xtruct_in = (TTestXtruct *) g_object_new(T_TEST_TYPE_XTRUCT, nullptr);
435 assert (t_test_thrift_test_client_test_struct (iface, &xtruct_in, xtruct_out, &error) == TRUE);
436 assert (error == nullptr);
437
438 xtruct2_out = (TTestXtruct2 *) g_object_new (T_TEST_TYPE_XTRUCT2, nullptr);
439 xtruct2_out->byte_thing = 1;
440 xtruct2_out->__isset_byte_thing = TRUE;
441 if (xtruct2_out->struct_thing != nullptr)
442 g_object_unref(xtruct2_out->struct_thing);
443 xtruct2_out->struct_thing = xtruct_out;
444 xtruct2_out->__isset_struct_thing = TRUE;
445 xtruct2_out->i32_thing = 123;
446 xtruct2_out->__isset_i32_thing = TRUE;
447 xtruct2_in = (TTestXtruct2 *) g_object_new (T_TEST_TYPE_XTRUCT2, nullptr);
448 assert (t_test_thrift_test_client_test_nest (iface, &xtruct2_in, xtruct2_out, &error) == TRUE);
449 assert (error == nullptr);
450
451 g_object_unref (xtruct2_out);
452 g_object_unref (xtruct2_in);
453 g_object_unref (xtruct_in);
454
455 map_out = g_hash_table_new (nullptr, nullptr);
456 map_in = g_hash_table_new (nullptr, nullptr); g_hash_table_insert (map_out, &i32, &i32);
457 assert (t_test_thrift_test_client_test_map (iface, &map_in, map_out, &error) == TRUE);
458 assert (error == nullptr);
459 g_hash_table_destroy (map_out);
460 g_hash_table_destroy (map_in);
461
462 map_out = g_hash_table_new (nullptr, nullptr);
463 map_in = g_hash_table_new (nullptr, nullptr);
464 g_hash_table_insert (map_out, g_strdup ("a"), g_strdup ("123"));
465 g_hash_table_insert (map_out, g_strdup ("a b"), g_strdup ("with spaces "));
466 g_hash_table_insert (map_out, g_strdup ("same"), g_strdup ("same"));
467 g_hash_table_insert (map_out, g_strdup ("0"), g_strdup ("numeric key"));
468 assert (t_test_thrift_test_client_test_string_map (iface, &map_in, map_out, &error) == TRUE);
469 assert (error == nullptr);
470 g_hash_table_destroy (map_out);
471 g_hash_table_destroy (map_in);
472
473 set_out = g_hash_table_new (nullptr, nullptr);
474 set_in = g_hash_table_new (nullptr, nullptr);
475 g_hash_table_insert (set_out, &i32, &i32);
476 assert (t_test_thrift_test_client_test_set (iface, &set_in, set_out, &error) == TRUE);
477 assert (error == nullptr);
478 g_hash_table_destroy (set_out);
479 g_hash_table_destroy (set_in);
480
481 list_out = g_array_new(TRUE, TRUE, sizeof(gint32));
482 list_in = g_array_new(TRUE, TRUE, sizeof(gint32));
483 another_i32 = 456;
484 g_array_append_val (list_out, i32);
485 g_array_append_val (list_out, another_i32);
486 assert (t_test_thrift_test_client_test_list (iface, &list_in, list_out, &error) == TRUE);
487 assert (error == nullptr);
488 g_array_free (list_out, TRUE);
489 g_array_free (list_in, TRUE);
490
491 enum_out = T_TEST_NUMBERZ_ONE;
492 assert (t_test_thrift_test_client_test_enum (iface, &enum_in, enum_out, &error) == TRUE);
493 assert (enum_in == enum_out);
494 assert (error == nullptr);
495
496 user_id_out = 12345;
497 assert (t_test_thrift_test_client_test_typedef (iface, &user_id_in, user_id_out, &error) == TRUE);
498 assert (user_id_in == user_id_out);
499 assert (error == nullptr);
500
501 map_in = g_hash_table_new (nullptr, nullptr);
502 assert (t_test_thrift_test_client_test_map_map (iface, &map_in, i32, &error) == TRUE);
503 assert (error == nullptr);
504 g_hash_table_destroy (map_in);
505
506 // insanity
507 insanity_out = (TTestInsanity *) g_object_new (T_TEST_TYPE_INSANITY, nullptr);
508 insanity_out->userMap = g_hash_table_new (nullptr, nullptr);
509 g_hash_table_insert (insanity_out->userMap, GINT_TO_POINTER (enum_out), &user_id_out);
510
511 xtruct1 = (TTestXtruct *) g_object_new (T_TEST_TYPE_XTRUCT, nullptr);
512 xtruct1->byte_thing = 1;
513 xtruct1->__isset_byte_thing = TRUE;
514 xtruct1->i32_thing = 15;
515 xtruct1->__isset_i32_thing = TRUE;
516 xtruct1->i64_thing = 151;
517 xtruct1->__isset_i64_thing = TRUE;
518 xtruct1->string_thing = g_strdup ("abc123");
519 xtruct1->__isset_string_thing = TRUE;
520 xtruct2 = (TTestXtruct *) g_object_new (T_TEST_TYPE_XTRUCT, nullptr);
521 xtruct2->byte_thing = 1;
522 xtruct2->__isset_byte_thing = TRUE;
523 xtruct2->i32_thing = 15;
524 xtruct2->__isset_i32_thing = TRUE;
525 xtruct2->i64_thing = 151;
526 xtruct2->__isset_i64_thing = TRUE;
527 xtruct2->string_thing = g_strdup ("abc123");
528 xtruct2->__isset_string_thing = TRUE;
529
530 insanity_in = g_hash_table_new (nullptr, nullptr);
531 g_ptr_array_add (insanity_out->xtructs, xtruct1);
532 g_ptr_array_add (insanity_out->xtructs, xtruct2);
533 assert (t_test_thrift_test_client_test_insanity (iface, &insanity_in, insanity_out, &error) == TRUE);
534
535 g_hash_table_unref (insanity_in);
536 g_ptr_array_free (insanity_out->xtructs, TRUE);
537
538 multi_map_out = g_hash_table_new (nullptr, nullptr);
539 string = g_strdup ("abc123");
540 g_hash_table_insert (multi_map_out, &i16, string);
541 multi_in = (TTestXtruct *) g_object_new (T_TEST_TYPE_XTRUCT, nullptr);
542 assert (t_test_thrift_test_client_test_multi (iface, &multi_in, byte, i32, i64, multi_map_out, enum_out, user_id_out, &error) == TRUE);
543 assert (multi_in->i32_thing == i32);
544 assert (multi_in->i64_thing == i64);
545 g_object_unref (multi_in);
546 g_hash_table_unref (multi_map_out);
547 g_free (string);
548
549 assert (t_test_thrift_test_client_test_exception (iface, "Xception", &xception, &error) == FALSE);
550 assert (xception->errorCode == 1001);
551 g_error_free (error);
552 error = nullptr;
553 g_object_unref (xception);
554 xception = nullptr;
555
556 assert (t_test_thrift_test_client_test_exception (iface, "ApplicationException", &xception, &error) == FALSE);
557 g_error_free (error);
558 error = nullptr;
559 assert (xception == nullptr);
560
561 assert (t_test_thrift_test_client_test_exception (iface, "Test", &xception, &error) == TRUE);
562 assert (error == nullptr);
563
564 multi_in = (TTestXtruct*) g_object_new (T_TEST_TYPE_XTRUCT, nullptr);
565 assert (t_test_thrift_test_client_test_multi_exception (iface, &multi_in, "Xception", nullptr, &xception, &xception2, &error) == FALSE);
566 assert (xception->errorCode == 1001);
567 assert (xception2 == nullptr);
568 g_error_free (error);
569 error = nullptr;
570 g_object_unref (xception);
571 g_object_unref (multi_in);
572 xception = nullptr;
573 multi_in = nullptr;
574
575 multi_in = (TTestXtruct*) g_object_new (T_TEST_TYPE_XTRUCT, nullptr);
576 assert (t_test_thrift_test_client_test_multi_exception (iface, &multi_in, "Xception2", nullptr, &xception, &xception2, &error) == FALSE);
577 assert (xception2->errorCode == 2002);
578 assert (xception == nullptr);
579 g_error_free (error);
580 error = nullptr;
581 g_object_unref (xception2);
582 g_object_unref (multi_in);
583 xception2 = nullptr;
584 multi_in = nullptr;
585
586 multi_in = (TTestXtruct*) g_object_new (T_TEST_TYPE_XTRUCT, nullptr);
587 assert (t_test_thrift_test_client_test_multi_exception (iface, &multi_in, nullptr , nullptr, &xception, &xception2, &error) == TRUE);
588 assert (error == nullptr);
589 g_object_unref(multi_in);
590 multi_in = nullptr;
591
592 assert (t_test_thrift_test_client_test_oneway (iface, 1, &error) == TRUE);
593 assert (error == nullptr);
594
595 /* sleep to let the oneway call go through */
596 sleep (5);
597
598 thrift_transport_close (THRIFT_TRANSPORT(tsocket), nullptr);
599 g_object_unref (client);
600 g_object_unref (protocol);
601 g_object_unref (tsocket);
602}
603
604
605} /* extern "C" */
606
607
608static void
609bailout (int signum)
610{
611 THRIFT_UNUSED_VARIABLE (signum);
612
613 exit (1);
614}
615
616int
617main (void)
618{
619 int status;
620 int pid = fork ();
621 assert (pid >= 0);
622
623 if (pid == 0) /* child */
624 {
625 std::shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());
626 std::shared_ptr<TestHandler> testHandler(new TestHandler());
627 std::shared_ptr<ThriftTestProcessor> testProcessor(new ThriftTestProcessor(testHandler));
628 std::shared_ptr<TServerSocket> serverSocket(new TServerSocket(TEST_PORT));
629 std::shared_ptr<TZlibTransportFactory> transportFactory(new TZlibTransportFactory());
630 //std::shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
631 TSimpleServer simpleServer(testProcessor, serverSocket, transportFactory, protocolFactory);
632 signal (SIGALRM, bailout);
633 alarm (60);
634 simpleServer.serve();
635 } else {
636 sleep (1);
637 test_thrift_client ();
638 kill (pid, SIGINT);
639 assert (wait (&status) == pid);
640 }
641
642 return 0;
643}
644