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