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

Unified 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/test/scoped_task_environment.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/test/scoped_task_environment.cc
diff --git a/base/test/scoped_task_environment.cc b/base/test/scoped_task_environment.cc
new file mode 100644
index 0000000000000000000000000000000000000000..8744b4a6b11427fe84c82bb7567708fff11b552c
--- /dev/null
+++ b/base/test/scoped_task_environment.cc
@@ -0,0 +1,41 @@
+// Copyright 2017 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_environment.h"
+
+#include "base/run_loop.h"
+#include "base/task_scheduler/scheduler_worker_pool_params.h"
+#include "base/task_scheduler/task_scheduler.h"
+#include "base/time/time.h"
+
+namespace base {
+namespace test {
+
+ScopedTaskEnvironment::ScopedTaskEnvironment() {
+ DCHECK(!TaskScheduler::GetInstance());
+
+ // Instantiate a TaskScheduler with 1 thread in each of its 4 pools. Threads
+ // stay alive even when they don't have work.
+ constexpr int kMaxThreads = 1;
+ const TimeDelta kSuggestedReclaimTime = TimeDelta::Max();
+ const SchedulerWorkerPoolParams worker_pool_params(
+ SchedulerWorkerPoolParams::StandbyThreadPolicy::ONE, kMaxThreads,
+ kSuggestedReclaimTime);
+ TaskScheduler::CreateAndSetDefaultTaskScheduler(
+ "ScopedTaskEnvironment", {worker_pool_params, worker_pool_params,
+ worker_pool_params, worker_pool_params});
+ task_scheduler_ = TaskScheduler::GetInstance();
+}
+
+ScopedTaskEnvironment::~ScopedTaskEnvironment() {
+ RunLoop().RunUntilIdle();
+
+ DCHECK_EQ(TaskScheduler::GetInstance(), task_scheduler_);
+ TaskScheduler::GetInstance()->Shutdown();
+ TaskScheduler::GetInstance()->JoinForTesting();
+ TaskScheduler::SetInstance(nullptr);
+}
+
+} // namespace test
+} // namespace base
« 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