[glib] Add socket postmortem test



commit c5202bc5e9201acdaa207fb9ca5ba64f78fd1b3d
Author: Michael Catanzaro <mcatanzaro igalia com>
Date:   Mon Oct 23 10:06:48 2017 -0500

    Add socket postmortem test
    
    Checks that after a GSocket is closed, a source created off it
    with g_socket_create_source() will dispatch exactly once with
    G_IO_NVAL.
    
    Based on a patch by Mikhail Zabaluev
    
    https://bugzilla.gnome.org/show_bug.cgi?id=723655

 gio/tests/socket.c |   48 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 48 insertions(+), 0 deletions(-)
---
diff --git a/gio/tests/socket.c b/gio/tests/socket.c
index 4343dd5..0835a66 100644
--- a/gio/tests/socket.c
+++ b/gio/tests/socket.c
@@ -1386,6 +1386,53 @@ test_unix_connection_ancillary_data (void)
    * g_unix_connection_receive_credentials().
    */
 }
+
+static gboolean
+postmortem_source_cb (GSocket      *socket,
+                      GIOCondition  condition,
+                      gpointer      user_data)
+{
+  gboolean *been_here = user_data;
+
+  g_assert_cmpint (condition, ==, G_IO_NVAL);
+
+  *been_here = TRUE;
+  return FALSE;
+}
+
+static void
+test_source_postmortem (void)
+{
+  GMainContext *context;
+  GSocket *socket;
+  GSource *source;
+  GError *error = NULL;
+  gboolean callback_visited = FALSE;
+
+  socket = g_socket_new (G_SOCKET_FAMILY_UNIX, G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_DEFAULT, &error);
+  g_assert_no_error (error);
+
+  context = g_main_context_new ();
+
+  source = g_socket_create_source (socket, G_IO_IN, NULL);
+  g_source_set_callback (source, (GSourceFunc) postmortem_source_cb,
+                         &callback_visited, NULL);
+  g_source_attach (source, context);
+  g_source_unref (source);
+
+  g_socket_close (socket, &error);
+  g_assert_no_error (error);
+  g_object_unref (socket);
+
+  /* Test that, after a socket is closed, its source callback should be called
+   * exactly once. */
+  g_main_context_iteration (context, FALSE);
+  g_assert (callback_visited);
+  g_assert (!g_main_context_pending (context));
+
+  g_main_context_unref (context);
+}
+
 #endif /* G_OS_UNIX */
 
 static void
@@ -1643,6 +1690,7 @@ main (int   argc,
   g_test_add_func ("/socket/unix-from-fd", test_unix_from_fd);
   g_test_add_func ("/socket/unix-connection", test_unix_connection);
   g_test_add_func ("/socket/unix-connection-ancillary-data", test_unix_connection_ancillary_data);
+  g_test_add_func ("/socket/source-postmortem", test_source_postmortem);
 #endif
   g_test_add_func ("/socket/reuse/tcp", test_reuse_tcp);
   g_test_add_func ("/socket/reuse/udp", test_reuse_udp);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]