Chromium Code Reviews| Index: base/test/scoped_task_scheduler.cc |
| diff --git a/base/test/scoped_task_scheduler.cc b/base/test/scoped_task_scheduler.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..ac2c72839f77efc31630af2918cc59552c2214d0 |
| --- /dev/null |
| +++ b/base/test/scoped_task_scheduler.cc |
| @@ -0,0 +1,47 @@ |
| +// Copyright 2016 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_task_scheduler.h" |
| + |
| +#include <vector> |
| + |
| +#include "base/bind.h" |
| +#include "base/task_scheduler/scheduler_worker_pool_params.h" |
| +#include "base/task_scheduler/task_scheduler.h" |
| +#include "base/threading/platform_thread.h" |
| +#include "base/time/time.h" |
| + |
| +namespace base { |
| +namespace test { |
| + |
| +namespace { |
| + |
| +size_t GetWorkerPoolIndexForTraits(const TaskTraits&) { |
| + // All TaskTraits are mapped to the single worker pool. |
| + return 0; |
| +} |
| + |
| +} // namespace |
| + |
| +ScopedTaskScheduler::ScopedTaskScheduler() { |
| + DCHECK(!TaskScheduler::GetInstance()); |
| + |
| + // Create a TaskScheduler with a single thread to make tests deterministic. |
| + std::vector<SchedulerWorkerPoolParams> worker_pool_params_vector; |
| + worker_pool_params_vector.emplace_back( |
| + "Test", ThreadPriority::NORMAL, |
| + SchedulerWorkerPoolParams::IORestriction::ALLOWED, 1, TimeDelta::Max()); |
| + TaskScheduler::CreateAndSetDefaultTaskScheduler( |
| + worker_pool_params_vector, Bind(&GetWorkerPoolIndexForTraits)); |
| +} |
| + |
| +ScopedTaskScheduler::~ScopedTaskScheduler() { |
| + DCHECK(TaskScheduler::GetInstance()); |
|
robliao
2016/11/16 21:23:05
Maybe we can keep a pointer and check the pointer
fdoray
2016/11/16 22:04:45
Done.
|
| + TaskScheduler::GetInstance()->Shutdown(); |
| + TaskScheduler::GetInstance()->JoinForTesting(); |
| + TaskScheduler::SetInstance(nullptr); |
| +} |
| + |
| +} // namespace test |
| +} // namespace base |