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

Side by Side Diff: base/task_scheduler/worker_thread_unittest.cc

Issue 1698183005: Reference CL for the new task scheduler. (Closed) Base URL: https://luckyluke-private.googlesource.com/src@bigmaster2
Patch Set: Created 4 years, 10 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 unified diff | Download patch
« no previous file with comments | « base/task_scheduler/worker_thread.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 #include "base/task_scheduler/worker_thread.h"
6
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/callback_forward.h"
10 #include "base/logging.h"
11 #include "base/synchronization/condition_variable.h"
12 #include "base/task_scheduler/delayed_task_manager.h"
13 #include "base/task_scheduler/priority_queue.h"
14 #include "base/task_scheduler/scheduler_lock.h"
15 #include "base/task_scheduler/shutdown_manager.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17
18 namespace base {
19 namespace internal {
20
21 class TaskSchedulerWorkerThreadTest : public testing::Test {
22 protected:
23 TaskSchedulerWorkerThreadTest()
24 : worker_thread_(WorkerThread::CreateWorkerThread(
25 ThreadPriority::NORMAL,
26 &shared_priority_queue_,
27 Bind(&TaskSchedulerWorkerThreadTest::ThreadMainEntryCallback,
28 Unretained(this)),
29 Bind(&TaskSchedulerWorkerThreadTest::ReinsertSequenceCallback,
30 Unretained(this)),
31 Bind(&TaskSchedulerWorkerThreadTest::BecomesIdleCallback,
32 Unretained(this)),
33 &delayed_task_manager_,
34 &shutdown_manager_)),
35 cv_(lock_.RawLockForConditionVariable()),
36 num_thread_main_entries_(0),
37 last_posted_task_index_(0),
38 last_run_task_index_(0),
39 ran_task_that_should_not_run_(false),
40 ran_tasks_in_wrong_order_(true),
41 shared_priority_queue_(Bind(&DoNothing)),
42 delayed_task_manager_(
43 Bind(&WorkerThread::WakeUp, Unretained(worker_thread_.get())),
44 &shutdown_manager_) {}
45
46 void ThreadMainEntryCallback() {
47 num_thread_main_entries_++;
48 }
49
50 WorkerThread::ReinsertSequenceCallback GetReinsertSequenceCallback() {
51 return Bind(&TaskSchedulerWorkerThreadTest::ReinsertSequenceCallback,
52 Unretained(this));
53 }
54
55 Closure GetTaskThatShouldRunClosure() {
56 ++last_posted_task_index_;
57 return Bind(&TaskSchedulerWorkerThreadTest::RunTaskThatShouldRun,
58 Unretained(this), last_posted_task_index_);
59 }
60
61 Closure GetTaskThatShouldNotRunClosure() {
62 return Bind(&TaskSchedulerWorkerThreadTest::RunTaskThatShouldNotRun,
63 Unretained(this));
64 }
65
66 void WaitUntilLastPostedTaskHasRun() {
67 AutoSchedulerLock auto_lock(lock_);
68 while (last_posted_task_index_ != last_run_task_index_)
69 cv_.Wait();
70 }
71
72 void ShutdownAndJoinForTesting() {
73 shutdown_manager_.set_is_shutting_down_for_testing(false);
74 worker_thread_->ShutdownAndJoinForTesting();
75 }
76
77 int num_thread_main_entries() const {
78 return num_thread_main_entries_;
79 }
80
81 bool ran_task_that_should_not_run() const {
82 return ran_task_that_should_not_run_;
83 }
84
85 ShutdownManager shutdown_manager_;
86 scoped_ptr<WorkerThread> worker_thread_;
87
88 private:
89 void ReinsertSequenceCallback(scoped_refptr<Sequence> sequence,
90 const WorkerThread* worker_thread) {
91 NOTREACHED();
92 }
93
94 void BecomesIdleCallback(WorkerThread* worker_thread) {
95 // TODO(fdoray).
96 }
97
98 void RunTaskThatShouldRun(size_t index) {
99 AutoSchedulerLock auto_lock(lock_);
100
101 if (index != last_run_task_index_ + 1)
102 ran_tasks_in_wrong_order_ = true;
103
104 last_run_task_index_ = index;
105 cv_.Signal();
106 }
107
108 void RunTaskThatShouldNotRun() { ran_task_that_should_not_run_ = true; }
109
110 // Lock protecting |cv_|.
111 SchedulerLock lock_;
112
113 // Condition variable signaled each time a task completes its execution.
114 ConditionVariable cv_;
115
116 // Number of invocations of the thread main entry callback.
117 int num_thread_main_entries_;
118
119 // Index of the last posted task.
120 size_t last_posted_task_index_;
121
122 // Index of the last run task.
123 size_t last_run_task_index_;
124
125 // True if a task that shouldn't run has run.
126 bool ran_task_that_should_not_run_;
127
128 // True if tasks were run in the wrong order.
129 bool ran_tasks_in_wrong_order_;
130
131 PriorityQueue shared_priority_queue_;
132
133 DelayedTaskManager delayed_task_manager_;
134 };
135
136 TEST_F(TaskSchedulerWorkerThreadTest, PostSingleTask) {
137 ASSERT_NE(nullptr, worker_thread_.get());
138
139 worker_thread_->CreateTaskRunnerWithTraits(TaskTraits(),
140 ExecutionMode::SINGLE_THREADED)
141 ->PostTask(FROM_HERE, GetTaskThatShouldRunClosure());
142
143 WaitUntilLastPostedTaskHasRun();
144 ShutdownAndJoinForTesting();
145 ASSERT_EQ(1, num_thread_main_entries());
146 }
147
148 TEST_F(TaskSchedulerWorkerThreadTest, PostMultipleTasksNoWaitBetweenPosts) {
149 ASSERT_NE(nullptr, worker_thread_.get());
150
151 auto task_runner = worker_thread_->CreateTaskRunnerWithTraits(
152 TaskTraits(), ExecutionMode::SINGLE_THREADED);
153
154 for (size_t i = 0; i < 100; ++i)
155 task_runner->PostTask(FROM_HERE, GetTaskThatShouldRunClosure());
156
157 WaitUntilLastPostedTaskHasRun();
158 ShutdownAndJoinForTesting();
159 }
160
161 TEST_F(TaskSchedulerWorkerThreadTest, PostMultipleTasksWaitBetweenPosts) {
162 ASSERT_NE(nullptr, worker_thread_.get());
163
164 auto task_runner = worker_thread_->CreateTaskRunnerWithTraits(
165 TaskTraits(), ExecutionMode::SINGLE_THREADED);
166
167 for (size_t i = 0; i < 100; ++i) {
168 task_runner->PostTask(FROM_HERE, GetTaskThatShouldRunClosure());
169 WaitUntilLastPostedTaskHasRun();
170 }
171
172 ShutdownAndJoinForTesting();
173 }
174
175 TEST_F(TaskSchedulerWorkerThreadTest, PostMultipleTasksTwoTaskRunners) {
176 ASSERT_NE(nullptr, worker_thread_.get());
177
178 auto task_runner_a = worker_thread_->CreateTaskRunnerWithTraits(
179 TaskTraits(), ExecutionMode::SINGLE_THREADED);
180 auto task_runner_b = worker_thread_->CreateTaskRunnerWithTraits(
181 TaskTraits(), ExecutionMode::SINGLE_THREADED);
182
183 for (size_t i = 0; i < 100; ++i) {
184 task_runner_a->PostTask(FROM_HERE, GetTaskThatShouldRunClosure());
185 task_runner_b->PostTask(FROM_HERE, GetTaskThatShouldRunClosure());
186 }
187
188 WaitUntilLastPostedTaskHasRun();
189 ShutdownAndJoinForTesting();
190 }
191
192 TEST_F(TaskSchedulerWorkerThreadTest, PostDelayedTasks) {
193 ASSERT_NE(nullptr, worker_thread_.get());
194
195 auto task_runner = worker_thread_->CreateTaskRunnerWithTraits(
196 TaskTraits(), ExecutionMode::SINGLE_THREADED);
197
198 for (size_t i = 0; i < 10; ++i) {
199 task_runner->PostDelayedTask(FROM_HERE, GetTaskThatShouldRunClosure(),
200 TimeDelta::FromMilliseconds(i * 50));
201 }
202
203 WaitUntilLastPostedTaskHasRun();
204 ShutdownAndJoinForTesting();
205 }
206
207 TEST_F(TaskSchedulerWorkerThreadTest, ShutdownBehavior) {
208 ASSERT_NE(nullptr, worker_thread_.get());
209
210 shutdown_manager_.set_is_shutting_down_for_testing(true);
211
212 // Post tasks with different shutdown behaviors.
213 worker_thread_->CreateTaskRunnerWithTraits(
214 TaskTraits().WithShutdownBehavior(
215 TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN),
216 ExecutionMode::SINGLE_THREADED)
217 ->PostTask(FROM_HERE, GetTaskThatShouldNotRunClosure());
218 worker_thread_->CreateTaskRunnerWithTraits(
219 TaskTraits().WithShutdownBehavior(
220 TaskShutdownBehavior::SKIP_ON_SHUTDOWN),
221 ExecutionMode::SINGLE_THREADED)
222 ->PostTask(FROM_HERE, GetTaskThatShouldNotRunClosure());
223 worker_thread_->CreateTaskRunnerWithTraits(
224 TaskTraits().WithShutdownBehavior(
225 TaskShutdownBehavior::BLOCK_SHUTDOWN),
226 ExecutionMode::SINGLE_THREADED)
227 ->PostTask(FROM_HERE, GetTaskThatShouldRunClosure());
228
229 WaitUntilLastPostedTaskHasRun();
230 ShutdownAndJoinForTesting();
231 EXPECT_FALSE(ran_task_that_should_not_run());
232 }
233
234 } // namespace internal
235 } // namespace base
OLDNEW
« no previous file with comments | « base/task_scheduler/worker_thread.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698