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.
44 static GMainLoop *main_loop;
46 #define BUFSIZE 5000 /* Larger than the circular buffer in
47 * giowin32.c on purpose.
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)
98 recv_message (GIOChannel *channel,
102 gint fd = g_io_channel_unix_get_fd (channel);
104 g_print ("gio-test: ...from %d:%s%s%s%s\n", fd,
105 (cond & G_IO_ERR) ? " ERR" : "",
106 (cond & G_IO_HUP) ? " HUP" : "",
107 (cond & G_IO_IN) ? " IN" : "",
108 (cond & G_IO_PRI) ? " PRI" : "");
110 if (cond & (G_IO_ERR | G_IO_HUP))
112 g_source_remove (*(guint *) data);
115 g_main_quit (main_loop);
126 error = read_all (fd, channel, (gchar *) &seq, sizeof (seq), &nb);
127 if (error == G_IO_ERROR_NONE)
131 g_print ("gio-test: ...from %d: EOF\n", fd);
135 g_assert (nb == sizeof (nbytes));
137 for (i = 0; i < nkiddies; i++)
138 if (seqtab[i].fd == fd)
140 if (seq != seqtab[i].seq)
142 g_print ("gio-test: ...from &d: invalid sequence number %d, expected %d\n",
144 g_assert_not_reached ();
150 error = read_all (fd, channel, (gchar *) &nbytes, sizeof (nbytes), &nb);
153 if (error != G_IO_ERROR_NONE)
158 g_print ("gio-test: ...from %d: EOF\n", fd);
162 g_assert (nb == sizeof (nbytes));
164 if (nbytes >= BUFSIZE)
166 g_print ("gio-test: ...from %d: nbytes = %d (%#x)!\n", fd, nbytes, nbytes);
167 g_assert_not_reached ();
169 g_assert (nbytes >= 0 && nbytes < BUFSIZE);
171 g_print ("gio-test: ...from %d: %d bytes\n", fd, nbytes);
175 error = read_all (fd, channel, buf, nbytes, &nb);
177 if (error != G_IO_ERROR_NONE)
182 g_print ("gio-test: ...from %d: EOF\n", fd);
186 for (j = 0; j < nbytes; j++)
187 if (buf[j] != ' ' + ((nbytes + j) % 95))
189 g_print ("gio-test: ...from %d: buf[%d] == '%c', should be '%c'\n",
190 fd, j, buf[j], 'a' + ((nbytes + j) % 32));
191 g_assert_not_reached ();
193 g_print ("gio-test: ...from %d: OK\n", fd);
207 GIOChannel *my_read_channel;
216 nkiddies = (argc == 1 ? 1 : atoi(argv[1]));
217 seqtab = g_malloc (nkiddies * 2 * sizeof (int));
219 for (i = 0; i < nkiddies; i++)
221 int pipe_to_sub[2], pipe_from_sub[2];
223 if (pipe (pipe_to_sub) == -1 ||
224 pipe (pipe_from_sub) == -1)
225 perror ("pipe"), exit (1);
228 seqtab[i].fd = pipe_from_sub[0];
231 my_read_channel = g_io_channel_unix_new (pipe_from_sub[0]);
233 id = g_new (guint, 1);
235 g_io_add_watch (my_read_channel,
236 G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP,
240 cmdline = g_strdup_printf ("%s %d %d &", argv[0],
241 pipe_to_sub[0], pipe_from_sub[1]);
247 gchar *readfd = g_strdup_printf ("%d", pipe_to_sub[0]);
248 gchar *writefd = g_strdup_printf ("%d", pipe_from_sub[1]);
249 _spawnl (_P_NOWAIT, argv[0], argv[0], readfd, writefd, NULL);
254 close (pipe_to_sub[0]);
255 close (pipe_from_sub [1]);
258 g_get_current_time (&start);
259 pollresult = g_io_channel_win32_wait_for_condition (my_read_channel, G_IO_IN, 100);
260 g_get_current_time (&end);
261 if (end.tv_usec < start.tv_usec)
262 end.tv_sec--, end.tv_usec += 1000000;
263 g_print ("gio-test: had to wait %ld.%03ld s, result:%d\n",
264 end.tv_sec - start.tv_sec,
265 (end.tv_usec - start.tv_usec) / 1000,
270 main_loop = g_main_new (FALSE);
272 g_main_run (main_loop);
284 g_get_current_time (&tv);
286 readfd = atoi (argv[1]);
287 writefd = atoi (argv[2]);
289 srand (tv.tv_sec ^ (tv.tv_usec / 1000) ^ readfd ^ (writefd << 4));
291 for (i = 0; i < 20 + rand() % 20; i++)
293 g_usleep (100 + (rand() % 10) * 5000);
294 buflen = rand() % BUFSIZE;
295 for (j = 0; j < buflen; j++)
296 buf[j] = ' ' + ((buflen + j) % 95);
297 g_print ("gio-test: child writing %d bytes to %d\n", buflen, writefd);
298 write (writefd, &i, sizeof (i));
299 write (writefd, &buflen, sizeof (buflen));
300 write (writefd, buf, buflen);
302 g_print ("gio-test: child exiting, closing %d\n", writefd);