blob: 3ae9325d4da02e8cf51f6b4d06646a501db2cf0a [file] [log] [blame]
Roger Meierb3c84092014-09-01 21:53:40 +02001/*
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#include <glib-object.h>
21#include <inttypes.h>
22#include <signal.h>
23#include <stdio.h>
24#include <string.h>
25
26#include <sys/time.h>
27
28#include <thrift/c_glib/thrift.h>
29#include <thrift/c_glib/protocol/thrift_binary_protocol.h>
Chandler May6dde90b2016-01-10 06:01:10 +000030#include <thrift/c_glib/protocol/thrift_compact_protocol.h>
Roger Meierb3c84092014-09-01 21:53:40 +020031#include <thrift/c_glib/transport/thrift_buffered_transport.h>
32#include <thrift/c_glib/transport/thrift_framed_transport.h>
33#include <thrift/c_glib/transport/thrift_socket.h>
34#include <thrift/c_glib/transport/thrift_transport.h>
35
36#include "../gen-c_glib/t_test_thrift_test.h"
37
38/* Handle SIGPIPE signals (indicating the server has closed the
39 connection prematurely) by outputting an error message before
40 exiting. */
41static void
Roger Meier15df0762014-09-29 20:50:56 +020042sigpipe_handler (int signal_number)
43{
Roger Meierb3c84092014-09-01 21:53:40 +020044 THRIFT_UNUSED_VAR (signal_number);
45
46 /* Flush standard output to make sure the test results so far are
47 logged */
48 fflush (stdout);
49
50 fputs ("Broken pipe (server closed connection prematurely)\n", stderr);
51 fflush (stderr);
52
53 /* Re-raise the signal, this time invoking the default signal
54 handler, to terminate the program */
55 raise (SIGPIPE);
56}
57
58/* Compare two gint32 values. Used for sorting and finding integer
59 values within a GList. */
60static gint
Roger Meier15df0762014-09-29 20:50:56 +020061gint32_compare (gconstpointer a, gconstpointer b)
62{
Roger Meierb3c84092014-09-01 21:53:40 +020063 gint32 int32_a = *(gint32 *)a;
64 gint32 int32_b = *(gint32 *)b;
65 int result = 0;
66
67 if (int32_a < int32_b)
68 result = -1;
69 else if (int32_a > int32_b)
70 result = 1;
71
72 return result;
73}
74
75int
Roger Meier15df0762014-09-29 20:50:56 +020076main (int argc, char **argv)
77{
Roger Meierb3c84092014-09-01 21:53:40 +020078 static gchar *host = NULL;
79 static gint port = 9090;
80 static gchar *transport_option = NULL;
81 static gchar *protocol_option = NULL;
82 static gint num_tests = 1;
83
84 static
85 GOptionEntry option_entries[] ={
86 { "host", 0, 0, G_OPTION_ARG_STRING, &host,
87 "Host to connect (=localhost)", NULL },
88 { "port", 0, 0, G_OPTION_ARG_INT, &port,
89 "Port number to connect (=9090)", NULL },
90 { "transport", 0, 0, G_OPTION_ARG_STRING, &transport_option,
91 "Transport: buffered, framed (=buffered)", NULL },
92 { "protocol", 0, 0, G_OPTION_ARG_STRING, &protocol_option,
Chandler May6dde90b2016-01-10 06:01:10 +000093 "Protocol: binary, compact (=binary)", NULL },
Roger Meierb3c84092014-09-01 21:53:40 +020094 { "testloops", 'n', 0, G_OPTION_ARG_INT, &num_tests,
95 "Number of tests (=1)", NULL },
96 { NULL }
97 };
98
99 struct sigaction sigpipe_action;
100
101 GType transport_type = THRIFT_TYPE_BUFFERED_TRANSPORT;
102 gchar *transport_name = "buffered";
103 GType protocol_type = THRIFT_TYPE_BINARY_PROTOCOL;
104 gchar *protocol_name = "binary";
105
106 ThriftSocket *socket;
107 ThriftTransport *transport;
108 ThriftProtocol *protocol;
109
110 TTestThriftTestIf *test_client;
111
112 struct timeval time_start, time_stop, time_elapsed;
113 guint64 time_elapsed_usec, time_total_usec = 0;
114 guint64 time_min_usec = G_MAXUINT64, time_max_usec = 0, time_avg_usec;
115
116 GOptionContext *option_context;
117 gboolean options_valid = TRUE;
118 int test_num = 0;
119 int fail_count = 0;
120 GError *error = NULL;
121
Roger Meier15df0762014-09-29 20:50:56 +0200122#if (!GLIB_CHECK_VERSION (2, 36, 0))
123 g_type_init ();
124#endif
125
Roger Meierb3c84092014-09-01 21:53:40 +0200126 /* Configure and parse our command-line options */
127 option_context = g_option_context_new (NULL);
128 g_option_context_add_main_entries (option_context,
129 option_entries,
130 NULL);
Roger Meier15df0762014-09-29 20:50:56 +0200131 if (!g_option_context_parse (option_context,
132 &argc,
133 &argv,
134 &error)) {
Roger Meierb3c84092014-09-01 21:53:40 +0200135 fprintf (stderr, "%s\n", error->message);
136 return 255;
137 }
138 g_option_context_free (option_context);
139
140 /* Set remaining default values for unspecified options */
141 if (host == NULL)
142 host = g_strdup ("localhost");
143
144 /* Validate the parsed options */
Chandler May6dde90b2016-01-10 06:01:10 +0000145 if (protocol_option != NULL) {
146 if (strncmp (protocol_option, "compact", 8) == 0) {
147 protocol_type = THRIFT_TYPE_COMPACT_PROTOCOL;
148 protocol_name = "compact";
149 }
150 else if (strncmp (protocol_option, "binary", 7) != 0) {
151 fprintf (stderr, "Unknown protocol type %s\n", protocol_option);
152 options_valid = FALSE;
153 }
Roger Meierb3c84092014-09-01 21:53:40 +0200154 }
155
156 if (transport_option != NULL) {
157 if (strncmp (transport_option, "framed", 7) == 0) {
158 transport_type = THRIFT_TYPE_FRAMED_TRANSPORT;
159 transport_name = "framed";
160 }
161 else if (strncmp (transport_option, "buffered", 9) != 0) {
162 fprintf (stderr, "Unknown transport type %s\n", transport_option);
163 options_valid = FALSE;
164 }
165 }
166
Roger Meier15df0762014-09-29 20:50:56 +0200167 if (!options_valid)
Roger Meierb3c84092014-09-01 21:53:40 +0200168 return 254;
169
170 printf ("Connecting (%s/%s) to: %s:%d\n",
171 transport_name,
172 protocol_name,
173 host,
174 port);
175
176 /* Install our SIGPIPE handler, which outputs an error message to
177 standard error before exiting so testers can know what
178 happened */
179 memset (&sigpipe_action, 0, sizeof (sigpipe_action));
180 sigpipe_action.sa_handler = sigpipe_handler;
181 sigpipe_action.sa_flags = SA_RESETHAND;
182 sigaction (SIGPIPE, &sigpipe_action, NULL);
183
184 /* Establish all our connection objects */
185 socket = g_object_new (THRIFT_TYPE_SOCKET,
186 "hostname", host,
187 "port", port,
188 NULL);
189 transport = g_object_new (transport_type,
190 "transport", socket,
191 NULL);
192 protocol = g_object_new (protocol_type,
193 "transport", transport,
194 NULL);
195 test_client = g_object_new (T_TEST_TYPE_THRIFT_TEST_CLIENT,
196 "input_protocol", protocol,
197 "output_protocol", protocol,
198 NULL);
199
200 /* Execute the actual tests */
201 for (test_num = 0; test_num < num_tests; ++test_num) {
Roger Meier15df0762014-09-29 20:50:56 +0200202 if (thrift_transport_open (transport, &error)) {
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900203 gchar *string = NULL;
204 gboolean boolean = 0;
205 gint8 byte = 0;
206 gint32 int32 = 0;
207 gint64 int64 = 0;
208 gdouble dub = 0;
Roger Meierb3c84092014-09-01 21:53:40 +0200209
210 gint byte_thing, i32_thing, inner_byte_thing, inner_i32_thing;
211 gint64 i64_thing, inner_i64_thing;
212
Nobuaki Sukegawa01ede042015-09-29 02:16:53 +0900213 TTestXtruct *xtruct_out, *xtruct_out2, *xtruct_in, *inner_xtruct_in;
Roger Meierb3c84092014-09-01 21:53:40 +0200214 TTestXtruct2 *xtruct2_out, *xtruct2_in;
215
216 GHashTable *map_out, *map_in, *inner_map_in;
217 GHashTable *set_out, *set_in;
218 gpointer key, value;
219 gint32 *i32_key_ptr, *i32_value_ptr;
220 GHashTableIter hash_table_iter, inner_hash_table_iter;
221 GList *keys_out, *keys_in, *keys_elem;
222
223 GArray *list_out, *list_in;
224
225 TTestNumberz numberz;
Nobuaki Sukegawa01ede042015-09-29 02:16:53 +0900226 TTestNumberz numberz2;
Roger Meierb3c84092014-09-01 21:53:40 +0200227
Nobuaki Sukegawa01ede042015-09-29 02:16:53 +0900228 TTestUserId user_id, *user_id_ptr, *user_id_ptr2;
Roger Meierb3c84092014-09-01 21:53:40 +0200229
230 TTestInsanity *insanity_out, *insanity_in;
231 GHashTable *user_map;
232 GHashTableIter user_map_iter;
233 GPtrArray *xtructs;
234
235 TTestXception *xception = NULL;
236 TTestXception2 *xception2 = NULL;
237
238 gboolean oneway_result;
239 struct timeval oneway_start, oneway_end, oneway_elapsed;
240 gint oneway_elapsed_usec;
241
242 gboolean first;
243 gint32 i, j;
244
245 printf ("Test #%d, connect %s:%d\n", test_num + 1, host, port);
246 gettimeofday (&time_start, NULL);
247
248 /* These test routines have been ported from the C++ test
249 client, care being taken to ensure their output remains as
250 close as possible to the original to facilitate diffs.
251
252 For simplicity comments have been omitted, but every routine
253 has the same basic structure:
254
255 - Create and populate data structures as necessary.
256
257 - Format and output (to the console) a representation of the
258 outgoing data.
259
260 - Issue the remote method call to the server.
261
262 - Format and output a representation of the returned data.
263
264 - Verify the returned data matches what was expected.
265
266 - Deallocate any created data structures.
267
268 Note the recognized values and expected behaviour of each
269 remote method are described in ThriftTest.thrift, which
270 you'll find in the top-level "test" folder. */
271
272 /**
273 * VOID TEST
274 */
275 printf ("testVoid()");
Roger Meier15df0762014-09-29 20:50:56 +0200276 if (t_test_thrift_test_if_test_void (test_client, &error)) {
Roger Meierb3c84092014-09-01 21:53:40 +0200277 printf (" = void\n");
278 }
279 else {
280 printf ("%s\n", error->message);
281 g_error_free (error);
282 error = NULL;
283
284 fail_count++;
285 }
286
287 /**
288 * STRING TEST
289 */
290 printf ("testString(\"Test\")");
291 if (t_test_thrift_test_if_test_string (test_client,
292 &string,
293 "Test",
Roger Meier15df0762014-09-29 20:50:56 +0200294 &error)) {
Roger Meierb3c84092014-09-01 21:53:40 +0200295 printf (" = \"%s\"\n", string);
296 if (strncmp (string, "Test", 5) != 0)
297 fail_count++;
298
299 g_free (string);
300 string = NULL;
301 }
302 else {
303 printf ("%s\n", error->message);
304 g_error_free (error);
305 error = NULL;
306
307 fail_count++;
308 }
309
310 /**
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900311 * BOOL TEST
312 */
313 printf ("testByte(true)");
314 if (t_test_thrift_test_if_test_bool (test_client,
315 &boolean,
316 1,
317 &error)) {
318 printf (" = %s\n", boolean ? "true" : "false");
319 if (boolean != 1)
320 fail_count++;
321 }
322 else {
323 printf ("%s\n", error->message);
324 g_error_free (error);
325 error = NULL;
326
327 fail_count++;
328 }
329 printf ("testByte(false)");
330 if (t_test_thrift_test_if_test_bool (test_client,
331 &boolean,
332 0,
333 &error)) {
334 printf (" = %s\n", boolean ? "true" : "false");
335 if (boolean != 0)
336 fail_count++;
337 }
338 else {
339 printf ("%s\n", error->message);
340 g_error_free (error);
341 error = NULL;
342
343 fail_count++;
344 }
345
346 /**
Roger Meierb3c84092014-09-01 21:53:40 +0200347 * BYTE TEST
348 */
349 printf ("testByte(1)");
350 if (t_test_thrift_test_if_test_byte (test_client,
351 &byte,
352 1,
Roger Meier15df0762014-09-29 20:50:56 +0200353 &error)) {
Roger Meierb3c84092014-09-01 21:53:40 +0200354 printf (" = %d\n", byte);
355 if (byte != 1)
356 fail_count++;
357 }
358 else {
359 printf ("%s\n", error->message);
360 g_error_free (error);
361 error = NULL;
362
363 fail_count++;
364 }
Nobuaki Sukegawa30f465d2015-10-10 10:45:42 +0900365 printf ("testByte(-1)");
366 if (t_test_thrift_test_if_test_byte (test_client,
367 &byte,
368 -1,
369 &error)) {
370 printf (" = %d\n", byte);
371 if (byte != -1)
372 fail_count++;
373 }
374 else {
375 printf ("%s\n", error->message);
376 g_error_free (error);
377 error = NULL;
378
379 fail_count++;
380 }
Roger Meierb3c84092014-09-01 21:53:40 +0200381
382 /**
383 * I32 TEST
384 */
385 printf ("testI32(-1)");
386 if (t_test_thrift_test_if_test_i32 (test_client,
387 &int32,
388 -1,
Roger Meier15df0762014-09-29 20:50:56 +0200389 &error)) {
Roger Meierb3c84092014-09-01 21:53:40 +0200390 printf (" = %d\n", int32);
391 if (int32 != -1)
392 fail_count++;
393 }
394 else {
395 printf ("%s\n", error->message);
396 g_error_free (error);
397 error = NULL;
398
399 fail_count++;
400 }
401
402 /**
403 * I64 TEST
404 */
405 printf ("testI64(-34359738368)");
406 if (t_test_thrift_test_if_test_i64 (test_client,
407 &int64,
408 (gint64)-34359738368,
Roger Meier15df0762014-09-29 20:50:56 +0200409 &error)) {
Roger Meierb3c84092014-09-01 21:53:40 +0200410 printf (" = %" PRId64 "\n", int64);
411 if (int64 != (gint64)-34359738368)
412 fail_count++;
413 }
414 else {
415 printf ("%s\n", error->message);
416 g_error_free (error);
417 error = NULL;
418
419 fail_count++;
420 }
421
422 /**
423 * DOUBLE TEST
424 */
425 printf("testDouble(-5.2098523)");
426 if (t_test_thrift_test_if_test_double (test_client,
427 &dub,
428 -5.2098523,
Roger Meier15df0762014-09-29 20:50:56 +0200429 &error)) {
Roger Meierb3c84092014-09-01 21:53:40 +0200430 printf (" = %f\n", dub);
431 if ((dub - (-5.2098523)) > 0.001)
432 fail_count++;
433 }
434 else {
435 printf ("%s\n", error->message);
436 g_error_free (error);
437 error = NULL;
438
439 fail_count++;
440 }
441
Jens Geyer8bcfdd92014-12-14 03:14:26 +0100442 // TODO: add testBinary()
443
Roger Meierb3c84092014-09-01 21:53:40 +0200444 /**
445 * STRUCT TEST
446 */
447 printf ("testStruct({\"Zero\", 1, -3, -5})");
448 xtruct_out = g_object_new (T_TEST_TYPE_XTRUCT,
449 "string_thing", "Zero",
450 "byte_thing", 1,
451 "i32_thing", -3,
452 "i64_thing", -5LL,
453 NULL);
454 xtruct_in = g_object_new (T_TEST_TYPE_XTRUCT, NULL);
455
456 if (t_test_thrift_test_if_test_struct (test_client,
457 &xtruct_in,
458 xtruct_out,
Roger Meier15df0762014-09-29 20:50:56 +0200459 &error)) {
Roger Meierb3c84092014-09-01 21:53:40 +0200460 g_object_get (xtruct_in,
461 "string_thing", &string,
462 "byte_thing", &byte_thing,
463 "i32_thing", &i32_thing,
464 "i64_thing", &i64_thing,
465 NULL);
466
467 printf (" = {\"%s\", %d, %d, %" PRId64 "}\n",
468 string,
469 byte_thing,
470 i32_thing,
471 i64_thing);
472 if ((string == NULL || strncmp (string, "Zero", 5) != 0) ||
473 byte_thing != 1 ||
474 i32_thing != -3 ||
475 i64_thing != (gint64)-5)
476 fail_count++;
477 }
478 else {
479 printf ("%s\n", error->message);
480 g_error_free (error);
481 error = NULL;
482
483 fail_count++;
484 }
485 g_object_unref (xtruct_in);
486
487 /**
488 * NESTED STRUCT TEST
489 */
490 printf ("testNest({1, {\"Zero\", 1, -3, -5}), 5}");
491 xtruct2_out = g_object_new (T_TEST_TYPE_XTRUCT2,
492 "byte_thing", 1,
493 "struct_thing", xtruct_out,
494 "i32_thing", 5,
495 NULL);
496 xtruct2_in = g_object_new (T_TEST_TYPE_XTRUCT2, NULL);
497
498 if (t_test_thrift_test_if_test_nest (test_client,
499 &xtruct2_in,
500 xtruct2_out,
Roger Meier15df0762014-09-29 20:50:56 +0200501 &error)) {
Roger Meierb3c84092014-09-01 21:53:40 +0200502 g_object_get (xtruct2_in,
503 "byte_thing", &byte_thing,
504 "struct_thing", &xtruct_in,
505 "i32_thing", &i32_thing,
506 NULL);
507 g_object_get (xtruct_in,
508 "string_thing", &string,
509 "byte_thing", &inner_byte_thing,
510 "i32_thing", &inner_i32_thing,
511 "i64_thing", &inner_i64_thing,
512 NULL);
513
514 printf (" = {%d, {\"%s\", %d, %d, %" PRId64 "}, %d}\n",
515 byte_thing,
516 string,
517 inner_byte_thing,
518 inner_i32_thing,
519 inner_i64_thing,
520 i32_thing);
521 if (byte_thing != 1 ||
522 (string == NULL || strncmp (string, "Zero", 5) != 0) ||
523 inner_byte_thing != 1 ||
524 inner_i32_thing != -3 ||
525 inner_i64_thing != (gint64)-5 ||
526 i32_thing != 5)
527 fail_count++;
528 }
529 else {
530 printf ("%s\n", error->message);
531 g_error_free (error);
532 error = NULL;
533
534 fail_count++;
535 }
536
537 g_object_unref (xtruct_in);
538 g_object_unref (xtruct2_in);
539 g_object_unref (xtruct2_out);
540 g_object_unref (xtruct_out);
541
542 /**
543 * MAP TEST
544 */
545 map_out = g_hash_table_new_full (g_int_hash,
546 g_int_equal,
547 g_free,
548 g_free);
549 for (i = 0; i < 5; ++i) {
550 i32_key_ptr = g_malloc (sizeof *i32_key_ptr);
551 i32_value_ptr = g_malloc (sizeof *i32_value_ptr);
552
553 *i32_key_ptr = i;
554 *i32_value_ptr = i - 10;
555
556 g_hash_table_insert (map_out, i32_key_ptr, i32_value_ptr);
557 }
558 printf ("testMap({");
559 first = TRUE;
560 g_hash_table_iter_init (&hash_table_iter, map_out);
561 while (g_hash_table_iter_next (&hash_table_iter,
562 &key,
Roger Meier15df0762014-09-29 20:50:56 +0200563 &value)) {
564 if (first)
Roger Meierb3c84092014-09-01 21:53:40 +0200565 first = FALSE;
566 else
567 printf (", ");
568
569 printf ("%d => %d", *(gint32 *)key, *(gint32 *)value);
570 }
571 printf ("})");
572
573 map_in = g_hash_table_new_full (g_int_hash,
574 g_int_equal,
575 g_free,
576 g_free);
577
578 if (t_test_thrift_test_if_test_map (test_client,
579 &map_in,
580 map_out,
Roger Meier15df0762014-09-29 20:50:56 +0200581 &error)) {
Roger Meierb3c84092014-09-01 21:53:40 +0200582 printf (" = {");
583 first = TRUE;
584 g_hash_table_iter_init (&hash_table_iter, map_in);
585 while (g_hash_table_iter_next (&hash_table_iter,
586 &key,
Roger Meier15df0762014-09-29 20:50:56 +0200587 &value)) {
588 if (first)
Roger Meierb3c84092014-09-01 21:53:40 +0200589 first = FALSE;
590 else
591 printf (", ");
592
593 printf ("%d => %d", *(gint32 *)key, *(gint32 *)value);
594 }
595 printf ("}\n");
596
597 if (g_hash_table_size (map_in) != g_hash_table_size (map_out))
598 fail_count++;
599 else {
600 g_hash_table_iter_init (&hash_table_iter, map_out);
601 while (g_hash_table_iter_next (&hash_table_iter,
602 &key,
Roger Meier15df0762014-09-29 20:50:56 +0200603 &value)) {
Roger Meierb3c84092014-09-01 21:53:40 +0200604 gpointer in_value = g_hash_table_lookup (map_in, key);
605 if (in_value == NULL ||
606 *(gint32 *)in_value != *(gint32 *)value) {
607 fail_count++;
608 break;
609 }
610 }
611 }
612 }
613 else {
614 printf ("%s\n", error->message);
615 g_error_free (error);
616 error = NULL;
617
618 fail_count++;
619 }
620
621 g_hash_table_unref (map_in);
622 g_hash_table_unref (map_out);
623
624 /**
625 * STRING MAP TEST
626 */
627 map_out = g_hash_table_new_full (g_str_hash,
628 g_str_equal,
629 NULL,
630 NULL);
631 g_hash_table_insert (map_out, "a", "2");
632 g_hash_table_insert (map_out, "b", "blah");
633 g_hash_table_insert (map_out, "some", "thing");
634 printf ("testStringMap({");
635 first = TRUE;
636 g_hash_table_iter_init (&hash_table_iter, map_out);
637 while (g_hash_table_iter_next (&hash_table_iter,
638 &key,
Roger Meier15df0762014-09-29 20:50:56 +0200639 &value)) {
640 if (first)
Roger Meierb3c84092014-09-01 21:53:40 +0200641 first = FALSE;
642 else
643 printf (", ");
644
645 printf ("\"%s\" => \"%s\"", (gchar *)key, (gchar *)value);
646 }
647 printf (")}");
648
649 map_in = g_hash_table_new_full (g_str_hash,
650 g_str_equal,
651 g_free,
652 g_free);
653
654 if (t_test_thrift_test_if_test_string_map (test_client,
655 &map_in,
656 map_out,
Roger Meier15df0762014-09-29 20:50:56 +0200657 &error)) {
Roger Meierb3c84092014-09-01 21:53:40 +0200658 printf (" = {");
659 first = TRUE;
660 g_hash_table_iter_init (&hash_table_iter, map_in);
661 while (g_hash_table_iter_next (&hash_table_iter,
662 &key,
Roger Meier15df0762014-09-29 20:50:56 +0200663 &value)) {
664 if (first)
Roger Meierb3c84092014-09-01 21:53:40 +0200665 first = FALSE;
666 else
667 printf (", ");
668
669 printf ("\"%s\" => \"%s\"", (gchar *)key, (gchar *)value);
670 }
671 printf ("}\n");
672
673 if (g_hash_table_size (map_in) != g_hash_table_size (map_out))
674 fail_count++;
675 else {
676 g_hash_table_iter_init (&hash_table_iter, map_out);
677 while (g_hash_table_iter_next (&hash_table_iter,
678 &key,
Roger Meier15df0762014-09-29 20:50:56 +0200679 &value)) {
Roger Meierb3c84092014-09-01 21:53:40 +0200680 gpointer in_value = g_hash_table_lookup (map_in, key);
681 if (in_value == NULL ||
682 strcmp ((gchar *)in_value, (gchar *)value) != 0) {
683 fail_count++;
684 break;
685 }
686 }
687 }
688 }
689 else {
690 printf ("%s\n", error->message);
691 g_error_free (error);
692 error = NULL;
693
694 fail_count++;
695 }
696
697 g_hash_table_unref (map_in);
698 g_hash_table_unref (map_out);
699
700 /**
701 * SET TEST
702 */
703 set_out = g_hash_table_new_full (g_int_hash, g_int_equal, g_free, NULL);
704 for (i = -2; i < 3; ++i) {
705 i32_key_ptr = g_malloc (sizeof *i32_key_ptr);
706 *i32_key_ptr = i;
707
708 g_hash_table_insert (set_out, i32_key_ptr, NULL);
709 }
710 printf ("testSet({");
711 first = TRUE;
712 keys_out = g_hash_table_get_keys (set_out);
713 keys_elem = keys_out;
714 while (keys_elem != NULL) {
Roger Meier15df0762014-09-29 20:50:56 +0200715 if (first)
Roger Meierb3c84092014-09-01 21:53:40 +0200716 first = FALSE;
717 else
718 printf (", ");
719
720 printf ("%d", *(gint32 *)keys_elem->data);
721
722 keys_elem = keys_elem->next;
723 }
724 printf ("})");
725
726 set_in = g_hash_table_new_full (g_int_hash, g_int_equal, g_free, NULL);
727
728 if (t_test_thrift_test_if_test_set (test_client,
729 &set_in,
730 set_out,
Roger Meier15df0762014-09-29 20:50:56 +0200731 &error)) {
Roger Meierb3c84092014-09-01 21:53:40 +0200732 printf(" = {");
733 first = TRUE;
734 keys_in = g_hash_table_get_keys (set_in);
735 keys_elem = keys_in;
736 while (keys_elem != NULL) {
Roger Meier15df0762014-09-29 20:50:56 +0200737 if (first)
Roger Meierb3c84092014-09-01 21:53:40 +0200738 first = FALSE;
739 else
740 printf (", ");
741
742 printf ("%d", *(gint32 *)keys_elem->data);
743
744 keys_elem = keys_elem->next;
745 }
746 printf ("}\n");
747
748 if (g_list_length (keys_in) != g_list_length (keys_out))
749 fail_count++;
750 else {
751 keys_elem = keys_out;
752 while (keys_elem != NULL) {
753 if (g_list_find_custom (keys_in,
754 keys_elem->data,
755 gint32_compare) == NULL) {
756 fail_count++;
757 break;
758 }
759
760 keys_elem = keys_elem->next;
761 }
762 }
763
764 g_list_free (keys_in);
765 }
766 else {
767 printf ("%s\n", error->message);
768 g_error_free (error);
769 error = NULL;
770
771 fail_count++;
772 }
773
774 g_hash_table_unref (set_in);
775 g_list_free (keys_out);
776 g_hash_table_unref (set_out);
777
778 /**
779 * LIST TEST
780 */
781 list_out = g_array_new (FALSE, TRUE, sizeof (gint32));
782 for (i = -2; i < 3; ++i) {
783 g_array_append_val (list_out, i);
784 }
785 printf ("testList({");
786 first = TRUE;
787 for (i = 0; i < (gint32)list_out->len; ++i) {
Roger Meier15df0762014-09-29 20:50:56 +0200788 if (first)
Roger Meierb3c84092014-09-01 21:53:40 +0200789 first = FALSE;
790 else
791 printf (", ");
792
793 printf ("%d", g_array_index (list_out, gint32, i));
794 }
795 printf ("})");
796
797 list_in = g_array_new (FALSE, TRUE, sizeof (gint32));
798
799 if (t_test_thrift_test_if_test_list (test_client,
800 &list_in,
801 list_out,
Roger Meier15df0762014-09-29 20:50:56 +0200802 &error)) {
Roger Meierb3c84092014-09-01 21:53:40 +0200803 printf (" = {");
804 first = TRUE;
805 for (i = 0; i < (gint32)list_in->len; ++i) {
Roger Meier15df0762014-09-29 20:50:56 +0200806 if (first)
Roger Meierb3c84092014-09-01 21:53:40 +0200807 first = FALSE;
808 else
809 printf (", ");
810
811 printf ("%d", g_array_index (list_in, gint32, i));
812 }
813 printf ("}\n");
814
815 if (list_in->len != list_out->len ||
816 memcmp (list_in->data,
817 list_out->data,
818 list_in->len * sizeof (gint32)) != 0)
819 fail_count++;
820 }
821 else {
822 printf ("%s\n", error->message);
823 g_error_free (error);
824 error = NULL;
825
826 fail_count++;
827 }
828
829 g_array_unref (list_in);
830 g_array_unref (list_out);
831
832 /**
833 * ENUM TEST
834 */
835 printf("testEnum(ONE)");
836 if (t_test_thrift_test_if_test_enum (test_client,
837 &numberz,
838 T_TEST_NUMBERZ_ONE,
Roger Meier15df0762014-09-29 20:50:56 +0200839 &error)) {
Roger Meierb3c84092014-09-01 21:53:40 +0200840 printf(" = %d\n", numberz);
841 if (numberz != T_TEST_NUMBERZ_ONE)
842 fail_count++;
843 }
844 else {
845 printf ("%s\n", error->message);
846 g_error_free (error);
847 error = NULL;
848
849 fail_count++;
850 }
851
852 printf("testEnum(TWO)");
853 if (t_test_thrift_test_if_test_enum (test_client,
854 &numberz,
855 T_TEST_NUMBERZ_TWO,
Roger Meier15df0762014-09-29 20:50:56 +0200856 &error)) {
Roger Meierb3c84092014-09-01 21:53:40 +0200857 printf(" = %d\n", numberz);
858 if (numberz != T_TEST_NUMBERZ_TWO)
859 fail_count++;
860 }
861 else {
862 printf ("%s\n", error->message);
863 g_error_free (error);
864 error = NULL;
865
866 fail_count++;
867 }
868
869 printf("testEnum(THREE)");
870 if (t_test_thrift_test_if_test_enum (test_client,
871 &numberz,
872 T_TEST_NUMBERZ_THREE,
Roger Meier15df0762014-09-29 20:50:56 +0200873 &error)) {
Roger Meierb3c84092014-09-01 21:53:40 +0200874 printf(" = %d\n", numberz);
875 if (numberz != T_TEST_NUMBERZ_THREE)
876 fail_count++;
877 }
878 else {
879 printf ("%s\n", error->message);
880 g_error_free (error);
881 error = NULL;
882
883 fail_count++;
884 }
885
886 printf("testEnum(FIVE)");
887 if (t_test_thrift_test_if_test_enum (test_client,
888 &numberz,
889 T_TEST_NUMBERZ_FIVE,
Roger Meier15df0762014-09-29 20:50:56 +0200890 &error)) {
Roger Meierb3c84092014-09-01 21:53:40 +0200891 printf(" = %d\n", numberz);
892 if (numberz != T_TEST_NUMBERZ_FIVE)
893 fail_count++;
894 }
895 else {
896 printf ("%s\n", error->message);
897 g_error_free (error);
898 error = NULL;
899
900 fail_count++;
901 }
902
903 printf("testEnum(EIGHT)");
904 if (t_test_thrift_test_if_test_enum (test_client,
905 &numberz,
906 T_TEST_NUMBERZ_EIGHT,
Roger Meier15df0762014-09-29 20:50:56 +0200907 &error)) {
Roger Meierb3c84092014-09-01 21:53:40 +0200908 printf(" = %d\n", numberz);
909 if (numberz != T_TEST_NUMBERZ_EIGHT)
910 fail_count++;
911 }
912 else {
913 printf ("%s\n", error->message);
914 g_error_free (error);
915 error = NULL;
916
917 fail_count++;
918 }
919
920 /**
921 * TYPEDEF TEST
922 */
923 printf ("testTypedef(309858235082523)");
924 if (t_test_thrift_test_if_test_typedef (test_client,
925 &user_id,
926 309858235082523LL,
Roger Meier15df0762014-09-29 20:50:56 +0200927 &error)) {
Roger Meierb3c84092014-09-01 21:53:40 +0200928 printf(" = %" PRId64 "\n", user_id);
929 if (user_id != 309858235082523LL)
930 fail_count++;
931 }
932 else {
933 printf ("%s\n", error->message);
934 g_error_free (error);
935 error = NULL;
936
937 fail_count++;
938 }
939
940 /**
941 * NESTED MAP TEST
942 */
943 printf ("testMapMap(1)");
944 map_in = g_hash_table_new_full (g_int_hash,
945 g_int_equal,
946 g_free,
947 (GDestroyNotify)g_hash_table_unref);
948 if (t_test_thrift_test_if_test_map_map (test_client,
949 &map_in,
950 1,
Roger Meier15df0762014-09-29 20:50:56 +0200951 &error)) {
Roger Meierb3c84092014-09-01 21:53:40 +0200952 g_hash_table_iter_init (&hash_table_iter, map_in);
953
954 printf (" = {");
955 while (g_hash_table_iter_next (&hash_table_iter,
956 &key,
Roger Meier15df0762014-09-29 20:50:56 +0200957 &value)) {
Roger Meierb3c84092014-09-01 21:53:40 +0200958 printf ("%d => {", *(gint32 *)key);
959
960 g_hash_table_iter_init (&inner_hash_table_iter,
961 (GHashTable *)value);
962 while (g_hash_table_iter_next (&inner_hash_table_iter,
963 &key,
Roger Meier15df0762014-09-29 20:50:56 +0200964 &value)) {
Roger Meierb3c84092014-09-01 21:53:40 +0200965 printf ("%d => %d, ", *(gint32 *)key, *(gint32 *)value);
966 }
967
968 printf ("}, ");
969 }
970 printf ("}\n");
971
972 if (g_hash_table_size (map_in) != 2)
973 fail_count++;
974 else {
975 gint32 inner_keys[] = {1, 2, 3, 4};
976 gint32 i32_key;
977
978 i32_key = -4;
979 inner_map_in = g_hash_table_lookup (map_in, &i32_key);
980 if (inner_map_in == NULL ||
981 g_hash_table_size (inner_map_in) != 4)
982 fail_count++;
983 else {
984 keys_in = g_hash_table_get_keys (inner_map_in);
985 keys_in = g_list_sort (keys_in, gint32_compare);
986
987 for (i = 0; i < 4; i++) {
988 keys_elem = g_list_nth (keys_in, 3 - i);
989
990 if (*(gint32 *)keys_elem->data != (-1 * inner_keys[i]) ||
991 *(gint32 *)g_hash_table_lookup (inner_map_in,
992 keys_elem->data) !=
993 (-1 * inner_keys[i])) {
994 fail_count++;
995 break;
996 }
997 }
998
999 g_list_free (keys_in);
1000 }
1001
1002 i32_key = 4;
1003 inner_map_in = g_hash_table_lookup (map_in, &i32_key);
1004 if (inner_map_in == NULL ||
1005 g_hash_table_size (inner_map_in) != 4)
1006 fail_count++;
1007 else {
1008 keys_in = g_hash_table_get_keys (inner_map_in);
1009 keys_in = g_list_sort (keys_in, gint32_compare);
1010
1011 for (i = 0; i < 4; i++) {
1012 keys_elem = g_list_nth (keys_in, i);
1013
1014 if (*(gint32 *)keys_elem->data != inner_keys[i] ||
1015 *(gint32 *)g_hash_table_lookup (inner_map_in,
1016 keys_elem->data) !=
1017 inner_keys[i]) {
1018 fail_count++;
1019 break;
1020 }
1021 }
1022
1023 g_list_free (keys_in);
1024 }
1025 }
1026 }
1027 else {
1028 printf ("%s\n", error->message);
1029 g_error_free (error);
1030 error = NULL;
1031
1032 fail_count++;
1033 }
1034
1035 g_hash_table_unref (map_in);
1036
1037 /**
1038 * INSANITY TEST
1039 */
1040 insanity_out = g_object_new (T_TEST_TYPE_INSANITY, NULL);
1041 g_object_get (insanity_out,
1042 "userMap", &user_map,
1043 "xtructs", &xtructs,
1044 NULL);
1045
1046 numberz = T_TEST_NUMBERZ_FIVE;
Nobuaki Sukegawa01ede042015-09-29 02:16:53 +09001047 numberz2 = T_TEST_NUMBERZ_EIGHT;
Roger Meierb3c84092014-09-01 21:53:40 +02001048 user_id_ptr = g_malloc (sizeof *user_id_ptr);
Nobuaki Sukegawa01ede042015-09-29 02:16:53 +09001049 *user_id_ptr = 5;
1050 user_id_ptr2 = g_malloc (sizeof *user_id_ptr);
1051 *user_id_ptr2 = 8;
Roger Meierb3c84092014-09-01 21:53:40 +02001052 g_hash_table_insert (user_map, (gpointer)numberz, user_id_ptr);
Nobuaki Sukegawa01ede042015-09-29 02:16:53 +09001053 g_hash_table_insert (user_map, (gpointer)numberz2, user_id_ptr2);
Roger Meierb3c84092014-09-01 21:53:40 +02001054 g_hash_table_unref (user_map);
1055
1056 xtruct_out = g_object_new (T_TEST_TYPE_XTRUCT,
Nobuaki Sukegawa01ede042015-09-29 02:16:53 +09001057 "string_thing", "Hello2",
1058 "byte_thing", 2,
1059 "i32_thing", 2,
1060 "i64_thing", 2LL,
Roger Meierb3c84092014-09-01 21:53:40 +02001061 NULL);
Nobuaki Sukegawa01ede042015-09-29 02:16:53 +09001062 xtruct_out2 = g_object_new (T_TEST_TYPE_XTRUCT,
1063 "string_thing", "Goodbye4",
1064 "byte_thing", 4,
1065 "i32_thing", 4,
1066 "i64_thing", 4LL,
1067 NULL);
1068 g_ptr_array_add (xtructs, xtruct_out2);
Roger Meierb3c84092014-09-01 21:53:40 +02001069 g_ptr_array_add (xtructs, xtruct_out);
1070 g_ptr_array_unref (xtructs);
1071
1072 map_in = g_hash_table_new_full (g_int64_hash,
1073 g_int64_equal,
1074 g_free,
1075 (GDestroyNotify)g_hash_table_unref);
1076
1077 printf("testInsanity()");
1078 if (t_test_thrift_test_if_test_insanity (test_client,
1079 &map_in,
1080 insanity_out,
Roger Meier15df0762014-09-29 20:50:56 +02001081 &error)) {
Roger Meierb3c84092014-09-01 21:53:40 +02001082 printf (" = {");
1083 g_hash_table_iter_init (&hash_table_iter, map_in);
1084 while (g_hash_table_iter_next (&hash_table_iter,
1085 &key,
Roger Meier15df0762014-09-29 20:50:56 +02001086 &value)) {
Roger Meierb3c84092014-09-01 21:53:40 +02001087 printf ("%" PRId64 " => {", *(TTestUserId *)key);
1088
1089 g_hash_table_iter_init (&inner_hash_table_iter,
1090 (GHashTable *)value);
1091 while (g_hash_table_iter_next (&inner_hash_table_iter,
1092 &key,
Roger Meier15df0762014-09-29 20:50:56 +02001093 &value)) {
Roger Meierb3c84092014-09-01 21:53:40 +02001094 printf ("%d => {", (TTestNumberz)key);
1095
1096 g_object_get ((TTestInsanity *)value,
1097 "userMap", &user_map,
1098 "xtructs", &xtructs,
1099 NULL);
1100
1101 printf ("{");
1102 g_hash_table_iter_init (&user_map_iter, user_map);
1103 while (g_hash_table_iter_next (&user_map_iter,
1104 &key,
Roger Meier15df0762014-09-29 20:50:56 +02001105 &value)) {
Roger Meierb3c84092014-09-01 21:53:40 +02001106 printf ("%d => %" PRId64 ", ",
1107 (TTestNumberz)key,
1108 *(TTestUserId *)value);
1109 }
1110 printf ("}, ");
1111 g_hash_table_unref (user_map);
1112
1113 printf("{");
1114 for (i = 0; i < (gint32)xtructs->len; ++i) {
1115 xtruct_in = g_ptr_array_index (xtructs, i);
1116 g_object_get (xtruct_in,
1117 "string_thing", &string,
1118 "byte_thing", &byte_thing,
1119 "i32_thing", &i32_thing,
1120 "i64_thing", &i64_thing,
1121 NULL);
1122
1123 printf ("{\"%s\", %d, %d, %" PRId64 "}, ",
1124 string,
1125 byte_thing,
1126 i32_thing,
1127 i64_thing);
1128 }
1129 printf ("}");
1130 g_ptr_array_unref (xtructs);
1131
1132 printf ("}, ");
1133 }
1134 printf("}, ");
1135 }
1136 printf("}\n");
1137
1138 if (g_hash_table_size (map_in) != 2)
1139 fail_count++;
1140 else {
1141 TTestNumberz numberz_key_values[] = {
1142 T_TEST_NUMBERZ_TWO, T_TEST_NUMBERZ_THREE
1143 };
1144 gint user_map_values[] = { 5, 8 };
1145 TTestUserId user_id_key;
1146
1147 user_id_key = 1;
1148 inner_map_in = g_hash_table_lookup (map_in, &user_id_key);
1149 if (inner_map_in == NULL ||
1150 g_hash_table_size (inner_map_in) != 2)
1151 fail_count++;
1152 else {
1153 TTestNumberz numberz_key;
1154
1155 for (i = 0; i < 2; ++i) {
1156 numberz_key = numberz_key_values[i];
1157 insanity_in =
1158 g_hash_table_lookup (inner_map_in,
1159 (gconstpointer)numberz_key);
1160 if (insanity_in == NULL)
1161 fail_count++;
1162 else {
1163 g_object_get (insanity_in,
1164 "userMap", &user_map,
1165 "xtructs", &xtructs,
1166 NULL);
1167
1168 if (user_map == NULL)
1169 fail_count++;
1170 else {
1171 if (g_hash_table_size (user_map) != 2)
1172 fail_count++;
1173 else {
1174 for (j = 0; j < 2; ++j) {
1175 numberz_key = (TTestNumberz)user_map_values[j];
1176
1177 value =
1178 g_hash_table_lookup (user_map,
1179 (gconstpointer)numberz_key);
1180 if (value == NULL ||
1181 *(TTestUserId *)value != (TTestUserId)user_map_values[j])
1182 fail_count++;
1183 }
1184 }
1185
1186 g_hash_table_unref (user_map);
1187 }
1188
1189 if (xtructs == NULL)
1190 fail_count++;
1191 else {
1192 if (xtructs->len != 2)
1193 fail_count++;
1194 else {
1195 xtruct_in = g_ptr_array_index (xtructs, 0);
1196 g_object_get (xtruct_in,
1197 "string_thing", &string,
1198 "byte_thing", &byte_thing,
1199 "i32_thing", &i32_thing,
1200 "i64_thing", &i64_thing,
1201 NULL);
1202 if ((string == NULL ||
1203 strncmp (string, "Goodbye4", 9) != 0) ||
1204 byte_thing != 4 ||
1205 i32_thing != 4 ||
1206 i64_thing != 4)
1207 fail_count++;
1208
1209 if (string != NULL)
1210 g_free (string);
1211
1212 xtruct_in = g_ptr_array_index (xtructs, 1);
1213 g_object_get (xtruct_in,
1214 "string_thing", &string,
1215 "byte_thing", &byte_thing,
1216 "i32_thing", &i32_thing,
1217 "i64_thing", &i64_thing,
1218 NULL);
1219 if ((string == NULL ||
1220 strncmp (string, "Hello2", 7) != 0) ||
1221 byte_thing != 2 ||
1222 i32_thing != 2 ||
1223 i64_thing != 2)
1224 fail_count++;
1225
1226 if (string != NULL)
1227 g_free (string);
1228 }
1229
1230 g_ptr_array_unref (xtructs);
1231 }
1232 }
1233 }
1234 }
1235
1236 user_id_key = 2;
1237 inner_map_in = g_hash_table_lookup (map_in, &user_id_key);
1238 if (inner_map_in == NULL ||
1239 g_hash_table_size (inner_map_in) != 1)
1240 fail_count++;
1241 else {
1242 insanity_in =
1243 g_hash_table_lookup (inner_map_in,
1244 (gconstpointer)T_TEST_NUMBERZ_SIX);
1245 if (insanity_in == NULL)
1246 fail_count++;
1247 else {
1248 g_object_get (insanity_in,
1249 "userMap", &user_map,
1250 "xtructs", &xtructs,
1251 NULL);
1252
1253 if (user_map == NULL)
1254 fail_count++;
1255 else {
1256 if (g_hash_table_size (user_map) != 0)
1257 fail_count++;
1258
1259 g_hash_table_unref (user_map);
1260 }
1261
1262 if (xtructs == NULL)
1263 fail_count++;
1264 else {
1265 if (xtructs->len != 0)
1266 fail_count++;
1267
1268 g_ptr_array_unref (xtructs);
1269 }
1270 }
1271 }
1272 }
1273 }
1274 else {
1275 printf ("%s\n", error->message);
1276 g_error_free (error);
1277 error = NULL;
1278
1279 fail_count++;
1280 }
1281
1282 g_hash_table_unref (map_in);
1283 g_object_unref (insanity_out);
1284
1285 /* test exception */
1286 printf ("testClient.testException(\"Xception\") =>");
Roger Meier15df0762014-09-29 20:50:56 +02001287 if (!t_test_thrift_test_if_test_exception (test_client,
1288 "Xception",
1289 &xception,
1290 &error) &&
Roger Meierb3c84092014-09-01 21:53:40 +02001291 xception != NULL) {
1292 g_object_get (xception,
1293 "errorCode", &int32,
1294 "message", &string,
1295 NULL);
1296 printf (" {%u, \"%s\"}\n", int32, string);
1297 g_free (string);
1298
1299 g_object_unref (xception);
1300 xception = NULL;
1301
1302 g_error_free (error);
1303 error = NULL;
1304 }
1305 else {
1306 printf (" void\nFAILURE\n");
1307 fail_count++;
1308
1309 if (xception != NULL) {
1310 g_object_unref (xception);
1311 xception = NULL;
1312 }
1313
1314 if (error != NULL) {
1315 g_error_free (error);
1316 error = NULL;
1317 }
1318 }
1319
1320 printf ("testClient.testException(\"TException\") =>");
Roger Meier15df0762014-09-29 20:50:56 +02001321 if (!t_test_thrift_test_if_test_exception (test_client,
1322 "TException",
1323 &xception,
1324 &error) &&
Roger Meierb3c84092014-09-01 21:53:40 +02001325 xception == NULL &&
1326 error != NULL) {
1327 printf (" Caught TException\n");
1328
1329 g_error_free (error);
1330 error = NULL;
1331 }
1332 else {
1333 printf (" void\nFAILURE\n");
1334 fail_count++;
1335
1336 if (xception != NULL) {
1337 g_object_unref (xception);
1338 xception = NULL;
1339 }
1340
1341 if (error != NULL) {
1342 g_error_free (error);
1343 error = NULL;
1344 }
1345 }
1346
1347 printf ("testClient.testException(\"success\") =>");
1348 if (t_test_thrift_test_if_test_exception (test_client,
1349 "success",
1350 &xception,
Roger Meier15df0762014-09-29 20:50:56 +02001351 &error))
Roger Meierb3c84092014-09-01 21:53:40 +02001352 printf (" void\n");
1353 else {
1354 printf (" void\nFAILURE\n");
1355 fail_count++;
1356
1357 if (xception != NULL) {
1358 g_object_unref (xception);
1359 xception = NULL;
1360 }
1361
1362 g_error_free (error);
1363 error = NULL;
1364 }
1365
1366 g_assert (error == NULL);
1367
1368 /* test multi exception */
1369 printf ("testClient.testMultiException(\"Xception\", \"test 1\") =>");
1370 xtruct_in = g_object_new (T_TEST_TYPE_XTRUCT, NULL);
Roger Meier15df0762014-09-29 20:50:56 +02001371 if (!t_test_thrift_test_if_test_multi_exception (test_client,
1372 &xtruct_in,
1373 "Xception",
1374 "test 1",
1375 &xception,
1376 &xception2,
1377 &error) &&
Roger Meierb3c84092014-09-01 21:53:40 +02001378 xception != NULL &&
1379 xception2 == NULL) {
1380 g_object_get (xception,
1381 "errorCode", &int32,
1382 "message", &string,
1383 NULL);
1384 printf (" {%u, \"%s\"}\n", int32, string);
1385 g_free (string);
1386
1387 g_object_unref (xception);
1388 xception = NULL;
1389
1390 g_error_free (error);
1391 error = NULL;
1392 }
1393 else {
1394 printf (" result\nFAILURE\n");
1395 fail_count++;
1396
1397 if (xception != NULL) {
1398 g_object_unref (xception);
1399 xception = NULL;
1400 }
1401
1402 if (xception2 != NULL) {
1403 g_object_unref (xception2);
1404 xception = NULL;
1405 }
1406
1407 if (error != NULL) {
1408 g_error_free (error);
1409 error = NULL;
1410 }
1411 }
1412 g_object_unref (xtruct_in);
1413
1414 printf ("testClient.testMultiException(\"Xception2\", \"test 2\") =>");
1415 xtruct_in = g_object_new (T_TEST_TYPE_XTRUCT, NULL);
Roger Meier15df0762014-09-29 20:50:56 +02001416 if (!t_test_thrift_test_if_test_multi_exception (test_client,
1417 &xtruct_in,
1418 "Xception2",
1419 "test 2",
1420 &xception,
1421 &xception2,
1422 &error) &&
Roger Meierb3c84092014-09-01 21:53:40 +02001423 xception == NULL &&
1424 xception2 != NULL) {
1425 g_object_get (xception2,
1426 "errorCode", &int32,
1427 "struct_thing", &inner_xtruct_in,
1428 NULL);
1429 g_object_get (inner_xtruct_in,
1430 "string_thing", &string,
1431 NULL);
1432 printf (" {%u, {\"%s\"}}\n", int32, string);
1433 g_free (string);
1434
1435 g_object_unref (inner_xtruct_in);
1436 inner_xtruct_in = NULL;
1437
1438 g_object_unref (xception2);
1439 xception2 = NULL;
1440
1441 g_error_free (error);
1442 error = NULL;
1443 }
1444 else {
1445 printf (" result\nFAILURE\n");
1446 fail_count++;
1447
1448 if (xception != NULL) {
1449 g_object_unref (xception);
1450 xception = NULL;
1451 }
1452
1453 if (xception2 != NULL) {
1454 g_object_unref (xception2);
1455 xception = NULL;
1456 }
1457
1458 if (error != NULL) {
1459 g_error_free (error);
1460 error = NULL;
1461 }
1462 }
1463 g_object_unref (xtruct_in);
1464
1465 printf ("testClient.testMultiException(\"success\", \"test 3\") =>");
1466 xtruct_in = g_object_new (T_TEST_TYPE_XTRUCT, NULL);
1467 if (t_test_thrift_test_if_test_multi_exception (test_client,
1468 &xtruct_in,
1469 "success",
1470 "test 3",
1471 &xception,
1472 &xception2,
Roger Meier15df0762014-09-29 20:50:56 +02001473 &error) &&
Roger Meierb3c84092014-09-01 21:53:40 +02001474 xception == NULL &&
1475 xception2 == NULL) {
1476 g_object_get (xtruct_in,
1477 "string_thing", &string,
1478 NULL);
1479 printf (" {{\"%s\"}}\n", string);
1480 g_free (string);
1481 }
1482 else {
1483 printf (" result\nFAILURE\n");
1484 fail_count++;
1485
1486 if (xception != NULL) {
1487 g_object_unref (xception);
1488 xception = NULL;
1489 }
1490
1491 if (xception2 != NULL) {
1492 g_object_unref (xception2);
1493 xception = NULL;
1494 }
1495
1496 if (error != NULL) {
1497 g_error_free (error);
1498 error = NULL;
1499 }
1500 }
1501 g_object_unref (xtruct_in);
1502
1503 /* test oneway void */
1504 printf ("testClient.testOneway(1) =>");
1505 gettimeofday (&oneway_start, NULL);
1506 oneway_result = t_test_thrift_test_if_test_oneway (test_client,
1507 1,
1508 &error);
1509 gettimeofday (&oneway_end, NULL);
1510 timersub (&oneway_end, &oneway_start, &oneway_elapsed);
1511 oneway_elapsed_usec =
1512 oneway_elapsed.tv_sec * 1000 * 1000 + oneway_elapsed.tv_usec;
1513
Roger Meier15df0762014-09-29 20:50:56 +02001514 if (oneway_result) {
Roger Meierb3c84092014-09-01 21:53:40 +02001515 if (oneway_elapsed_usec > 200 * 1000) {
1516 printf (" FAILURE - took %.2f ms\n",
1517 (double)oneway_elapsed_usec / 1000.0);
1518 fail_count++;
1519 }
1520 else
1521 printf (" success - took %.2f ms\n",
1522 (double)oneway_elapsed_usec / 1000.0);
1523 }
1524 else {
1525 printf ("%s\n", error->message);
1526 g_error_free (error);
1527 error = NULL;
1528
1529 fail_count++;
1530 }
1531
1532 /**
1533 * redo a simple test after the oneway to make sure we aren't "off by
1534 * one" -- if the server treated oneway void like normal void, this next
1535 * test will fail since it will get the void confirmation rather than
1536 * the correct result. In this circumstance, the client will receive the
1537 * error:
1538 *
1539 * application error: Wrong method name
1540 */
1541 /**
1542 * I32 TEST
1543 */
1544 printf ("re-test testI32(-1)");
1545 if (t_test_thrift_test_if_test_i32 (test_client,
1546 &int32,
1547 -1,
Roger Meier15df0762014-09-29 20:50:56 +02001548 &error)) {
Roger Meierb3c84092014-09-01 21:53:40 +02001549 printf (" = %d\n", int32);
1550 if (int32 != -1)
1551 fail_count++;
1552 }
1553 else {
1554 printf ("%s\n", error->message);
1555 g_error_free (error);
1556 error = NULL;
1557
1558 fail_count++;
1559 }
1560
1561 gettimeofday (&time_stop, NULL);
1562 timersub (&time_stop, &time_start, &time_elapsed);
1563 time_elapsed_usec =
1564 time_elapsed.tv_sec * 1000 * 1000 + time_elapsed.tv_usec;
1565
1566 printf("Total time: %" PRIu64 " us\n", time_elapsed_usec);
1567
1568 time_total_usec += time_elapsed_usec;
1569 if (time_elapsed_usec < time_min_usec)
1570 time_min_usec = time_elapsed_usec;
1571 if (time_elapsed_usec > time_max_usec)
1572 time_max_usec = time_elapsed_usec;
1573
1574 thrift_transport_close (transport, &error);
1575 }
1576 else {
1577 printf ("Connect failed: %s\n", error->message);
1578 g_error_free (error);
1579 error = NULL;
1580
1581 return 1;
1582 }
1583 }
1584
1585 /* All done---output statistics */
1586 puts ("\nAll tests done.");
1587
1588 time_avg_usec = time_total_usec / num_tests;
1589
1590 printf ("Min time: %" PRIu64 " us\n", time_min_usec);
1591 printf ("Max time: %" PRIu64 " us\n", time_max_usec);
1592 printf ("Avg time: %" PRIu64 " us\n", time_avg_usec);
1593
1594 g_object_unref (test_client);
1595 g_object_unref (protocol);
1596 g_object_unref (transport);
1597 g_free (host);
1598
1599 return fail_count;
1600}