Commit a4dd23fd authored by Francois Doray's avatar Francois Doray Committed by Commit Bot

[base] Ensure that tests don't change the thread priority.

This CL verifies that the thread priority is normal when a test process
is launched and before/after each test. The goal is to avoid having
tests that assume they run at normal priority be disabled because of
other misbehaving tests (e.g. https://crbug.com/931706).

Bug: 931706
Change-Id: I5ca18b87720b5305285be481f7b60ff5c941a324
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1804786
Commit-Queue: Scott Violet <sky@chromium.org>
Reviewed-by: default avatarNico Weber <thakis@chromium.org>
Reviewed-by: default avatarScott Violet <sky@chromium.org>
Reviewed-by: default avatarGabriel Charette <gab@chromium.org>
Auto-Submit: François Doray <fdoray@chromium.org>
Cr-Commit-Position: refs/heads/master@{#697261}
parent 7bd5b9f6
......@@ -34,9 +34,10 @@ class AshContentTestSuite : public content::ContentTestSuiteBase {
protected:
// content::ContentTestSuiteBase:
void Initialize() override {
// Browser tests are expected not to tear-down various globals. (Must run
// before the base class is initialized.)
// Browser tests are expected not to tear-down various globals and may
// complete with the thread priority being above NORMAL.
base::TestSuite::DisableCheckForLeakedGlobals();
base::TestSuite::DisableCheckForThreadPriorityAtTestEnd();
ContentTestSuiteBase::Initialize();
ui_controls::InstallUIControlsAura(ash::test::CreateAshUIControls());
}
......
......@@ -37,6 +37,7 @@
#include "base/test/multiprocess_test.h"
#include "base/test/test_switches.h"
#include "base/test/test_timeouts.h"
#include "base/threading/platform_thread.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "testing/gmock/include/gmock/gmock.h"
......@@ -188,6 +189,31 @@ class CheckProcessPriority : public testing::EmptyTestEventListener {
};
#endif // !defined(OS_IOS)
class CheckThreadPriority : public testing::EmptyTestEventListener {
public:
CheckThreadPriority(bool check_thread_priority_at_test_end)
: check_thread_priority_at_test_end_(check_thread_priority_at_test_end) {
CHECK_EQ(base::PlatformThread::GetCurrentThreadPriority(),
base::ThreadPriority::NORMAL);
}
void OnTestStart(const testing::TestInfo& test) override {
EXPECT_EQ(base::PlatformThread::GetCurrentThreadPriority(),
base::ThreadPriority::NORMAL);
}
void OnTestEnd(const testing::TestInfo& test) override {
if (check_thread_priority_at_test_end_) {
EXPECT_EQ(base::PlatformThread::GetCurrentThreadPriority(),
base::ThreadPriority::NORMAL);
}
}
private:
const bool check_thread_priority_at_test_end_;
DISALLOW_COPY_AND_ASSIGN(CheckThreadPriority);
};
const std::string& GetProfileName() {
static const NoDestructor<std::string> profile_name([]() {
const CommandLine& command_line = *CommandLine::ForCurrentProcess();
......@@ -381,6 +407,11 @@ void TestSuite::DisableCheckForProcessPriority() {
check_for_process_priority_ = false;
}
void TestSuite::DisableCheckForThreadPriorityAtTestEnd() {
DCHECK(!is_initialized_);
check_for_thread_priority_at_test_end_ = false;
}
void TestSuite::UnitTestAssertHandler(const char* file,
int line,
const StringPiece summary,
......@@ -571,6 +602,8 @@ void TestSuite::Initialize() {
if (check_for_process_priority_)
listeners.Append(new CheckProcessPriority);
#endif
listeners.Append(
new CheckThreadPriority(check_for_thread_priority_at_test_end_));
AddTestLauncherResultPrinter();
......
......@@ -46,6 +46,10 @@ class TestSuite {
// Disables checks for process priority. Most tests should not use this.
void DisableCheckForProcessPriority();
// Disables checks for thread priority at test end. This may be used for tests
// that each run in their own process.
void DisableCheckForThreadPriorityAtTestEnd();
// Disables checks for certain global objects being leaked across tests.
void DisableCheckForLeakedGlobals();
......@@ -94,6 +98,7 @@ class TestSuite {
bool check_for_leaked_globals_ = true;
bool check_for_process_priority_ = true;
bool check_for_thread_priority_at_test_end_ = true;
bool is_initialized_ = false;
......
......@@ -30,8 +30,10 @@ class BrowserTestSuiteRunnerChromeOS : public ChromeTestSuiteRunner {
public:
int RunTestSuite(int argc, char** argv) override {
BrowserTestSuiteChromeOS test_suite(argc, argv);
// Browser tests are expected not to tear-down various globals.
// Browser tests are expected not to tear-down various globals and may
// complete with the thread priority being above NORMAL.
test_suite.DisableCheckForLeakedGlobals();
test_suite.DisableCheckForThreadPriorityAtTestEnd();
return test_suite.Run();
}
};
......
......@@ -76,8 +76,10 @@ ChromeTestSuiteRunner::~ChromeTestSuiteRunner() {}
int ChromeTestSuiteRunner::RunTestSuite(int argc, char** argv) {
ChromeTestSuite test_suite(argc, argv);
// Browser tests are expected not to tear-down various globals.
// Browser tests are expected not to tear-down various globals and may
// complete with the thread priority being above NORMAL.
test_suite.DisableCheckForLeakedGlobals();
test_suite.DisableCheckForThreadPriorityAtTestEnd();
#if defined(OS_ANDROID)
// Android browser tests run child processes as threads instead.
content::ContentTestSuiteBase::RegisterInProcessThreads();
......
......@@ -41,8 +41,10 @@ class InteractiveUITestSuite : public ChromeTestSuite {
protected:
// ChromeTestSuite overrides:
void Initialize() override {
// Browser tests are expected not to tear-down various globals.
// Browser tests are expected not to tear-down various globals and may
// complete with the thread priority being above NORMAL.
base::TestSuite::DisableCheckForLeakedGlobals();
base::TestSuite::DisableCheckForThreadPriorityAtTestEnd();
ChromeTestSuite::Initialize();
......
......@@ -23,8 +23,10 @@ class CastTestLauncherDelegate : public content::TestLauncherDelegate {
int RunTestSuite(int argc, char** argv) override {
base::TestSuite test_suite(argc, argv);
// Browser tests are expected not to tear-down various globals.
// Browser tests are expected not to tear-down various globals and may
// complete with the thread priority being above NORMAL.
test_suite.DisableCheckForLeakedGlobals();
test_suite.DisableCheckForThreadPriorityAtTestEnd();
return test_suite.Run();
}
......
......@@ -39,9 +39,10 @@ class ContentBrowserTestSuite : public ContentTestSuiteBase {
protected:
void Initialize() override {
// Browser tests are expected not to tear-down various globals. (Must run
// before the base class is initialized.)
// Browser tests are expected not to tear-down various globals and may
// complete with the thread priority being above NORMAL.
base::TestSuite::DisableCheckForLeakedGlobals();
base::TestSuite::DisableCheckForThreadPriorityAtTestEnd();
ContentTestSuiteBase::Initialize();
......
......@@ -13,8 +13,10 @@ namespace extensions {
int AppShellTestLauncherDelegate::RunTestSuite(int argc, char** argv) {
base::TestSuite test_suite(argc, argv);
// Browser tests are expected not to tear-down various globals.
// Browser tests are expected not to tear-down various globals and may
// complete with the thread priority being above NORMAL.
test_suite.DisableCheckForLeakedGlobals();
test_suite.DisableCheckForThreadPriorityAtTestEnd();
return test_suite.Run();
}
......
......@@ -25,8 +25,10 @@ class WebEngineTestLauncherDelegate : public content::TestLauncherDelegate {
// content::TestLauncherDelegate implementation:
int RunTestSuite(int argc, char** argv) override {
base::TestSuite test_suite(argc, argv);
// Browser tests are expected not to tear-down various globals.
// Browser tests are expected not to tear-down various globals and may
// complete with the thread priority being above NORMAL.
test_suite.DisableCheckForLeakedGlobals();
test_suite.DisableCheckForThreadPriorityAtTestEnd();
return test_suite.Run();
}
......
......@@ -41,8 +41,10 @@ class HeadlessTestLauncherDelegate : public content::TestLauncherDelegate {
// content::TestLauncherDelegate implementation:
int RunTestSuite(int argc, char** argv) override {
base::TestSuite test_suite(argc, argv);
// Browser tests are expected not to tear-down various globals.
// Browser tests are expected not to tear-down various globals and may
// complete with the thread priority being above NORMAL.
test_suite.DisableCheckForLeakedGlobals();
test_suite.DisableCheckForThreadPriorityAtTestEnd();
return test_suite.Run();
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment