0

Revert "[base] Make test Run() timeouts fatal unless using GTest."

This reverts commit e7f522eeb9.

Reason for revert: suspect causing compile failure on linux-chromeos-chrome.

Sample build: https://ci.chromium.org/p/chrome/builders/ci/linux-chromeos-chrome/2923

Sample log: https://logs.chromium.org/logs/chrome/buildbucket/cr-buildbucket.appspot.com/8888898680396285712/+/steps/compile/0/stdout?format=raw

E.g.
../../chrome/browser/ui/ash/assistant/assistant_test_mixin.cc:323:42: error: variable has incomplete type 'const base::test::ScopedRunLoopTimeout'
  const base::test::ScopedRunLoopTimeout run_timeout(wait_timeout);
                                         ^
../../base/run_loop.h:28:7: note: forward declaration of 'base::test::ScopedRunLoopTimeout'
class ScopedRunLoopTimeout;
      ^
../../chrome/browser/ui/ash/assistant/assistant_test_mixin.cc:356:42: error: variable has incomplete type 'const base::test::ScopedRunLoopTimeout'
  const base::test::ScopedRunLoopTimeout run_timeout(wait_timeout);
                                         ^
../../base/run_loop.h:28:7: note: forward declaration of 'base::test::ScopedRunLoopTimeout'
class ScopedRunLoopTimeout;
      ^
../../chrome/browser/ui/ash/assistant/assistant_test_mixin.cc:371:42: error: variable has incomplete type 'const base::test::ScopedRunLoopTimeout'
  const base::test::ScopedRunLoopTimeout run_timeout(wait_timeout);
                                         ^
../../base/run_loop.h:28:7: note: forward declaration of 'base::test::ScopedRunLoopTimeout'
class ScopedRunLoopTimeout;
      ^
3 errors generated.


