mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-04-17 21:08:49 +02:00
Convert tests/gio-test.c to g_test framework
This commit is contained in:
parent
da2702646c
commit
dbe8e52bde
135
tests/gio-test.c
135
tests/gio-test.c
@ -19,9 +19,6 @@
|
|||||||
* Just run it. Optional parameter is number of sub-processes.
|
* Just run it. Optional parameter is number of sub-processes.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#undef G_DISABLE_ASSERT
|
|
||||||
#undef G_LOG_DOMAIN
|
|
||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
|
||||||
#include <glib.h>
|
#include <glib.h>
|
||||||
@ -47,11 +44,11 @@
|
|||||||
static int nrunning;
|
static int nrunning;
|
||||||
static GMainLoop *main_loop;
|
static GMainLoop *main_loop;
|
||||||
|
|
||||||
#define BUFSIZE 5000 /* Larger than the circular buffer in
|
/* Larger than the circular buffer in giowin32.c on purpose */
|
||||||
* giowin32.c on purpose.
|
#define BUFSIZE 5000
|
||||||
*/
|
|
||||||
|
|
||||||
static int nkiddies;
|
static int nkiddies;
|
||||||
|
static char *exec_name;
|
||||||
|
|
||||||
static struct {
|
static struct {
|
||||||
int fd;
|
int fd;
|
||||||
@ -143,8 +140,7 @@ recv_message (GIOChannel *channel,
|
|||||||
shutdown_source (data);
|
shutdown_source (data);
|
||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
g_assert_cmpuint (nb, ==, sizeof (nbytes));
|
||||||
g_assert (nb == sizeof (nbytes));
|
|
||||||
|
|
||||||
for (i = 0; i < nkiddies; i++)
|
for (i = 0; i < nkiddies; i++)
|
||||||
if (seqtab[i].fd == fd)
|
if (seqtab[i].fd == fd)
|
||||||
@ -154,7 +150,8 @@ recv_message (GIOChannel *channel,
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
error = read_all (fd, channel, (gchar *) &nbytes, sizeof (nbytes), &nb);
|
error =
|
||||||
|
read_all (fd, channel, (gchar *) &nbytes, sizeof (nbytes), &nb);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (error != G_IO_ERROR_NONE)
|
if (error != G_IO_ERROR_NONE)
|
||||||
@ -166,11 +163,9 @@ recv_message (GIOChannel *channel,
|
|||||||
shutdown_source (data);
|
shutdown_source (data);
|
||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
g_assert_cmpuint (nb, ==, sizeof (nbytes));
|
||||||
|
|
||||||
g_assert (nb == sizeof (nbytes));
|
g_assert_cmpuint (nbytes, <, BUFSIZE);
|
||||||
|
|
||||||
g_assert_cmpint (nbytes, <, BUFSIZE);
|
|
||||||
g_assert (nbytes < BUFSIZE);
|
|
||||||
g_debug ("gio-test: ...from %d: %d bytes", fd, nbytes);
|
g_debug ("gio-test: ...from %d: %d bytes", fd, nbytes);
|
||||||
if (nbytes > 0)
|
if (nbytes > 0)
|
||||||
{
|
{
|
||||||
@ -187,7 +182,7 @@ recv_message (GIOChannel *channel,
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (j = 0; j < nbytes; j++)
|
for (j = 0; j < nbytes; j++)
|
||||||
g_assert (buf[j] == ' ' + (char) ((nbytes + j) % 95));
|
g_assert_cmpint (buf[j], ==, ' ' + (char) ((nbytes + j) % 95));
|
||||||
g_debug ("gio-test: ...from %d: OK", fd);
|
g_debug ("gio-test: ...from %d: OK", fd);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -195,7 +190,6 @@ recv_message (GIOChannel *channel,
|
|||||||
}
|
}
|
||||||
|
|
||||||
#ifdef G_OS_WIN32
|
#ifdef G_OS_WIN32
|
||||||
|
|
||||||
static gboolean
|
static gboolean
|
||||||
recv_windows_message (GIOChannel *channel,
|
recv_windows_message (GIOChannel *channel,
|
||||||
GIOCondition cond,
|
GIOCondition cond,
|
||||||
@ -242,20 +236,15 @@ window_procedure (HWND hwnd,
|
|||||||
hwnd, message, wparam, (gintptr) lparam);
|
hwnd, message, wparam, (gintptr) lparam);
|
||||||
return DefWindowProc (hwnd, message, wparam, lparam);
|
return DefWindowProc (hwnd, message, wparam, lparam);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
int
|
static void
|
||||||
main (int argc,
|
spawn_process (int children_nb)
|
||||||
char **argv)
|
|
||||||
{
|
{
|
||||||
if (argc < 3)
|
|
||||||
{
|
|
||||||
/* Parent */
|
|
||||||
|
|
||||||
GIOChannel *my_read_channel;
|
GIOChannel *my_read_channel;
|
||||||
gchar *cmdline;
|
gchar *cmdline;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
#ifdef G_OS_WIN32
|
#ifdef G_OS_WIN32
|
||||||
GTimeVal start, end;
|
GTimeVal start, end;
|
||||||
GPollFD pollfd;
|
GPollFD pollfd;
|
||||||
@ -264,12 +253,7 @@ main (int argc,
|
|||||||
static WNDCLASS wcl;
|
static WNDCLASS wcl;
|
||||||
HWND hwnd;
|
HWND hwnd;
|
||||||
GIOChannel *windows_messages_channel;
|
GIOChannel *windows_messages_channel;
|
||||||
#endif
|
|
||||||
|
|
||||||
nkiddies = (argc == 1 ? 1 : atoi(argv[1]));
|
|
||||||
seqtab = g_malloc (nkiddies * 2 * sizeof (int));
|
|
||||||
|
|
||||||
#ifdef G_OS_WIN32
|
|
||||||
wcl.style = 0;
|
wcl.style = 0;
|
||||||
wcl.lpfnWndProc = window_procedure;
|
wcl.lpfnWndProc = window_procedure;
|
||||||
wcl.cbClsExtra = 0;
|
wcl.cbClsExtra = 0;
|
||||||
@ -297,18 +281,24 @@ main (int argc,
|
|||||||
exit (1);
|
exit (1);
|
||||||
}
|
}
|
||||||
|
|
||||||
windows_messages_channel = g_io_channel_win32_new_messages ((guint) (guintptr) hwnd);
|
windows_messages_channel =
|
||||||
|
g_io_channel_win32_new_messages ((guint) (guintptr) hwnd);
|
||||||
g_io_add_watch (windows_messages_channel, G_IO_IN, recv_windows_message, 0);
|
g_io_add_watch (windows_messages_channel, G_IO_IN, recv_windows_message, 0);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
nkiddies = (children_nb > 0 ? children_nb : 1);
|
||||||
|
seqtab = g_malloc (nkiddies * 2 * sizeof (int));
|
||||||
|
|
||||||
for (i = 0; i < nkiddies; i++)
|
for (i = 0; i < nkiddies; i++)
|
||||||
{
|
{
|
||||||
int pipe_to_sub[2], pipe_from_sub[2];
|
|
||||||
guint *id;
|
guint *id;
|
||||||
|
int pipe_to_sub[2], pipe_from_sub[2];
|
||||||
|
|
||||||
if (pipe (pipe_to_sub) == -1 ||
|
if (pipe (pipe_to_sub) == -1 || pipe (pipe_from_sub) == -1)
|
||||||
pipe (pipe_from_sub) == -1)
|
{
|
||||||
perror ("pipe"), exit (1);
|
perror ("pipe");
|
||||||
|
exit (1);
|
||||||
|
}
|
||||||
|
|
||||||
seqtab[i].fd = pipe_from_sub[0];
|
seqtab[i].fd = pipe_from_sub[0];
|
||||||
seqtab[i].seq = 0;
|
seqtab[i].seq = 0;
|
||||||
@ -316,28 +306,27 @@ main (int argc,
|
|||||||
my_read_channel = g_io_channel_unix_new (pipe_from_sub[0]);
|
my_read_channel = g_io_channel_unix_new (pipe_from_sub[0]);
|
||||||
|
|
||||||
id = g_new (guint, 1);
|
id = g_new (guint, 1);
|
||||||
*id =
|
*id = g_io_add_watch_full (my_read_channel,
|
||||||
g_io_add_watch_full (my_read_channel,
|
|
||||||
G_PRIORITY_DEFAULT,
|
G_PRIORITY_DEFAULT,
|
||||||
G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP,
|
G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP,
|
||||||
recv_message,
|
recv_message,
|
||||||
id, g_free);
|
id, g_free);
|
||||||
|
|
||||||
nrunning++;
|
nrunning++;
|
||||||
|
|
||||||
#ifdef G_OS_WIN32
|
#ifdef G_OS_WIN32
|
||||||
cmdline = g_strdup_printf ("%d:%d:0x%p",
|
/* Spawn new Win32 process */
|
||||||
pipe_to_sub[0],
|
cmdline =
|
||||||
pipe_from_sub[1],
|
g_strdup_printf ("%d:%d:0x%p", pipe_to_sub[0], pipe_from_sub[1], hwnd);
|
||||||
hwnd);
|
_spawnl (_P_NOWAIT, exec_name, exec_name, "--child", cmdline, NULL);
|
||||||
_spawnl (_P_NOWAIT, argv[0], argv[0], "--child", cmdline, NULL);
|
|
||||||
#else
|
#else
|
||||||
cmdline = g_strdup_printf ("%s --child %d:%d &", argv[0],
|
/* Spawn new Unix process */
|
||||||
pipe_to_sub[0], pipe_from_sub[1]);
|
cmdline = g_strdup_printf ("%s --child %d:%d &",
|
||||||
|
exec_name, pipe_to_sub[0], pipe_from_sub[1]);
|
||||||
system (cmdline);
|
system (cmdline);
|
||||||
g_free (cmdline);
|
|
||||||
#endif
|
#endif
|
||||||
|
g_free (cmdline);
|
||||||
|
|
||||||
|
/* Closing pipes */
|
||||||
close (pipe_to_sub[0]);
|
close (pipe_to_sub[0]);
|
||||||
close (pipe_from_sub[1]);
|
close (pipe_from_sub[1]);
|
||||||
|
|
||||||
@ -350,46 +339,43 @@ main (int argc,
|
|||||||
end.tv_sec--, end.tv_usec += 1000000;
|
end.tv_sec--, end.tv_usec += 1000000;
|
||||||
g_print ("gio-test: had to wait %ld.%03ld s, result:%d\n",
|
g_print ("gio-test: had to wait %ld.%03ld s, result:%d\n",
|
||||||
end.tv_sec - start.tv_sec,
|
end.tv_sec - start.tv_sec,
|
||||||
(end.tv_usec - start.tv_usec) / 1000,
|
(end.tv_usec - start.tv_usec) / 1000, pollresult);
|
||||||
pollresult);
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
g_io_channel_unref (my_read_channel);
|
g_io_channel_unref (my_read_channel);
|
||||||
}
|
}
|
||||||
|
|
||||||
main_loop = g_main_loop_new (NULL, FALSE);
|
main_loop = g_main_loop_new (NULL, FALSE);
|
||||||
|
|
||||||
g_main_loop_run (main_loop);
|
g_main_loop_run (main_loop);
|
||||||
|
|
||||||
g_main_loop_unref (main_loop);
|
g_main_loop_unref (main_loop);
|
||||||
g_free (seqtab);
|
g_free (seqtab);
|
||||||
}
|
}
|
||||||
else if (argc == 3)
|
|
||||||
{
|
|
||||||
/* Child */
|
|
||||||
|
|
||||||
|
static void
|
||||||
|
run_process (int argc, char *argv[])
|
||||||
|
{
|
||||||
int readfd, writefd;
|
int readfd, writefd;
|
||||||
|
GTimeVal tv;
|
||||||
|
char buf[BUFSIZE];
|
||||||
|
int buflen, i, j, n;
|
||||||
#ifdef G_OS_WIN32
|
#ifdef G_OS_WIN32
|
||||||
HWND hwnd;
|
HWND hwnd;
|
||||||
#endif
|
#endif
|
||||||
int i, j;
|
|
||||||
char buf[BUFSIZE];
|
|
||||||
int buflen;
|
|
||||||
GTimeVal tv;
|
|
||||||
int n;
|
|
||||||
|
|
||||||
g_get_current_time (&tv);
|
g_get_current_time (&tv);
|
||||||
|
|
||||||
|
/* Extract parameters */
|
||||||
sscanf (argv[2], "%d:%d%n", &readfd, &writefd, &n);
|
sscanf (argv[2], "%d:%d%n", &readfd, &writefd, &n);
|
||||||
|
|
||||||
#ifdef G_OS_WIN32
|
#ifdef G_OS_WIN32
|
||||||
sscanf (argv[2] + n, ":0x%p", &hwnd);
|
sscanf (argv[2] + n, ":0x%p", &hwnd);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
srand (tv.tv_sec ^ (tv.tv_usec / 1000) ^ readfd ^ (writefd << 4));
|
srand (tv.tv_sec ^ (tv.tv_usec / 1000) ^ readfd ^ (writefd << 4));
|
||||||
|
|
||||||
for (i = 0; i < 20 + rand() % 20; i++)
|
for (i = 0; i < 20 + rand () % 10; i++)
|
||||||
{
|
{
|
||||||
g_usleep (100 + (rand() % 10) * 5000);
|
g_usleep ((100 + rand () % 10) * 2500);
|
||||||
buflen = rand () % BUFSIZE;
|
buflen = rand () % BUFSIZE;
|
||||||
for (j = 0; j < buflen; j++)
|
for (j = 0; j < buflen; j++)
|
||||||
buf[j] = ' ' + ((buflen + j) % 95);
|
buf[j] = ' ' + ((buflen + j) % 95);
|
||||||
@ -400,7 +386,7 @@ main (int argc,
|
|||||||
write (writefd, buf, buflen);
|
write (writefd, buf, buflen);
|
||||||
|
|
||||||
#ifdef G_OS_WIN32
|
#ifdef G_OS_WIN32
|
||||||
if (rand() % 100 < 5)
|
if (i % 10 == 0)
|
||||||
{
|
{
|
||||||
int msg = WM_USER + (rand () % 100);
|
int msg = WM_USER + (rand () % 100);
|
||||||
WPARAM wparam = rand ();
|
WPARAM wparam = rand ();
|
||||||
@ -414,8 +400,33 @@ main (int argc,
|
|||||||
g_debug ("gio-test: child exiting, closing %d", writefd);
|
g_debug ("gio-test: child exiting, closing %d", writefd);
|
||||||
close (writefd);
|
close (writefd);
|
||||||
}
|
}
|
||||||
else
|
|
||||||
g_print ("Huh?\n");
|
|
||||||
|
|
||||||
|
static void
|
||||||
|
test_io_basics (void)
|
||||||
|
{
|
||||||
|
spawn_process (1);
|
||||||
|
#ifndef G_OS_WIN32
|
||||||
|
spawn_process (5);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
main (int argc, char *argv[])
|
||||||
|
{
|
||||||
|
/* Get executable name */
|
||||||
|
exec_name = argv[0];
|
||||||
|
|
||||||
|
/* Run the tests */
|
||||||
|
g_test_init (&argc, &argv, NULL);
|
||||||
|
|
||||||
|
/* Run subprocess, if it is the case */
|
||||||
|
if (argc > 2)
|
||||||
|
{
|
||||||
|
run_process (argc, argv);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
g_test_add_func ("/gio/io-basics", test_io_basics);
|
||||||
|
|
||||||
|
return g_test_run ();
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user