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.
58 recv_message (GIOChannel *channel,
62 gint fd = g_io_channel_unix_get_fd (channel);
64 g_print ("testgio: ...from %d:%s%s%s%s\n", fd,
65 (cond & G_IO_ERR) ? " ERR" : "",
66 (cond & G_IO_HUP) ? " HUP" : "",
67 (cond & G_IO_IN) ? " IN" : "",
68 (cond & G_IO_PRI) ? " PRI" : "");
70 if (cond & (G_IO_ERR | G_IO_HUP))
72 g_source_remove (*(guint *) data);
75 g_main_quit (main_loop);
86 error = g_io_channel_read (channel, (gchar *) &seq, sizeof (seq), &nb);
87 if (error == G_IO_ERROR_NONE)
91 g_print ("testgio: ...from %d: EOF\n", fd);
95 g_assert (nb == sizeof (nbytes));
97 for (i = 0; i < nkiddies; i++)
98 if (seqtab[i].fd == fd)
100 if (seq != seqtab[i].seq)
102 g_print ("testgio: ...from &d: invalid sequence number %d, expected %d\n",
104 g_assert_not_reached ();
110 error = g_io_channel_read (channel, (gchar *) &nbytes, sizeof (nbytes), &nb);
113 if (error != G_IO_ERROR_NONE)
115 g_print ("testgio: ...from %d: G_IO_ERROR_%s\n", fd,
116 (error == G_IO_ERROR_AGAIN ? "AGAIN" :
117 (error == G_IO_ERROR_INVAL ? "INVAL" :
118 (error == G_IO_ERROR_UNKNOWN ? "UNKNOWN" : "???"))));
125 g_print ("testgio: ...from %d: EOF\n", fd);
129 g_assert (nb == sizeof (nbytes));
131 if (nbytes >= BUFSIZE)
133 g_print ("testgio: ...from %d: nbytes = %d (%#x)!\n", fd, nbytes, nbytes);
134 g_assert_not_reached ();
136 g_assert (nbytes >= 0 && nbytes < BUFSIZE);
138 g_print ("testgio: ...from %d: %d bytes\n", fd, nbytes);
142 error = g_io_channel_read (channel, buf, nbytes, &nb);
144 if (error != G_IO_ERROR_NONE)
146 g_print ("testgio: ...from %d: G_IO_ERROR_%s\n", fd,
147 (error == G_IO_ERROR_AGAIN ? "AGAIN" :
148 (error == G_IO_ERROR_INVAL ? "INVAL" :
149 (error == G_IO_ERROR_UNKNOWN ? "UNKNOWN" : "???"))));
156 g_print ("testgio: ...from %d: nb=%d != nbytes=%d\n",
158 g_assert_not_reached ();
161 for (j = 0; j < nbytes; j++)
162 if (buf[j] != ' ' + ((nbytes + j) % 95))
164 g_print ("testgio: ...from %d: buf[%d] == '%c', should be '%c'\n",
165 fd, j, buf[j], 'a' + ((nbytes + j) % 32));
166 g_assert_not_reached ();
168 g_print ("testgio: ...from %d: OK\n", fd);
182 GIOChannel *my_read_channel;
191 nkiddies = (argc == 1 ? 1 : atoi(argv[1]));
192 seqtab = g_malloc (nkiddies * 2 * sizeof (int));
194 for (i = 0; i < nkiddies; i++)
196 int pipe_to_sub[2], pipe_from_sub[2];
198 if (pipe (pipe_to_sub) == -1 ||
199 pipe (pipe_from_sub) == -1)
200 perror ("pipe"), exit (1);
203 seqtab[i].fd = pipe_from_sub[0];
206 my_read_channel = g_io_channel_unix_new (pipe_from_sub[0]);
208 id = g_new (guint, 1);
210 g_io_add_watch (my_read_channel,
211 G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP,
215 cmdline = g_strdup_printf ("%s %d %d &", argv[0],
216 pipe_to_sub[0], pipe_from_sub[1]);
222 gchar *readfd = g_strdup_printf ("%d", pipe_to_sub[0]);
223 gchar *writefd = g_strdup_printf ("%d", pipe_from_sub[1]);
224 _spawnl (_P_NOWAIT, argv[0], argv[0], readfd, writefd, NULL);
229 close (pipe_to_sub[0]);
230 close (pipe_from_sub [1]);
233 g_get_current_time (&start);
234 pollresult = g_io_channel_win32_wait_for_condition (my_read_channel, G_IO_IN, 100);
235 g_get_current_time (&end);
236 if (end.tv_usec < start.tv_usec)
237 end.tv_sec--, end.tv_usec += 1000000;
238 g_print ("testgio: had to wait %ld.%03ld s, result:%d\n",
239 end.tv_sec - start.tv_sec,
240 (end.tv_usec - start.tv_usec) / 1000,
245 main_loop = g_main_new (FALSE);
247 g_main_run (main_loop);
259 g_get_current_time (&tv);
261 readfd = atoi (argv[1]);
262 writefd = atoi (argv[2]);
264 srand (tv.tv_sec ^ (tv.tv_usec / 1000) ^ readfd ^ (writefd << 4));
266 for (i = 0; i < 20 + rand() % 20; i++)
268 g_usleep (100 + (rand() % 10) * 5000);
269 buflen = rand() % BUFSIZE;
270 for (j = 0; j < buflen; j++)
271 buf[j] = ' ' + ((buflen + j) % 95);
272 g_print ("testgio: child writing %d bytes to %d\n", buflen, writefd);
273 write (writefd, &i, sizeof (i));
274 write (writefd, &buflen, sizeof (buflen));
275 write (writefd, buf, buflen);
277 g_print ("testgio: child exiting, closing %d\n", writefd);