Original change's description:
> [base] Make test Run() timeouts fatal unless using GTest.
> 
> The Run() timeouts set for tests now default to crashing the calling
> process. This ensures that Run() timeouts cause visible failures
> regardless of whether the calling suite uses GTest.  Suites which do use
> GTest can switch to having timeouts reported via GTEST_FAIL() by calling:
> 
>   base::test::ScopedRunLoopTimeout::SetUseGTestFailOnTimeout();
> 
> Test suites derived from base::TestSuite will have this option set for
> them as part of the TestSuite initialization.
> 
> The RunLoop::ScopedRunTimeoutForTest helper is moved to
> base::test::ScopedRunLoopTimeout, to allow it to have GTest dependencies.
> 
> Bug: 1021777, 1014767
> Change-Id: Id372c666c6455e56e52034ae528b417a0c23143c
> Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1932464
> Commit-Queue: Wez <wez@chromium.org>
> Auto-Submit: Wez <wez@chromium.org>
> Reviewed-by: Scott Violet <sky@chromium.org>
> Reviewed-by: Gabriel Charette <gab@chromium.org>
> Cr-Commit-Position: refs/heads/master@{#739760}

TBR=sky@chromium.org,wez@chromium.org,gab@chromium.org,kmarshall@chromium.org

Change-Id: Ib0927699042f216971448da8b1d70619c0545367
No-Presubmit: true
No-Tree-Checks: true
No-Try: true
Bug: 1021777, 1014767
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2045697
Reviewed-by: Takashi Sakamoto <tasak@google.com>
Commit-Queue: Takashi Sakamoto <tasak@google.com>
Cr-Commit-Position: refs/heads/master@{#739790}
This commit is contained in:
Takashi Sakamoto
2020-02-10 04:58:55 +00:00
committed by Commit Bot
parent 736aeeec58
commit 21fa8d0bbd
28 changed files with 205 additions and 362 deletions

@@ -2700,7 +2700,6 @@ test("base_unittests") {
"test/scoped_feature_list_unittest.cc", "test/scoped_feature_list_unittest.cc",
"test/scoped_mock_clock_override_unittest.cc", "test/scoped_mock_clock_override_unittest.cc",
"test/scoped_mock_time_message_loop_task_runner_unittest.cc", "test/scoped_mock_time_message_loop_task_runner_unittest.cc",
"test/scoped_run_loop_timeout_unittest.cc",
"test/task_environment_unittest.cc", "test/task_environment_unittest.cc",
"test/test_mock_time_task_runner_unittest.cc", "test/test_mock_time_task_runner_unittest.cc",
"test/test_pending_task_unittest.cc", "test/test_pending_task_unittest.cc",

@@ -15,7 +15,9 @@ namespace base {
namespace fuchsia { namespace fuchsia {
ServiceDirectoryTestBase::ServiceDirectoryTestBase() ServiceDirectoryTestBase::ServiceDirectoryTestBase()
: run_timeout_(TestTimeouts::action_timeout()) { : run_timeout_(TestTimeouts::action_timeout(), BindRepeating([]() {
ADD_FAILURE() << "Run() timed out.";
})) {
// Mount service dir and publish the service. // Mount service dir and publish the service.
outgoing_directory_ = std::make_unique<sys::OutgoingDirectory>(); outgoing_directory_ = std::make_unique<sys::OutgoingDirectory>();
fidl::InterfaceHandle<::fuchsia::io::Directory> directory; fidl::InterfaceHandle<::fuchsia::io::Directory> directory;

@@ -13,7 +13,7 @@
#include "base/fuchsia/scoped_service_binding.h" #include "base/fuchsia/scoped_service_binding.h"
#include "base/fuchsia/test_interface_impl.h" #include "base/fuchsia/test_interface_impl.h"
#include "base/fuchsia/testfidl/cpp/fidl.h" #include "base/fuchsia/testfidl/cpp/fidl.h"
#include "base/test/scoped_run_loop_timeout.h" #include "base/run_loop.h"
#include "base/test/task_environment.h" #include "base/test/task_environment.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
@@ -29,7 +29,7 @@ class ServiceDirectoryTestBase : public testing::Test {
zx_status_t expected_error); zx_status_t expected_error);
protected: protected:
const test::ScopedRunLoopTimeout run_timeout_; const RunLoop::ScopedRunTimeoutForTest run_timeout_;
base::test::SingleThreadTaskEnvironment task_environment_{ base::test::SingleThreadTaskEnvironment task_environment_{
base::test::SingleThreadTaskEnvironment::MainThreadType::IO}; base::test::SingleThreadTaskEnvironment::MainThreadType::IO};

@@ -34,18 +34,49 @@ void ProxyToTaskRunner(scoped_refptr<SequencedTaskRunner> task_runner,
task_runner->PostTask(FROM_HERE, std::move(closure)); task_runner->PostTask(FROM_HERE, std::move(closure));
} }
ThreadLocalPointer<const RunLoop::RunLoopTimeout>& RunLoopTimeoutTLS() { ThreadLocalPointer<RunLoop::ScopedRunTimeoutForTest>&
static NoDestructor<ThreadLocalPointer<const RunLoop::RunLoopTimeout>> tls; ScopedRunTimeoutForTestTLS() {
static NoDestructor<ThreadLocalPointer<RunLoop::ScopedRunTimeoutForTest>> tls;
return *tls; return *tls;
} }
void OnRunLoopTimeout(RunLoop* run_loop, OnceClosure on_timeout) { void OnRunTimeout(RunLoop* run_loop, OnceClosure on_timeout) {
run_loop->Quit(); run_loop->Quit();
std::move(on_timeout).Run(); std::move(on_timeout).Run();
} }
} // namespace } // namespace
RunLoop::ScopedRunTimeoutForTest::ScopedRunTimeoutForTest(
TimeDelta timeout,
RepeatingClosure on_timeout)
: timeout_(timeout),
on_timeout_(std::move(on_timeout)),
nested_timeout_(ScopedRunTimeoutForTestTLS().Get()) {
DCHECK_GT(timeout_, TimeDelta());
DCHECK(on_timeout_);
ScopedRunTimeoutForTestTLS().Set(this);
}
RunLoop::ScopedRunTimeoutForTest::~ScopedRunTimeoutForTest() {
ScopedRunTimeoutForTestTLS().Set(nested_timeout_);
}
// static
const RunLoop::ScopedRunTimeoutForTest*
RunLoop::ScopedRunTimeoutForTest::Current() {
return ScopedRunTimeoutForTestTLS().Get();
}
RunLoop::ScopedDisableRunTimeoutForTest::ScopedDisableRunTimeoutForTest()
: nested_timeout_(ScopedRunTimeoutForTestTLS().Get()) {
ScopedRunTimeoutForTestTLS().Set(nullptr);
}
RunLoop::ScopedDisableRunTimeoutForTest::~ScopedDisableRunTimeoutForTest() {
ScopedRunTimeoutForTestTLS().Set(nested_timeout_);
}
RunLoop::Delegate::Delegate() { RunLoop::Delegate::Delegate() {
// The Delegate can be created on another thread. It is only bound in // The Delegate can be created on another thread. It is only bound in
// RegisterDelegateForCurrentThread(). // RegisterDelegateForCurrentThread().
@@ -106,16 +137,16 @@ void RunLoop::Run() {
if (!BeforeRun()) if (!BeforeRun())
return; return;
// If there is a RunLoopTimeout active then set the timeout. // If there is a ScopedRunTimeoutForTest active then set the timeout.
// TODO(crbug.com/905412): Use real-time for Run() timeouts so that they // TODO(crbug.com/905412): Use real-time for Run() timeouts so that they
// can be applied even in tests which mock TimeTicks::Now(). // can be applied even in tests which mock TimeTicks::Now().
CancelableOnceClosure cancelable_timeout; CancelableOnceClosure cancelable_timeout;
const RunLoopTimeout* run_timeout = GetTimeoutForCurrentThread(); ScopedRunTimeoutForTest* run_timeout = ScopedRunTimeoutForTestTLS().Get();
if (run_timeout) { if (run_timeout) {
cancelable_timeout.Reset( cancelable_timeout.Reset(
BindOnce(&OnRunLoopTimeout, Unretained(this), run_timeout->on_timeout)); BindOnce(&OnRunTimeout, Unretained(this), run_timeout->on_timeout()));
origin_task_runner_->PostDelayedTask( ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE, cancelable_timeout.callback(), run_timeout->timeout); FROM_HERE, cancelable_timeout.callback(), run_timeout->timeout());
} }
DCHECK_EQ(this, delegate_->active_run_loops_.top()); DCHECK_EQ(this, delegate_->active_run_loops_.top());
@@ -273,12 +304,10 @@ RunLoop::ScopedDisallowRunningForTesting::~ScopedDisallowRunningForTesting() =
#endif // DCHECK_IS_ON() #endif // DCHECK_IS_ON()
void RunLoop::RunUntilConditionForTest(RepeatingCallback<bool()> condition) { void RunLoop::RunUntilConditionForTest(RepeatingCallback<bool()> condition) {
CHECK(GetTimeoutForCurrentThread()); CHECK(ScopedRunTimeoutForTest::Current());
const RunLoopTimeout* old_timeout = GetTimeoutForCurrentThread(); OnceClosure on_failure = ScopedRunTimeoutForTest::Current()->on_timeout();
RunLoopTimeout run_timeout; ScopedRunTimeoutForTest run_timeout(
run_timeout.timeout = old_timeout->timeout; ScopedRunTimeoutForTest::Current()->timeout(), DoNothing());
run_timeout.on_timeout = DoNothing();
RunLoopTimeoutTLS().Set(&run_timeout);
auto check_condition = BindRepeating( auto check_condition = BindRepeating(
[](const RepeatingCallback<bool()>& condition, RunLoop* loop) { [](const RepeatingCallback<bool()>& condition, RunLoop* loop) {
if (condition.Run()) if (condition.Run())
@@ -290,22 +319,7 @@ void RunLoop::RunUntilConditionForTest(RepeatingCallback<bool()> condition) {
check_condition); check_condition);
Run(); Run();
if (!condition.Run()) if (!condition.Run())
old_timeout->on_timeout.Run(); std::move(on_failure).Run();
RunLoopTimeoutTLS().Set(old_timeout);
}
RunLoop::RunLoopTimeout::RunLoopTimeout() = default;
RunLoop::RunLoopTimeout::~RunLoopTimeout() = default;
// static
void RunLoop::SetTimeoutForCurrentThread(const RunLoopTimeout* timeout) {
RunLoopTimeoutTLS().Set(timeout);
}
// static
const RunLoop::RunLoopTimeout* RunLoop::GetTimeoutForCurrentThread() {
return RunLoopTimeoutTLS().Get();
} }
bool RunLoop::BeforeRun() { bool RunLoop::BeforeRun() {

@@ -12,7 +12,6 @@
#include "base/callback.h" #include "base/callback.h"
#include "base/compiler_specific.h" #include "base/compiler_specific.h"
#include "base/containers/stack.h" #include "base/containers/stack.h"
#include "base/gtest_prod_util.h"
#include "base/macros.h" #include "base/macros.h"
#include "base/memory/ref_counted.h" #include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
@@ -23,12 +22,6 @@
#include "build/build_config.h" #include "build/build_config.h"
namespace base { namespace base {
namespace test {
class ScopedRunLoopTimeout;
class ScopedDisableRunLoopTimeout;
} // namespace test
#if defined(OS_ANDROID) #if defined(OS_ANDROID)
class MessagePumpForUI; class MessagePumpForUI;
#endif #endif
@@ -253,6 +246,65 @@ class BASE_EXPORT RunLoop {
static void QuitCurrentWhenIdleDeprecated(); static void QuitCurrentWhenIdleDeprecated();
static RepeatingClosure QuitCurrentWhenIdleClosureDeprecated(); static RepeatingClosure QuitCurrentWhenIdleClosureDeprecated();
// Configures all RunLoop::Run() calls on the current thread to run the
// supplied |on_timeout| callback if they run for longer than |timeout|.
//
// Specifying Run() timeouts per-thread avoids the need to cope with Run()s
// executing concurrently with ScopedRunTimeoutForTest initialization or
// teardown, and allows "default" timeouts to be specified by suites, rather
// than explicitly configuring them for every RunLoop, in each test.
//
// Tests can temporarily override any currently-active Run() timeout, e.g. to
// allow a step to Run() for longer than the suite's default timeout, by
// creating a new ScopedRunTimeoutForTest on their stack, e.g:
//
// ScopedRunTimeoutForTest default_timeout(kDefaultRunTimeout, on_timeout);
// ... do other test stuff ...
// RunLoop().Run(); // Run for up to kDefaultRunTimeout.
// ...
// {
// ScopedRunTimeoutForTest specific_timeout(kTestSpecificTimeout, ...);
// RunLoop().Run(); // Run for up to kTestSpecificTimeout.
// }
// ...
// RunLoop().Run(); // Run for up to kDefaultRunTimeout.
//
// The currently-active timeout can also be temporarily disabled:
// ScopedDisableRunTimeoutForTest disable_timeout;
//
// TaskEnvironment applies a default Run() timeout after which a
// LOG(FATAL) is performed, to dump a crash stack for diagnosis. Tests adding
// their own Run() timeouts can use e.g. MakeExpectedNotRunClosure().
class BASE_EXPORT ScopedRunTimeoutForTest {
public:
ScopedRunTimeoutForTest(TimeDelta timeout, RepeatingClosure on_timeout);
~ScopedRunTimeoutForTest();
// Returns the active ScopedRunTimeoutForTest on the calling thread, if any,
// or null otherwise.
static const RunLoop::ScopedRunTimeoutForTest* Current();
TimeDelta timeout() const { return timeout_; }
const RepeatingClosure& on_timeout() const { return on_timeout_; }
private:
const TimeDelta timeout_;
const RepeatingClosure on_timeout_;
ScopedRunTimeoutForTest* const nested_timeout_;
DISALLOW_COPY_AND_ASSIGN(ScopedRunTimeoutForTest);
};
class BASE_EXPORT ScopedDisableRunTimeoutForTest {
public:
ScopedDisableRunTimeoutForTest();
~ScopedDisableRunTimeoutForTest();
private:
ScopedRunTimeoutForTest* const nested_timeout_;
DISALLOW_COPY_AND_ASSIGN(ScopedDisableRunTimeoutForTest);
};
// Run() will DCHECK if called while there's a ScopedDisallowRunningForTesting // Run() will DCHECK if called while there's a ScopedDisallowRunningForTesting
// in scope on its thread. This is useful to add safety to some test // in scope on its thread. This is useful to add safety to some test
// constructs which allow multiple task runners to share the main thread in // constructs which allow multiple task runners to share the main thread in
@@ -283,15 +335,6 @@ class BASE_EXPORT RunLoop {
// is not feasible. Use Run() and QuitClosure() where possible. // is not feasible. Use Run() and QuitClosure() where possible.
void RunUntilConditionForTest(RepeatingCallback<bool()> condition); void RunUntilConditionForTest(RepeatingCallback<bool()> condition);
// Support for //base/test/scoped_run_loop_timeout.h.
// This must be public for access by the implementation code in run_loop.cc.
struct BASE_EXPORT RunLoopTimeout {
RunLoopTimeout();
~RunLoopTimeout();
TimeDelta timeout;
RepeatingClosure on_timeout;
};
private: private:
FRIEND_TEST_ALL_PREFIXES(MessageLoopTypedTest, RunLoopQuitOrderAfter); FRIEND_TEST_ALL_PREFIXES(MessageLoopTypedTest, RunLoopQuitOrderAfter);
@@ -307,13 +350,6 @@ class BASE_EXPORT RunLoop {
friend class MessagePumpUIApplication; friend class MessagePumpUIApplication;
#endif #endif
// Support for //base/test/scoped_run_loop_timeout.h.
friend class test::ScopedRunLoopTimeout;
friend class test::ScopedDisableRunLoopTimeout;
static void SetTimeoutForCurrentThread(const RunLoopTimeout* timeout);
static const RunLoopTimeout* GetTimeoutForCurrentThread();
// Return false to abort the Run. // Return false to abort the Run.
bool BeforeRun(); bool BeforeRun();
void AfterRun(); void AfterRun();

@@ -19,7 +19,6 @@
#include "base/synchronization/waitable_event.h" #include "base/synchronization/waitable_event.h"
#include "base/test/bind_test_util.h" #include "base/test/bind_test_util.h"
#include "base/test/gtest_util.h" #include "base/test/gtest_util.h"
#include "base/test/scoped_run_loop_timeout.h"
#include "base/test/task_environment.h" #include "base/test/task_environment.h"
#include "base/test/test_timeouts.h" #include "base/test/test_timeouts.h"
#include "base/threading/platform_thread.h" #include "base/threading/platform_thread.h"
@@ -556,11 +555,10 @@ TEST(RunLoopUntilConditionTest, FailsTestOnTimeout) {
// Expect the Run() timeout to be run when |condition| is false and the loop // Expect the Run() timeout to be run when |condition| is false and the loop
// times out. // times out.
const test::ScopedRunLoopTimeout short_timeout( const RunLoop::ScopedRunTimeoutForTest short_timeout(
TimeDelta::FromMilliseconds(10)); TimeDelta::FromMilliseconds(10),
EXPECT_FATAL_FAILURE( MakeExpectedRunAtLeastOnceClosure(FROM_HERE));
RunLoop().RunUntilConditionForTest(BindRepeating([]() { return false; })), RunLoop().RunUntilConditionForTest(BindRepeating([]() { return false; }));
"RunLoop::Run() timed out.");
} }
TEST(RunLoopUntilConditionTest, FailsTestIfConditionNotMetOnQuit) { TEST(RunLoopUntilConditionTest, FailsTestIfConditionNotMetOnQuit) {
@@ -569,13 +567,12 @@ TEST(RunLoopUntilConditionTest, FailsTestIfConditionNotMetOnQuit) {
// Expect the Run() timeout to be run when |condition| is false and the loop // Expect the Run() timeout to be run when |condition| is false and the loop
// Quit()s prematurely. // Quit()s prematurely.
const test::ScopedRunLoopTimeout short_timeout( const RunLoop::ScopedRunTimeoutForTest short_timeout(
TimeDelta::FromMilliseconds(10)); TimeDelta::FromMilliseconds(10),
MakeExpectedRunAtLeastOnceClosure(FROM_HERE));
// Running with a never-true condition will fire the on-timeout callback. // Running with a never-true condition will fire the on-timeout callback.
EXPECT_FATAL_FAILURE( RunLoop().RunUntilConditionForTest(BindRepeating([]() { return false; }));
RunLoop().RunUntilConditionForTest(BindRepeating([]() { return false; })),
"RunLoop::Run() timed out.");
} }
TEST(RunLoopUntilConditionTest, NoEffectIfConditionMetOnQuit) { TEST(RunLoopUntilConditionTest, NoEffectIfConditionMetOnQuit) {
@@ -583,8 +580,8 @@ TEST(RunLoopUntilConditionTest, NoEffectIfConditionMetOnQuit) {
RunLoop loop; RunLoop loop;
// Verify that the call does not trigger the Run() timeout. // Verify that the call does not trigger the Run() timeout.
const test::ScopedRunLoopTimeout short_timeout( const RunLoop::ScopedRunTimeoutForTest short_timeout(
TimeDelta::FromMilliseconds(10)); TimeDelta::FromMilliseconds(10), MakeExpectedNotRunClosure(FROM_HERE));
SequencedTaskRunnerHandle::Get()->PostTask(FROM_HERE, loop.QuitClosure()); SequencedTaskRunnerHandle::Get()->PostTask(FROM_HERE, loop.QuitClosure());
RunLoop().RunUntilConditionForTest(BindRepeating([]() { return true; })); RunLoop().RunUntilConditionForTest(BindRepeating([]() { return true; }));
} }
@@ -596,8 +593,8 @@ TEST(RunLoopUntilConditionTest, NoEffectIfConditionMetOnTimeout) {
// Verify that the call does not trigger the Run() timeout. // Verify that the call does not trigger the Run() timeout.
// Note that |short_timeout| must be shorter than the RunUntilConditionForTest // Note that |short_timeout| must be shorter than the RunUntilConditionForTest
// polling frequency. // polling frequency.
const test::ScopedRunLoopTimeout short_timeout( const RunLoop::ScopedRunTimeoutForTest short_timeout(
TimeDelta::FromMilliseconds(10)); TimeDelta::FromMilliseconds(10), MakeExpectedNotRunClosure(FROM_HERE));
RunLoop().RunUntilConditionForTest(BindRepeating([]() { return true; })); RunLoop().RunUntilConditionForTest(BindRepeating([]() { return true; }));
} }
@@ -606,7 +603,8 @@ TEST(RunLoopUntilConditionTest, QuitsLoopIfConditionMetOnPoll) {
RunLoop loop; RunLoop loop;
// Configure a long timeout so it won't fire before we poll. // Configure a long timeout so it won't fire before we poll.
const test::ScopedRunLoopTimeout long_timeout(TestTimeouts::action_timeout()); const RunLoop::ScopedRunTimeoutForTest long_timeout(
TestTimeouts::action_timeout(), MakeExpectedNotRunClosure(FROM_HERE));
// Arrange to post a task to the loop after the Run()-timeout has been // Arrange to post a task to the loop after the Run()-timeout has been
// started, set to run after the |condition| is polled and before the Run() // started, set to run after the |condition| is polled and before the Run()

@@ -104,8 +104,6 @@ static_library("test_support") {
"scoped_mock_time_message_loop_task_runner.h", "scoped_mock_time_message_loop_task_runner.h",
"scoped_path_override.cc", "scoped_path_override.cc",
"scoped_path_override.h", "scoped_path_override.h",
"scoped_run_loop_timeout.cc",
"scoped_run_loop_timeout.h",
"sequenced_task_runner_test_template.cc", "sequenced_task_runner_test_template.cc",
"sequenced_task_runner_test_template.h", "sequenced_task_runner_test_template.h",
"simple_test_clock.cc", "simple_test_clock.cc",

@@ -1,65 +0,0 @@
// Copyright 2019 The Chromium 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 "base/test/scoped_run_loop_timeout.h"
#include "base/bind.h"
#include "base/location.h"
#include "base/time/time.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace base {
namespace test {
namespace {
bool g_add_gtest_failure_on_timeout = false;
}
ScopedRunLoopTimeout::ScopedRunLoopTimeout(TimeDelta timeout)
: nested_timeout_(RunLoop::GetTimeoutForCurrentThread()) {
DCHECK_GT(timeout, TimeDelta());
run_timeout_.timeout = timeout;
if (g_add_gtest_failure_on_timeout) {
run_timeout_.on_timeout = base::BindRepeating(
[]() { GTEST_FAIL() << "RunLoop::Run() timed out."; });
} else {
run_timeout_.on_timeout = base::BindRepeating(
[]() { LOG(FATAL) << "RunLoop::Run() timed out."; });
}
RunLoop::SetTimeoutForCurrentThread(&run_timeout_);
}
ScopedRunLoopTimeout::~ScopedRunLoopTimeout() {
RunLoop::SetTimeoutForCurrentThread(nested_timeout_);
}
// static
bool ScopedRunLoopTimeout::ExistsForCurrentThread() {
return RunLoop::GetTimeoutForCurrentThread() != nullptr;
}
// static
void ScopedRunLoopTimeout::SetAddGTestFailureOnTimeout() {
g_add_gtest_failure_on_timeout = true;
}
// static
const RunLoop::RunLoopTimeout*
ScopedRunLoopTimeout::GetTimeoutForCurrentThread() {
return RunLoop::GetTimeoutForCurrentThread();
}
ScopedDisableRunLoopTimeout::ScopedDisableRunLoopTimeout()
: nested_timeout_(RunLoop::GetTimeoutForCurrentThread()) {
RunLoop::SetTimeoutForCurrentThread(nullptr);
}
ScopedDisableRunLoopTimeout::~ScopedDisableRunLoopTimeout() {
RunLoop::SetTimeoutForCurrentThread(nested_timeout_);
}
} // namespace test
} // namespace base

@@ -1,97 +0,0 @@
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef BASE_TEST_SCOPED_RUN_LOOP_TIMEOUT_H_
#define BASE_TEST_SCOPED_RUN_LOOP_TIMEOUT_H_
#include "base/run_loop.h"
#include "base/time/time.h"
namespace content {
FORWARD_DECLARE_TEST(ContentBrowserTest, RunTimeoutInstalled);
}
namespace base {
namespace test {
FORWARD_DECLARE_TEST(TaskEnvironmentTest, SetsDefaultRunTimeout);
// Configures all RunLoop::Run() calls on the current thread to run the
// supplied |on_timeout| callback if they run for longer than |timeout|.
//
// Specifying Run() timeouts per-thread avoids the need to cope with Run()s
// executing concurrently with ScopedRunLoopTimeout initialization or
// teardown, and allows "default" timeouts to be specified by suites, rather
// than explicitly configuring them for every RunLoop, in each test.
//
// This is used by test classes including TaskEnvironment and TestSuite to
// set a default Run() timeout on the main thread of all tests which use them.
//
// Tests which have steps which need to Run() for longer than their suite's
// default (if any) allows can override the active timeout by creating a nested
// ScopedRunLoopTimeout on their stack, e.g:
//
// ScopedRunLoopTimeout default_timeout(kDefaultRunTimeout);
// ... do other test stuff ...
// RunLoop().Run(); // Run for up to kDefaultRunTimeout.
// ...
// {
// ScopedRunLoopTimeout specific_timeout(kTestSpecificTimeout);
// RunLoop().Run(); // Run for up to kTestSpecificTimeout.
// }
// ...
// RunLoop().Run(); // Run for up to kDefaultRunTimeout.
//
// The currently-active timeout can also be temporarily disabled:
// ScopedDisableRunLoopTimeout disable_timeout;
//
// By default LOG(FATAL) will be invoked on Run() timeout. Test binaries
// can opt-in to using ADD_FAILURE() instead by calling
// SetAddGTestFailureOnTimeout() during process initialization.
//
// TaskEnvironment applies a default Run() timeout.
class ScopedRunLoopTimeout {
public:
ScopedRunLoopTimeout(TimeDelta timeout);
~ScopedRunLoopTimeout();
ScopedRunLoopTimeout(const ScopedRunLoopTimeout&) = delete;
ScopedRunLoopTimeout& operator=(const ScopedRunLoopTimeout&) = delete;
// Returns true if there is a Run() timeout configured on the current thread.
static bool ExistsForCurrentThread();
static void SetAddGTestFailureOnTimeout();
protected:
FRIEND_TEST_ALL_PREFIXES(ScopedRunLoopRunTimeoutTest, TimesOut);
FRIEND_TEST_ALL_PREFIXES(ScopedRunLoopRunTimeoutTest, RunTasksUntilTimeout);
FRIEND_TEST_ALL_PREFIXES(TaskEnvironmentTest, SetsDefaultRunTimeout);
FRIEND_TEST_ALL_PREFIXES(content::ContentBrowserTest, RunTimeoutInstalled);
// Exposes the RunLoopTimeout to the friend tests (see above).
static const RunLoop::RunLoopTimeout* GetTimeoutForCurrentThread();
const RunLoop::RunLoopTimeout* const nested_timeout_;
RunLoop::RunLoopTimeout run_timeout_;
};
class ScopedDisableRunLoopTimeout {
public:
ScopedDisableRunLoopTimeout();
~ScopedDisableRunLoopTimeout();
ScopedDisableRunLoopTimeout(const ScopedDisableRunLoopTimeout&) = delete;
ScopedDisableRunLoopTimeout& operator=(const ScopedDisableRunLoopTimeout&) =
delete;
private:
const RunLoop::RunLoopTimeout* const nested_timeout_;
};
} // namespace test
} // namespace base
#endif // BASE_TEST_SCOPED_RUN_LOOP_TIMEOUT_H_

@@ -1,65 +0,0 @@
// Copyright 2020 The Chromium 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 "base/test/scoped_run_loop_timeout.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/location.h"
#include "base/test/bind_test_util.h"
#include "base/test/gtest_util.h"
#include "base/test/task_environment.h"
#include "base/threading/sequenced_task_runner_handle.h"
#include "base/time/time.h"
#include "testing/gtest/include/gtest/gtest-spi.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace base {
namespace test {
TEST(ScopedRunLoopTimeoutTest, TimesOut) {
TaskEnvironment task_environment;
RunLoop run_loop;
static constexpr auto kArbitraryTimeout = TimeDelta::FromMilliseconds(10);
ScopedRunLoopTimeout run_timeout(kArbitraryTimeout);
// Since the delayed task will be posted only after the message pump starts
// running, the ScopedRunLoopTimeout will already have started to elapse,
// so if Run() exits at the correct time then our delayed task will not run.
SequencedTaskRunnerHandle::Get()->PostTask(
FROM_HERE,
BindOnce(IgnoreResult(&SequencedTaskRunner::PostDelayedTask),
SequencedTaskRunnerHandle::Get(), FROM_HERE,
MakeExpectedNotRunClosure(FROM_HERE), kArbitraryTimeout));
// This task should get to run before Run() times-out.
SequencedTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE, MakeExpectedRunClosure(FROM_HERE), kArbitraryTimeout);
// EXPECT_FATAL_FAILURE() can only reference globals and statics.
static RunLoop& static_loop = run_loop;
EXPECT_FATAL_FAILURE(static_loop.Run(), "Run() timed out.");
}
TEST(ScopedRunLoopTimeoutTest, RunTasksUntilTimeout) {
TaskEnvironment task_environment;
RunLoop run_loop;
static constexpr auto kArbitraryTimeout = TimeDelta::FromMilliseconds(10);
ScopedRunLoopTimeout run_timeout(kArbitraryTimeout);
// Posting a task with the same delay as our timeout, immediately before
// calling Run(), means it should get to run. Since this uses QuitWhenIdle(),
// the Run() timeout callback should also get to run.
SequencedTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE, MakeExpectedRunClosure(FROM_HERE), kArbitraryTimeout);
// EXPECT_FATAL_FAILURE() can only reference globals and statics.
static RunLoop& static_loop = run_loop;
EXPECT_FATAL_FAILURE(static_loop.Run(), "Run() timed out.");
}
} // namespace test
} // namespace base

@@ -380,11 +380,22 @@ TaskEnvironment::TaskEnvironment(
scoped_lazy_task_runner_list_for_testing_( scoped_lazy_task_runner_list_for_testing_(
std::make_unique<internal::ScopedLazyTaskRunnerListForTesting>()), std::make_unique<internal::ScopedLazyTaskRunnerListForTesting>()),
// TODO(https://crbug.com/922098): Enable Run() timeouts even for // TODO(https://crbug.com/922098): Enable Run() timeouts even for
// instances created with TimeSource::MOCK_TIME. // instances created with *MOCK_TIME.
run_loop_timeout_(mock_time_domain_ run_loop_timeout_(
? nullptr mock_time_domain_
: std::make_unique<ScopedRunLoopTimeout>( ? nullptr
TestTimeouts::action_timeout())) { : std::make_unique<RunLoop::ScopedRunTimeoutForTest>(
TestTimeouts::action_timeout(),
BindRepeating(
[](sequence_manager::SequenceManager*
sequence_manager) {
ADD_FAILURE()
<< "RunLoop::Run() timed out with the following "
"pending task(s) in its TaskEnvironment's "
"main thread queue:\n"
<< sequence_manager->DescribeAllPendingTasks();
},
Unretained(sequence_manager_.get())))) {
CHECK(!base::ThreadTaskRunnerHandle::IsSet()); CHECK(!base::ThreadTaskRunnerHandle::IsSet());
// If |subclass_creates_default_taskrunner| is true then initialization is // If |subclass_creates_default_taskrunner| is true then initialization is
// deferred until DeferredInitFromSubclass(). // deferred until DeferredInitFromSubclass().

@@ -11,10 +11,10 @@
#include "base/macros.h" #include "base/macros.h"
#include "base/memory/ref_counted.h" #include "base/memory/ref_counted.h"
#include "base/observer_list.h" #include "base/observer_list.h"
#include "base/run_loop.h"
#include "base/single_thread_task_runner.h" #include "base/single_thread_task_runner.h"
#include "base/task/lazy_thread_pool_task_runner.h" #include "base/task/lazy_thread_pool_task_runner.h"
#include "base/task/sequence_manager/sequence_manager.h" #include "base/task/sequence_manager/sequence_manager.h"
#include "base/test/scoped_run_loop_timeout.h"
#include "base/threading/thread_checker.h" #include "base/threading/thread_checker.h"
#include "base/time/time.h" #include "base/time/time.h"
#include "base/traits_bag.h" #include "base/traits_bag.h"
@@ -394,7 +394,7 @@ class TaskEnvironment {
scoped_lazy_task_runner_list_for_testing_; scoped_lazy_task_runner_list_for_testing_;
// Sets RunLoop::Run() to LOG(FATAL) if not Quit() in a timely manner. // Sets RunLoop::Run() to LOG(FATAL) if not Quit() in a timely manner.
std::unique_ptr<ScopedRunLoopTimeout> run_loop_timeout_; std::unique_ptr<RunLoop::ScopedRunTimeoutForTest> run_loop_timeout_;
std::unique_ptr<bool> owns_instance_ = std::make_unique<bool>(true); std::unique_ptr<bool> owns_instance_ = std::make_unique<bool>(true);

@@ -23,7 +23,6 @@
#include "base/test/gtest_util.h" #include "base/test/gtest_util.h"
#include "base/test/mock_callback.h" #include "base/test/mock_callback.h"
#include "base/test/mock_log.h" #include "base/test/mock_log.h"
#include "base/test/scoped_run_loop_timeout.h"
#include "base/test/test_timeouts.h" #include "base/test/test_timeouts.h"
#include "base/threading/platform_thread.h" #include "base/threading/platform_thread.h"
#include "base/threading/sequence_local_storage_slot.h" #include "base/threading/sequence_local_storage_slot.h"
@@ -839,8 +838,8 @@ TEST_F(TaskEnvironmentTest, ThreadPoolPoolAllowsMTA) {
#endif // defined(OS_WIN) #endif // defined(OS_WIN)
TEST_F(TaskEnvironmentTest, SetsDefaultRunTimeout) { TEST_F(TaskEnvironmentTest, SetsDefaultRunTimeout) {
const RunLoop::RunLoopTimeout* old_run_timeout = const RunLoop::ScopedRunTimeoutForTest* old_run_timeout =
ScopedRunLoopTimeout::GetTimeoutForCurrentThread(); RunLoop::ScopedRunTimeoutForTest::Current();
{ {
TaskEnvironment task_environment; TaskEnvironment task_environment;
@@ -848,21 +847,22 @@ TEST_F(TaskEnvironmentTest, SetsDefaultRunTimeout) {
// TaskEnvironment should set a default Run() timeout that fails the // TaskEnvironment should set a default Run() timeout that fails the
// calling test (before test_launcher_timeout()). // calling test (before test_launcher_timeout()).
const RunLoop::RunLoopTimeout* run_timeout = const RunLoop::ScopedRunTimeoutForTest* run_timeout =
ScopedRunLoopTimeout::GetTimeoutForCurrentThread(); RunLoop::ScopedRunTimeoutForTest::Current();
EXPECT_NE(run_timeout, old_run_timeout); EXPECT_NE(run_timeout, old_run_timeout);
EXPECT_TRUE(run_timeout); EXPECT_TRUE(run_timeout);
if (!debug::BeingDebugged()) { if (!debug::BeingDebugged()) {
EXPECT_LT(run_timeout->timeout, TestTimeouts::test_launcher_timeout()); EXPECT_LT(run_timeout->timeout(), TestTimeouts::test_launcher_timeout());
} }
static const RepeatingClosure& static_on_timeout = run_timeout->on_timeout; EXPECT_NONFATAL_FAILURE({ run_timeout->on_timeout().Run(); },
EXPECT_FATAL_FAILURE(static_on_timeout.Run(), "RunLoop::Run() timed out"); "RunLoop::Run() timed out");
} }
EXPECT_EQ(ScopedRunLoopTimeout::GetTimeoutForCurrentThread(), EXPECT_EQ(RunLoop::ScopedRunTimeoutForTest::Current(), old_run_timeout);
old_run_timeout);
} }
namespace {}
TEST_F(TaskEnvironmentTest, DescribePendingMainThreadTasks) { TEST_F(TaskEnvironmentTest, DescribePendingMainThreadTasks) {
TaskEnvironment task_environment; TaskEnvironment task_environment;
ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, DoNothing()); ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, DoNothing());
@@ -1028,7 +1028,7 @@ TEST_F(TaskEnvironmentTest, RunLoopDriveable) {
// Disable Run() timeout here, otherwise we'll fast-forward to it before we // Disable Run() timeout here, otherwise we'll fast-forward to it before we
// reach the quit task. // reach the quit task.
ScopedDisableRunLoopTimeout disable_timeout; RunLoop::ScopedDisableRunTimeoutForTest disable_timeout;
RunLoop run_loop; RunLoop run_loop;
ThreadTaskRunnerHandle::Get()->PostDelayedTask( ThreadTaskRunnerHandle::Get()->PostDelayedTask(

@@ -37,7 +37,6 @@
#include "base/test/mock_entropy_provider.h" #include "base/test/mock_entropy_provider.h"
#include "base/test/multiprocess_test.h" #include "base/test/multiprocess_test.h"
#include "base/test/scoped_feature_list.h" #include "base/test/scoped_feature_list.h"
#include "base/test/scoped_run_loop_timeout.h"
#include "base/test/test_switches.h" #include "base/test/test_switches.h"
#include "base/test/test_timeouts.h" #include "base/test/test_timeouts.h"
#include "base/threading/platform_thread.h" #include "base/threading/platform_thread.h"
@@ -581,8 +580,6 @@ void TestSuite::SuppressErrorDialogs() {
void TestSuite::Initialize() { void TestSuite::Initialize() {
DCHECK(!is_initialized_); DCHECK(!is_initialized_);
test::ScopedRunLoopTimeout::SetAddGTestFailureOnTimeout();
const CommandLine* command_line = CommandLine::ForCurrentProcess(); const CommandLine* command_line = CommandLine::ForCurrentProcess();
#if !defined(OS_IOS) #if !defined(OS_IOS)
if (command_line->HasSwitch(switches::kWaitForDebugger)) { if (command_line->HasSwitch(switches::kWaitForDebugger)) {

@@ -430,8 +430,9 @@ TEST_F(OfflinePageUtilsTest, TestGetCachedOfflinePageSizeNoPageInModel) {
#if defined(OS_ANDROID) #if defined(OS_ANDROID)
// TODO(https://crbug.com/1002762): Fix this test to run in < action_timeout() // TODO(https://crbug.com/1002762): Fix this test to run in < action_timeout()
// on the Android bots. // on the Android bots.
const base::test::ScopedRunLoopTimeout increased_run_timeout( const base::RunLoop::ScopedRunTimeoutForTest increased_run_timeout(
TestTimeouts::action_max_timeout()); TestTimeouts::action_max_timeout(),
base::MakeExpectedNotRunClosure(FROM_HERE, "RunLoop::Run() timed out."));
#endif // defined(OS_ANDROID) #endif // defined(OS_ANDROID)
clock()->Advance(base::TimeDelta::FromHours(3)); clock()->Advance(base::TimeDelta::FromHours(3));

@@ -13,6 +13,7 @@
#include "ash/public/mojom/assistant_state_controller.mojom-shared.h" #include "ash/public/mojom/assistant_state_controller.mojom-shared.h"
#include "base/auto_reset.h" #include "base/auto_reset.h"
#include "base/run_loop.h" #include "base/run_loop.h"
#include "base/test/bind_test_util.h"
#include "base/time/time.h" #include "base/time/time.h"
#include "chrome/browser/chromeos/login/test/embedded_test_server_mixin.h" #include "chrome/browser/chromeos/login/test/embedded_test_server_mixin.h"
#include "chrome/browser/chromeos/login/test/fake_gaia_mixin.h" #include "chrome/browser/chromeos/login/test/fake_gaia_mixin.h"
@@ -320,8 +321,8 @@ void AssistantTestMixin::TearDownOnMainThread() {
void AssistantTestMixin::StartAssistantAndWaitForReady( void AssistantTestMixin::StartAssistantAndWaitForReady(
base::TimeDelta wait_timeout) { base::TimeDelta wait_timeout) {
const base::test::ScopedRunLoopTimeout run_timeout(wait_timeout); const base::RunLoop::ScopedRunTimeoutForTest run_timeout(
wait_timeout, base::MakeExpectedNotRunClosure(FROM_HERE));
// Note: You might be tempted to call this function from SetUpOnMainThread(), // Note: You might be tempted to call this function from SetUpOnMainThread(),
// but that will not work as the Assistant service can not start until // but that will not work as the Assistant service can not start until
// |BrowserTestBase| calls InitializeNetworkProcess(), which it only does // |BrowserTestBase| calls InitializeNetworkProcess(), which it only does
@@ -353,7 +354,8 @@ void AssistantTestMixin::SendTextQuery(const std::string& query) {
void AssistantTestMixin::ExpectCardResponse( void AssistantTestMixin::ExpectCardResponse(
const std::string& expected_response, const std::string& expected_response,
base::TimeDelta wait_timeout) { base::TimeDelta wait_timeout) {
const base::test::ScopedRunLoopTimeout run_timeout(wait_timeout); const base::RunLoop::ScopedRunTimeoutForTest run_timeout(
wait_timeout, base::MakeExpectedNotRunClosure(FROM_HERE));
CardResponseWaiter waiter(test_api_->ui_element_container(), CardResponseWaiter waiter(test_api_->ui_element_container(),
{expected_response}); {expected_response});
waiter.RunUntilResponseReceived(); waiter.RunUntilResponseReceived();
@@ -368,7 +370,8 @@ void AssistantTestMixin::ExpectTextResponse(
void AssistantTestMixin::ExpectAnyOfTheseTextResponses( void AssistantTestMixin::ExpectAnyOfTheseTextResponses(
const std::vector<std::string>& expected_responses, const std::vector<std::string>& expected_responses,
base::TimeDelta wait_timeout) { base::TimeDelta wait_timeout) {
const base::test::ScopedRunLoopTimeout run_timeout(wait_timeout); const base::RunLoop::ScopedRunTimeoutForTest run_timeout(
wait_timeout, base::MakeExpectedNotRunClosure(FROM_HERE));
TextResponseWaiter waiter(test_api_->ui_element_container(), TextResponseWaiter waiter(test_api_->ui_element_container(),
expected_responses); expected_responses);
waiter.RunUntilResponseReceived(); waiter.RunUntilResponseReceived();

@@ -15,7 +15,6 @@
#include "base/single_thread_task_runner.h" #include "base/single_thread_task_runner.h"
#include "base/strings/stringprintf.h" #include "base/strings/stringprintf.h"
#include "base/test/bind_test_util.h" #include "base/test/bind_test_util.h"
#include "base/test/scoped_run_loop_timeout.h"
#include "base/test/task_environment.h" #include "base/test/task_environment.h"
#include "base/threading/thread_task_runner_handle.h" #include "base/threading/thread_task_runner_handle.h"
#include "chrome/common/cloud_print/cloud_print_constants.h" #include "chrome/common/cloud_print/cloud_print_constants.h"
@@ -609,8 +608,13 @@ void PrinterJobHandlerTest::BeginTest(int timeout_seconds) {
base::RunLoop run_loop; base::RunLoop run_loop;
active_run_loop_quit_closure_ = run_loop.QuitWhenIdleClosure(); active_run_loop_quit_closure_ = run_loop.QuitWhenIdleClosure();
base::test::ScopedRunLoopTimeout run_timeout( base::RunLoop::ScopedRunTimeoutForTest run_timeout(
base::TimeDelta::FromSeconds(timeout_seconds)); base::TimeDelta::FromSeconds(timeout_seconds),
base::BindLambdaForTesting([&]() {
ADD_FAILURE();
run_loop.QuitWhenIdle();
}));
run_loop.Run(); run_loop.Run();
} }

@@ -8,8 +8,8 @@
#include "base/files/file_path.h" #include "base/files/file_path.h"
#include "base/files/file_util.h" #include "base/files/file_util.h"
#include "base/run_loop.h"
#include "base/strings/stringprintf.h" #include "base/strings/stringprintf.h"
#include "base/test/scoped_run_loop_timeout.h"
#include "base/test/test_file_util.h" #include "base/test/test_file_util.h"
#include "base/timer/elapsed_timer.h" #include "base/timer/elapsed_timer.h"
#include "components/visitedlink/browser/visitedlink_writer.h" #include "components/visitedlink/browser/visitedlink_writer.h"
@@ -183,7 +183,7 @@ TEST_F(VisitedLink, TestAddAndQuery) {
// Tests how long it takes to write and read a large database to and from disk. // Tests how long it takes to write and read a large database to and from disk.
TEST_F(VisitedLink, TestBigTable) { TEST_F(VisitedLink, TestBigTable) {
base::test::ScopedDisableRunLoopTimeout disable_run_timeout; base::RunLoop::ScopedDisableRunTimeoutForTest disable_run_timeout;
// create a big DB // create a big DB
{ {
TimeLogger table_initialization_timer(kMetricTableInitMs); TimeLogger table_initialization_timer(kMetricTableInitMs);

@@ -28,7 +28,6 @@
#include "base/task/post_task.h" #include "base/task/post_task.h"
#include "base/task/thread_pool/thread_pool_instance.h" #include "base/task/thread_pool/thread_pool_instance.h"
#include "base/test/bind_test_util.h" #include "base/test/bind_test_util.h"
#include "base/test/scoped_run_loop_timeout.h"
#include "base/test/test_timeouts.h" #include "base/test/test_timeouts.h"
#include "base/threading/thread_restrictions.h" #include "base/threading/thread_restrictions.h"
#include "build/build_config.h" #include "build/build_config.h"
@@ -604,14 +603,16 @@ void BrowserTestBase::ProxyRunTestOnMainThreadLoop() {
#endif #endif
// Install a RunLoop timeout if none is present but do not override tests that // Install a RunLoop timeout if none is present but do not override tests that
// set a ScopedLoopRunTimeout from their fixture's constructor (which // set a ScopedRunTimeoutForTest from their fixture's constructor (which
// happens as part of setting up the test factory in gtest while // happens as part of setting up the test factory in gtest while
// ProxyRunTestOnMainThreadLoop() happens later as part of SetUp()). // ProxyRunTestOnMainThreadLoop() happens later as part of SetUp()).
base::Optional<base::test::ScopedRunLoopTimeout> scoped_run_timeout; base::Optional<base::RunLoop::ScopedRunTimeoutForTest> scoped_run_timeout;
if (!base::test::ScopedRunLoopTimeout::ExistsForCurrentThread()) { if (!base::RunLoop::ScopedRunTimeoutForTest::Current()) {
// TODO(https://crbug.com/918724): determine whether the timeout can be // TODO(https://crbug.com/918724): determine whether the timeout can be
// reduced from action_max_timeout() to action_timeout(). // reduced from action_max_timeout() to action_timeout().
scoped_run_timeout.emplace(TestTimeouts::action_max_timeout()); scoped_run_timeout.emplace(TestTimeouts::action_max_timeout(),
base::MakeExpectedNotRunClosure(
FROM_HERE, "RunLoop::Run() timed out."));
} }
#if defined(OS_POSIX) #if defined(OS_POSIX)

@@ -16,7 +16,6 @@
#include "base/test/launcher/test_launcher.h" #include "base/test/launcher/test_launcher.h"
#include "base/test/launcher/test_launcher_test_utils.h" #include "base/test/launcher/test_launcher_test_utils.h"
#include "base/test/scoped_feature_list.h" #include "base/test/scoped_feature_list.h"
#include "base/test/scoped_run_loop_timeout.h"
#include "base/test/test_switches.h" #include "base/test/test_switches.h"
#include "base/test/test_timeouts.h" #include "base/test/test_timeouts.h"
#include "base/threading/thread_restrictions.h" #include "base/threading/thread_restrictions.h"
@@ -302,14 +301,12 @@ IN_PROC_BROWSER_TEST_F(ContentBrowserTest, NonNestableTask) {
IN_PROC_BROWSER_TEST_F(ContentBrowserTest, RunTimeoutInstalled) { IN_PROC_BROWSER_TEST_F(ContentBrowserTest, RunTimeoutInstalled) {
// Verify that a RunLoop timeout is installed and shorter than the test // Verify that a RunLoop timeout is installed and shorter than the test
// timeout itself. // timeout itself.
const base::RunLoop::RunLoopTimeout* run_timeout = const auto* run_timeout = base::RunLoop::ScopedRunTimeoutForTest::Current();
base::test::ScopedRunLoopTimeout::GetTimeoutForCurrentThread();
EXPECT_TRUE(run_timeout); EXPECT_TRUE(run_timeout);
EXPECT_LT(run_timeout->timeout, TestTimeouts::test_launcher_timeout()); EXPECT_LT(run_timeout->timeout(), TestTimeouts::test_launcher_timeout());
static const base::RepeatingClosure& static_on_timeout = EXPECT_NONFATAL_FAILURE({ run_timeout->on_timeout().Run(); },
run_timeout->on_timeout; "RunLoop::Run() timed out");
EXPECT_FATAL_FAILURE(static_on_timeout.Run(), "RunLoop::Run() timed out");
} }
} // namespace content } // namespace content

@@ -691,10 +691,10 @@ Tests can run the `base::test::TaskEnvironment`'s message pump using a
`RunLoop::QuitClosure()`), or to `RunUntilIdle()` ready-to-run tasks and `RunLoop::QuitClosure()`), or to `RunUntilIdle()` ready-to-run tasks and
immediately return. immediately return.
TaskEnvironment configures RunLoop::Run() to GTEST_FAIL() if it hasn't been TaskEnvironment configures RunLoop::Run() to LOG(FATAL) if it hasn't been
explicitly quit after TestTimeouts::action_timeout(). This is preferable to explicitly quit after TestTimeouts::action_timeout(). This is preferable to
having the test hang if the code under test fails to trigger the RunLoop to having the test hang if the code under test fails to trigger the RunLoop to
quit. The timeout can be overridden with base::test::ScopedRunLoopTimeout. quit. The timeout can be overridden with ScopedRunTimeoutForTest.
```cpp ```cpp
class MyTest : public testing::Test { class MyTest : public testing::Test {

@@ -11,7 +11,6 @@
#include "base/callback_forward.h" #include "base/callback_forward.h"
#include "base/hash/md5.h" #include "base/hash/md5.h"
#include "base/run_loop.h" #include "base/run_loop.h"
#include "base/test/scoped_run_loop_timeout.h"
#include "base/test/task_environment.h" #include "base/test/task_environment.h"
#include "media/audio/clockless_audio_sink.h" #include "media/audio/clockless_audio_sink.h"
#include "media/audio/null_audio_sink.h" #include "media/audio/null_audio_sink.h"
@@ -159,7 +158,7 @@ class PipelineIntegrationTestBase : public Pipeline::Client {
bool fuzzing_; bool fuzzing_;
#if defined(ADDRESS_SANITIZER) || defined(UNDEFINED_SANITIZER) #if defined(ADDRESS_SANITIZER) || defined(UNDEFINED_SANITIZER)
// TODO(https://crbug.com/924030): ASAN causes Run() timeouts to be reached. // TODO(https://crbug.com/924030): ASAN causes Run() timeouts to be reached.
const base::test::ScopedDisableRunLoopTimeout disable_run_timeout_; const base::RunLoop::ScopedDisableRunTimeoutForTest disable_run_timeout_;
#endif #endif
std::unique_ptr<Demuxer> demuxer_; std::unique_ptr<Demuxer> demuxer_;
std::unique_ptr<DataSource> data_source_; std::unique_ptr<DataSource> data_source_;

@@ -20,6 +20,7 @@
#include "base/run_loop.h" #include "base/run_loop.h"
#include "base/single_thread_task_runner.h" #include "base/single_thread_task_runner.h"
#include "base/strings/string_number_conversions.h" #include "base/strings/string_number_conversions.h"
#include "base/test/bind_test_util.h"
#include "base/test/metrics/histogram_tester.h" #include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h" #include "base/test/scoped_feature_list.h"
#include "base/test/simple_test_tick_clock.h" #include "base/test/simple_test_tick_clock.h"
@@ -779,8 +780,9 @@ TEST_F(ThroughputAnalyzerTest, TestThroughputWithNetworkRequestsOverlap) {
// of network requests overlap, and the minimum number of in flight requests // of network requests overlap, and the minimum number of in flight requests
// when taking an observation is more than 1. // when taking an observation is more than 1.
TEST_F(ThroughputAnalyzerTest, TestThroughputWithMultipleNetworkRequests) { TEST_F(ThroughputAnalyzerTest, TestThroughputWithMultipleNetworkRequests) {
const base::test::ScopedRunLoopTimeout increased_run_timeout( const base::RunLoop::ScopedRunTimeoutForTest increased_run_timeout(
TestTimeouts::action_max_timeout()); TestTimeouts::action_max_timeout(),
base::MakeExpectedNotRunClosure(FROM_HERE, "RunLoop::Run() timed out."));
const base::TickClock* tick_clock = base::DefaultTickClock::GetInstance(); const base::TickClock* tick_clock = base::DefaultTickClock::GetInstance();
TestNetworkQualityEstimator network_quality_estimator; TestNetworkQualityEstimator network_quality_estimator;

@@ -226,11 +226,14 @@ class SimpleLoaderTestHelper : public SimpleURLLoaderStreamConsumer {
// StartSimpleLoaderAndWait, but exposed so some tests can start the // StartSimpleLoaderAndWait, but exposed so some tests can start the
// SimpleURLLoader directly. // SimpleURLLoader directly.
void Wait() { void Wait() {
const base::test::ScopedRunLoopTimeout run_timeout( base::RunLoop::ScopedRunTimeoutForTest run_timeout(
// Some of the bots run tests quite slowly, and the default timeout is // Some of the bots run tests quite slowly, and the default timeout is
// too short for them for some of the heavier weight tests. // too short for them for some of the heavier weight tests.
// See https://crbug.com/1046745 and https://crbug.com/1035127. // See https://crbug.com/1046745 and https://crbug.com/1035127.
TestTimeouts::action_max_timeout()); TestTimeouts::action_max_timeout(), base::BindLambdaForTesting([&]() {
ADD_FAILURE() << "Run loop timed out";
run_loop_.Quit();
}));
run_loop_.Run(); run_loop_.Run();
} }

@@ -35,7 +35,7 @@ include_rules = [
specific_include_rules = { specific_include_rules = {
".*test.*": [ ".*test.*": [
"+base/run_loop.h", "+base/run_loop.h",
"+base/test/scoped_run_loop_timeout.h", "+base/test/bind_test_util.h",
"+base/test/test_timeouts.h", "+base/test/test_timeouts.h",
"+base/threading/thread.h", "+base/threading/thread.h",
"+media/audio/audio_sink_parameters.h", "+media/audio/audio_sink_parameters.h",

@@ -10,7 +10,7 @@
#include "base/memory/scoped_refptr.h" #include "base/memory/scoped_refptr.h"
#include "base/run_loop.h" #include "base/run_loop.h"
#include "base/single_thread_task_runner.h" #include "base/single_thread_task_runner.h"
#include "base/test/scoped_run_loop_timeout.h" #include "base/test/bind_test_util.h"
#include "base/test/test_timeouts.h" #include "base/test/test_timeouts.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/platform/scheduler/test/renderer_scheduler_test_support.h" #include "third_party/blink/public/platform/scheduler/test/renderer_scheduler_test_support.h"
@@ -261,7 +261,11 @@ class WebRtcMediaStreamTrackAdapterMapStressTest
: public WebRtcMediaStreamTrackAdapterMapTest { : public WebRtcMediaStreamTrackAdapterMapTest {
public: public:
WebRtcMediaStreamTrackAdapterMapStressTest() WebRtcMediaStreamTrackAdapterMapStressTest()
: increased_run_timeout_(TestTimeouts::action_max_timeout()) {} : WebRtcMediaStreamTrackAdapterMapTest(),
increased_run_timeout_(
TestTimeouts::action_max_timeout(),
base::MakeExpectedNotRunClosure(FROM_HERE,
"RunLoop::Run() timed out.")) {}
void RunStressTest(size_t iterations) { void RunStressTest(size_t iterations) {
base::RunLoop run_loop; base::RunLoop run_loop;
@@ -340,7 +344,7 @@ class WebRtcMediaStreamTrackAdapterMapStressTest
private: private:
// TODO(https://crbug.com/1002761): Fix this test to run in < action_timeout() // TODO(https://crbug.com/1002761): Fix this test to run in < action_timeout()
// on slower bots (e.g. Debug, ASAN, etc). // on slower bots (e.g. Debug, ASAN, etc).
const base::test::ScopedRunLoopTimeout increased_run_timeout_; const base::RunLoop::ScopedRunTimeoutForTest increased_run_timeout_;
size_t remaining_iterations_; size_t remaining_iterations_;
}; };

@@ -10,6 +10,7 @@
#include "base/bind.h" #include "base/bind.h"
#include "base/macros.h" #include "base/macros.h"
#include "base/run_loop.h" #include "base/run_loop.h"
#include "base/test/bind_test_util.h"
#include "base/test/task_environment.h" #include "base/test/task_environment.h"
#include "base/test/test_simple_task_runner.h" #include "base/test/test_simple_task_runner.h"
#include "base/test/test_timeouts.h" #include "base/test/test_timeouts.h"
@@ -205,8 +206,9 @@ TEST_P(SnapshotAuraTest, MAYBE_FullScreenWindow) {
#if defined(OS_LINUX) #if defined(OS_LINUX)
// TODO(https://crbug.com/1002716): Fix this test to run in < action_timeout() // TODO(https://crbug.com/1002716): Fix this test to run in < action_timeout()
// on the Linux Debug & TSAN bots. // on the Linux Debug & TSAN bots.
const base::test::ScopedRunLoopTimeout increased_run_timeout( const base::RunLoop::ScopedRunTimeoutForTest increased_run_timeout(
TestTimeouts::action_max_timeout()); TestTimeouts::action_max_timeout(),
base::MakeExpectedNotRunClosure(FROM_HERE, "RunLoop::Run() timed out."));
#endif // defined(OS_LINUX) #endif // defined(OS_LINUX)
#if defined(OS_WIN) #if defined(OS_WIN)

@@ -13,7 +13,6 @@
#include "base/power_monitor/power_monitor.h" #include "base/power_monitor/power_monitor.h"
#include "base/power_monitor/power_monitor_device_source.h" #include "base/power_monitor/power_monitor_device_source.h"
#include "base/run_loop.h" #include "base/run_loop.h"
#include "base/test/scoped_run_loop_timeout.h"
#include "base/test/task_environment.h" #include "base/test/task_environment.h"
#include "base/test/test_discardable_memory_allocator.h" #include "base/test/test_discardable_memory_allocator.h"
#include "base/test/test_timeouts.h" #include "base/test/test_timeouts.h"
@@ -134,7 +133,7 @@ int main(int argc, char** argv) {
#endif #endif
// This app isn't a test and shouldn't timeout. // This app isn't a test and shouldn't timeout.
base::test::ScopedDisableRunLoopTimeout disable_timeout; base::RunLoop::ScopedDisableRunTimeoutForTest disable_timeout;
base::RunLoop run_loop; base::RunLoop run_loop;
views::examples::ShowExamplesWindow(run_loop.QuitClosure()); views::examples::ShowExamplesWindow(run_loop.QuitClosure());