blob: 7c0f24a5ed0b4f13560916c5c2850dbdd97c6a18 [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
CJCombrink4b909092024-04-27 19:51:39 +0200110 std::string testUuid(const std::string thing) override {
111 cout << "[C -> C++] testUuid(\"" << std::hex << thing << "\")" << '\n';
112 return thing;
113 }
114
zeshuai007037753e2020-11-30 11:16:10 +0800115 void testStruct(Xtruct& out, const Xtruct &thing) override {
CJCombrink4a280d52024-03-14 19:57:41 +0100116 cout << "[C -> C++] testStruct({\"" << thing.string_thing << "\", " << (int)thing.byte_thing << ", " << thing.i32_thing << ", " << thing.i64_thing << "})" << '\n';
zeshuai007037753e2020-11-30 11:16:10 +0800117 out = thing;
118 }
119
120 void testNest(Xtruct2& out, const Xtruct2& nest) override {
121 const Xtruct &thing = nest.struct_thing;
CJCombrink4a280d52024-03-14 19:57:41 +0100122 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 +0800123 out = nest;
124 }
125
126 void testMap(map<int32_t, int32_t> &out, const map<int32_t, int32_t> &thing) override {
127 cout << "[C -> C++] testMap({";
128 map<int32_t, int32_t>::const_iterator m_iter;
129 bool first = true;
130 for (m_iter = thing.begin(); m_iter != thing.end(); ++m_iter) {
131 if (first) {
132 first = false;
133 } else {
134 cout << ", ";
135 }
136 cout << m_iter->first << " => " << m_iter->second;
137 }
CJCombrink4a280d52024-03-14 19:57:41 +0100138 cout << "})" << '\n';
zeshuai007037753e2020-11-30 11:16:10 +0800139 out = thing;
140 }
141
142 void testStringMap(map<std::string, std::string> &out, const map<std::string, std::string> &thing) override {
143 cout << "[C -> C++] testStringMap({";
144 map<std::string, std::string>::const_iterator m_iter;
145 bool first = true;
146 for (m_iter = thing.begin(); m_iter != thing.end(); ++m_iter) {
147 if (first) {
148 first = false;
149 } else {
150 cout << ", ";
151 }
152 cout << "\"" << m_iter->first << "\" => \"" << m_iter->second << "\"";
153 }
CJCombrink4a280d52024-03-14 19:57:41 +0100154 cout << "})" << '\n';
zeshuai007037753e2020-11-30 11:16:10 +0800155 out = thing;
156 }
157
158
159 void testSet(set<int32_t> &out, const set<int32_t> &thing) override {
160 cout << "[C -> C++] testSet({";
161 set<int32_t>::const_iterator s_iter;
162 bool first = true;
163 for (s_iter = thing.begin(); s_iter != thing.end(); ++s_iter) {
164 if (first) {
165 first = false;
166 } else {
167 cout << ", ";
168 }
169 cout << *s_iter;
170 }
CJCombrink4a280d52024-03-14 19:57:41 +0100171 cout << "})" << '\n';
zeshuai007037753e2020-11-30 11:16:10 +0800172 out = thing;
173 }
174
175 void testList(vector<int32_t> &out, const vector<int32_t> &thing) override {
176 cout << "[C -> C++] testList({";
177 vector<int32_t>::const_iterator l_iter;
178 bool first = true;
179 for (l_iter = thing.begin(); l_iter != thing.end(); ++l_iter) {
180 if (first) {
181 first = false;
182 } else {
183 cout << ", ";
184 }
185 cout << *l_iter;
186 }
CJCombrink4a280d52024-03-14 19:57:41 +0100187 cout << "})" << '\n';
zeshuai007037753e2020-11-30 11:16:10 +0800188 out = thing;
189 }
190
191 Numberz::type testEnum(const Numberz::type thing) override {
CJCombrink4a280d52024-03-14 19:57:41 +0100192 cout << "[C -> C++] testEnum(" << thing << ")" << '\n';
zeshuai007037753e2020-11-30 11:16:10 +0800193 return thing;
194 }
195
196 UserId testTypedef(const UserId thing) override {
CJCombrink4a280d52024-03-14 19:57:41 +0100197 cout << "[C -> C++] testTypedef(" << thing << ")" << '\n';
CJCombrink4b909092024-04-27 19:51:39 +0200198 return thing;
199 }
zeshuai007037753e2020-11-30 11:16:10 +0800200
201 void testMapMap(map<int32_t, map<int32_t,int32_t> > &mapmap, const int32_t hello) override {
CJCombrink4a280d52024-03-14 19:57:41 +0100202 cout << "[C -> C++] testMapMap(" << hello << ")" << '\n';
zeshuai007037753e2020-11-30 11:16:10 +0800203
204 map<int32_t,int32_t> pos;
205 map<int32_t,int32_t> neg;
206 for (int i = 1; i < 5; i++) {
207 pos.insert(make_pair(i,i));
208 neg.insert(make_pair(-i,-i));
209 }
210
211 mapmap.insert(make_pair(4, pos));
212 mapmap.insert(make_pair(-4, neg));
213
214 }
215
216 void testInsanity(map<UserId, map<Numberz::type,Insanity> > &insane, const Insanity &argument) override {
217 THRIFT_UNUSED_VARIABLE (argument);
218
CJCombrink4a280d52024-03-14 19:57:41 +0100219 cout << "[C -> C++] testInsanity()" << '\n';
zeshuai007037753e2020-11-30 11:16:10 +0800220
221 Xtruct hello;
222 hello.string_thing = "Hello2";
223 hello.byte_thing = 2;
224 hello.i32_thing = 2;
225 hello.i64_thing = 2;
226
227 Xtruct goodbye;
228 goodbye.string_thing = "Goodbye4";
229 goodbye.byte_thing = 4;
230 goodbye.i32_thing = 4;
231 goodbye.i64_thing = 4;
232
233 Insanity crazy;
234 crazy.userMap.insert(make_pair(Numberz::EIGHT, 8));
235 crazy.xtructs.push_back(goodbye);
236
237 Insanity looney;
238 crazy.userMap.insert(make_pair(Numberz::FIVE, 5));
239 crazy.xtructs.push_back(hello);
240
241 map<Numberz::type, Insanity> first_map;
242 map<Numberz::type, Insanity> second_map;
243
244 first_map.insert(make_pair(Numberz::TWO, crazy));
245 first_map.insert(make_pair(Numberz::THREE, crazy));
246
247 second_map.insert(make_pair(Numberz::SIX, looney));
248
249 insane.insert(make_pair(1, first_map));
250 insane.insert(make_pair(2, second_map));
251
252 cout << "return = {";
253 map<UserId, map<Numberz::type,Insanity> >::const_iterator i_iter;
254 for (i_iter = insane.begin(); i_iter != insane.end(); ++i_iter) {
255 cout << i_iter->first << " => {";
256 map<Numberz::type,Insanity>::const_iterator i2_iter;
257 for (i2_iter = i_iter->second.begin();
258 i2_iter != i_iter->second.end();
259 ++i2_iter) {
260 cout << i2_iter->first << " => {";
261 map<Numberz::type, UserId> userMap = i2_iter->second.userMap;
262 map<Numberz::type, UserId>::const_iterator um;
263 cout << "{";
264 for (um = userMap.begin(); um != userMap.end(); ++um) {
265 cout << um->first << " => " << um->second << ", ";
266 }
267 cout << "}, ";
268
269 vector<Xtruct> xtructs = i2_iter->second.xtructs;
270 vector<Xtruct>::const_iterator x;
271 cout << "{";
272 for (x = xtructs.begin(); x != xtructs.end(); ++x) {
273 cout << "{\"" << x->string_thing << "\", " << (int)x->byte_thing << ", " << x->i32_thing << ", " << x->i64_thing << "}, ";
274 }
275 cout << "}";
276
277 cout << "}, ";
278 }
279 cout << "}, ";
280 }
CJCombrink4a280d52024-03-14 19:57:41 +0100281 cout << "}" << '\n';
zeshuai007037753e2020-11-30 11:16:10 +0800282
283
284 }
285
286 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 {
287 THRIFT_UNUSED_VARIABLE (arg3);
288 THRIFT_UNUSED_VARIABLE (arg4);
289 THRIFT_UNUSED_VARIABLE (arg5);
290
CJCombrink4a280d52024-03-14 19:57:41 +0100291 cout << "[C -> C++] testMulti()" << '\n';
zeshuai007037753e2020-11-30 11:16:10 +0800292
293 hello.string_thing = "Hello2";
294 hello.byte_thing = arg0;
295 hello.i32_thing = arg1;
296 hello.i64_thing = (int64_t)arg2;
297 }
298
299 void testException(const std::string &arg)
300 throw(Xception, apache::thrift::TException) override
301 {
CJCombrink4a280d52024-03-14 19:57:41 +0100302 cout << "[C -> C++] testException(" << arg << ")" << '\n';
zeshuai007037753e2020-11-30 11:16:10 +0800303 if (arg.compare("Xception") == 0) {
304 Xception e;
305 e.errorCode = 1001;
306 e.message = arg;
307 throw e;
308 } else if (arg.compare("ApplicationException") == 0) {
309 apache::thrift::TException e;
310 throw e;
311 } else {
312 Xtruct result;
313 result.string_thing = arg;
314 return;
315 }
316 }
317
318 void testMultiException(Xtruct &result, const std::string &arg0, const std::string &arg1) throw(Xception, Xception2) override {
319
CJCombrink4a280d52024-03-14 19:57:41 +0100320 cout << "[C -> C++] testMultiException(" << arg0 << ", " << arg1 << ")" << '\n';
zeshuai007037753e2020-11-30 11:16:10 +0800321
322 if (arg0.compare("Xception") == 0) {
323 Xception e;
324 e.errorCode = 1001;
325 e.message = "This is an Xception";
326 throw e;
327 } else if (arg0.compare("Xception2") == 0) {
328 Xception2 e;
329 e.errorCode = 2002;
330 e.struct_thing.string_thing = "This is an Xception2";
331 throw e;
332 } else {
333 result.string_thing = arg1;
334 return;
335 }
336 }
337
338 void testOneway(int sleepFor) override {
CJCombrink4a280d52024-03-14 19:57:41 +0100339 cout << "testOneway(" << sleepFor << "): Sleeping..." << '\n';
zeshuai007037753e2020-11-30 11:16:10 +0800340 sleep(sleepFor);
CJCombrink4a280d52024-03-14 19:57:41 +0100341 cout << "testOneway(" << sleepFor << "): done sleeping!" << '\n';
zeshuai007037753e2020-11-30 11:16:10 +0800342 }
343};
344
345// C CLIENT
346extern "C" {
347
348#undef THRIFT_SOCKET /* from lib/cpp */
349
350#include "t_test_thrift_test.h"
351#include "t_test_thrift_test_types.h"
352#include <thrift/c_glib/transport/thrift_socket.h>
353#include <thrift/c_glib/transport/thrift_zlib_transport.h>
354#include <thrift/c_glib/protocol/thrift_protocol.h>
355#include <thrift/c_glib/protocol/thrift_binary_protocol.h>
356
357static void
358test_thrift_client (void)
359{
360 ThriftSocket *tsocket = nullptr;
361 ThriftBinaryProtocol *protocol = nullptr;
362 ThriftZlibTransport *transport = nullptr;
363 TTestThriftTestClient *client = nullptr;
364 TTestThriftTestIf *iface = nullptr;
365 GError *error = nullptr;
366 gchar *string = nullptr;
367 gint8 byte = 0;
368 gint16 i16 = 0;
369 gint32 i32 = 0, another_i32 = 56789;
370 gint64 i64 = 0;
371 double dbl = 0.0;
372 TTestXtruct *xtruct_in, *xtruct_out;
373 TTestXtruct2 *xtruct2_in, *xtruct2_out;
374 GHashTable *map_in = nullptr, *map_out = nullptr;
375 GHashTable *set_in = nullptr, *set_out = nullptr;
376 GArray *list_in = nullptr, *list_out = nullptr;
377 TTestNumberz enum_in, enum_out;
378 TTestUserId user_id_in, user_id_out;
379 GHashTable *insanity_in = nullptr;
380 TTestXtruct *xtruct1, *xtruct2;
381 TTestInsanity *insanity_out = nullptr;
382 TTestXtruct *multi_in = nullptr;
383 GHashTable *multi_map_out = nullptr;
384 TTestXception *xception = nullptr;
385 TTestXception2 *xception2 = nullptr;
386
387#if (!GLIB_CHECK_VERSION (2, 36, 0))
388 // initialize gobject
389 g_type_init ();
390#endif
391
392 // create a C client
393 tsocket = (ThriftSocket *) g_object_new (THRIFT_TYPE_SOCKET,
394 "hostname", "localhost",
395 "port", TEST_PORT, nullptr);
396 transport = (ThriftZlibTransport *) g_object_new (THRIFT_TYPE_ZLIB_TRANSPORT,
397 "transport", tsocket, nullptr);
398 protocol = (ThriftBinaryProtocol *) g_object_new (THRIFT_TYPE_BINARY_PROTOCOL,
399 "transport",
400 transport, nullptr);
401 client = (TTestThriftTestClient *) g_object_new (T_TEST_TYPE_THRIFT_TEST_CLIENT, "input_protocol", protocol, "output_protocol", protocol, nullptr);
402 iface = T_TEST_THRIFT_TEST_IF (client);
403
404 // open and send
405 thrift_transport_open (THRIFT_TRANSPORT(transport), nullptr);
406
407 assert (t_test_thrift_test_client_test_void (iface, &error) == TRUE);
408 assert (error == nullptr);
409
410 assert (t_test_thrift_test_client_test_string (iface, &string, "test123", &error) == TRUE);
411 assert (strcmp (string, "test123") == 0);
412 g_free (string);
413 assert (error == nullptr);
414
415 assert (t_test_thrift_test_client_test_byte (iface, &byte, (gint8) 5, &error) == TRUE);
416 assert (byte == 5);
417 assert (error == nullptr);
418
419 assert (t_test_thrift_test_client_test_i32 (iface, &i32, 123, &error) == TRUE);
420 assert (i32 == 123);
421 assert (error == nullptr);
422
423 assert (t_test_thrift_test_client_test_i64 (iface, &i64, 12345, &error) == TRUE);
424 assert (i64 == 12345);
425 assert (error == nullptr);
426
427 assert (t_test_thrift_test_client_test_double (iface, &dbl, 5.6, &error) == TRUE);
428 assert (dbl == 5.6);
429 assert (error == nullptr);
430
431 xtruct_out = (TTestXtruct *) g_object_new (T_TEST_TYPE_XTRUCT, nullptr);
432 xtruct_out->byte_thing = 1;
433 xtruct_out->__isset_byte_thing = TRUE;
434 xtruct_out->i32_thing = 15;
435 xtruct_out->__isset_i32_thing = TRUE;
436 xtruct_out->i64_thing = 151;
437 xtruct_out->__isset_i64_thing = TRUE;
438 xtruct_out->string_thing = g_strdup ("abc123");
439 xtruct_out->__isset_string_thing = TRUE;
440 xtruct_in = (TTestXtruct *) g_object_new(T_TEST_TYPE_XTRUCT, nullptr);
441 assert (t_test_thrift_test_client_test_struct (iface, &xtruct_in, xtruct_out, &error) == TRUE);
442 assert (error == nullptr);
443
444 xtruct2_out = (TTestXtruct2 *) g_object_new (T_TEST_TYPE_XTRUCT2, nullptr);
445 xtruct2_out->byte_thing = 1;
446 xtruct2_out->__isset_byte_thing = TRUE;
447 if (xtruct2_out->struct_thing != nullptr)
448 g_object_unref(xtruct2_out->struct_thing);
449 xtruct2_out->struct_thing = xtruct_out;
450 xtruct2_out->__isset_struct_thing = TRUE;
451 xtruct2_out->i32_thing = 123;
452 xtruct2_out->__isset_i32_thing = TRUE;
453 xtruct2_in = (TTestXtruct2 *) g_object_new (T_TEST_TYPE_XTRUCT2, nullptr);
454 assert (t_test_thrift_test_client_test_nest (iface, &xtruct2_in, xtruct2_out, &error) == TRUE);
455 assert (error == nullptr);
456
457 g_object_unref (xtruct2_out);
458 g_object_unref (xtruct2_in);
459 g_object_unref (xtruct_in);
460
461 map_out = g_hash_table_new (nullptr, nullptr);
462 map_in = g_hash_table_new (nullptr, nullptr); g_hash_table_insert (map_out, &i32, &i32);
463 assert (t_test_thrift_test_client_test_map (iface, &map_in, map_out, &error) == TRUE);
464 assert (error == nullptr);
465 g_hash_table_destroy (map_out);
466 g_hash_table_destroy (map_in);
467
468 map_out = g_hash_table_new (nullptr, nullptr);
469 map_in = g_hash_table_new (nullptr, nullptr);
470 g_hash_table_insert (map_out, g_strdup ("a"), g_strdup ("123"));
471 g_hash_table_insert (map_out, g_strdup ("a b"), g_strdup ("with spaces "));
472 g_hash_table_insert (map_out, g_strdup ("same"), g_strdup ("same"));
473 g_hash_table_insert (map_out, g_strdup ("0"), g_strdup ("numeric key"));
474 assert (t_test_thrift_test_client_test_string_map (iface, &map_in, map_out, &error) == TRUE);
475 assert (error == nullptr);
476 g_hash_table_destroy (map_out);
477 g_hash_table_destroy (map_in);
478
479 set_out = g_hash_table_new (nullptr, nullptr);
480 set_in = g_hash_table_new (nullptr, nullptr);
481 g_hash_table_insert (set_out, &i32, &i32);
482 assert (t_test_thrift_test_client_test_set (iface, &set_in, set_out, &error) == TRUE);
483 assert (error == nullptr);
484 g_hash_table_destroy (set_out);
485 g_hash_table_destroy (set_in);
486
487 list_out = g_array_new(TRUE, TRUE, sizeof(gint32));
488 list_in = g_array_new(TRUE, TRUE, sizeof(gint32));
489 another_i32 = 456;
490 g_array_append_val (list_out, i32);
491 g_array_append_val (list_out, another_i32);
492 assert (t_test_thrift_test_client_test_list (iface, &list_in, list_out, &error) == TRUE);
493 assert (error == nullptr);
494 g_array_free (list_out, TRUE);
495 g_array_free (list_in, TRUE);
496
497 enum_out = T_TEST_NUMBERZ_ONE;
498 assert (t_test_thrift_test_client_test_enum (iface, &enum_in, enum_out, &error) == TRUE);
499 assert (enum_in == enum_out);
500 assert (error == nullptr);
501
502 user_id_out = 12345;
503 assert (t_test_thrift_test_client_test_typedef (iface, &user_id_in, user_id_out, &error) == TRUE);
504 assert (user_id_in == user_id_out);
505 assert (error == nullptr);
506
507 map_in = g_hash_table_new (nullptr, nullptr);
508 assert (t_test_thrift_test_client_test_map_map (iface, &map_in, i32, &error) == TRUE);
509 assert (error == nullptr);
510 g_hash_table_destroy (map_in);
511
512 // insanity
513 insanity_out = (TTestInsanity *) g_object_new (T_TEST_TYPE_INSANITY, nullptr);
514 insanity_out->userMap = g_hash_table_new (nullptr, nullptr);
515 g_hash_table_insert (insanity_out->userMap, GINT_TO_POINTER (enum_out), &user_id_out);
516
517 xtruct1 = (TTestXtruct *) g_object_new (T_TEST_TYPE_XTRUCT, nullptr);
518 xtruct1->byte_thing = 1;
519 xtruct1->__isset_byte_thing = TRUE;
520 xtruct1->i32_thing = 15;
521 xtruct1->__isset_i32_thing = TRUE;
522 xtruct1->i64_thing = 151;
523 xtruct1->__isset_i64_thing = TRUE;
524 xtruct1->string_thing = g_strdup ("abc123");
525 xtruct1->__isset_string_thing = TRUE;
526 xtruct2 = (TTestXtruct *) g_object_new (T_TEST_TYPE_XTRUCT, nullptr);
527 xtruct2->byte_thing = 1;
528 xtruct2->__isset_byte_thing = TRUE;
529 xtruct2->i32_thing = 15;
530 xtruct2->__isset_i32_thing = TRUE;
531 xtruct2->i64_thing = 151;
532 xtruct2->__isset_i64_thing = TRUE;
533 xtruct2->string_thing = g_strdup ("abc123");
534 xtruct2->__isset_string_thing = TRUE;
535
536 insanity_in = g_hash_table_new (nullptr, nullptr);
537 g_ptr_array_add (insanity_out->xtructs, xtruct1);
538 g_ptr_array_add (insanity_out->xtructs, xtruct2);
539 assert (t_test_thrift_test_client_test_insanity (iface, &insanity_in, insanity_out, &error) == TRUE);
540
541 g_hash_table_unref (insanity_in);
542 g_ptr_array_free (insanity_out->xtructs, TRUE);
543
544 multi_map_out = g_hash_table_new (nullptr, nullptr);
545 string = g_strdup ("abc123");
546 g_hash_table_insert (multi_map_out, &i16, string);
547 multi_in = (TTestXtruct *) g_object_new (T_TEST_TYPE_XTRUCT, nullptr);
548 assert (t_test_thrift_test_client_test_multi (iface, &multi_in, byte, i32, i64, multi_map_out, enum_out, user_id_out, &error) == TRUE);
549 assert (multi_in->i32_thing == i32);
550 assert (multi_in->i64_thing == i64);
551 g_object_unref (multi_in);
552 g_hash_table_unref (multi_map_out);
553 g_free (string);
554
555 assert (t_test_thrift_test_client_test_exception (iface, "Xception", &xception, &error) == FALSE);
556 assert (xception->errorCode == 1001);
557 g_error_free (error);
558 error = nullptr;
559 g_object_unref (xception);
560 xception = nullptr;
561
562 assert (t_test_thrift_test_client_test_exception (iface, "ApplicationException", &xception, &error) == FALSE);
563 g_error_free (error);
564 error = nullptr;
565 assert (xception == nullptr);
566
567 assert (t_test_thrift_test_client_test_exception (iface, "Test", &xception, &error) == TRUE);
568 assert (error == nullptr);
569
570 multi_in = (TTestXtruct*) g_object_new (T_TEST_TYPE_XTRUCT, nullptr);
571 assert (t_test_thrift_test_client_test_multi_exception (iface, &multi_in, "Xception", nullptr, &xception, &xception2, &error) == FALSE);
572 assert (xception->errorCode == 1001);
573 assert (xception2 == nullptr);
574 g_error_free (error);
575 error = nullptr;
576 g_object_unref (xception);
577 g_object_unref (multi_in);
578 xception = nullptr;
579 multi_in = nullptr;
580
581 multi_in = (TTestXtruct*) g_object_new (T_TEST_TYPE_XTRUCT, nullptr);
582 assert (t_test_thrift_test_client_test_multi_exception (iface, &multi_in, "Xception2", nullptr, &xception, &xception2, &error) == FALSE);
583 assert (xception2->errorCode == 2002);
584 assert (xception == nullptr);
585 g_error_free (error);
586 error = nullptr;
587 g_object_unref (xception2);
588 g_object_unref (multi_in);
589 xception2 = nullptr;
590 multi_in = nullptr;
591
592 multi_in = (TTestXtruct*) g_object_new (T_TEST_TYPE_XTRUCT, nullptr);
593 assert (t_test_thrift_test_client_test_multi_exception (iface, &multi_in, nullptr , nullptr, &xception, &xception2, &error) == TRUE);
594 assert (error == nullptr);
595 g_object_unref(multi_in);
596 multi_in = nullptr;
597
598 assert (t_test_thrift_test_client_test_oneway (iface, 1, &error) == TRUE);
599 assert (error == nullptr);
600
601 /* sleep to let the oneway call go through */
602 sleep (5);
603
604 thrift_transport_close (THRIFT_TRANSPORT(tsocket), nullptr);
605 g_object_unref (client);
606 g_object_unref (protocol);
607 g_object_unref (tsocket);
608}
609
610
611} /* extern "C" */
612
613
614static void
615bailout (int signum)
616{
617 THRIFT_UNUSED_VARIABLE (signum);
618
619 exit (1);
620}
621
622int
623main (void)
624{
625 int status;
626 int pid = fork ();
627 assert (pid >= 0);
628
629 if (pid == 0) /* child */
630 {
631 std::shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());
632 std::shared_ptr<TestHandler> testHandler(new TestHandler());
633 std::shared_ptr<ThriftTestProcessor> testProcessor(new ThriftTestProcessor(testHandler));
634 std::shared_ptr<TServerSocket> serverSocket(new TServerSocket(TEST_PORT));
635 std::shared_ptr<TZlibTransportFactory> transportFactory(new TZlibTransportFactory());
636 //std::shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
637 TSimpleServer simpleServer(testProcessor, serverSocket, transportFactory, protocolFactory);
638 signal (SIGALRM, bailout);
639 alarm (60);
640 simpleServer.serve();
641 } else {
642 sleep (1);
643 test_thrift_client ();
644 kill (pid, SIGINT);
645 assert (wait (&status) == pid);
646 }
647
648 return 0;
649}
650