OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef BASE_TASK_SCHEDULER_TEST_TASK_FACTORY_H_ |
| 6 #define BASE_TASK_SCHEDULER_TEST_TASK_FACTORY_H_ |
| 7 |
| 8 #include <stddef.h> |
| 9 |
| 10 #include <unordered_set> |
| 11 |
| 12 #include "base/macros.h" |
| 13 #include "base/memory/ref_counted.h" |
| 14 #include "base/synchronization/condition_variable.h" |
| 15 #include "base/synchronization/lock.h" |
| 16 #include "base/task_runner.h" |
| 17 #include "base/task_scheduler/task_traits.h" |
| 18 #include "base/threading/thread_checker_impl.h" |
| 19 |
| 20 namespace base { |
| 21 |
| 22 class WaitableEvent; |
| 23 |
| 24 namespace internal { |
| 25 namespace test { |
| 26 |
| 27 // A TestTaskFactory posts tasks to a TaskRunner and verifies that they run as |
| 28 // expected. Generates a test failure when: |
| 29 // - The ExecutionMode of the TaskRunner is SEQUENCED or SINGLE_THREADED and |
| 30 // Tasks don't run in posting order. |
| 31 // - The ExecutionMode of the TaskRunner is SINGLE_THREADED and Tasks don't |
| 32 // run on the same thread. |
| 33 // - A Task runs more than once. |
| 34 class TestTaskFactory { |
| 35 public: |
| 36 enum class PostNestedTask { |
| 37 YES, |
| 38 NO, |
| 39 }; |
| 40 |
| 41 // Constructs a TestTaskFactory that posts tasks to |task_runner|. |
| 42 // |execution_mode| is the ExecutionMode of |task_runner|. |
| 43 TestTaskFactory(scoped_refptr<TaskRunner> task_runner, |
| 44 ExecutionMode execution_mode); |
| 45 |
| 46 ~TestTaskFactory(); |
| 47 |
| 48 // Posts a task. If |post_nested_task| is YES, the task will post a new task |
| 49 // when it runs. If |event| is set, the task will block until it is signaled. |
| 50 // Returns true if the task is posted. |
| 51 bool PostTask(PostNestedTask post_nested_task, WaitableEvent* event); |
| 52 |
| 53 // 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. |
| 55 void WaitForAllTasksToRun() const; |
| 56 |
| 57 const TaskRunner* task_runner() const { return task_runner_.get(); } |
| 58 |
| 59 private: |
| 60 void RunTaskCallback(size_t task_index, |
| 61 PostNestedTask post_nested_task, |
| 62 WaitableEvent* event); |
| 63 |
| 64 // Synchronizes access to all members. |
| 65 mutable Lock lock_; |
| 66 |
| 67 // Condition variable signaled when a task runs. |
| 68 mutable ConditionVariable cv_; |
| 69 |
| 70 // Task runner through which this factory posts tasks. |
| 71 const scoped_refptr<TaskRunner> task_runner_; |
| 72 |
| 73 // Execution mode of |task_runner_|. |
| 74 const ExecutionMode execution_mode_; |
| 75 |
| 76 // Number of tasks posted by PostTask(). |
| 77 size_t num_posted_tasks_ = 0; |
| 78 |
| 79 // Indexes of tasks that ran. |
| 80 std::unordered_set<size_t> ran_tasks_; |
| 81 |
| 82 // Used to verify that all tasks run on the same thread when |execution_mode_| |
| 83 // is SINGLE_THREADED. |
| 84 ThreadCheckerImpl thread_checker_; |
| 85 |
| 86 DISALLOW_COPY_AND_ASSIGN(TestTaskFactory); |
| 87 }; |
| 88 |
| 89 } // namespace test |
| 90 } // namespace internal |
| 91 } // namespace base |
| 92 |
| 93 #endif // BASE_TASK_SCHEDULER_TEST_TASK_FACTORY_H_ |
OLD | NEW |