base: Remove MessageLoop usage from tests
This patch removes MessageLoop usage from base unittests *except* for the
following ones:
- MessageLoop's own unit and perf tests
- SequenceManager unit and perf tests (need to keep MessageLoop interop
working for now)
- FileWatcher (needs a controlled threading environment)
- ObserverListThreadSafe (ditto)
- TaskObserverPerfTest (measures MessageLoop overhead)
- ThreadTest (Thread API needs updating)
These tests will eventually be migrated to the MessageLoop's
replacement or removed.
Bug: 891670
Change-Id: Ic32495a77819b9644cca823d64dea77754cf5c84
Reviewed-on: https://chromium-review.googlesource.com/c/1336147
Reviewed-by: Sami Kyöstilä <[email protected]>
Reviewed-by: François Doray <[email protected]>
Commit-Queue: Sami Kyöstilä <[email protected]>
Cr-Commit-Position: refs/heads/master@{#609257}
diff --git a/base/synchronization/waitable_event_watcher_unittest.cc b/base/synchronization/waitable_event_watcher_unittest.cc
index ec056eff..c1cb720 100644
--- a/base/synchronization/waitable_event_watcher_unittest.cc
+++ b/base/synchronization/waitable_event_watcher_unittest.cc
@@ -8,11 +8,12 @@
#include "base/callback.h"
#include "base/macros.h"
#include "base/memory/ptr_util.h"
-#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "base/synchronization/waitable_event.h"
+#include "base/test/scoped_task_environment.h"
#include "base/threading/platform_thread.h"
#include "base/threading/sequenced_task_runner_handle.h"
+#include "base/threading/thread_task_runner_handle.h"
#include "build/build_config.h"
#include "testing/gtest/include/gtest/gtest.h"
@@ -20,12 +21,12 @@
namespace {
-// The message loops on which each waitable event timer should be tested.
-const MessageLoop::Type testing_message_loops[] = {
- MessageLoop::TYPE_DEFAULT,
- MessageLoop::TYPE_IO,
+// The main thread types on which each waitable event should be tested.
+const test::ScopedTaskEnvironment::MainThreadType testing_main_threads[] = {
+ test::ScopedTaskEnvironment::MainThreadType::DEFAULT,
+ test::ScopedTaskEnvironment::MainThreadType::IO,
#if !defined(OS_IOS) // iOS does not allow direct running of the UI loop.
- MessageLoop::TYPE_UI,
+ test::ScopedTaskEnvironment::MainThreadType::UI,
#endif
};
@@ -48,10 +49,11 @@
} // namespace
class WaitableEventWatcherTest
- : public testing::TestWithParam<MessageLoop::Type> {};
+ : public testing::TestWithParam<
+ test::ScopedTaskEnvironment::MainThreadType> {};
TEST_P(WaitableEventWatcherTest, BasicSignalManual) {
- MessageLoop message_loop(GetParam());
+ test::ScopedTaskEnvironment scoped_task_environment(GetParam());
// A manual-reset event that is not yet signaled.
WaitableEvent event(WaitableEvent::ResetPolicy::MANUAL,
@@ -69,7 +71,7 @@
}
TEST_P(WaitableEventWatcherTest, BasicSignalAutomatic) {
- MessageLoop message_loop(GetParam());
+ test::ScopedTaskEnvironment scoped_task_environment(GetParam());
WaitableEvent event(WaitableEvent::ResetPolicy::AUTOMATIC,
WaitableEvent::InitialState::NOT_SIGNALED);
@@ -87,7 +89,7 @@
}
TEST_P(WaitableEventWatcherTest, BasicCancel) {
- MessageLoop message_loop(GetParam());
+ test::ScopedTaskEnvironment scoped_task_environment(GetParam());
// A manual-reset event that is not yet signaled.
WaitableEvent event(WaitableEvent::ResetPolicy::MANUAL,
@@ -102,7 +104,7 @@
}
TEST_P(WaitableEventWatcherTest, CancelAfterSet) {
- MessageLoop message_loop(GetParam());
+ test::ScopedTaskEnvironment scoped_task_environment(GetParam());
// A manual-reset event that is not yet signaled.
WaitableEvent event(WaitableEvent::ResetPolicy::MANUAL,
@@ -130,8 +132,8 @@
EXPECT_EQ(1, counter);
}
-TEST_P(WaitableEventWatcherTest, OutlivesMessageLoop) {
- // Simulate a MessageLoop that dies before an WaitableEventWatcher. This
+TEST_P(WaitableEventWatcherTest, OutlivesTaskEnvironment) {
+ // Simulate a task environment that dies before an WaitableEventWatcher. This
// ordinarily doesn't happen when people use the Thread class, but it can
// happen when people use the Singleton pattern or atexit.
WaitableEvent event(WaitableEvent::ResetPolicy::MANUAL,
@@ -139,7 +141,7 @@
{
std::unique_ptr<WaitableEventWatcher> watcher;
{
- MessageLoop message_loop(GetParam());
+ test::ScopedTaskEnvironment scoped_task_environment(GetParam());
watcher = std::make_unique<WaitableEventWatcher>();
watcher->StartWatching(&event, BindOnce(&QuitWhenSignaled),
@@ -149,7 +151,7 @@
}
TEST_P(WaitableEventWatcherTest, SignaledAtStartManual) {
- MessageLoop message_loop(GetParam());
+ test::ScopedTaskEnvironment scoped_task_environment(GetParam());
WaitableEvent event(WaitableEvent::ResetPolicy::MANUAL,
WaitableEvent::InitialState::SIGNALED);
@@ -164,7 +166,7 @@
}
TEST_P(WaitableEventWatcherTest, SignaledAtStartAutomatic) {
- MessageLoop message_loop(GetParam());
+ test::ScopedTaskEnvironment scoped_task_environment(GetParam());
WaitableEvent event(WaitableEvent::ResetPolicy::AUTOMATIC,
WaitableEvent::InitialState::SIGNALED);
@@ -180,7 +182,7 @@
}
TEST_P(WaitableEventWatcherTest, StartWatchingInCallback) {
- MessageLoop message_loop(GetParam());
+ test::ScopedTaskEnvironment scoped_task_environment(GetParam());
WaitableEvent event(WaitableEvent::ResetPolicy::MANUAL,
WaitableEvent::InitialState::NOT_SIGNALED);
@@ -204,7 +206,7 @@
}
TEST_P(WaitableEventWatcherTest, MultipleWatchersManual) {
- MessageLoop message_loop(GetParam());
+ test::ScopedTaskEnvironment scoped_task_environment(GetParam());
WaitableEvent event(WaitableEvent::ResetPolicy::MANUAL,
WaitableEvent::InitialState::NOT_SIGNALED);
@@ -239,7 +241,7 @@
// Tests that only one async waiter gets called back for an auto-reset event.
TEST_P(WaitableEventWatcherTest, MultipleWatchersAutomatic) {
- MessageLoop message_loop(GetParam());
+ test::ScopedTaskEnvironment scoped_task_environment(GetParam());
WaitableEvent event(WaitableEvent::ResetPolicy::AUTOMATIC,
WaitableEvent::InitialState::NOT_SIGNALED);
@@ -299,17 +301,18 @@
// To help detect errors around deleting WaitableEventWatcher, an additional
// bool parameter is used to test sleeping between watching and deletion.
class WaitableEventWatcherDeletionTest
- : public testing::TestWithParam<std::tuple<MessageLoop::Type, bool>> {};
+ : public testing::TestWithParam<
+ std::tuple<test::ScopedTaskEnvironment::MainThreadType, bool>> {};
TEST_P(WaitableEventWatcherDeletionTest, DeleteUnder) {
- MessageLoop::Type message_loop_type;
+ test::ScopedTaskEnvironment::MainThreadType main_thread_type;
bool delay_after_delete;
- std::tie(message_loop_type, delay_after_delete) = GetParam();
+ std::tie(main_thread_type, delay_after_delete) = GetParam();
// Delete the WaitableEvent out from under the Watcher. This is explictly
// allowed by the interface.
- MessageLoop message_loop(message_loop_type);
+ test::ScopedTaskEnvironment scoped_task_environment(main_thread_type);
{
WaitableEventWatcher watcher;
@@ -334,13 +337,13 @@
}
TEST_P(WaitableEventWatcherDeletionTest, SignalAndDelete) {
- MessageLoop::Type message_loop_type;
+ test::ScopedTaskEnvironment::MainThreadType main_thread_type;
bool delay_after_delete;
- std::tie(message_loop_type, delay_after_delete) = GetParam();
+ std::tie(main_thread_type, delay_after_delete) = GetParam();
// Signal and immediately delete the WaitableEvent out from under the Watcher.
- MessageLoop message_loop(message_loop_type);
+ test::ScopedTaskEnvironment scoped_task_environment(main_thread_type);
{
WaitableEventWatcher watcher;
@@ -371,13 +374,13 @@
// Tests deleting the WaitableEventWatcher between signaling the event and
// when the callback should be run.
TEST_P(WaitableEventWatcherDeletionTest, DeleteWatcherBeforeCallback) {
- MessageLoop::Type message_loop_type;
+ test::ScopedTaskEnvironment::MainThreadType main_thread_type;
bool delay_after_delete;
- std::tie(message_loop_type, delay_after_delete) = GetParam();
+ std::tie(main_thread_type, delay_after_delete) = GetParam();
- MessageLoop message_loop(message_loop_type);
+ test::ScopedTaskEnvironment scoped_task_environment(main_thread_type);
scoped_refptr<SingleThreadTaskRunner> task_runner =
- message_loop.task_runner();
+ ThreadTaskRunnerHandle::Get();
// Flag used to esnure that the |watcher_callback| never runs.
bool did_callback = false;
@@ -418,12 +421,11 @@
INSTANTIATE_TEST_CASE_P(,
WaitableEventWatcherTest,
- testing::ValuesIn(testing_message_loops));
+ testing::ValuesIn(testing_main_threads));
INSTANTIATE_TEST_CASE_P(
,
WaitableEventWatcherDeletionTest,
- testing::Combine(testing::ValuesIn(testing_message_loops),
- testing::Bool()));
+ testing::Combine(testing::ValuesIn(testing_main_threads), testing::Bool()));
} // namespace base