| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef BASE_TASK_SCHEDULER_TEST_TASK_FACTORY_H_ | 5 #ifndef BASE_TASK_SCHEDULER_TEST_TASK_FACTORY_H_ |
| 6 #define BASE_TASK_SCHEDULER_TEST_TASK_FACTORY_H_ | 6 #define BASE_TASK_SCHEDULER_TEST_TASK_FACTORY_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include <unordered_set> | 10 #include <unordered_set> |
| 11 | 11 |
| 12 #include "base/callback_forward.h" |
| 12 #include "base/macros.h" | 13 #include "base/macros.h" |
| 13 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 14 #include "base/synchronization/condition_variable.h" | 15 #include "base/synchronization/condition_variable.h" |
| 15 #include "base/synchronization/lock.h" | 16 #include "base/synchronization/lock.h" |
| 16 #include "base/task_runner.h" | 17 #include "base/task_runner.h" |
| 17 #include "base/task_scheduler/task_traits.h" | 18 #include "base/task_scheduler/task_traits.h" |
| 18 #include "base/threading/thread_checker_impl.h" | 19 #include "base/threading/thread_checker_impl.h" |
| 19 | 20 |
| 20 namespace base { | 21 namespace base { |
| 21 | 22 |
| 22 class WaitableEvent; | 23 class WaitableEvent; |
| 23 | 24 |
| 24 namespace internal { | 25 namespace internal { |
| 25 namespace test { | 26 namespace test { |
| 26 | 27 |
| 27 // A TestTaskFactory posts tasks to a TaskRunner and verifies that they run as | 28 // A TestTaskFactory posts tasks to a TaskRunner and verifies that they run as |
| 28 // expected. Generates a test failure when: | 29 // expected. Generates a test failure when: |
| 30 // - The RunsTasksOnCurrentThread() method of the TaskRunner returns false on a |
| 31 // thread on which a Task is run. |
| 29 // - The ExecutionMode of the TaskRunner is SEQUENCED or SINGLE_THREADED and | 32 // - The ExecutionMode of the TaskRunner is SEQUENCED or SINGLE_THREADED and |
| 30 // Tasks don't run in posting order. | 33 // Tasks don't run in posting order. |
| 31 // - The ExecutionMode of the TaskRunner is SINGLE_THREADED and Tasks don't | 34 // - The ExecutionMode of the TaskRunner is SINGLE_THREADED and Tasks don't |
| 32 // run on the same thread. | 35 // run on the same thread. |
| 33 // - A Task runs more than once. | 36 // - A Task runs more than once. |
| 34 class TestTaskFactory { | 37 class TestTaskFactory { |
| 35 public: | 38 public: |
| 36 enum class PostNestedTask { | 39 enum class PostNestedTask { |
| 37 YES, | 40 YES, |
| 38 NO, | 41 NO, |
| 39 }; | 42 }; |
| 40 | 43 |
| 41 // Constructs a TestTaskFactory that posts tasks to |task_runner|. | 44 // Constructs a TestTaskFactory that posts tasks to |task_runner|. |
| 42 // |execution_mode| is the ExecutionMode of |task_runner|. | 45 // |execution_mode| is the ExecutionMode of |task_runner|. |
| 43 TestTaskFactory(scoped_refptr<TaskRunner> task_runner, | 46 TestTaskFactory(scoped_refptr<TaskRunner> task_runner, |
| 44 ExecutionMode execution_mode); | 47 ExecutionMode execution_mode); |
| 45 | 48 |
| 46 ~TestTaskFactory(); | 49 ~TestTaskFactory(); |
| 47 | 50 |
| 48 // Posts a task. If |post_nested_task| is YES, the task will post a new task | 51 // Posts a task. The posted task will: |
| 49 // when it runs. If |event| is set, the task will block until it is signaled. | 52 // - Post a new task if |post_nested_task| is YES. |
| 50 // Returns true if the task is posted. | 53 // - Verify conditions in which the task runs (see potential failures above). |
| 51 bool PostTask(PostNestedTask post_nested_task, WaitableEvent* event); | 54 // - Run |closure| if it is not null. |
| 55 bool PostTask(PostNestedTask post_nested_task, const Closure& closure); |
| 52 | 56 |
| 53 // Waits for all tasks posted by PostTask() to start running. It is not | 57 // Waits for all tasks posted by PostTask() to start running. It is not |
| 54 // guaranteed that the tasks have completed their execution when this returns. | 58 // guaranteed that the tasks have completed their execution when this returns. |
| 55 void WaitForAllTasksToRun() const; | 59 void WaitForAllTasksToRun() const; |
| 56 | 60 |
| 57 const TaskRunner* task_runner() const { return task_runner_.get(); } | 61 const TaskRunner* task_runner() const { return task_runner_.get(); } |
| 58 | 62 |
| 59 private: | 63 private: |
| 60 void RunTaskCallback(size_t task_index, | 64 void RunTaskCallback(size_t task_index, |
| 61 PostNestedTask post_nested_task, | 65 PostNestedTask post_nested_task, |
| 62 WaitableEvent* event); | 66 const Closure& closure); |
| 63 | 67 |
| 64 // Synchronizes access to all members. | 68 // Synchronizes access to all members. |
| 65 mutable Lock lock_; | 69 mutable Lock lock_; |
| 66 | 70 |
| 67 // Condition variable signaled when a task runs. | 71 // Condition variable signaled when a task runs. |
| 68 mutable ConditionVariable cv_; | 72 mutable ConditionVariable cv_; |
| 69 | 73 |
| 70 // Task runner through which this factory posts tasks. | 74 // Task runner through which this factory posts tasks. |
| 71 const scoped_refptr<TaskRunner> task_runner_; | 75 const scoped_refptr<TaskRunner> task_runner_; |
| 72 | 76 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 84 ThreadCheckerImpl thread_checker_; | 88 ThreadCheckerImpl thread_checker_; |
| 85 | 89 |
| 86 DISALLOW_COPY_AND_ASSIGN(TestTaskFactory); | 90 DISALLOW_COPY_AND_ASSIGN(TestTaskFactory); |
| 87 }; | 91 }; |
| 88 | 92 |
| 89 } // namespace test | 93 } // namespace test |
| 90 } // namespace internal | 94 } // namespace internal |
| 91 } // namespace base | 95 } // namespace base |
| 92 | 96 |
| 93 #endif // BASE_TASK_SCHEDULER_TEST_TASK_FACTORY_H_ | 97 #endif // BASE_TASK_SCHEDULER_TEST_TASK_FACTORY_H_ |
| OLD | NEW |