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

[base] Ensure that tests don't change the process priority (reland).

The original CL
https://chromium-review.googlesource.com/c/chromium/src/+/1679434
was reverted because of failures on test end on Mac OS 10.11.
This CL simply disables that check on Mac, so we can benefit from
process priority check on other platforms ASAP.
See diff:
https://chromium-review.googlesource.com/c/chromium/src/+/1760873/2..3/base/test/test_suite.cc

This CL verifies that a test process is not backgrounded when it 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/931721).

Bug: 931721
Change-Id: Ib60d728c960f84026c84f24395d38847c1941573
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1760873Reviewed-by: default avatarGabriel Charette <gab@chromium.org>
Commit-Queue: Gabriel Charette <gab@chromium.org>
Auto-Submit: François Doray <fdoray@chromium.org>
Cr-Commit-Position: refs/heads/master@{#688201}
parent 82d4cfe9
......@@ -27,6 +27,8 @@
#include "base/path_service.h"
#include "base/process/launch.h"
#include "base/process/memory.h"
#include "base/process/process.h"
#include "base/process/process_handle.h"
#include "base/task/thread_pool/thread_pool.h"
#include "base/test/gtest_xml_unittest_result_printer.h"
#include "base/test/gtest_xml_util.h"
......@@ -43,6 +45,7 @@
#if defined(OS_MACOSX)
#include "base/mac/scoped_nsautorelease_pool.h"
#include "base/process/port_provider_mac.h"
#if defined(OS_IOS)
#include "base/test/test_listener_ios.h"
#endif // OS_IOS
......@@ -144,6 +147,47 @@ class CheckForLeakedGlobals : public testing::EmptyTestEventListener {
DISALLOW_COPY_AND_ASSIGN(CheckForLeakedGlobals);
};
// base::Process is not available on iOS
#if !defined(OS_IOS)
class CheckProcessPriority : public testing::EmptyTestEventListener {
public:
CheckProcessPriority() { CHECK(!IsProcessBackgrounded()); }
void OnTestStart(const testing::TestInfo& test) override {
EXPECT_FALSE(IsProcessBackgrounded());
}
void OnTestEnd(const testing::TestInfo& test) override {
#if !defined(OS_MACOSX)
// Flakes are found on Mac OS 10.11. See https://crbug.com/931721#c7.
EXPECT_FALSE(IsProcessBackgrounded());
#endif
}
private:
#if defined(OS_MACOSX)
// Returns the calling process's task port, ignoring its argument.
class CurrentProcessPortProvider : public PortProvider {
mach_port_t TaskForPid(ProcessHandle process) const override {
// This PortProvider implementation only works for the current process.
CHECK_EQ(process, base::GetCurrentProcessHandle());
return mach_task_self();
}
};
#endif
bool IsProcessBackgrounded() const {
#if defined(OS_MACOSX)
CurrentProcessPortProvider port_provider;
return Process::Current().IsProcessBackgrounded(&port_provider);
#else
return Process::Current().IsProcessBackgrounded();
#endif
}
DISALLOW_COPY_AND_ASSIGN(CheckProcessPriority);
};
#endif // !defined(OS_IOS)
const std::string& GetProfileName() {
static const NoDestructor<std::string> profile_name([]() {
const CommandLine& command_line = *CommandLine::ForCurrentProcess();
......@@ -188,7 +232,7 @@ void InitializeLogging() {
} // namespace
int RunUnitTestsUsingBaseTestSuite(int argc, char **argv) {
int RunUnitTestsUsingBaseTestSuite(int argc, char** argv) {
TestSuite test_suite(argc, argv);
return LaunchUnitTests(argc, argv,
BindOnce(&TestSuite::Run, Unretained(&test_suite)));
......@@ -401,9 +445,8 @@ void AbortHandler(int signal) {
void TestSuite::SuppressErrorDialogs() {
#if defined(OS_WIN)
UINT new_flags = SEM_FAILCRITICALERRORS |
SEM_NOGPFAULTERRORBOX |
SEM_NOOPENFILEERRORBOX;
UINT new_flags =
SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX | SEM_NOOPENFILEERRORBOX;
// Preserve existing error mode, as discussed at
// http://blogs.msdn.com/oldnewthing/archive/2004/07/27/198410.aspx
......@@ -519,6 +562,9 @@ void TestSuite::Initialize() {
listeners.Append(new ResetCommandLineBetweenTests);
if (check_for_leaked_globals_)
listeners.Append(new CheckForLeakedGlobals);
#if !defined(OS_IOS)
listeners.Append(new CheckProcessPriority);
#endif
AddTestLauncherResultPrinter();
......
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