Commit 43847232 authored by Gabriel Charette's avatar Gabriel Charette Committed by Commit Bot

[ScopedTaskEnvironment] Enable MTA in unit test thread pools

This is a prereq for
https://chromium-review.googlesource.com/c/chromium/src/+/1338221
without this change, the MTA assertions fail in unit tests even though
they are correct in prod.

This change matches the browser process' params @
content/browser/startup_helper.cc

It has the adverse side-effect however of enabling the MTA in renderer
unit tests but the downside there is not as bad as it just means some
COM asserts may pass in unit tests where they wouldn't in integration
tests or prod. I think that's okay because unit tests are already
generally very loose on allowing I/O, waits, etc. Misuse will still be
caught, but that's just not unit tests' main role.

R=fdoray@chromium.org

Bug: 708584, 707362
Change-Id: I68c5e41c24396885af43427d09c11e1e84ecea43
Reviewed-on: https://chromium-review.googlesource.com/c/1338305
Commit-Queue: Gabriel Charette <gab@chromium.org>
Reviewed-by: default avatarFrançois Doray <fdoray@chromium.org>
Cr-Commit-Position: refs/heads/master@{#608772}
parent 76c35f61
...@@ -145,8 +145,25 @@ ScopedTaskEnvironment::ScopedTaskEnvironment( ...@@ -145,8 +145,25 @@ ScopedTaskEnvironment::ScopedTaskEnvironment(
TaskScheduler::SetInstance(std::make_unique<internal::TaskSchedulerImpl>( TaskScheduler::SetInstance(std::make_unique<internal::TaskSchedulerImpl>(
"ScopedTaskEnvironment", WrapUnique(task_tracker_))); "ScopedTaskEnvironment", WrapUnique(task_tracker_)));
task_scheduler_ = TaskScheduler::GetInstance(); task_scheduler_ = TaskScheduler::GetInstance();
TaskScheduler::GetInstance()->Start({worker_pool_params, worker_pool_params, TaskScheduler::GetInstance()->Start({
worker_pool_params, worker_pool_params}); worker_pool_params, worker_pool_params, worker_pool_params,
worker_pool_params
#if defined(OS_WIN)
,
// Enable the MTA in unit tests to match the browser process'
// TaskScheduler configuration.
//
// This has the adverse side-effect of enabling the MTA in non-browser
// unit tests as well but the downside there is not as bad as not having
// it in browser unit tests. It just means some COM asserts may pass in
// unit tests where they wouldn't in integration tests or prod. That's
// okay because unit tests are already generally very loose on allowing
// I/O, waits, etc. Such misuse will still be caught in later phases
// (and COM usage should already be pretty much inexistent in sandboxed
// processes).
TaskScheduler::InitParams::SharedWorkerPoolEnvironment::COM_MTA
#endif
});
if (execution_control_mode_ == ExecutionMode::QUEUED) if (execution_control_mode_ == ExecutionMode::QUEUED)
CHECK(task_tracker_->DisallowRunTasks()); CHECK(task_tracker_->DisallowRunTasks());
......
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
#include "base/threading/sequence_local_storage_slot.h" #include "base/threading/sequence_local_storage_slot.h"
#include "base/threading/thread_task_runner_handle.h" #include "base/threading/thread_task_runner_handle.h"
#include "base/time/tick_clock.h" #include "base/time/tick_clock.h"
#include "base/win/com_init_util.h"
#include "build/build_config.h" #include "build/build_config.h"
#include "testing/gmock/include/gmock/gmock.h" #include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
...@@ -305,6 +306,18 @@ TEST_F(ScopedTaskEnvironmentTest, FastForwardAdvanceTickClock) { ...@@ -305,6 +306,18 @@ TEST_F(ScopedTaskEnvironmentTest, FastForwardAdvanceTickClock) {
EXPECT_EQ(kLongTaskDelay * 2, tick_clock->NowTicks() - tick_clock_ref); EXPECT_EQ(kLongTaskDelay * 2, tick_clock->NowTicks() - tick_clock_ref);
} }
#if defined(OS_WIN)
// Regression test to ensure that ScopedTaskEnvironment enables the MTA in the
// thread pool (so that the test environment matches that of the browser process
// and com_init_util.h's assertions are happy in unit tests).
TEST_F(ScopedTaskEnvironmentTest, TaskSchedulerPoolAllowsMTA) {
ScopedTaskEnvironment scoped_task_environment;
PostTask(FROM_HERE,
BindOnce(&win::AssertComApartmentType, win::ComApartmentType::MTA));
scoped_task_environment.RunUntilIdle();
}
#endif // defined(OS_WIN)
namespace { namespace {
class MockLifetimeObserver : public ScopedTaskEnvironment::LifetimeObserver { class MockLifetimeObserver : public ScopedTaskEnvironment::LifetimeObserver {
......
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