Update contrib.
1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 2000 Tor Lillqvist
3 * Portion Copyright © 2008-09 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
20 /* A test program for the main loop and IO channel code.
21 * Just run it. Optional parameter is number of sub-processes.
25 #undef G_DISABLE_ASSERT
40 #include "mrt2_glib2_test.h"
41 void *child_function(void*);
42 gboolean child_terminated = FALSE;
43 int from_descriptor, to_descriptor;
60 static GMainLoop *main_loop;
65 #define BUFSIZE 5000 /* Larger than the circular buffer in giowin32.c on purpose.*/
86 GIOError error = G_IO_ERROR_NONE;
89 /* g_io_channel_read() doesn't necessarily return all the
90 * data we want at once.
95 error = g_io_channel_read (channel, bufp, left, &nb);
97 if (error != G_IO_ERROR_NONE)
99 g_print ("gio-test: ...from %d: G_IO_ERROR_%s\n", fd,
100 (error == G_IO_ERROR_AGAIN ? "AGAIN" :
101 (error == G_IO_ERROR_INVAL ? "INVAL" :
102 (error == G_IO_ERROR_UNKNOWN ? "UNKNOWN" : "???"))));
103 if (error == G_IO_ERROR_AGAIN)
117 shutdown_source (gpointer data)
119 if (g_source_remove (*(guint *) data))
123 g_main_loop_quit (main_loop);
128 recv_message (GIOChannel *channel,
132 gint fd = g_io_channel_unix_get_fd (channel);
133 gboolean retval = TRUE;
136 g_print ("gio-test: ...from %d:%s%s%s%s\n", fd,
137 (cond & G_IO_ERR) ? " ERR" : "",
138 (cond & G_IO_HUP) ? " HUP" : "",
139 (cond & G_IO_IN) ? " IN" : "",
140 (cond & G_IO_PRI) ? " PRI" : "");
143 if ((cond & (G_IO_ERR | G_IO_HUP)))
145 shutdown_source (data);
157 error = read_all (fd, channel, (gchar *) &seq, sizeof (seq), &nb);
158 if (error == G_IO_ERROR_NONE)
163 g_print ("gio-test: ...from %d: EOF\n", fd);
165 shutdown_source (data);
169 g_assert (nb == sizeof (nbytes));
171 for (i = 0; i < nkiddies; i++)
172 if (seqtab[i].fd == fd)
174 if (seq != seqtab[i].seq)
176 g_print ("gio-test: ...from %d: invalid sequence number %d, expected %d\n",
177 fd, seq, seqtab[i].seq);
178 g_assert_not_reached ();
180 g_assert(FALSE && "gio-test failed");
187 error = read_all (fd, channel, (gchar *) &nbytes, sizeof (nbytes), &nb);
190 if (error != G_IO_ERROR_NONE)
196 g_print ("gio-test: ...from %d: EOF\n", fd);
198 shutdown_source (data);
202 g_assert (nb == sizeof (nbytes));
204 if (nbytes >= BUFSIZE)
206 g_print ("gio-test: ...from %d: nbytes = %d (%#x)!\n", fd, nbytes, nbytes);
207 g_assert_not_reached ();
208 g_assert(FALSE && "gio-test failed");
210 g_assert (nbytes >= 0 && nbytes < BUFSIZE);
212 g_print ("gio-test: ...from %d: %d bytes\n", fd, nbytes);
216 error = read_all (fd, channel, buf, nbytes, &nb);
218 shutdown_source (data);
221 if (error != G_IO_ERROR_NONE)
227 g_print ("gio-test: ...from %d: EOF\n", fd);
229 shutdown_source (data);
233 for (j = 0; j < nbytes; j++)
234 if (buf[j] != ' ' + ((nbytes + j) % 95))
236 g_print ("gio-test: ...from %d: buf[%d] == '%c', should be '%c'\n",
237 fd, j, buf[j], 'a' + ((nbytes + j) % 32));
238 g_assert_not_reached ();
239 g_assert(FALSE && "gio-test failed");
242 g_print ("gio-test: ...from %d: OK\n", fd);
252 recv_windows_message (GIOChannel *channel,
262 error = g_io_channel_read (channel, &msg, sizeof (MSG), &nb);
264 if (error != G_IO_ERROR_NONE)
266 g_print ("gio-test: ...reading Windows message: G_IO_ERROR_%s\n",
267 (error == G_IO_ERROR_AGAIN ? "AGAIN" :
268 (error == G_IO_ERROR_INVAL ? "INVAL" :
269 (error == G_IO_ERROR_UNKNOWN ? "UNKNOWN" : "???"))));
270 if (error == G_IO_ERROR_AGAIN)
276 g_print ("gio-test: ...Windows message for %#x: %d,%d,%d\n",
277 msg.hwnd, msg.message, msg.wParam, msg.lParam);
283 window_procedure (HWND hwnd,
288 g_print ("gio-test: window_procedure for %#x: %d,%d,%d\n",
289 hwnd, message, wparam, lparam);
290 return DefWindowProc (hwnd, message, wparam, lparam);
302 pthread_t thread_child;
304 g_log_set_handler (NULL, G_LOG_FLAG_FATAL| G_LOG_FLAG_RECURSION | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG, &mrtLogHandler, NULL);
305 g_set_print_handler(mrtPrintHandler);
313 GIOChannel *my_read_channel;
324 GIOChannel *windows_messages_channel;
327 nkiddies = (argc == 1 ? 1 : atoi(argv[1]));
328 seqtab = g_malloc (nkiddies * 2 * sizeof (int));
332 wcl.lpfnWndProc = window_procedure;
335 wcl.hInstance = GetModuleHandle (NULL);
338 wcl.hbrBackground = NULL;
339 wcl.lpszMenuName = NULL;
340 wcl.lpszClassName = "gio-test";
342 klass = RegisterClass (&wcl);
346 g_print ("gio-test: RegisterClass failed\n");
350 hwnd = CreateWindow (MAKEINTATOM(klass), "gio-test", 0, 0, 0, 10, 10,
351 NULL, NULL, wcl.hInstance, NULL);
354 g_print ("gio-test: CreateWindow failed\n");
358 windows_messages_channel = g_io_channel_win32_new_messages ((guint)hwnd);
359 g_io_add_watch (windows_messages_channel, G_IO_IN, recv_windows_message, 0);
362 for (i = 0; i < nkiddies; i++)
364 int pipe_to_sub[2], pipe_from_sub[2];
366 if (pipe (pipe_to_sub) == -1 ||
367 pipe (pipe_from_sub) == -1)
368 perror ("pipe"), exit (1);
370 seqtab[i].fd = pipe_from_sub[0];
373 my_read_channel = g_io_channel_unix_new (pipe_from_sub[0]);
375 id = g_new (guint, 1);
377 g_io_add_watch (my_read_channel,
378 G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP,
385 cmdline = g_strdup_g_print ("%d:%d:%d",
389 _spawnl (_P_NOWAIT, argv[0], argv[0], "--child", cmdline, NULL);
391 cmdline = g_strdup_printf ("%s --child %d:%d &", argv[0],
392 pipe_to_sub[0], pipe_from_sub[1]);
398 from_descriptor = pipe_to_sub[0];
399 to_descriptor = pipe_from_sub[1];
400 rc = pthread_create(&thread_child,NULL,child_function,NULL);
405 g_print("Failed to create thread.\n");
413 close (pipe_to_sub[0]);
414 close (pipe_from_sub [1]);
418 g_get_current_time (&start);
419 g_io_channel_win32_make_pollfd (my_read_channel, G_IO_IN, &pollfd);
420 pollresult = g_io_channel_win32_poll (&pollfd, 1, 100);
421 g_get_current_time (&end);
422 if (end.tv_usec < start.tv_usec)
423 end.tv_sec--, end.tv_usec += 1000000;
424 g_print ("gio-test: had to wait %ld.%03ld s, result:%d\n",
425 end.tv_sec - start.tv_sec,
426 (end.tv_usec - start.tv_usec) / 1000,
431 main_loop = g_main_loop_new (NULL, FALSE);
432 g_main_loop_run (main_loop);
449 g_get_current_time (&tv);
451 sscanf (argv[2], "%d:%d%n", &readfd, &writefd, &n);
454 sscanf (argv[2] + n, ":%d", &hwnd);
457 srand (tv.tv_sec ^ (tv.tv_usec / 1000) ^ readfd ^ (writefd << 4));
459 for (i = 0; i < 20 + rand() % 20; i++)
461 g_usleep (100 + (rand() % 10) * 5000);
462 buflen = rand() % BUFSIZE;
463 for (j = 0; j < buflen; j++)
464 buf[j] = ' ' + ((buflen + j) % 95);
466 g_print ("gio-test: child writing %d+%d bytes to %d\n",
467 (int)(sizeof(i) + sizeof(buflen)), buflen, writefd);
469 write (writefd, &i, sizeof (i));
470 write (writefd, &buflen, sizeof (buflen));
471 write (writefd, buf, buflen);
474 if (rand() % 100 < 5)
476 int msg = WM_USER + (rand() % 100);
477 WPARAM wparam = rand ();
478 LPARAM lparam = rand ();
479 g_print ("gio-test: child posting message %d,%d,%d to %#x\n",
480 msg, wparam, lparam, hwnd);
481 PostMessage (hwnd, msg, wparam, lparam);
486 g_print ("gio-test: child exiting, closing %d\n", writefd);
495 close(from_descriptor);
496 close(to_descriptor);
500 printf("Completed tests\n");
504 testResultXml("gio-test");
505 #endif /* EMULATOR */
511 void *child_function(void* t)
521 g_get_current_time (&tv);
524 readfd=from_descriptor;
525 writefd=to_descriptor;
527 srand (tv.tv_sec ^ (tv.tv_usec / 1000) ^ readfd ^ (writefd << 4));
529 for (i = 0; i < 5; i++)
532 child_terminated = TRUE;
533 g_usleep (100 + (rand() % 10) * 5000);
534 buflen = rand() % BUFSIZE;
535 for (j = 0; j < buflen; j++)
536 buf[j] = ' ' + ((buflen + j) % 95);
538 g_print ("gio-test: child writing %d+%d bytes to %d\n",
539 (int)(sizeof(i) + sizeof(buflen)), buflen, writefd);
541 write (writefd, &i, sizeof (i));
542 write (writefd, &buflen, sizeof (buflen));
543 write (writefd, buf, buflen);
546 g_print ("gio-test: child exiting, closing %d\n", writefd);