update_engine: Convert update_manager to chromeos::MessageLoop.
The update_manager/event_loop.* files were basically an abstraction of
the glib main loop in order to make it easier to replace it later.
This patch removes those files and replaces their functions with the
chromeos::MessageLoop interface, backing it up with a FakeMessageLoop
during test, and a real GlibMessageLoop during normal execution. This
patch reduces the running time of the unittest considerably since there
is no need to wait for the timeouts.
BUG=chromium:419827,chromium:402066
TEST=Unittest still pass. Tested on a link device that the UM still runs.
Change-Id: Id572248ff4c9c8be7226ef8c653a5c94ab9c1677
Reviewed-on: https://chromium-review.googlesource.com/276892
Reviewed-by: Alex Deymo <deymo@chromium.org>
Commit-Queue: Alex Deymo <deymo@chromium.org>
Tested-by: Alex Deymo <deymo@chromium.org>
diff --git a/update_manager/chromeos_policy_unittest.cc b/update_manager/chromeos_policy_unittest.cc
index 5905437..2c935fd 100644
--- a/update_manager/chromeos_policy_unittest.cc
+++ b/update_manager/chromeos_policy_unittest.cc
@@ -10,6 +10,7 @@
#include <vector>
#include <base/time/time.h>
+#include <chromeos/message_loops/fake_message_loop.h>
#include <gtest/gtest.h>
#include "update_engine/fake_clock.h"
@@ -31,12 +32,17 @@
class UmChromeOSPolicyTest : public ::testing::Test {
protected:
void SetUp() override {
+ loop_.SetAsCurrent();
SetUpDefaultClock();
eval_ctx_ = new EvaluationContext(&fake_clock_, TimeDelta::FromSeconds(5));
SetUpDefaultState();
SetUpDefaultDevicePolicy();
}
+ void TearDown() override {
+ EXPECT_FALSE(loop_.PendingTasks());
+ }
+
// Sets the clock to fixed values.
void SetUpDefaultClock() {
fake_clock_.SetMonotonicTime(Time::FromInternalValue(12345678L));
@@ -178,6 +184,7 @@
<< "\nEvaluation context: " << eval_ctx_->DumpContext();
}
+ chromeos::FakeMessageLoop loop_{nullptr};
FakeClock fake_clock_;
FakeState fake_state_;
scoped_refptr<EvaluationContext> eval_ctx_;
diff --git a/update_manager/evaluation_context.cc b/update_manager/evaluation_context.cc
index 3584ac8..0c4ef21 100644
--- a/update_manager/evaluation_context.cc
+++ b/update_manager/evaluation_context.cc
@@ -19,6 +19,7 @@
using base::Closure;
using base::Time;
using base::TimeDelta;
+using chromeos::MessageLoop;
using chromeos_update_engine::ClockInterface;
using std::string;
using std::unique_ptr;
@@ -75,8 +76,8 @@
if (it.first->GetMode() == kVariableModeAsync)
it.first->RemoveObserver(this);
}
- CancelMainLoopEvent(timeout_event_);
- timeout_event_ = kEventIdNull;
+ MessageLoop::current()->CancelTask(timeout_event_);
+ timeout_event_ = MessageLoop::kTaskIdNull;
return unique_ptr<Closure>(callback_.release());
}
@@ -101,7 +102,7 @@
void EvaluationContext::OnTimeout() {
DLOG(INFO) << "OnTimeout() called due to "
<< (timeout_marks_expiration_ ? "expiration" : "poll interval");
- timeout_event_ = kEventIdNull;
+ timeout_event_ = MessageLoop::kTaskIdNull;
is_expired_ = timeout_marks_expiration_;
OnValueChangedOrTimeout();
}
@@ -202,7 +203,7 @@
if (!timeout.is_max()) {
DLOG(INFO) << "Waiting for timeout in "
<< chromeos_update_engine::utils::FormatTimeDelta(timeout);
- timeout_event_ = RunFromMainLoopAfterTimeout(
+ timeout_event_ = MessageLoop::current()->PostDelayedTask(
base::Bind(&EvaluationContext::OnTimeout,
weak_ptr_factory_.GetWeakPtr()),
timeout);
diff --git a/update_manager/evaluation_context.h b/update_manager/evaluation_context.h
index ecb37e0..3d259f3 100644
--- a/update_manager/evaluation_context.h
+++ b/update_manager/evaluation_context.h
@@ -14,10 +14,10 @@
#include <base/memory/ref_counted.h>
#include <base/memory/weak_ptr.h>
#include <base/time/time.h>
+#include <chromeos/message_loops/message_loop.h>
#include "update_engine/clock_interface.h"
#include "update_engine/update_manager/boxed_value.h"
-#include "update_engine/update_manager/event_loop.h"
#include "update_engine/update_manager/variable.h"
namespace chromeos_update_manager {
@@ -149,9 +149,10 @@
// is_expired().
std::unique_ptr<base::Closure> callback_;
- // The EventId returned by the event loop identifying the timeout callback.
+ // The TaskId returned by the message loop identifying the timeout callback.
// Used for canceling the timeout callback.
- EventId timeout_event_ = kEventIdNull;
+ chromeos::MessageLoop::TaskId timeout_event_ =
+ chromeos::MessageLoop::kTaskIdNull;
// Whether a timeout event firing marks the expiration of the evaluation
// context.
diff --git a/update_manager/evaluation_context_unittest.cc b/update_manager/evaluation_context_unittest.cc
index b945301..0ba9594 100644
--- a/update_manager/evaluation_context_unittest.cc
+++ b/update_manager/evaluation_context_unittest.cc
@@ -8,10 +8,11 @@
#include <string>
#include <base/bind.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"
-#include "update_engine/test_utils.h"
#include "update_engine/update_manager/fake_variable.h"
#include "update_engine/update_manager/generic_variables.h"
#include "update_engine/update_manager/mock_variable.h"
@@ -21,9 +22,10 @@
using base::Closure;
using base::Time;
using base::TimeDelta;
+using chromeos::MessageLoop;
+using chromeos::MessageLoopRunMaxIterations;
+using chromeos::MessageLoopRunUntil;
using chromeos_update_engine::FakeClock;
-using chromeos_update_engine::test_utils::RunGMainLoopMaxIterations;
-using chromeos_update_engine::test_utils::RunGMainLoopUntil;
using std::string;
using std::unique_ptr;
using testing::Return;
@@ -72,6 +74,7 @@
class UmEvaluationContextTest : public ::testing::Test {
protected:
void SetUp() override {
+ loop_.SetAsCurrent();
// Apr 22, 2009 19:25:00 UTC (this is a random reference point).
fake_clock_.SetMonotonicTime(Time::FromTimeT(1240428300));
// Mar 2, 2006 1:23:45 UTC.
@@ -100,10 +103,13 @@
EXPECT_TRUE(fake_async_var_.observer_list_.empty());
EXPECT_TRUE(fake_const_var_.observer_list_.empty());
EXPECT_TRUE(fake_poll_var_.observer_list_.empty());
+
+ EXPECT_FALSE(loop_.PendingTasks());
}
TimeDelta default_timeout_ = TimeDelta::FromSeconds(5);
+ chromeos::FakeMessageLoop loop_{nullptr};
FakeClock fake_clock_;
scoped_refptr<EvaluationContext> eval_ctx_;
@@ -200,14 +206,14 @@
bool value = false;
EXPECT_TRUE(eval_ctx_->RunOnValueChangeOrTimeout(Bind(&SetTrue, &value)));
// Check that the scheduled callback isn't run until we signal a ValueChaged.
- RunGMainLoopMaxIterations(100);
+ MessageLoopRunMaxIterations(MessageLoop::current(), 100);
EXPECT_FALSE(value);
fake_async_var_.NotifyValueChanged();
EXPECT_FALSE(value);
// Ensure that the scheduled callback isn't run until we are back on the main
// loop.
- RunGMainLoopMaxIterations(100);
+ MessageLoopRunMaxIterations(MessageLoop::current(), 100);
EXPECT_TRUE(value);
}
@@ -222,7 +228,7 @@
// The scheduled event should still work.
fake_async_var_.NotifyValueChanged();
- RunGMainLoopMaxIterations(100);
+ MessageLoopRunMaxIterations(MessageLoop::current(), 100);
EXPECT_TRUE(value);
}
@@ -234,9 +240,11 @@
bool value = false;
EXPECT_TRUE(eval_ctx_->RunOnValueChangeOrTimeout(Bind(&SetTrue, &value)));
// Check that the scheduled callback isn't run until the timeout occurs.
- RunGMainLoopMaxIterations(10);
+ MessageLoopRunMaxIterations(MessageLoop::current(), 10);
EXPECT_FALSE(value);
- RunGMainLoopUntil(10000, Bind(&GetBoolean, &value));
+ MessageLoopRunUntil(MessageLoop::current(),
+ TimeDelta::FromSeconds(10),
+ Bind(&GetBoolean, &value));
EXPECT_TRUE(value);
}
@@ -249,9 +257,11 @@
bool value = false;
EXPECT_TRUE(eval_ctx_->RunOnValueChangeOrTimeout(Bind(&SetTrue, &value)));
// Check that the scheduled callback isn't run until the timeout occurs.
- RunGMainLoopMaxIterations(10);
+ MessageLoopRunMaxIterations(MessageLoop::current(), 10);
EXPECT_FALSE(value);
- RunGMainLoopUntil(10000, Bind(&GetBoolean, &value));
+ MessageLoopRunUntil(MessageLoop::current(),
+ TimeDelta::FromSeconds(10),
+ Bind(&GetBoolean, &value));
EXPECT_TRUE(value);
// Ensure that we cannot reschedule an evaluation.
@@ -274,7 +284,7 @@
// This should not trigger the callback since the EvaluationContext waiting
// for it is gone, and it should have remove all its observers.
fake_async_var_.NotifyValueChanged();
- RunGMainLoopMaxIterations(100);
+ MessageLoopRunMaxIterations(MessageLoop::current(), 100);
EXPECT_FALSE(value);
}
@@ -293,7 +303,9 @@
Closure closure = Bind(EvaluateRepeatedly, evaluation, eval_ctx_,
&num_reevaluations, &done);
ASSERT_TRUE(eval_ctx_->RunOnValueChangeOrTimeout(closure));
- RunGMainLoopUntil(10000, Bind(&GetBoolean, &done));
+ MessageLoopRunUntil(MessageLoop::current(),
+ TimeDelta::FromSeconds(10),
+ Bind(&GetBoolean, &done));
EXPECT_EQ(0, num_reevaluations);
}
@@ -321,7 +333,9 @@
// seconds). Our callback should not be called because the EvaluationContext
// was removed before the timeout event is attended.
eval_ctx_ = nullptr;
- RunGMainLoopUntil(1000, Bind(&GetBoolean, &value));
+ MessageLoopRunUntil(MessageLoop::current(),
+ TimeDelta::FromSeconds(10),
+ Bind(&GetBoolean, &value));
EXPECT_FALSE(value);
}
diff --git a/update_manager/event_loop.cc b/update_manager/event_loop.cc
deleted file mode 100644
index bc2940b..0000000
--- a/update_manager/event_loop.cc
+++ /dev/null
@@ -1,57 +0,0 @@
-// Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include "update_engine/update_manager/event_loop.h"
-
-#include <cmath>
-
-using base::Closure;
-
-namespace {
-
-// Called by the GLib's main loop when is time to call the callback scheduled
-// with RunFromMainLopp() and similar functions. The pointer to the callback
-// passed when scheduling it is passed to this functions as a gpointer on
-// |user_data|.
-gboolean OnRanFromMainLoop(gpointer user_data) {
- Closure* callback_p = reinterpret_cast<Closure*>(user_data);
- callback_p->Run();
- return FALSE; // Removes the source since a callback can only be called once.
-}
-
-void DestroyClosure(gpointer user_data) {
- delete reinterpret_cast<Closure*>(user_data);
-}
-
-} // namespace
-
-namespace chromeos_update_manager {
-
-EventId RunFromMainLoop(const Closure& callback) {
- Closure* callback_p = new Closure(callback);
- return g_idle_add_full(G_PRIORITY_DEFAULT,
- OnRanFromMainLoop,
- reinterpret_cast<gpointer>(callback_p),
- DestroyClosure);
-}
-
-EventId RunFromMainLoopAfterTimeout(
- const Closure& callback,
- base::TimeDelta timeout) {
- Closure* callback_p = new Closure(callback);
- return g_timeout_add_seconds_full(
- G_PRIORITY_DEFAULT,
- static_cast<guint>(ceil(timeout.InSecondsF())),
- OnRanFromMainLoop,
- reinterpret_cast<gpointer>(callback_p),
- DestroyClosure);
-}
-
-bool CancelMainLoopEvent(EventId event) {
- if (event != kEventIdNull)
- return g_source_remove(event);
- return false;
-}
-
-} // namespace chromeos_update_manager
diff --git a/update_manager/event_loop.h b/update_manager/event_loop.h
deleted file mode 100644
index c639f88..0000000
--- a/update_manager/event_loop.h
+++ /dev/null
@@ -1,37 +0,0 @@
-// Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// TODO(deymo): These functions interact with the glib's main loop. This should
-// be replaced by the libbase main loop once the process is migrated to that
-// main loop.
-
-#ifndef UPDATE_ENGINE_UPDATE_MANAGER_EVENT_LOOP_H_
-#define UPDATE_ENGINE_UPDATE_MANAGER_EVENT_LOOP_H_
-
-#include <glib.h>
-
-#include <base/callback.h>
-#include <base/time/time.h>
-
-namespace chromeos_update_manager {
-
-typedef guint EventId;
-
-// A null EventId doesn't identify any valid event.
-static constexpr EventId kEventIdNull = 0;
-
-// Schedules the passed |callback| to run from the GLib's main loop after a
-// timeout if it is given.
-EventId RunFromMainLoop(const base::Closure& callback);
-EventId RunFromMainLoopAfterTimeout(const base::Closure& callback,
- base::TimeDelta timeout);
-
-// Removes the pending call |event| from the main loop. The value passed is the
-// one returned by the functions RunFromMainLoop*() when the call was scheduled.
-// Returns whether the event was found and removed.
-bool CancelMainLoopEvent(EventId event);
-
-} // namespace chromeos_update_manager
-
-#endif // UPDATE_ENGINE_UPDATE_MANAGER_EVENT_LOOP_H_
diff --git a/update_manager/event_loop_unittest.cc b/update_manager/event_loop_unittest.cc
deleted file mode 100644
index 4f8f768..0000000
--- a/update_manager/event_loop_unittest.cc
+++ /dev/null
@@ -1,62 +0,0 @@
-// Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include "update_engine/update_manager/event_loop.h"
-
-#include <base/bind.h>
-#include <gtest/gtest.h>
-
-#include "update_engine/test_utils.h"
-
-using base::Bind;
-using base::TimeDelta;
-using chromeos_update_engine::test_utils::RunGMainLoopMaxIterations;
-using chromeos_update_engine::test_utils::RunGMainLoopUntil;
-
-namespace {
-
-// Sets the value of the passed pointer to true.
-void SetTrue(bool* value) {
- *value = true;
-}
-
-bool GetBoolean(bool* value) {
- return *value;
-}
-
-} // namespace
-
-namespace chromeos_update_manager {
-
-class EventLoopTest : public ::testing::Test {};
-
-TEST(EventLoopTest, RunFromMainLoopTest) {
- bool called = false;
- EventId ev = RunFromMainLoop(Bind(SetTrue, &called));
- EXPECT_NE(0, ev);
- RunGMainLoopMaxIterations(100);
- EXPECT_TRUE(called);
-}
-
-// Tests that we can cancel events right after we schedule them.
-TEST(EventLoopTest, RunFromMainLoopCancelTest) {
- bool called = false;
- EventId ev = RunFromMainLoop(Bind(SetTrue, &called));
- EXPECT_NE(0, ev);
- EXPECT_TRUE(CancelMainLoopEvent(ev));
- RunGMainLoopMaxIterations(100);
- EXPECT_FALSE(called);
-}
-
-TEST(EventLoopTest, RunFromMainLoopAfterTimeoutTest) {
- bool called = false;
- EventId ev = RunFromMainLoopAfterTimeout(Bind(SetTrue, &called),
- TimeDelta::FromSeconds(1));
- EXPECT_NE(0, ev);
- RunGMainLoopUntil(10000, Bind(GetBoolean, &called));
- // Check that the main loop finished before the 10 seconds timeout.
- EXPECT_TRUE(called);
-}
-
-} // namespace chromeos_update_manager
diff --git a/update_manager/generic_variables_unittest.cc b/update_manager/generic_variables_unittest.cc
index 65f7f51..372670f 100644
--- a/update_manager/generic_variables_unittest.cc
+++ b/update_manager/generic_variables_unittest.cc
@@ -7,12 +7,15 @@
#include <memory>
#include <base/callback.h>
+#include <chromeos/message_loops/fake_message_loop.h>
+#include <chromeos/message_loops/message_loop.h>
+#include <chromeos/message_loops/message_loop_utils.h>
#include <gtest/gtest.h>
-#include "update_engine/test_utils.h"
#include "update_engine/update_manager/umtest_utils.h"
-using chromeos_update_engine::test_utils::RunGMainLoopMaxIterations;
+using chromeos::MessageLoop;
+using chromeos::MessageLoopRunMaxIterations;
using std::unique_ptr;
namespace chromeos_update_manager {
@@ -132,11 +135,18 @@
}
class UmAsyncCopyVariableTest : public ::testing::Test {
- public:
+ protected:
+ void SetUp() override {
+ loop_.SetAsCurrent();
+ }
+
void TearDown() override {
// No remaining event on the main loop.
- EXPECT_EQ(0, RunGMainLoopMaxIterations(1));
+ EXPECT_FALSE(loop_.PendingTasks());
}
+
+
+ chromeos::FakeMessageLoop loop_{nullptr};
};
TEST_F(UmAsyncCopyVariableTest, ConstructorTest) {
@@ -150,7 +160,7 @@
var.SetValue(5);
UmTestUtils::ExpectVariableHasValue(5, &var);
// Execute all the pending observers.
- RunGMainLoopMaxIterations(100);
+ MessageLoopRunMaxIterations(MessageLoop::current(), 100);
}
TEST_F(UmAsyncCopyVariableTest, UnsetValueTest) {
@@ -158,7 +168,7 @@
var.UnsetValue();
UmTestUtils::ExpectVariableNotSet(&var);
// Execute all the pending observers.
- RunGMainLoopMaxIterations(100);
+ MessageLoopRunMaxIterations(MessageLoop::current(), 100);
}
class CallCounterObserver : public BaseVariable::ObserverInterface {
@@ -178,22 +188,22 @@
// Check that a different value fires the notification.
var.SetValue(5);
- RunGMainLoopMaxIterations(100);
+ MessageLoopRunMaxIterations(MessageLoop::current(), 100);
EXPECT_EQ(1, observer.calls_count_);
// Check the same value doesn't.
var.SetValue(5);
- RunGMainLoopMaxIterations(100);
+ MessageLoopRunMaxIterations(MessageLoop::current(), 100);
EXPECT_EQ(1, observer.calls_count_);
// Check that unsetting a previously set value fires the notification.
var.UnsetValue();
- RunGMainLoopMaxIterations(100);
+ MessageLoopRunMaxIterations(MessageLoop::current(), 100);
EXPECT_EQ(2, observer.calls_count_);
// Check that unsetting again doesn't.
var.UnsetValue();
- RunGMainLoopMaxIterations(100);
+ MessageLoopRunMaxIterations(MessageLoop::current(), 100);
EXPECT_EQ(2, observer.calls_count_);
var.RemoveObserver(&observer);
diff --git a/update_manager/real_device_policy_provider.cc b/update_manager/real_device_policy_provider.cc
index e3a378a..557987f 100644
--- a/update_manager/real_device_policy_provider.cc
+++ b/update_manager/real_device_policy_provider.cc
@@ -17,6 +17,7 @@
#include "update_engine/utils.h"
using base::TimeDelta;
+using chromeos::MessageLoop;
using policy::DevicePolicy;
using std::set;
using std::string;
@@ -30,7 +31,7 @@
namespace chromeos_update_manager {
RealDevicePolicyProvider::~RealDevicePolicyProvider() {
- CancelMainLoopEvent(scheduled_refresh_);
+ MessageLoop::current()->CancelTask(scheduled_refresh_);
// Detach signal handler, free manager proxy.
dbus_->ProxyDisconnectSignal(manager_proxy_,
login_manager::kPropertyChangeCompleteSignal,
@@ -83,7 +84,7 @@
void RealDevicePolicyProvider::RefreshDevicePolicyAndReschedule() {
RefreshDevicePolicy();
- scheduled_refresh_ = RunFromMainLoopAfterTimeout(
+ scheduled_refresh_ = MessageLoop::current()->PostDelayedTask(
base::Bind(&RealDevicePolicyProvider::RefreshDevicePolicyAndReschedule,
base::Unretained(this)),
TimeDelta::FromMinutes(kDevicePolicyRefreshRateInMinutes));
diff --git a/update_manager/real_device_policy_provider.h b/update_manager/real_device_policy_provider.h
index f60d9eb..b052d59 100644
--- a/update_manager/real_device_policy_provider.h
+++ b/update_manager/real_device_policy_provider.h
@@ -8,12 +8,12 @@
#include <set>
#include <string>
+#include <chromeos/message_loops/message_loop.h>
#include <gtest/gtest_prod.h> // for FRIEND_TEST
#include <policy/libpolicy.h>
#include "update_engine/dbus_wrapper_interface.h"
#include "update_engine/update_manager/device_policy_provider.h"
-#include "update_engine/update_manager/event_loop.h"
#include "update_engine/update_manager/generic_variables.h"
namespace chromeos_update_manager {
@@ -116,7 +116,8 @@
policy::PolicyProvider* policy_provider_;
// Used to schedule refreshes of the device policy.
- EventId scheduled_refresh_ = kEventIdNull;
+ chromeos::MessageLoop::TaskId scheduled_refresh_ =
+ chromeos::MessageLoop::kTaskIdNull;
// The DBus interface (mockable) and a session manager proxy.
chromeos_update_engine::DBusWrapperInterface* const dbus_;
diff --git a/update_manager/real_device_policy_provider_unittest.cc b/update_manager/real_device_policy_provider_unittest.cc
index 4c385f8..75d7c48 100644
--- a/update_manager/real_device_policy_provider_unittest.cc
+++ b/update_manager/real_device_policy_provider_unittest.cc
@@ -7,6 +7,9 @@
#include <memory>
#include <chromeos/dbus/service_constants.h>
+#include <chromeos/message_loops/fake_message_loop.h>
+#include <chromeos/message_loops/message_loop.h>
+#include <chromeos/message_loops/message_loop_utils.h>
#include <gtest/gtest.h>
#include <policy/mock_device_policy.h>
#include <policy/mock_libpolicy.h>
@@ -16,7 +19,8 @@
#include "update_engine/update_manager/umtest_utils.h"
using base::TimeDelta;
-using chromeos_update_engine::test_utils::RunGMainLoopMaxIterations;
+using chromeos::MessageLoop;
+using chromeos::MessageLoopRunMaxIterations;
using std::set;
using std::string;
using std::unique_ptr;
@@ -43,6 +47,7 @@
class UmRealDevicePolicyProviderTest : public ::testing::Test {
protected:
void SetUp() override {
+ loop_.SetAsCurrent();
provider_.reset(new RealDevicePolicyProvider(&mock_dbus_,
&mock_policy_provider_));
// By default, we have a device policy loaded. Tests can call
@@ -54,7 +59,7 @@
void TearDown() override {
// Check for leaked callbacks on the main loop.
- EXPECT_EQ(0, RunGMainLoopMaxIterations(100));
+ EXPECT_EQ(0, MessageLoopRunMaxIterations(MessageLoop::current(), 100));
// We need to set these expectation before the object is destroyed but
// after it finished running the test so the values of signal_callback_ and
@@ -67,6 +72,7 @@
EXPECT_CALL(mock_dbus_, ProxyUnref(kFakeManagerProxy));
provider_.reset();
+ EXPECT_FALSE(loop_.PendingTasks());
}
void SetUpNonExistentDevicePolicy() {
@@ -111,6 +117,7 @@
SaveArg<3>(&signal_callback_data_)));
}
+ chromeos::FakeMessageLoop loop_{nullptr};
chromeos_update_engine::MockDBusWrapper mock_dbus_;
testing::NiceMock<policy::MockDevicePolicy> mock_device_policy_;
testing::NiceMock<policy::MockPolicyProvider> mock_policy_provider_;
@@ -124,7 +131,7 @@
TEST_F(UmRealDevicePolicyProviderTest, RefreshScheduledTest) {
// Check that the RefreshPolicy gets scheduled by checking the EventId.
EXPECT_TRUE(provider_->Init());
- EXPECT_NE(kEventIdNull, provider_->scheduled_refresh_);
+ EXPECT_NE(MessageLoop::kTaskIdNull, provider_->scheduled_refresh_);
}
TEST_F(UmRealDevicePolicyProviderTest, FirstReload) {
diff --git a/update_manager/update_manager-inl.h b/update_manager/update_manager-inl.h
index 06bda93..9463c76 100644
--- a/update_manager/update_manager-inl.h
+++ b/update_manager/update_manager-inl.h
@@ -9,9 +9,9 @@
#include <string>
#include <base/bind.h>
+#include <chromeos/message_loops/message_loop.h>
#include "update_engine/update_manager/evaluation_context.h"
-#include "update_engine/update_manager/event_loop.h"
namespace chromeos_update_manager {
@@ -144,7 +144,7 @@
base::Closure eval_callback = base::Bind(
&UpdateManager::OnPolicyReadyToEvaluate<R, ExpectedArgs...>,
base::Unretained(this), ec, callback, policy_method, args...);
- RunFromMainLoop(eval_callback);
+ chromeos::MessageLoop::current()->PostTask(eval_callback);
}
} // namespace chromeos_update_manager
diff --git a/update_manager/update_manager_unittest.cc b/update_manager/update_manager_unittest.cc
index 1d79c5c..cf097fa 100644
--- a/update_manager/update_manager_unittest.cc
+++ b/update_manager/update_manager_unittest.cc
@@ -14,12 +14,15 @@
#include <vector>
#include <base/bind.h>
+#include <base/test/simple_test_clock.h>
#include <base/time/time.h>
+#include <chromeos/message_loops/fake_message_loop.h>
+#include <chromeos/message_loops/message_loop.h>
+#include <chromeos/message_loops/message_loop_utils.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "update_engine/fake_clock.h"
-#include "update_engine/test_utils.h"
#include "update_engine/update_manager/default_policy.h"
#include "update_engine/update_manager/fake_state.h"
#include "update_engine/update_manager/mock_policy.h"
@@ -29,9 +32,10 @@
using base::Callback;
using base::Time;
using base::TimeDelta;
+using chromeos::MessageLoop;
+using chromeos::MessageLoopRunMaxIterations;
using chromeos_update_engine::ErrorCode;
using chromeos_update_engine::FakeClock;
-using chromeos_update_engine::test_utils::RunGMainLoopMaxIterations;
using std::pair;
using std::string;
using std::tuple;
@@ -61,11 +65,18 @@
class UmUpdateManagerTest : public ::testing::Test {
protected:
void SetUp() override {
+ loop_.SetAsCurrent();
fake_state_ = new FakeState();
umut_.reset(new UpdateManager(&fake_clock_, TimeDelta::FromSeconds(5),
TimeDelta::FromSeconds(1), fake_state_));
}
+ void TearDown() override {
+ EXPECT_FALSE(loop_.PendingTasks());
+ }
+
+ base::SimpleTestClock test_clock_;
+ chromeos::FakeMessageLoop loop_{&test_clock_};
FakeState* fake_state_; // Owned by the umut_.
FakeClock fake_clock_;
unique_ptr<UpdateManager> umut_;
@@ -231,7 +242,7 @@
umut_->AsyncPolicyRequest(callback, &Policy::UpdateCheckAllowed);
// The callback should wait until we run the main loop for it to be executed.
EXPECT_EQ(0, calls.size());
- RunGMainLoopMaxIterations(100);
+ MessageLoopRunMaxIterations(MessageLoop::current(), 100);
EXPECT_EQ(1, calls.size());
}
@@ -248,14 +259,14 @@
umut_->AsyncPolicyRequest(callback, &Policy::UpdateCheckAllowed);
// Run the main loop, ensure that policy was attempted once before deferring
// to the default.
- RunGMainLoopMaxIterations(100);
+ MessageLoopRunMaxIterations(MessageLoop::current(), 100);
EXPECT_EQ(1, num_called);
ASSERT_EQ(1, calls.size());
EXPECT_EQ(EvalStatus::kSucceeded, calls[0].first);
// Wait for the timeout to expire, run the main loop again, ensure that
// nothing happened.
- sleep(2);
- RunGMainLoopMaxIterations(10);
+ test_clock_.Advance(TimeDelta::FromSeconds(2));
+ MessageLoopRunMaxIterations(MessageLoop::current(), 10);
EXPECT_EQ(1, num_called);
EXPECT_EQ(1, calls.size());
}
@@ -276,24 +287,24 @@
umut_->AsyncPolicyRequest(callback, &Policy::UpdateCheckAllowed);
// Run the main loop, ensure that policy was attempted once but the callback
// was not invoked.
- RunGMainLoopMaxIterations(100);
+ MessageLoopRunMaxIterations(MessageLoop::current(), 100);
EXPECT_EQ(1, num_called);
EXPECT_EQ(0, calls.size());
// Wait for the expiration timeout to expire, run the main loop again,
// ensure that reevaluation occurred but callback was not invoked (i.e.
// default policy was not consulted).
- sleep(2);
+ test_clock_.Advance(TimeDelta::FromSeconds(2));
fake_clock_.SetWallclockTime(fake_clock_.GetWallclockTime() +
TimeDelta::FromSeconds(2));
- RunGMainLoopMaxIterations(10);
+ MessageLoopRunMaxIterations(MessageLoop::current(), 10);
EXPECT_EQ(2, num_called);
EXPECT_EQ(0, calls.size());
// Wait for reevaluation due to delay to happen, ensure that it occurs and
// that the callback is invoked.
- sleep(2);
+ test_clock_.Advance(TimeDelta::FromSeconds(2));
fake_clock_.SetWallclockTime(fake_clock_.GetWallclockTime() +
TimeDelta::FromSeconds(2));
- RunGMainLoopMaxIterations(10);
+ MessageLoopRunMaxIterations(MessageLoop::current(), 10);
EXPECT_EQ(3, num_called);
ASSERT_EQ(1, calls.size());
EXPECT_EQ(EvalStatus::kSucceeded, calls[0].first);
diff --git a/update_manager/variable.h b/update_manager/variable.h
index c47f488..9f80461 100644
--- a/update_manager/variable.h
+++ b/update_manager/variable.h
@@ -12,10 +12,9 @@
#include <base/bind.h>
#include <base/logging.h>
#include <base/time/time.h>
+#include <chromeos/message_loops/message_loop.h>
#include <gtest/gtest_prod.h> // for FRIEND_TEST
-#include "update_engine/update_manager/event_loop.h"
-
namespace chromeos_update_manager {
// The VariableMode specifies important behavior of the variable in terms of
@@ -108,8 +107,9 @@
// to avoid scheduling these callbacks when it is not needed, we check
// first the list of observers.
if (!observer_list_.empty()) {
- RunFromMainLoop(base::Bind(&BaseVariable::OnValueChangedNotification,
- base::Unretained(this)));
+ chromeos::MessageLoop::current()->PostTask(
+ base::Bind(&BaseVariable::OnValueChangedNotification,
+ base::Unretained(this)));
}
}
diff --git a/update_manager/variable_unittest.cc b/update_manager/variable_unittest.cc
index 642b5dd..000176b 100644
--- a/update_manager/variable_unittest.cc
+++ b/update_manager/variable_unittest.cc
@@ -6,12 +6,14 @@
#include <vector>
+#include <chromeos/message_loops/fake_message_loop.h>
+#include <chromeos/message_loops/message_loop.h>
+#include <chromeos/message_loops/message_loop_utils.h>
#include <gtest/gtest.h>
-#include "update_engine/test_utils.h"
-
using base::TimeDelta;
-using chromeos_update_engine::test_utils::RunGMainLoopMaxIterations;
+using chromeos::MessageLoop;
+using chromeos::MessageLoopRunMaxIterations;
using std::string;
using std::vector;
@@ -37,26 +39,35 @@
DISALLOW_COPY_AND_ASSIGN(DefaultVariable);
};
-TEST(UmBaseVariableTest, GetNameTest) {
+class UmBaseVariableTest : public ::testing::Test {
+ protected:
+ void SetUp() override {
+ loop_.SetAsCurrent();
+ }
+
+ chromeos::FakeMessageLoop loop_{nullptr};
+};
+
+TEST_F(UmBaseVariableTest, GetNameTest) {
DefaultVariable<int> var("var", kVariableModeConst);
EXPECT_EQ(var.GetName(), string("var"));
}
-TEST(UmBaseVariableTest, GetModeTest) {
+TEST_F(UmBaseVariableTest, GetModeTest) {
DefaultVariable<int> var("var", kVariableModeConst);
EXPECT_EQ(var.GetMode(), kVariableModeConst);
DefaultVariable<int> other_var("other_var", kVariableModePoll);
EXPECT_EQ(other_var.GetMode(), kVariableModePoll);
}
-TEST(UmBaseVariableTest, DefaultPollIntervalTest) {
+TEST_F(UmBaseVariableTest, DefaultPollIntervalTest) {
DefaultVariable<int> const_var("const_var", kVariableModeConst);
EXPECT_EQ(const_var.GetPollInterval(), TimeDelta());
DefaultVariable<int> poll_var("poll_var", kVariableModePoll);
EXPECT_EQ(poll_var.GetPollInterval(), TimeDelta::FromMinutes(5));
}
-TEST(UmBaseVariableTest, GetPollIntervalTest) {
+TEST_F(UmBaseVariableTest, GetPollIntervalTest) {
DefaultVariable<int> var("var", TimeDelta::FromMinutes(3));
EXPECT_EQ(var.GetMode(), kVariableModePoll);
EXPECT_EQ(var.GetPollInterval(), TimeDelta::FromMinutes(3));
@@ -72,7 +83,7 @@
vector<BaseVariable*> calls_;
};
-TEST(UmBaseVariableTest, RepeatedObserverTest) {
+TEST_F(UmBaseVariableTest, RepeatedObserverTest) {
DefaultVariable<int> var("var", kVariableModeAsync);
BaseVariableObserver observer;
var.AddObserver(&observer);
@@ -85,14 +96,14 @@
EXPECT_EQ(var.observer_list_.size(), 0);
}
-TEST(UmBaseVariableTest, NotifyValueChangedTest) {
+TEST_F(UmBaseVariableTest, NotifyValueChangedTest) {
DefaultVariable<int> var("var", kVariableModeAsync);
BaseVariableObserver observer1;
var.AddObserver(&observer1);
// Simulate a value change on the variable's implementation.
var.NotifyValueChanged();
ASSERT_EQ(0, observer1.calls_.size());
- RunGMainLoopMaxIterations(100);
+ MessageLoopRunMaxIterations(MessageLoop::current(), 100);
ASSERT_EQ(1, observer1.calls_.size());
// Check that the observer is called with the right argument.
@@ -101,7 +112,7 @@
BaseVariableObserver observer2;
var.AddObserver(&observer2);
var.NotifyValueChanged();
- RunGMainLoopMaxIterations(100);
+ MessageLoopRunMaxIterations(MessageLoop::current(), 100);
// Check that all the observers are called.
EXPECT_EQ(2, observer1.calls_.size());
@@ -135,7 +146,7 @@
// Tests that we can remove an observer from a Variable on the ValueChanged()
// call to that observer.
-TEST(UmBaseVariableTest, NotifyValueRemovesObserversTest) {
+TEST_F(UmBaseVariableTest, NotifyValueRemovesObserversTest) {
DefaultVariable<int> var("var", kVariableModeAsync);
BaseVariableObserverRemover observer1;
BaseVariableObserverRemover observer2;
@@ -150,7 +161,7 @@
observer2.OnCallRemoveObserver(&observer2);
var.NotifyValueChanged();
- RunGMainLoopMaxIterations(100);
+ MessageLoopRunMaxIterations(MessageLoop::current(), 100);
EXPECT_EQ(1, observer1.get_calls() + observer2.get_calls());
}