test-utils: Separate failable and non-failable functions

test_object_try_whatever() now has libdbus-like OOM handling,
while test_object_whatever() has GLib-like OOM handling. This is
because an overwhelming majority of the callers of these functions
either didn't check for OOM anyway, or checked for it but then
aborted. In the uncommon case where we do care, we can use the _try_
version.

Bug: https://bugs.freedesktop.org/show_bug.cgi?id=100317
Reviewed-by: Philip Withnall <withnall@endlessm.com>
Signed-off-by: Simon McVittie <smcv@collabora.com>
This commit is contained in:
Simon McVittie 2017-11-27 19:26:03 +00:00
parent 38ff6bd20d
commit f59b4f9226
10 changed files with 54 additions and 26 deletions

View File

@ -196,8 +196,7 @@ setup (Fixture *f,
f->right_conn = dbus_bus_get_private (DBUS_BUS_SESSION, &f->e);
test_assert_no_error (&f->e);
if (!test_connection_setup (f->ctx, f->right_conn))
g_error ("OOM");
test_connection_setup (f->ctx, f->right_conn);
}
else
{

View File

@ -251,8 +251,7 @@ setup_connection (Fixture *f,
dbus_server_set_new_connection_function (f->server,
new_conn_cb, f, NULL);
if (!test_server_setup (f->loop, f->server))
g_error ("failed to set up server");
test_server_setup (f->loop, f->server);
address = dbus_server_get_address (f->server);
g_assert (address != NULL);
@ -261,8 +260,7 @@ setup_connection (Fixture *f,
g_assert (f->connection != NULL);
dbus_free (address);
if (!test_connection_setup (f->loop, f->connection))
g_error ("failed to set up connection");
test_connection_setup (f->loop, f->connection);
while (f->server_connection == NULL)
_dbus_loop_iterate (f->loop, TRUE);

View File

@ -29,7 +29,7 @@ new_connection_callback (DBusServer *server,
{
TestServiceData *testdata = data;
if (!test_connection_setup (testdata->loop, new_connection))
if (!test_connection_try_setup (testdata->loop, new_connection))
dbus_connection_close (new_connection);
}
@ -114,8 +114,7 @@ main (int argc, char *argv[])
dbus_server_set_new_connection_function (server, new_connection_callback,
testdata, NULL);
if (!test_server_setup (loop, server))
die ("server setup failed");
test_server_setup (loop, server);
fprintf (stderr, "server running mainloop\n");
_dbus_loop_run (loop);

View File

@ -38,8 +38,7 @@ open_destroy_shared_session_bus_connection (void)
if (loop == NULL)
die ("No memory\n");
if (!test_connection_setup (loop, connection))
die ("No memory\n");
test_connection_setup (loop, connection);
test_connection_shutdown (loop, connection);

View File

@ -64,8 +64,7 @@ main (int argc,
if (loop == NULL)
die ("No memory\n");
if (!test_connection_setup (loop, connection))
die ("No memory\n");
test_connection_setup (loop, connection);
TestName(connection, "org.freedesktop.DBus.Test", TRUE);
TestName(connection, "org.freedesktop.DBus.Test-2", TRUE);

View File

@ -452,9 +452,8 @@ main (int argc,
loop = _dbus_loop_new ();
if (loop == NULL)
die ("No memory\n");
if (!test_connection_setup (loop, connection))
die ("No memory\n");
test_connection_setup (loop, connection);
if (!dbus_connection_add_filter (connection,
filter_func, NULL, NULL))

View File

@ -145,9 +145,8 @@ main (int argc,
loop = _dbus_loop_new ();
if (loop == NULL)
die ("No memory\n");
if (!test_connection_setup (loop, connection))
die ("No memory\n");
test_connection_setup (loop, connection);
if (!dbus_connection_add_filter (connection,
filter_func, NULL, NULL))

View File

@ -346,7 +346,7 @@ test_try_connect_to_bus (TestMainContext *ctx,
g_assert (dbus_bus_get_unique_name (conn) != NULL);
if (!test_connection_setup (ctx, conn))
if (!test_connection_try_setup (ctx, conn))
{
_DBUS_SET_OOM (&error);
goto fail;

View File

@ -98,8 +98,8 @@ cdata_new (DBusLoop *loop,
}
dbus_bool_t
test_connection_setup (TestMainContext *ctx,
DBusConnection *connection)
test_connection_try_setup (TestMainContext *ctx,
DBusConnection *connection)
{
DBusLoop *loop = ctx;
CData *cd;
@ -165,6 +165,14 @@ die (const char *message)
exit (1);
}
void
test_connection_setup (TestMainContext *ctx,
DBusConnection *connection)
{
if (!test_connection_try_setup (ctx, connection))
die ("Not enough memory to set up connection");
}
void
test_connection_shutdown (TestMainContext *ctx,
DBusConnection *connection)
@ -268,8 +276,8 @@ remove_server_timeout (DBusTimeout *timeout,
}
dbus_bool_t
test_server_setup (TestMainContext *ctx,
DBusServer *server)
test_server_try_setup (TestMainContext *ctx,
DBusServer *server)
{
DBusLoop *loop = ctx;
ServerData *sd;
@ -311,6 +319,14 @@ test_server_setup (TestMainContext *ctx,
return FALSE;
}
void
test_server_setup (TestMainContext *ctx,
DBusServer *server)
{
if (!test_server_try_setup (ctx, server))
die ("Not enough memory to set up server");
}
void
test_server_shutdown (TestMainContext *ctx,
DBusServer *server)
@ -332,6 +348,17 @@ test_server_shutdown (TestMainContext *ctx,
TestMainContext *
test_main_context_get (void)
{
TestMainContext *ret = _dbus_loop_new ();
if (ret == NULL)
die ("Out of memory");
return ret;
}
TestMainContext *
test_main_context_try_get (void)
{
return _dbus_loop_new ();
}

View File

@ -10,18 +10,27 @@
#include <dbus/dbus-internals.h>
typedef DBusLoop TestMainContext;
_DBUS_GNUC_WARN_UNUSED_RESULT
TestMainContext *test_main_context_get (void);
_DBUS_GNUC_WARN_UNUSED_RESULT
TestMainContext *test_main_context_try_get (void);
TestMainContext *test_main_context_ref (TestMainContext *ctx);
void test_main_context_unref (TestMainContext *ctx);
void test_main_context_iterate (TestMainContext *ctx,
dbus_bool_t may_block);
dbus_bool_t test_connection_setup (TestMainContext *ctx,
_DBUS_GNUC_WARN_UNUSED_RESULT
dbus_bool_t test_connection_try_setup (TestMainContext *ctx,
DBusConnection *connection);
void test_connection_setup (TestMainContext *ctx,
DBusConnection *connection);
void test_connection_shutdown (TestMainContext *ctx,
DBusConnection *connection);
dbus_bool_t test_server_setup (TestMainContext *ctx,
_DBUS_GNUC_WARN_UNUSED_RESULT
dbus_bool_t test_server_try_setup (TestMainContext *ctx,
DBusServer *server);
void test_server_setup (TestMainContext *ctx,
DBusServer *server);
void test_server_shutdown (TestMainContext *ctx,
DBusServer *server);