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 | |
26 // A TestTaskFactory posts tasks to a TaskRunner and verifies that they run as | |
27 // expected. | |
28 class TestTaskFactory { | |
robliao
2016/04/21 01:38:17
This should go into the test namespace.
fdoray
2016/04/22 15:54:03
Done.
| |
29 public: | |
30 enum class PostNestedTask { | |
31 YES, | |
32 NO, | |
33 }; | |
34 | |
35 // Constructs a TestTaskFactory that posts tasks to |task_runner|. | |
36 // |execution_mode| is the ExecutionMode of |task_runner|. | |
37 TestTaskFactory(scoped_refptr<TaskRunner> task_runner, | |
38 ExecutionMode execution_mode); | |
39 | |
40 ~TestTaskFactory(); | |
41 | |
42 // Posts a task. If |post_nested_task| is YES, the task will post a new task | |
43 // when it runs. If |event| is set, the task will block until it is signaled. | |
44 void PostTask(PostNestedTask post_nested_task, WaitableEvent* event); | |
45 | |
46 // Waits for all tasks posted by PostTask() to start running. It is not | |
47 // guaranteed that the tasks have completed their execution when this returns. | |
48 void WaitForAllTasksToRun() const; | |
49 | |
50 const TaskRunner* task_runner() const { return task_runner_.get(); } | |
51 | |
52 private: | |
53 void RunTaskCallback(size_t task_index, | |
54 PostNestedTask post_nested_task, | |
55 WaitableEvent* event); | |
56 | |
57 // Synchronizes access to all members. | |
58 mutable Lock lock_; | |
59 | |
60 // Condition variable signaled when a task runs. | |
61 mutable ConditionVariable cv_; | |
62 | |
63 // Task runner through which this factory posts tasks. | |
64 const scoped_refptr<TaskRunner> task_runner_; | |
65 | |
66 // Execution mode of |task_runner_|. | |
67 const ExecutionMode execution_mode_; | |
68 | |
69 // Number of tasks posted by PostTask(). | |
70 size_t num_posted_tasks_ = 0; | |
71 | |
72 // Indexes of tasks that ran. | |
73 std::unordered_set<size_t> ran_tasks_; | |
74 | |
75 // Used to verify that all tasks run on the same thread when |execution_mode_| | |
76 // is SINGLE_THREADED. | |
77 ThreadCheckerImpl thread_checker_; | |
78 | |
79 DISALLOW_COPY_AND_ASSIGN(TestTaskFactory); | |
80 }; | |
81 | |
82 } // namespace internal | |
83 } // namespace base | |
84 | |
85 #endif // BASE_TASK_SCHEDULER_TEST_TASK_FACTORY_H_ | |
OLD | NEW |