Merge branch 'backport-1711-socket-fix-glib-2-66' into 'glib-2-66'

Backport !1711 “Fix race in socketclient-slow test” to glib-2-66

See merge request GNOME/glib!1723
This commit is contained in:
Emmanuele Bassi 2020-10-27 12:09:21 +00:00
commit cfbab734d1

View File

@ -79,23 +79,26 @@ on_connected_cancelled (GObject *source_object,
g_main_loop_quit (user_data); g_main_loop_quit (user_data);
} }
static int typedef struct
on_timer (GCancellable *cancel)
{ {
g_cancellable_cancel (cancel); GCancellable *cancellable;
return G_SOURCE_REMOVE; gboolean completed;
} } EventCallbackData;
static void static void
on_event (GSocketClient *client, on_event (GSocketClient *client,
GSocketClientEvent event, GSocketClientEvent event,
GSocketConnectable *connectable, GSocketConnectable *connectable,
GIOStream *connection, GIOStream *connection,
gboolean *got_completed_event) EventCallbackData *data)
{ {
if (event == G_SOCKET_CLIENT_COMPLETE) if (data->cancellable && event == G_SOCKET_CLIENT_CONNECTED)
{ {
*got_completed_event = TRUE; g_cancellable_cancel (data->cancellable);
}
else if (event == G_SOCKET_CLIENT_COMPLETE)
{
data->completed = TRUE;
g_assert_null (connection); g_assert_null (connection);
} }
} }
@ -108,8 +111,7 @@ test_happy_eyeballs_cancel_delayed (void)
GError *error = NULL; GError *error = NULL;
guint16 port; guint16 port;
GMainLoop *loop; GMainLoop *loop;
GCancellable *cancel; EventCallbackData data = { NULL, FALSE };
gboolean got_completed_event = FALSE;
/* This just tests that cancellation works as expected, still emits the completed signal, /* This just tests that cancellation works as expected, still emits the completed signal,
* and never returns a connection */ * and never returns a connection */
@ -122,17 +124,16 @@ test_happy_eyeballs_cancel_delayed (void)
g_socket_service_start (service); g_socket_service_start (service);
client = g_socket_client_new (); client = g_socket_client_new ();
cancel = g_cancellable_new (); data.cancellable = g_cancellable_new ();
g_socket_client_connect_to_host_async (client, "localhost", port, cancel, on_connected_cancelled, loop); g_socket_client_connect_to_host_async (client, "localhost", port, data.cancellable, on_connected_cancelled, loop);
g_timeout_add (1, (GSourceFunc) on_timer, cancel); g_signal_connect (client, "event", G_CALLBACK (on_event), &data);
g_signal_connect (client, "event", G_CALLBACK (on_event), &got_completed_event);
g_main_loop_run (loop); g_main_loop_run (loop);
g_assert_true (got_completed_event); g_assert_true (data.completed);
g_main_loop_unref (loop); g_main_loop_unref (loop);
g_object_unref (service); g_object_unref (service);
g_object_unref (client); g_object_unref (client);
g_object_unref (cancel); g_object_unref (data.cancellable);
} }
static void static void
@ -144,7 +145,7 @@ test_happy_eyeballs_cancel_instant (void)
guint16 port; guint16 port;
GMainLoop *loop; GMainLoop *loop;
GCancellable *cancel; GCancellable *cancel;
gboolean got_completed_event = FALSE; EventCallbackData data = { NULL, FALSE };
/* This tests the same things as above, test_happy_eyeballs_cancel_delayed(), but /* This tests the same things as above, test_happy_eyeballs_cancel_delayed(), but
* with different timing since it sends an already cancelled cancellable */ * with different timing since it sends an already cancelled cancellable */
@ -160,10 +161,10 @@ test_happy_eyeballs_cancel_instant (void)
cancel = g_cancellable_new (); cancel = g_cancellable_new ();
g_cancellable_cancel (cancel); g_cancellable_cancel (cancel);
g_socket_client_connect_to_host_async (client, "localhost", port, cancel, on_connected_cancelled, loop); g_socket_client_connect_to_host_async (client, "localhost", port, cancel, on_connected_cancelled, loop);
g_signal_connect (client, "event", G_CALLBACK (on_event), &got_completed_event); g_signal_connect (client, "event", G_CALLBACK (on_event), &data);
g_main_loop_run (loop); g_main_loop_run (loop);
g_assert_true (got_completed_event); g_assert_true (data.completed);
g_main_loop_unref (loop); g_main_loop_unref (loop);
g_object_unref (service); g_object_unref (service);
g_object_unref (client); g_object_unref (client);