From 67f6ffe35c6318cd510f49647e57aa01abeb4d70 Mon Sep 17 00:00:00 2001 From: Ted Gould Date: Fri, 15 Nov 2013 22:36:33 -0600 Subject: Nicer mainloop --- tests/name-watch-test.cc | 55 ++++++++++++++++++------------------------------ 1 file changed, 20 insertions(+), 35 deletions(-) (limited to 'tests/name-watch-test.cc') diff --git a/tests/name-watch-test.cc b/tests/name-watch-test.cc index c9737ec..ef182c8 100644 --- a/tests/name-watch-test.cc +++ b/tests/name-watch-test.cc @@ -21,6 +21,19 @@ class NameWatchTest : public ::testing::Test g_test_dbus_down(testbus); g_clear_object(&testbus); } + + static gboolean timeout_cb (gpointer user_data) { + GMainLoop * loop = static_cast(user_data); + g_main_loop_quit(loop); + return G_SOURCE_REMOVE; + } + + void loop (unsigned int ms) { + GMainLoop * loop = g_main_loop_new(NULL, FALSE); + g_timeout_add(ms, timeout_cb, loop); + g_main_loop_run(loop); + g_main_loop_unref(loop); + } }; typedef struct { @@ -63,22 +76,14 @@ TEST_F(NameWatchTest, BaseWatch) G_BUS_NAME_OWNER_FLAGS_NONE, NULL, NULL, NULL, NULL, NULL); - g_usleep(100000); - while (g_main_pending()) - g_main_iteration(TRUE); - g_usleep(100000); - while (g_main_pending()) - g_main_iteration(TRUE); - + loop(100); ASSERT_EQ(callback_count.appeared, 2); g_bus_unown_name(name1); g_bus_unown_name(name2); - g_usleep(100000); - while (g_main_pending()) - g_main_iteration(TRUE); + loop(100); ASSERT_EQ(callback_count.vanished, 2); @@ -105,22 +110,14 @@ TEST_F(NameWatchTest, NonMatches) G_BUS_NAME_OWNER_FLAGS_NONE, NULL, NULL, NULL, NULL, NULL); - g_usleep(100000); - while (g_main_pending()) - g_main_iteration(TRUE); - g_usleep(100000); - while (g_main_pending()) - g_main_iteration(TRUE); - + loop(100); ASSERT_EQ(callback_count.appeared, 0); g_bus_unown_name(name1); g_bus_unown_name(name2); - g_usleep(100000); - while (g_main_pending()) - g_main_iteration(TRUE); + loop(100); ASSERT_EQ(callback_count.vanished, 0); @@ -134,9 +131,7 @@ TEST_F(NameWatchTest, StartupNames) G_BUS_NAME_OWNER_FLAGS_NONE, NULL, NULL, NULL, NULL, NULL); - g_usleep(100000); - while (g_main_pending()) - g_main_iteration(TRUE); + loop(100); callback_count_t callback_count = {0}; @@ -147,23 +142,13 @@ TEST_F(NameWatchTest, StartupNames) &callback_count, NULL); - g_usleep(100000); - while (g_main_pending()) - g_main_iteration(TRUE); - g_usleep(100000); - while (g_main_pending()) - g_main_iteration(TRUE); - g_usleep(100000); - while (g_main_pending()) - g_main_iteration(TRUE); + loop(100); ASSERT_EQ(callback_count.appeared, 1); g_bus_unown_name(name1); - g_usleep(100000); - while (g_main_pending()) - g_main_iteration(TRUE); + loop(100); ASSERT_EQ(callback_count.vanished, 1); -- cgit v1.2.3