[glib/wip/smcv/disable-nonce-tcp-server: 1/4] gdbus-peer test: Create server GMainContext from main thread



commit 5c19eb4e39b14b77eb2cdeffce78150fb4423051
Author: Simon McVittie <smcv collabora com>
Date:   Mon Nov 4 12:10:16 2019 +0000

    gdbus-peer test: Create server GMainContext from main thread
    
    Signed-off-by: Simon McVittie <smcv collabora com>

 gio/tests/gdbus-peer.c | 81 ++++++++++++++++++++++++++------------------------
 1 file changed, 42 insertions(+), 39 deletions(-)
---
diff --git a/gio/tests/gdbus-peer.c b/gio/tests/gdbus-peer.c
index 7887ab917..770c6978b 100644
--- a/gio/tests/gdbus-peer.c
+++ b/gio/tests/gdbus-peer.c
@@ -69,11 +69,13 @@ static GMainLoop *loop = NULL;
 
 typedef struct
 {
+  GMainContext *service_context;
   gboolean accept_connection;
   gint num_connection_attempts;
   GPtrArray *current_connections;
   guint num_method_calls;
   gboolean signal_received;
+  gboolean seen_connection;
 } PeerData;
 
 static const gchar *test_interface_introspection_xml =
@@ -435,15 +437,13 @@ static gpointer
 service_thread_func (gpointer user_data)
 {
   PeerData *data = user_data;
-  GMainContext *service_context;
   GDBusAuthObserver *observer, *o;
   GError *error;
   GDBusServerFlags f;
   gchar *a, *g;
   gboolean b;
 
-  service_context = g_main_context_new ();
-  g_main_context_push_thread_default (service_context);
+  g_main_context_push_thread_default (data->service_context);
 
   error = NULL;
   observer = g_dbus_auth_observer_new ();
@@ -486,12 +486,11 @@ service_thread_func (gpointer user_data)
 
   g_dbus_server_start (server);
 
-  run_service_loop (service_context);
+  run_service_loop (data->service_context);
 
-  g_main_context_pop_thread_default (service_context);
+  g_main_context_pop_thread_default (data->service_context);
 
   teardown_service_loop ();
-  g_main_context_unref (service_context);
 
   /* test code specifically unrefs the server - see below */
   g_assert (server == NULL);
@@ -553,13 +552,11 @@ on_incoming_connection (GSocketService     *service,
 static gpointer
 service_thread_func (gpointer data)
 {
-  GMainContext *service_context;
   gchar *socket_path;
   GSocketAddress *address;
   GError *error;
 
-  service_context = g_main_context_new ();
-  g_main_context_push_thread_default (service_context);
+  g_main_context_push_thread_default (data->service_context);
 
   socket_path = g_strdup_printf ("/tmp/gdbus-test-pid-%d", getpid ());
   address = g_unix_socket_address_new (socket_path);
@@ -580,12 +577,11 @@ service_thread_func (gpointer data)
                     data);
   g_socket_service_start (service);
 
-  run_service_loop (service_context);
+  run_service_loop (data->service_context);
 
-  g_main_context_pop_thread_default (service_context);
+  g_main_context_pop_thread_default (data->service_context);
 
   teardown_service_loop ();
-  g_main_context_unref (service_context);
 
   g_object_unref (address);
   g_free (socket_path);
@@ -719,6 +715,7 @@ do_test_peer (void)
   gulong signal_handler_id;
 
   memset (&data, '\0', sizeof (PeerData));
+  data.service_context = g_main_context_new ();
   data.current_connections = g_ptr_array_new_with_free_func (g_object_unref);
 
   /* first try to connect when there is no server */
@@ -1024,6 +1021,7 @@ do_test_peer (void)
 
   g_main_loop_quit (service_loop);
   g_thread_join (service_thread);
+  g_main_context_unref (data.service_context);
 }
 
 static void
@@ -1071,6 +1069,7 @@ test_peer_signals (void)
 
   setup_test_address ();
   memset (&data, '\0', sizeof (PeerData));
+  data.service_context = g_main_context_new ();
   data.current_connections = g_ptr_array_new_with_free_func (g_object_unref);
 
   /* bring up a server - we run the server in a different thread to avoid deadlocks */
@@ -1127,6 +1126,7 @@ test_peer_signals (void)
 
   g_main_loop_unref (loop);
   g_free (test_guid);
+  g_main_context_unref (data.service_context);
 }
 
 /* ---------------------------------------------------------------------------------------------------- */
@@ -1232,7 +1232,6 @@ dmp_thread_func (gpointer user_data)
   GError *error;
   gchar *guid;
 
-  data->context = g_main_context_new ();
   g_main_context_push_thread_default (data->context);
 
   error = NULL;
@@ -1275,6 +1274,7 @@ delayed_message_processing (void)
   setup_test_address ();
 
   data = g_new0 (DmpData, 1);
