Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(41)

Side by Side Diff: base/test/scoped_task_environment.cc

Issue 2796403002: Add base::test::ScopedTaskEnvironment. (Closed)
Patch Set: CR-gab-9 Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/test/scoped_task_environment.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/test/scoped_task_environment.h"
6
7 #include "base/run_loop.h"
8 #include "base/task_scheduler/scheduler_worker_pool_params.h"
9 #include "base/task_scheduler/task_scheduler.h"
10 #include "base/time/time.h"
11
12 namespace base {
13 namespace test {
14
15 ScopedTaskEnvironment::ScopedTaskEnvironment() {
16 DCHECK(!TaskScheduler::GetInstance());
17
18 // Instantiate a TaskScheduler with 1 thread in each of its 4 pools. Threads
19 // stay alive even when they don't have work.
20 constexpr int kMaxThreads = 1;
21 const TimeDelta kSuggestedReclaimTime = TimeDelta::Max();
22 const SchedulerWorkerPoolParams worker_pool_params(
23 SchedulerWorkerPoolParams::StandbyThreadPolicy::ONE, kMaxThreads,
24 kSuggestedReclaimTime);
25 TaskScheduler::CreateAndSetDefaultTaskScheduler(
26 "ScopedTaskEnvironment", {worker_pool_params, worker_pool_params,
27 worker_pool_params, worker_pool_params});
28 task_scheduler_ = TaskScheduler::GetInstance();
29 }
30
31 ScopedTaskEnvironment::~ScopedTaskEnvironment() {
32 RunLoop().RunUntilIdle();
33
34 DCHECK_EQ(TaskScheduler::GetInstance(), task_scheduler_);
35 TaskScheduler::GetInstance()->Shutdown();
36 TaskScheduler::GetInstance()->JoinForTesting();
37 TaskScheduler::SetInstance(nullptr);
38 }
39
40 } // namespace test
41 } // namespace base
OLDNEW
« no previous file with comments | « base/test/scoped_task_environment.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698