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

Unified Diff: base/test/scoped_task_environment.h

Issue 2796403002: Add base::test::ScopedTaskEnvironment. (Closed)
Patch Set: self-review 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
Index: base/test/scoped_task_environment.h
diff --git a/base/test/scoped_task_environment.h b/base/test/scoped_task_environment.h
new file mode 100644
index 0000000000000000000000000000000000000000..17655bd9b7bb78bd7c22bc5041107c5a9ee937d9
--- /dev/null
+++ b/base/test/scoped_task_environment.h
@@ -0,0 +1,52 @@
+// 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.
+
+#ifndef BASE_TEST_SCOPED_TASK_ENVIRONMENT_H_
+#define BASE_TEST_SCOPED_TASK_ENVIRONMENT_H_
+
+#include "base/macros.h"
+#include "base/message_loop/message_loop.h"
+
+namespace base {
+
+class TaskScheduler;
+
+namespace test {
+
+// ScopedTaskEnvironment allows usage of these APIs within its scope:
+// - ThreadTaskRunnerHandle, on the thread where it lives
gab 2017/04/05 20:44:24 (Thread|Sequenced)TaskRunnerHandle
fdoray 2017/04/06 14:00:23 Done.
+// - base/task_scheduler/post_task.h, on any thread
+//
+// Tests that need either of these APIs should instantiate a
+// ScopedTaskEnvironment.
+//
+// Tasks posted to the ThreadTaskRunnerHandle run synchronously when
+// RunLoop::Run(UntilIdle) is called on the thread where the
+// ScopedTaskEnvironment lives.
+//
+// Tasks posted through base/task_scheduler/post_task.h run on dedicated threads
+// as they are posted.
+//
+// Future improvements documented in:
gab 2017/04/05 20:44:24 // Design and future improvements documented in
fdoray 2017/04/06 14:00:22 Done.
+// https://docs.google.com/document/d/1QabRo8c7D9LsYY3cEcaPQbOCLo8Tu-6VLykYXyl3Pkk/edit
+class ScopedTaskEnvironment {
+ public:
+ ScopedTaskEnvironment();
+
+ // Runs pending ThreadTaskRunnerHandle tasks and pending BLOCK_SHUTDOWN
+ // TaskScheduler tasks. Then, unregisters the TaskScheduler and the
+ // ThreadTaskRunnerHandle.
+ ~ScopedTaskEnvironment();
+
+ private:
+ MessageLoop message_loop_;
gab 2017/04/05 20:44:24 // Note: |message_loop_| is an implementation deta
fdoray 2017/04/06 14:00:23 Done.
+ const TaskScheduler* task_scheduler_ = nullptr;
+
+ DISALLOW_COPY_AND_ASSIGN(ScopedTaskEnvironment);
+};
+
+} // namespace test
+} // namespace base
+
+#endif // BASE_TEST_SCOPED_ASYNC_TASK_SCHEDULER_H_

Powered by Google App Engine
This is Rietveld 408576698