+  data->context = g_main_context_new ();
 
   service_thread = g_thread_new ("dmp",
                                  dmp_thread_func,
@@ -1366,12 +1366,10 @@ static gpointer
 nonce_tcp_service_thread_func (gpointer user_data)
 {
   PeerData *data = user_data;
-  GMainContext *service_context;
   GDBusAuthObserver *observer;
   GError *error;
 
-  service_context = g_main_context_new ();
-  g_main_context_push_thread_default (service_context);
+  g_main_context_push_thread_default (data->service_context);
 
   error = NULL;
   observer = g_dbus_auth_observer_new ();
@@ -1395,12 +1393,11 @@ nonce_tcp_service_thread_func (gpointer user_data)
 
   g_dbus_server_start (server);
 
-  run_service_loop (service_context);
+  run_service_loop (data->service_context);
 
-  g_main_context_pop_thread_default (service_context);
+  g_main_context_pop_thread_default (data->service_context);
 
   teardown_service_loop ();
-  g_main_context_unref (service_context);
 
   /* test code specifically unrefs the server - see below */
   g_assert (server == NULL);
@@ -1424,6 +1421,7 @@ test_nonce_tcp (void)
   loop = g_main_loop_new (NULL, FALSE);
 
   memset (&data, '\0', sizeof (PeerData));
+  data.service_context = g_main_context_new ();
   data.current_connections = g_ptr_array_new_with_free_func (g_object_unref);
 
   error = NULL;
@@ -1533,6 +1531,7 @@ test_nonce_tcp (void)
 
   g_main_loop_unref (loop);
   g_free (test_guid);
+  g_main_context_unref (data.service_context);
 }
 
 static void
@@ -1568,20 +1567,18 @@ tcp_anonymous_on_new_connection (GDBusServer     *server,
                                  GDBusConnection *connection,
                                  gpointer         user_data)
 {
-  gboolean *seen_connection = user_data;
-  *seen_connection = TRUE;
+  PeerData *data = user_data;
+  data->seen_connection = TRUE;
   return TRUE;
 }
 
 static gpointer
 tcp_anonymous_service_thread_func (gpointer user_data)
 {
-  gboolean *seen_connection = user_data;
-  GMainContext *service_context;
+  PeerData *data = user_data;
   GError *error;
 
-  service_context = g_main_context_new ();
-  g_main_context_push_thread_default (service_context);
+  g_main_context_push_thread_default (data->service_context);
 
   error = NULL;
   server = g_dbus_server_new_sync ("tcp:",
@@ -1595,16 +1592,15 @@ tcp_anonymous_service_thread_func (gpointer user_data)
   g_signal_connect (server,
                     "new-connection",
                     G_CALLBACK (tcp_anonymous_on_new_connection),
-                    seen_connection);
+                    data);
 
   g_dbus_server_start (server);
 
-  run_service_loop (service_context);
+  run_service_loop (data->service_context);
 
-  g_main_context_pop_thread_default (service_context);
+  g_main_context_pop_thread_default (data->service_context);
 
   teardown_service_loop ();
-  g_main_context_unref (service_context);
 
   return NULL;
 }
@@ -1612,18 +1608,21 @@ tcp_anonymous_service_thread_func (gpointer user_data)
 static void
 test_tcp_anonymous (void)
 {
-  gboolean seen_connection;
+  PeerData data;
   GThread *service_thread;
   GDBusConnection *connection;
   GError *error;
 
+  memset (&data, '\0', sizeof (PeerData));
+  data.service_context = g_main_context_new ();
+  data.seen_connection = FALSE;
+
   test_guid = g_dbus_generate_guid ();
   loop = g_main_loop_new (NULL, FALSE);
 
-  seen_connection = FALSE;
   service_thread = g_thread_new ("tcp-anon-service",
                                  tcp_anonymous_service_thread_func,
-                                 &seen_connection);
+                                 &data);
   await_service_loop ();
   g_assert (server != NULL);
 
@@ -1636,7 +1635,7 @@ test_tcp_anonymous (void)
   g_assert_no_error (error);
   g_assert (connection != NULL);
 
-  while (!seen_connection)
+  while (!data.seen_connection)
     g_thread_yield ();
 
   g_object_unref (connection);
@@ -1650,6 +1649,7 @@ test_tcp_anonymous (void)
 
   g_main_loop_unref (loop);
   g_free (test_guid);
+  g_main_context_unref (data.service_context);
 }
 
 /* ---------------------------------------------------------------------------------------------------- */
@@ -1732,12 +1732,11 @@ codegen_on_new_connection (GDBusServer *server,
 static gpointer
 codegen_service_thread_func (gpointer user_data)
 {
-  GMainContext   *service_context;
+  PeerData *peer_data = user_data;
   ExampleAnimal  *animal;
   GError         *error = NULL;
 
-  service_context = g_main_context_new ();
-  g_main_context_push_thread_default (service_context);
+  g_main_context_push_thread_default (data->service_context);
 
   /* Create the animal in the right thread context */
   animal = example_animal_skeleton_new ();
@@ -1760,14 +1759,13 @@ codegen_service_thread_func (gpointer user_data)
                     G_CALLBACK (codegen_on_new_connection),
                     animal);
 
-  run_service_loop (service_context);
+  run_service_loop (data->service_context);
 
   g_object_unref (animal);
 
-  g_main_context_pop_thread_default (service_context);
+  g_main_context_pop_thread_default (data->service_context);
 
   teardown_service_loop ();
-  g_main_context_unref (service_context);
 
   g_dbus_server_stop (codegen_server);
   g_object_unref (codegen_server);
@@ -1787,6 +1785,7 @@ codegen_quit_mainloop_timeout (gpointer data)
 static void
 codegen_test_peer (void)
 {
+  PeerData data;
   GDBusConnection     *connection;
   ExampleAnimal       *animal1, *animal2;
   GThread             *service_thread;
@@ -1794,6 +1793,9 @@ codegen_test_peer (void)
   GVariant            *value;
   const gchar         *s;
 
+  memset (&data, '\0', sizeof (PeerData));
+  data.service_context = g_main_context_new ();
+
   test_guid = g_dbus_generate_guid ();
   loop = g_main_loop_new (NULL, FALSE);
 
@@ -1907,6 +1909,7 @@ codegen_test_peer (void)
 
   g_main_loop_unref (loop);
   g_free (test_guid);
+  g_main_context_unref (data.service_context);
 }
 
 /* ---------------------------------------------------------------------------------------------------- */


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