Merge branch 'wip/pwithnall/fix-pollable-test' into 'main'

tests: Fix a potential race condition in pollable test

See merge request GNOME/glib!2702
This commit is contained in:
Philip Withnall 2022-06-01 13:15:32 +00:00
commit 972df061a7

View File

@ -38,10 +38,6 @@
#error Should have been able to find openpty on GNU/Linux #error Should have been able to find openpty on GNU/Linux
#endif #endif
static GMainLoop *loop;
static GPollableInputStream *in;
static GOutputStream *out;
static gboolean static gboolean
poll_source_callback (GPollableInputStream *input, poll_source_callback (GPollableInputStream *input,
gpointer user_data) gpointer user_data)
@ -64,49 +60,56 @@ poll_source_callback (GPollableInputStream *input,
} }
static gboolean static gboolean
check_source_readability_callback (gpointer user_data) check_source_not_readable_callback (gpointer user_data)
{ {
gboolean expected = GPOINTER_TO_INT (user_data); GPollableInputStream *in = G_POLLABLE_INPUT_STREAM (user_data);
gboolean readable;
g_assert_false (g_pollable_input_stream_is_readable (in));
readable = g_pollable_input_stream_is_readable (in);
g_assert_cmpint (readable, ==, expected);
return G_SOURCE_REMOVE; return G_SOURCE_REMOVE;
} }
typedef struct
{
GPollableInputStream *in; /* (unowned) */
GOutputStream *out; /* (unowned) */
} Streams;
static gboolean static gboolean
write_callback (gpointer user_data) write_callback (gpointer user_data)
{ {
Streams *streams = user_data;
const char *buf = "x"; const char *buf = "x";
gssize nwrote; gssize nwrote;
GError *error = NULL; GError *error = NULL;
g_assert_true (g_pollable_output_stream_is_writable (G_POLLABLE_OUTPUT_STREAM (out))); g_assert_true (g_pollable_output_stream_is_writable (G_POLLABLE_OUTPUT_STREAM (streams->out)));
nwrote = g_output_stream_write (out, buf, 2, NULL, &error); nwrote = g_output_stream_write (streams->out, buf, 2, NULL, &error);
g_assert_no_error (error); g_assert_no_error (error);
g_assert_cmpint (nwrote, ==, 2); g_assert_cmpint (nwrote, ==, 2);
g_assert_true (g_pollable_output_stream_is_writable (G_POLLABLE_OUTPUT_STREAM (out))); g_assert_true (g_pollable_output_stream_is_writable (G_POLLABLE_OUTPUT_STREAM (streams->out)));
/* Give the pipe a few ticks to propagate the write for sockets. On my /* Wait for the pipe to propagate the write for sockets. */
* iMac i7, 40 works, 30 doesn't. */ while (!g_pollable_input_stream_is_readable (streams->in));
g_usleep (80L); g_assert_true (g_pollable_input_stream_is_readable (streams->in));
check_source_readability_callback (GINT_TO_POINTER (TRUE));
return G_SOURCE_REMOVE; return G_SOURCE_REMOVE;
} }
static gboolean static gboolean
check_source_and_quit_callback (gpointer user_data) quit_callback (gpointer user_data)
{ {
check_source_readability_callback (user_data); GMainLoop *loop = user_data;
g_main_loop_quit (loop); g_main_loop_quit (loop);
return G_SOURCE_REMOVE; return G_SOURCE_REMOVE;
} }
static void static void
test_streams (void) test_streams (GPollableInputStream *in,
GOutputStream *out)
{ {
gboolean readable; gboolean readable;
GError *error = NULL; GError *error = NULL;
@ -114,12 +117,14 @@ test_streams (void)
gssize nread; gssize nread;
GSource *poll_source; GSource *poll_source;
gboolean success = FALSE; gboolean success = FALSE;
Streams streams;
GMainLoop *loop = NULL;
g_assert (g_pollable_input_stream_can_poll (in)); g_assert_true (g_pollable_input_stream_can_poll (in));
g_assert (g_pollable_output_stream_can_poll (G_POLLABLE_OUTPUT_STREAM (out))); g_assert_true (g_pollable_output_stream_can_poll (G_POLLABLE_OUTPUT_STREAM (out)));
readable = g_pollable_input_stream_is_readable (in); readable = g_pollable_input_stream_is_readable (in);
g_assert (!readable); g_assert_false (readable);
nread = g_pollable_input_stream_read_nonblocking (in, buf, 1, NULL, &error); nread = g_pollable_input_stream_read_nonblocking (in, buf, 1, NULL, &error);
g_assert_cmpint (nread, ==, -1); g_assert_cmpint (nread, ==, -1);
@ -147,11 +152,15 @@ test_streams (void)
g_source_attach (poll_source, NULL); g_source_attach (poll_source, NULL);
g_source_unref (poll_source); g_source_unref (poll_source);
g_idle_add_full (2, check_source_readability_callback, GINT_TO_POINTER (FALSE), NULL); streams.in = in;
g_idle_add_full (3, write_callback, NULL, NULL); streams.out = out;
g_idle_add_full (4, check_source_and_quit_callback, GINT_TO_POINTER (FALSE), NULL);
loop = g_main_loop_new (NULL, FALSE); loop = g_main_loop_new (NULL, FALSE);
g_idle_add_full (2, check_source_not_readable_callback, in, NULL);
g_idle_add_full (3, write_callback, &streams, NULL);
g_idle_add_full (4, check_source_not_readable_callback, in, NULL);
g_idle_add_full (5, quit_callback, loop, NULL);
g_main_loop_run (loop); g_main_loop_run (loop);
g_main_loop_unref (loop); g_main_loop_unref (loop);
@ -162,11 +171,14 @@ test_streams (void)
#define g_assert_not_pollable(fd) \ #define g_assert_not_pollable(fd) \
G_STMT_START { \ G_STMT_START { \
GPollableInputStream *in = NULL; \
GOutputStream *out = NULL; \
\
in = G_POLLABLE_INPUT_STREAM (g_unix_input_stream_new (fd, FALSE)); \ in = G_POLLABLE_INPUT_STREAM (g_unix_input_stream_new (fd, FALSE)); \
out = g_unix_output_stream_new (fd, FALSE); \ out = g_unix_output_stream_new (fd, FALSE); \
\ \
g_assert (!g_pollable_input_stream_can_poll (in)); \ g_assert_false (g_pollable_input_stream_can_poll (in)); \
g_assert (!g_pollable_output_stream_can_poll ( \ g_assert_false (g_pollable_output_stream_can_poll ( \
G_POLLABLE_OUTPUT_STREAM (out))); \ G_POLLABLE_OUTPUT_STREAM (out))); \
\ \
g_clear_object (&in); \ g_clear_object (&in); \
@ -177,6 +189,8 @@ static void
test_pollable_unix_pipe (void) test_pollable_unix_pipe (void)
{ {
int pipefds[2], status; int pipefds[2], status;
GPollableInputStream *in = NULL;
GOutputStream *out = NULL;
g_test_summary ("Test that pipes are considered pollable, just like sockets"); g_test_summary ("Test that pipes are considered pollable, just like sockets");
@ -186,7 +200,7 @@ test_pollable_unix_pipe (void)
in = G_POLLABLE_INPUT_STREAM (g_unix_input_stream_new (pipefds[0], TRUE)); in = G_POLLABLE_INPUT_STREAM (g_unix_input_stream_new (pipefds[0], TRUE));
out = g_unix_output_stream_new (pipefds[1], TRUE); out = g_unix_output_stream_new (pipefds[1], TRUE);
test_streams (); test_streams (in, out);
g_object_unref (in); g_object_unref (in);
g_object_unref (out); g_object_unref (out);
@ -195,6 +209,8 @@ test_pollable_unix_pipe (void)
static void static void
test_pollable_unix_pty (void) test_pollable_unix_pty (void)
{ {
GPollableInputStream *in = NULL;
GOutputStream *out = NULL;
#ifdef HAVE_OPENPTY #ifdef HAVE_OPENPTY
int a, b, status; int a, b, status;
#endif #endif
@ -213,7 +229,7 @@ test_pollable_unix_pty (void)
in = G_POLLABLE_INPUT_STREAM (g_unix_input_stream_new (a, TRUE)); in = G_POLLABLE_INPUT_STREAM (g_unix_input_stream_new (a, TRUE));
out = g_unix_output_stream_new (b, TRUE); out = g_unix_output_stream_new (b, TRUE);
test_streams (); test_streams (in, out);
g_object_unref (in); g_object_unref (in);
g_object_unref (out); g_object_unref (out);
@ -272,6 +288,8 @@ test_pollable_converter (void)
GError *error = NULL; GError *error = NULL;
GInputStream *ibase; GInputStream *ibase;
int pipefds[2], status; int pipefds[2], status;
GPollableInputStream *in = NULL;
GOutputStream *out = NULL;
status = pipe (pipefds); status = pipe (pipefds);
g_assert_cmpint (status, ==, 0); g_assert_cmpint (status, ==, 0);
@ -286,7 +304,7 @@ test_pollable_converter (void)
out = g_unix_output_stream_new (pipefds[1], TRUE); out = g_unix_output_stream_new (pipefds[1], TRUE);
test_streams (); test_streams (in, out);
g_object_unref (in); g_object_unref (in);
g_object_unref (out); g_object_unref (out);
@ -329,6 +347,8 @@ test_pollable_socket (void)
GSocketClient *client; GSocketClient *client;
GError *error = NULL; GError *error = NULL;
GSocketConnection *client_conn = NULL, *server_conn = NULL; GSocketConnection *client_conn = NULL, *server_conn = NULL;
GPollableInputStream *in = NULL;
GOutputStream *out = NULL;
iaddr = g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4); iaddr = g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4);
saddr = g_inet_socket_address_new (iaddr, 0); saddr = g_inet_socket_address_new (iaddr, 0);
@ -358,7 +378,7 @@ test_pollable_socket (void)
in = G_POLLABLE_INPUT_STREAM (g_io_stream_get_input_stream (G_IO_STREAM (client_conn))); in = G_POLLABLE_INPUT_STREAM (g_io_stream_get_input_stream (G_IO_STREAM (client_conn)));
out = g_io_stream_get_output_stream (G_IO_STREAM (server_conn)); out = g_io_stream_get_output_stream (G_IO_STREAM (server_conn));
test_streams (); test_streams (in, out);
g_object_unref (client_conn); g_object_unref (client_conn);
g_object_unref (server_conn); g_object_unref (server_conn);