gdbus-peer test: Create server GMainContext from main thread

Signed-off-by: Simon McVittie <smcv@collabora.com>
This commit is contained in:
Simon McVittie
2019-11-04 12:10:16 +00:00
parent d97d4828d0
commit 5c19eb4e39

View File

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