Skip to content

Commit

Permalink
[unit] Fixed occasional unit test failures. JB#48413
Browse files Browse the repository at this point in the history
Need to make sure that D-Bus calls are submitted after the
target object is registered. Otherwise there's a chance to
get org.freedesktop.DBus.Error.UnknownMethod
  • Loading branch information
monich committed May 22, 2020
1 parent 57c7924 commit ab5acbd
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 28 deletions.
65 changes: 55 additions & 10 deletions unit/common/test_dbus.c
@@ -1,6 +1,6 @@
/*
* Copyright (C) 2019 Jolla Ltd.
* Copyright (C) 2019 Slava Monich <slava.monich@jolla.com>
* Copyright (C) 2019-2020 Jolla Ltd.
* Copyright (C) 2019-2020 Slava Monich <slava.monich@jolla.com>
*
* You may use this file under the terms of BSD license as follows:
*
Expand Down Expand Up @@ -43,9 +43,45 @@ struct test_dbus {
GDBusServer* server;
GDBusAuthObserver* observer;
TestDBusStartFunc start;
TestDBusStartFunc start2;
void* user_data;
gboolean started;
guint start2_id;
};

static
gboolean
test_dbus_start2(
gpointer user_data)
{
TestDBus* self = user_data;

GDEBUG("Starting test stage 2");
self->start2_id = 0;
self->start2(self->client_connection, self->server_connection,
self->user_data);
return G_SOURCE_REMOVE;
}

static
void
test_dbus_start(
TestDBus* self)
{
if ((self->start || self->start2) && !self->started) {
self->started = TRUE;
if (self->start) {
GDEBUG("Starting the test");
self->start(self->client_connection, self->server_connection,
self->user_data);
}
if (self->start2) {
self->start2_id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE,
test_dbus_start2, self, NULL);
}
}
}

static
void
test_dbus_client_connection(
Expand All @@ -59,10 +95,8 @@ test_dbus_client_connection(
g_assert(!self->client_connection);
self->client_connection = g_dbus_connection_new_finish(res, NULL);
g_assert(self->client_connection);

if (self->client_connection && self->server_connection && self->start) {
self->start(self->client_connection, self->server_connection,
self->user_data);
if (self->client_connection && self->server_connection && !self->started) {
test_dbus_start(self);
}
}

Expand All @@ -78,10 +112,8 @@ test_dbus_server_connection(
GDEBUG("Got server connection");
g_assert(!self->server_connection);
g_object_ref(self->server_connection = connection);

if (self->client_connection && self->server_connection && self->start) {
self->start(self->client_connection, self->server_connection,
self->user_data);
if (self->client_connection && self->server_connection && !self->started) {
test_dbus_start(self);
}
return TRUE;
}
Expand All @@ -102,13 +134,23 @@ TestDBus*
test_dbus_new(
TestDBusStartFunc start,
void* user_data)
{
return test_dbus_new2(start, NULL, user_data);
}

TestDBus*
test_dbus_new2(
TestDBusStartFunc start,
TestDBusStartFunc start2,
void* user_data)
{
TestDBus* self = g_new0(TestDBus, 1);
char* guid = g_dbus_generate_guid();
char* tmpaddr;
const char* client_addr;

self->start = start;
self->start2 = start2;
self->user_data = user_data;
self->tmpdir = g_dir_make_tmp("test_dbus_XXXXXX", NULL);
tmpaddr = g_strconcat("unix:tmpdir=", self->tmpdir, NULL);
Expand Down Expand Up @@ -141,6 +183,9 @@ test_dbus_free(
TestDBus* self)
{
if (self) {
if (self->start2_id) {
g_source_remove(self->start2_id);
}
if (self->client_connection) {
g_object_unref(self->client_connection);
}
Expand Down
10 changes: 8 additions & 2 deletions unit/common/test_dbus.h
@@ -1,6 +1,6 @@
/*
* Copyright (C) 2019 Jolla Ltd.
* Copyright (C) 2019 Slava Monich <slava.monich@jolla.com>
* Copyright (C) 2019-2020 Jolla Ltd.
* Copyright (C) 2019-2020 Slava Monich <slava.monich@jolla.com>
*
* You may use this file under the terms of BSD license as follows:
*
Expand Down Expand Up @@ -49,6 +49,12 @@ test_dbus_new(
TestDBusStartFunc start,
void* user_data);

TestDBus*
test_dbus_new2(
TestDBusStartFunc start,
TestDBusStartFunc start2,
void* user_data);

void
test_dbus_free(
TestDBus* dbus);
Expand Down
42 changes: 26 additions & 16 deletions unit/plugins_dbus_service_plugin/test_plugins_dbus_service_plugin.c
@@ -1,6 +1,6 @@
/*
* Copyright (C) 2019 Jolla Ltd.
* Copyright (C) 2019 Slava Monich <slava.monich@jolla.com>
* Copyright (C) 2019-2020 Jolla Ltd.
* Copyright (C) 2019-2020 Slava Monich <slava.monich@jolla.com>
*
* You may use this file under the terms of BSD license as follows:
*
Expand Down Expand Up @@ -83,17 +83,27 @@ test_data_cleanup(
g_main_loop_unref(test->loop);
}

static
void
test_start(
GDBusConnection* client,
GDBusConnection* server,
void* user_data)
{
TestData* test = user_data;

test_server = server;
g_assert(nfc_manager_start(test->manager));
}

static
void
test_call(
TestData* test,
GDBusConnection* client,
GDBusConnection* server,
const char* method,
GAsyncReadyCallback callback)
{
test_server = server;
g_assert(nfc_manager_start(test->manager));
g_dbus_connection_call(client, NULL, "/", NFC_DAEMON_INTERFACE,
method, NULL, NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, callback, test);
}
Expand Down Expand Up @@ -148,7 +158,7 @@ dbus_service_name_own(
data->name = g_strdup(name);
data->bus_acquired = bus_acquired;
data->name_acquired = name_acquired;
g_idle_add_full(G_PRIORITY_HIGH, test_bus_acquired, data,
g_idle_add_full(G_PRIORITY_HIGH_IDLE, test_bus_acquired, data,
test_bus_acquired_free);
return TEST_NAME_OWN_ID;
}
Expand Down Expand Up @@ -196,7 +206,7 @@ test_get_all_start(
GDBusConnection* server,
void* test)
{
test_call((TestData*)test, client, server, "GetAll", test_get_all_done);
test_call((TestData*)test, client, "GetAll", test_get_all_done);
}

static
Expand All @@ -208,7 +218,7 @@ test_get_all(
TestDBus* dbus;

test_data_init(&test);
dbus = test_dbus_new(test_get_all_start, &test);
dbus = test_dbus_new2(test_start, test_get_all_start, &test);
test_run(&test_opt, test.loop);
test_data_cleanup(&test);
test_dbus_free(dbus);
Expand Down Expand Up @@ -248,7 +258,7 @@ test_get_interface_version_start(
GDBusConnection* server,
void* test)
{
test_call((TestData*)test, client, server, "GetInterfaceVersion",
test_call((TestData*)test, client, "GetInterfaceVersion",
test_get_interface_version_done);
}

Expand All @@ -261,7 +271,7 @@ test_get_interface_version(
TestDBus* dbus;

test_data_init(&test);
dbus = test_dbus_new(test_get_interface_version_start, &test);
dbus = test_dbus_new2(test_start, test_get_interface_version_start, &test);
test_run(&test_opt, test.loop);
test_data_cleanup(&test);
test_dbus_free(dbus);
Expand Down Expand Up @@ -301,7 +311,7 @@ test_get_adapters_start(
GDBusConnection* server,
void* test)
{
test_call((TestData*)test, client, server, "GetAdapters",
test_call((TestData*)test, client, "GetAdapters",
test_get_adapters_done);
}

Expand All @@ -314,7 +324,7 @@ test_get_adapters(
TestDBus* dbus;

test_data_init(&test);
dbus = test_dbus_new(test_get_adapters_start, &test);
dbus = test_dbus_new2(test_start, test_get_adapters_start, &test);
test_run(&test_opt, test.loop);
test_data_cleanup(&test);
test_dbus_free(dbus);
Expand Down Expand Up @@ -359,7 +369,7 @@ test_get_all2_start(
GDBusConnection* server,
void* test)
{
test_call((TestData*)test, client, server, "GetAll2", test_get_all2_done);
test_call((TestData*)test, client, "GetAll2", test_get_all2_done);
}

static
Expand All @@ -371,7 +381,7 @@ test_get_all2(
TestDBus* dbus;

test_data_init(&test);
dbus = test_dbus_new(test_get_all2_start, &test);
dbus = test_dbus_new2(test_start, test_get_all2_start, &test);
test_run(&test_opt, test.loop);
test_data_cleanup(&test);
test_dbus_free(dbus);
Expand Down Expand Up @@ -411,7 +421,7 @@ test_get_daemon_version_start(
GDBusConnection* server,
void* test)
{
test_call((TestData*)test, client, server, "GetDaemonVersion",
test_call((TestData*)test, client, "GetDaemonVersion",
test_get_daemon_version_done);
}

Expand All @@ -424,7 +434,7 @@ test_get_daemon_version(
TestDBus* dbus;

test_data_init(&test);
dbus = test_dbus_new(test_get_daemon_version_start, &test);
dbus = test_dbus_new2(test_start, test_get_daemon_version_start, &test);
test_run(&test_opt, test.loop);
test_data_cleanup(&test);
test_dbus_free(dbus);
Expand Down

0 comments on commit ab5acbd

Please sign in to comment.