Create base::test::ScopedTaskScheduler.

ScopedTaskScheduler initializes a TaskScheduler and allows usage of the
base/task_scheduler/post_task.h API within a scope. To make tests
deterministic, the TaskScheduler initialized by ScopedTaskScheduler has
a single thread.

BUG=553459

Review-Url: https://codereview.chromium.org/2511473004
Cr-Commit-Position: refs/heads/master@{#433702}


CrOS-Libchrome-Original-Commit: 8602d8a7269695a80f9d796ba7b2d69209f8cbdd
diff --git a/base/task_scheduler/task_scheduler.h b/base/task_scheduler/task_scheduler.h
index d4084c6..cbfaae9 100644
--- a/base/task_scheduler/task_scheduler.h
+++ b/base/task_scheduler/task_scheduler.h
@@ -82,6 +82,10 @@
   // other threads during the call. Returns immediately when shutdown completes.
   virtual void FlushForTesting() = 0;
 
+  // Joins all threads of this scheduler. Tasks that are already running are
+  // allowed to complete their execution. This can only be called once.
+  virtual void JoinForTesting() = 0;
+
   // CreateAndSetSimpleTaskScheduler(), CreateAndSetDefaultTaskScheduler(), and
   // SetInstance() register a TaskScheduler to handle tasks posted through the
   // post_task.h API for this process. The registered TaskScheduler will only be
diff --git a/base/task_scheduler/task_scheduler_impl.h b/base/task_scheduler/task_scheduler_impl.h
index 1483c5d..9a16103 100644
--- a/base/task_scheduler/task_scheduler_impl.h
+++ b/base/task_scheduler/task_scheduler_impl.h
@@ -62,10 +62,7 @@
   std::vector<const HistogramBase*> GetHistograms() const override;
   void Shutdown() override;
   void FlushForTesting() override;
-
-  // Joins all threads of this scheduler. Tasks that are already running are
-  // allowed to complete their execution. This can only be called once.
-  void JoinForTesting();
+  void JoinForTesting() override;
 
  private:
   explicit TaskSchedulerImpl(const WorkerPoolIndexForTraitsCallback&
diff --git a/base/test/scoped_task_scheduler.cc b/base/test/scoped_task_scheduler.cc
new file mode 100644
index 0000000..7696537
--- /dev/null
+++ b/base/test/scoped_task_scheduler.cc
@@ -0,0 +1,29 @@
+// 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 "base/task_scheduler/task_scheduler.h"
+
+namespace base {
+namespace test {
+
+ScopedTaskScheduler::ScopedTaskScheduler() {
+  DCHECK(!TaskScheduler::GetInstance());
+
+  // Create a TaskScheduler with a single thread to make tests deterministic.
+  constexpr int kMaxThreads = 1;
+  TaskScheduler::CreateAndSetSimpleTaskScheduler(kMaxThreads);
+  task_scheduler_ = TaskScheduler::GetInstance();
+}
+
+ScopedTaskScheduler::~ScopedTaskScheduler() {
+  DCHECK_EQ(task_scheduler_, TaskScheduler::GetInstance());
+  TaskScheduler::GetInstance()->Shutdown();
+  TaskScheduler::GetInstance()->JoinForTesting();
+  TaskScheduler::SetInstance(nullptr);
+}
+
+}  // namespace test
+}  // namespace base
diff --git a/base/test/scoped_task_scheduler.h b/base/test/scoped_task_scheduler.h
new file mode 100644
index 0000000..f2b252b
--- /dev/null
+++ b/base/test/scoped_task_scheduler.h
@@ -0,0 +1,41 @@
+// 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.
+
+#ifndef BASE_TEST_SCOPED_TASK_SCHEDULER_H_
+#define BASE_TEST_SCOPED_TASK_SCHEDULER_H_
+
+#include "base/macros.h"
+
+namespace base {
+
+class TaskScheduler;
+
+namespace test {
+
+// Initializes a TaskScheduler and allows usage of the
+// base/task_scheduler/post_task.h API within its scope.
+class ScopedTaskScheduler {
+ public:
+  // Initializes a TaskScheduler with default arguments.
+  ScopedTaskScheduler();
+
+  // Waits until all TaskScheduler tasks blocking shutdown complete their
+  // execution (see TaskShutdownBehavior). Then, joins all TaskScheduler threads
+  // and deletes the TaskScheduler.
+  //
+  // Note that joining TaskScheduler threads may involve waiting for
+  // CONTINUE_ON_SHUTDOWN tasks to complete their execution. Normally, in
+  // production, the process exits without joining TaskScheduler threads.
+  ~ScopedTaskScheduler();
+
+ private:
+  const TaskScheduler* task_scheduler_ = nullptr;
+
+  DISALLOW_COPY_AND_ASSIGN(ScopedTaskScheduler);
+};
+
+}  // namespace test
+}  // namespace base
+
+#endif  // BASE_TEST_SCOPED_TASK_SCHEDULER_H_
diff --git a/base/threading/sequenced_worker_pool_unittest.cc b/base/threading/sequenced_worker_pool_unittest.cc
index 39b424c..eccf15c 100644
--- a/base/threading/sequenced_worker_pool_unittest.cc
+++ b/base/threading/sequenced_worker_pool_unittest.cc
@@ -297,8 +297,7 @@
   // Destroys and unregisters the registered TaskScheduler, if any.
   void DeleteTaskScheduler() {
     if (TaskScheduler::GetInstance()) {
-      static_cast<internal::TaskSchedulerImpl*>(TaskScheduler::GetInstance())
-          ->JoinForTesting();
+      TaskScheduler::GetInstance()->JoinForTesting();
       TaskScheduler::SetInstance(nullptr);
     }
   }