1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 2000 Tor Lillqvist
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.
44 static GMainLoop *main_loop;
46 #define BUFSIZE 5000 /* Larger than the circular buffer in
47 * giowin32.c on purpose.
66 GIOError error = G_IO_ERROR_NONE;
69 /* g_io_channel_read() doesn't necessarily return all the
70 * data we want at once.
75 error = g_io_channel_read (channel, bufp, left, &nb);
77 if (error != G_IO_ERROR_NONE)
79 g_print ("gio-test: ...from %d: G_IO_ERROR_%s\n", fd,
80 (error == G_IO_ERROR_AGAIN ? "AGAIN" :
81 (error == G_IO_ERROR_INVAL ? "INVAL" :
82 (error == G_IO_ERROR_UNKNOWN ? "UNKNOWN" : "???"))));
83 if (error == G_IO_ERROR_AGAIN)
97 shutdown_source (gpointer data)
99 if (g_source_remove (*(guint *) data))
103 g_main_quit (main_loop);
108 recv_message (GIOChannel *channel,
112 gint fd = g_io_channel_unix_get_fd (channel);
113 gboolean retval = TRUE;
115 g_print ("gio-test: ...from %d:%s%s%s%s\n", fd,
116 (cond & G_IO_ERR) ? " ERR" : "",
117 (cond & G_IO_HUP) ? " HUP" : "",
118 (cond & G_IO_IN) ? " IN" : "",
119 (cond & G_IO_PRI) ? " PRI" : "");
121 if (cond & (G_IO_ERR | G_IO_HUP))
123 shutdown_source (data);
135 error = read_all (fd, channel, (gchar *) &seq, sizeof (seq), &nb);
136 if (error == G_IO_ERROR_NONE)
140 g_print ("gio-test: ...from %d: EOF\n", fd);
141 shutdown_source (data);
145 g_assert (nb == sizeof (nbytes));
147 for (i = 0; i < nkiddies; i++)
148 if (seqtab[i].fd == fd)
150 if (seq != seqtab[i].seq)
152 g_print ("gio-test: ...from %d: invalid sequence number %d, expected %d\n",
153 fd, seq, seqtab[i].seq);
154 g_assert_not_reached ();
160 error = read_all (fd, channel, (gchar *) &nbytes, sizeof (nbytes), &nb);
163 if (error != G_IO_ERROR_NONE)
168 g_print ("gio-test: ...from %d: EOF\n", fd);
169 shutdown_source (data);
173 g_assert (nb == sizeof (nbytes));
175 if (nbytes >= BUFSIZE)
177 g_print ("gio-test: ...from %d: nbytes = %d (%#x)!\n", fd, nbytes, nbytes);
178 g_assert_not_reached ();
180 g_assert (nbytes >= 0 && nbytes < BUFSIZE);
182 g_print ("gio-test: ...from %d: %d bytes\n", fd, nbytes);
186 error = read_all (fd, channel, buf, nbytes, &nb);
188 if (error != G_IO_ERROR_NONE)
193 g_print ("gio-test: ...from %d: EOF\n", fd);
194 shutdown_source (data);
198 for (j = 0; j < nbytes; j++)
199 if (buf[j] != ' ' + ((nbytes + j) % 95))
201 g_print ("gio-test: ...from %d: buf[%d] == '%c', should be '%c'\n",
202 fd, j, buf[j], 'a' + ((nbytes + j) % 32));
203 g_assert_not_reached ();
205 g_print ("gio-test: ...from %d: OK\n", fd);
219 GIOChannel *my_read_channel;
229 nkiddies = (argc == 1 ? 1 : atoi(argv[1]));
230 seqtab = g_malloc (nkiddies * 2 * sizeof (int));
232 for (i = 0; i < nkiddies; i++)
234 int pipe_to_sub[2], pipe_from_sub[2];
236 if (pipe (pipe_to_sub) == -1 ||
237 pipe (pipe_from_sub) == -1)
238 perror ("pipe"), exit (1);
241 seqtab[i].fd = pipe_from_sub[0];
244 my_read_channel = g_io_channel_unix_new (pipe_from_sub[0]);
246 id = g_new (guint, 1);
248 g_io_add_watch (my_read_channel,
249 G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP,
253 cmdline = g_strdup_printf ("%s %d %d &", argv[0],
254 pipe_to_sub[0], pipe_from_sub[1]);
260 gchar *readfd = g_strdup_printf ("%d", pipe_to_sub[0]);
261 gchar *writefd = g_strdup_printf ("%d", pipe_from_sub[1]);
262 _spawnl (_P_NOWAIT, argv[0], argv[0], readfd, writefd, NULL);
267 close (pipe_to_sub[0]);
268 close (pipe_from_sub [1]);
271 g_get_current_time (&start);
272 g_io_channel_win32_make_pollfd (my_read_channel, G_IO_IN, &pollfd);
273 pollresult = g_io_channel_win32_poll (&pollfd, 1, 100);
274 g_get_current_time (&end);
275 if (end.tv_usec < start.tv_usec)
276 end.tv_sec--, end.tv_usec += 1000000;
277 g_print ("gio-test: had to wait %ld.%03ld s, result:%d\n",
278 end.tv_sec - start.tv_sec,
279 (end.tv_usec - start.tv_usec) / 1000,
284 main_loop = g_main_new (FALSE);
286 g_main_run (main_loop);
298 g_get_current_time (&tv);
300 readfd = atoi (argv[1]);
301 writefd = atoi (argv[2]);
303 srand (tv.tv_sec ^ (tv.tv_usec / 1000) ^ readfd ^ (writefd << 4));
305 for (i = 0; i < 20 + rand() % 20; i++)
307 g_usleep (100 + (rand() % 10) * 5000);
308 buflen = rand() % BUFSIZE;
309 for (j = 0; j < buflen; j++)
310 buf[j] = ' ' + ((buflen + j) % 95);
311 g_print ("gio-test: child writing %d bytes to %d\n", buflen, writefd);
312 write (writefd, &i, sizeof (i));
313 write (writefd, &buflen, sizeof (buflen));
314 write (writefd, buf, buflen);
316 g_print ("gio-test: child exiting, closing %d\n", writefd);