update_engine: Migrate time-based glib main loop calls to MessageLoop.
This patch replaces most calls to g_idle_add* and g_timeout_add* with
the equivalent MessageLoop::Post*Task(). To maintain compatibility with
unittests running the main loop and doing I/O we instantiate a
GlibMessageLoop for those tests.
BUG=chromium:499886
TEST=unittests still pass.
Change-Id: Ic87ba69bc47391ac3c36d1bfc3ca28d069666af1
Reviewed-on: https://chromium-review.googlesource.com/281197
Reviewed-by: Alex Vakulenko <avakulenko@chromium.org>
Tested-by: Alex Deymo <deymo@chromium.org>
Commit-Queue: Alex Deymo <deymo@chromium.org>
Trybot-Ready: Alex Deymo <deymo@chromium.org>
diff --git a/utils_unittest.cc b/utils_unittest.cc
index 2c30d30..308345a 100644
--- a/utils_unittest.cc
+++ b/utils_unittest.cc
@@ -18,6 +18,8 @@
#include <base/files/scoped_temp_dir.h>
#include <base/strings/string_util.h>
#include <base/strings/stringprintf.h>
+#include <chromeos/message_loops/fake_message_loop.h>
+#include <chromeos/message_loops/message_loop_utils.h>
#include <gtest/gtest.h>
#include "update_engine/fake_clock.h"
@@ -26,6 +28,7 @@
#include "update_engine/prefs.h"
#include "update_engine/test_utils.h"
+using chromeos::FakeMessageLoop;
using std::map;
using std::string;
using std::vector;
@@ -411,22 +414,15 @@
0xb0, 0x04, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00});
}
-namespace {
-gboolean TerminateScheduleCrashReporterUploadTest(void* arg) {
- GMainLoop* loop = reinterpret_cast<GMainLoop*>(arg);
- g_main_loop_quit(loop);
- return FALSE; // Don't call this callback again
-}
-} // namespace
-
TEST(UtilsTest, ScheduleCrashReporterUploadTest) {
// Not much to test. At least this tests for memory leaks, crashes,
// log errors.
- GMainLoop* loop = g_main_loop_new(g_main_context_default(), FALSE);
+ FakeMessageLoop loop(nullptr);
+ loop.SetAsCurrent();
utils::ScheduleCrashReporterUpload();
- g_timeout_add_seconds(1, &TerminateScheduleCrashReporterUploadTest, loop);
- g_main_loop_run(loop);
- g_main_loop_unref(loop);
+ // Test that we scheduled one callback from the crash reporter.
+ EXPECT_EQ(1, chromeos::MessageLoopRunMaxIterations(&loop, 100));
+ EXPECT_FALSE(loop.PendingTasks());
}
TEST(UtilsTest, FormatTimeDeltaTest) {