tests: Use TestSignal rather than NameOwnerChanged to test signals

When testing that signals are delivered to the correct thread, and are
delivered the correct number of times, call `EmitSignal()` on the
`gdbus-testserver` to trigger a signal emission, and listen for that.

Previously, the code listened for `NameOwnerChanged` and connected to
the bus again to trigger emission of that. The problem with that is that
other things happening on the bus (for example, an old
`gdbus-testserver` instance disconnecting) can cause `NameOwnerChanged`
signal emissions. Sometimes, the `gdbus-threading` test was failing the
`signal_count == 1` assertion due to receiving more than one
`NameOwnerChanged` emission.

Signed-off-by: Philip Withnall <withnall@endlessm.com>

Helps: #1515
This commit is contained in:
Philip Withnall 2020-02-21 14:14:57 +00:00
parent cd0ab355cd
commit f74a987d21

View File

@ -137,7 +137,6 @@ test_delivery_in_thread_func (gpointer _data)
DeliveryData data; DeliveryData data;
GCancellable *ca; GCancellable *ca;
guint subscription_id; guint subscription_id;
GDBusConnection *priv_c;
GError *error = NULL; GError *error = NULL;
GVariant *result_variant = NULL; GVariant *result_variant = NULL;
@ -237,15 +236,14 @@ test_delivery_in_thread_func (gpointer _data)
/* /*
* Check that signals are delivered to the correct thread. * Check that signals are delivered to the correct thread.
* *
* First we subscribe to the signal, then we create a a private * First we subscribe to the signal, then we call EmitSignal(). This should
* connection. This should cause a NameOwnerChanged message from * cause a TestSignal emission from the testserver.
* the message bus.
*/ */
subscription_id = g_dbus_connection_signal_subscribe (c, subscription_id = g_dbus_connection_signal_subscribe (c,
"org.freedesktop.DBus", /* sender */ "com.example.TestService", /* sender */
"org.freedesktop.DBus", /* interface */ "com.example.Frob", /* interface */
"NameOwnerChanged", /* member */ "TestSignal", /* member */
"/org/freedesktop/DBus", /* path */ "/com/example/TestObject", /* path */
NULL, NULL,
G_DBUS_SIGNAL_FLAGS_NONE, G_DBUS_SIGNAL_FLAGS_NONE,
signal_handler, signal_handler,
@ -254,15 +252,28 @@ test_delivery_in_thread_func (gpointer _data)
g_assert_cmpuint (subscription_id, !=, 0); g_assert_cmpuint (subscription_id, !=, 0);
g_assert_cmpuint (data.signal_count, ==, 0); g_assert_cmpuint (data.signal_count, ==, 0);
priv_c = _g_bus_get_priv (G_BUS_TYPE_SESSION, NULL, &error); g_dbus_connection_call (c,
g_assert_no_error (error); "com.example.TestService", /* bus_name */
g_assert_nonnull (priv_c); "/com/example/TestObject", /* object path */
"com.example.Frob", /* interface name */
while (data.signal_count < 1) "EmitSignal", /* method name */
g_variant_new_parsed ("('hello', @o '/com/example/TestObject')"),
NULL,
G_DBUS_CALL_FLAGS_NONE,
-1,
NULL,
(GAsyncReadyCallback) async_result_cb,
&data);
while (data.async_result == NULL || data.signal_count < 1)
g_main_context_iteration (thread_context, TRUE); g_main_context_iteration (thread_context, TRUE);
g_assert_cmpuint (data.signal_count, ==, 1);
g_object_unref (priv_c); result_variant = g_dbus_connection_call_finish (c, data.async_result, &error);
g_assert_no_error (error);
g_assert_nonnull (result_variant);
g_clear_pointer (&result_variant, g_variant_unref);
g_clear_object (&data.async_result);
g_assert_cmpuint (data.signal_count, ==, 1);
g_dbus_connection_signal_unsubscribe (c, subscription_id); g_dbus_connection_signal_unsubscribe (c, subscription_id);
subscription_id = 0; subscription_id = 0;