1 // Copyright (c) 2012 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.
5 // SequencedTaskRunnerTest defines tests that implementations of
6 // SequencedTaskRunner should pass in order to be conformant.
7 // See task_runner_test_template.h for a description of how to use the
8 // constructs in this file; these work the same.
10 #ifndef BASE_TEST_SEQUENCED_TASK_RUNNER_TEST_TEMPLATE_H_
11 #define BASE_TEST_SEQUENCED_TASK_RUNNER_TEST_TEMPLATE_H_
17 #include "base/basictypes.h"
18 #include "base/bind.h"
19 #include "base/callback.h"
20 #include "base/memory/ref_counted.h"
21 #include "base/sequenced_task_runner.h"
22 #include "base/synchronization/condition_variable.h"
23 #include "base/synchronization/lock.h"
24 #include "base/time/time.h"
25 #include "testing/gtest/include/gtest/gtest.h"
32 enum Type
{ POST
, START
, END
};
33 TaskEvent(int i
, Type type
);
38 // Utility class used in the tests below.
39 class SequencedTaskTracker
: public RefCountedThreadSafe
<SequencedTaskTracker
> {
41 SequencedTaskTracker();
43 // Posts the non-nestable task |task|, and records its post event.
44 void PostWrappedNonNestableTask(
45 const scoped_refptr
<SequencedTaskRunner
>& task_runner
,
48 // Posts the nestable task |task|, and records its post event.
49 void PostWrappedNestableTask(
50 const scoped_refptr
<SequencedTaskRunner
>& task_runner
,
53 // Posts the delayed non-nestable task |task|, and records its post event.
54 void PostWrappedDelayedNonNestableTask(
55 const scoped_refptr
<SequencedTaskRunner
>& task_runner
,
59 // Posts |task_count| non-nestable tasks.
60 void PostNonNestableTasks(
61 const scoped_refptr
<SequencedTaskRunner
>& task_runner
,
64 const std::vector
<TaskEvent
>& GetTaskEvents() const;
66 // Returns after the tracker observes a total of |count| task completions.
67 void WaitForCompletedTasks(int count
);
70 friend class RefCountedThreadSafe
<SequencedTaskTracker
>;
72 ~SequencedTaskTracker();
74 // A task which runs |task|, recording the start and end events.
75 void RunTask(const Closure
& task
, int task_i
);
77 // Records a post event for task |i|. The owner is expected to be holding
78 // |lock_| (unlike |TaskStarted| and |TaskEnded|).
79 void TaskPosted(int i
);
81 // Records a start event for task |i|.
82 void TaskStarted(int i
);
84 // Records a end event for task |i|.
85 void TaskEnded(int i
);
87 // Protects events_, next_post_i_, task_end_count_ and task_end_cv_.
90 // The events as they occurred for each task (protected by lock_).
91 std::vector
<TaskEvent
> events_
;
93 // The ordinal to be used for the next task-posting task (protected by
97 // The number of task end events we've received.
99 ConditionVariable task_end_cv_
;
101 DISALLOW_COPY_AND_ASSIGN(SequencedTaskTracker
);
104 void PrintTo(const TaskEvent
& event
, std::ostream
* os
);
106 // Checks the non-nestable task invariants for all tasks in |events|.
108 // The invariants are:
109 // 1) Events started and ended in the same order that they were posted.
110 // 2) Events for an individual tasks occur in the order {POST, START, END},
111 // and there is only one instance of each event type for a task.
112 // 3) The only events between a task's START and END events are the POSTs of
113 // other tasks. I.e. tasks were run sequentially, not interleaved.
114 ::testing::AssertionResult
CheckNonNestableInvariants(
115 const std::vector
<TaskEvent
>& events
,
118 } // namespace internal
120 template <typename TaskRunnerTestDelegate
>
121 class SequencedTaskRunnerTest
: public testing::Test
{
123 SequencedTaskRunnerTest()
124 : task_tracker_(new internal::SequencedTaskTracker()) {}
126 const scoped_refptr
<internal::SequencedTaskTracker
> task_tracker_
;
127 TaskRunnerTestDelegate delegate_
;
130 TYPED_TEST_CASE_P(SequencedTaskRunnerTest
);
132 // This test posts N non-nestable tasks in sequence, and expects them to run
133 // in FIFO order, with no part of any two tasks' execution
134 // overlapping. I.e. that each task starts only after the previously-posted
136 TYPED_TEST_P(SequencedTaskRunnerTest
, SequentialNonNestable
) {
137 const int kTaskCount
= 1000;
139 this->delegate_
.StartTaskRunner();
140 const scoped_refptr
<SequencedTaskRunner
> task_runner
=
141 this->delegate_
.GetTaskRunner();
143 this->task_tracker_
->PostWrappedNonNestableTask(
144 task_runner
, Bind(&PlatformThread::Sleep
, TimeDelta::FromSeconds(1)));
145 for (int i
= 1; i
< kTaskCount
; ++i
) {
146 this->task_tracker_
->PostWrappedNonNestableTask(task_runner
, Closure());
149 this->delegate_
.StopTaskRunner();
151 EXPECT_TRUE(CheckNonNestableInvariants(this->task_tracker_
->GetTaskEvents(),
155 // This test posts N nestable tasks in sequence. It has the same expectations
156 // as SequentialNonNestable because even though the tasks are nestable, they
157 // will not be run nestedly in this case.
158 TYPED_TEST_P(SequencedTaskRunnerTest
, SequentialNestable
) {
159 const int kTaskCount
= 1000;
161 this->delegate_
.StartTaskRunner();
162 const scoped_refptr
<SequencedTaskRunner
> task_runner
=
163 this->delegate_
.GetTaskRunner();
165 this->task_tracker_
->PostWrappedNestableTask(
167 Bind(&PlatformThread::Sleep
, TimeDelta::FromSeconds(1)));
168 for (int i
= 1; i
< kTaskCount
; ++i
) {
169 this->task_tracker_
->PostWrappedNestableTask(task_runner
, Closure());
172 this->delegate_
.StopTaskRunner();
174 EXPECT_TRUE(CheckNonNestableInvariants(this->task_tracker_
->GetTaskEvents(),
178 // This test posts non-nestable tasks in order of increasing delay, and checks
179 // that that the tasks are run in FIFO order and that there is no execution
180 // overlap whatsoever between any two tasks.
181 TYPED_TEST_P(SequencedTaskRunnerTest
, SequentialDelayedNonNestable
) {
182 const int kTaskCount
= 20;
183 const int kDelayIncrementMs
= 50;
185 this->delegate_
.StartTaskRunner();
186 const scoped_refptr
<SequencedTaskRunner
> task_runner
=
187 this->delegate_
.GetTaskRunner();
189 for (int i
= 0; i
< kTaskCount
; ++i
) {
190 this->task_tracker_
->PostWrappedDelayedNonNestableTask(
193 TimeDelta::FromMilliseconds(kDelayIncrementMs
* i
));
196 this->task_tracker_
->WaitForCompletedTasks(kTaskCount
);
197 this->delegate_
.StopTaskRunner();
199 EXPECT_TRUE(CheckNonNestableInvariants(this->task_tracker_
->GetTaskEvents(),
203 // This test posts a fast, non-nestable task from within each of a number of
204 // slow, non-nestable tasks and checks that they all run in the sequence they
205 // were posted in and that there is no execution overlap whatsoever.
206 TYPED_TEST_P(SequencedTaskRunnerTest
, NonNestablePostFromNonNestableTask
) {
207 const int kParentCount
= 10;
208 const int kChildrenPerParent
= 10;
210 this->delegate_
.StartTaskRunner();
211 const scoped_refptr
<SequencedTaskRunner
> task_runner
=
212 this->delegate_
.GetTaskRunner();
214 for (int i
= 0; i
< kParentCount
; ++i
) {
216 &internal::SequencedTaskTracker::PostNonNestableTasks
,
220 this->task_tracker_
->PostWrappedNonNestableTask(task_runner
, task
);
223 this->delegate_
.StopTaskRunner();
225 EXPECT_TRUE(CheckNonNestableInvariants(
226 this->task_tracker_
->GetTaskEvents(),
227 kParentCount
* (kChildrenPerParent
+ 1)));
230 // This test posts two tasks with the same delay, and checks that the tasks are
231 // run in the order in which they were posted.
233 // NOTE: This is actually an approximate test since the API only takes a
234 // "delay" parameter, so we are not exactly simulating two tasks that get
235 // posted at the exact same time. It would be nice if the API allowed us to
236 // specify the desired run time.
237 TYPED_TEST_P(SequencedTaskRunnerTest
, DelayedTasksSameDelay
) {
238 const int kTaskCount
= 2;
239 const TimeDelta kDelay
= TimeDelta::FromMilliseconds(100);
241 this->delegate_
.StartTaskRunner();
242 const scoped_refptr
<SequencedTaskRunner
> task_runner
=
243 this->delegate_
.GetTaskRunner();
245 this->task_tracker_
->PostWrappedDelayedNonNestableTask(
246 task_runner
, Closure(), kDelay
);
247 this->task_tracker_
->PostWrappedDelayedNonNestableTask(
248 task_runner
, Closure(), kDelay
);
249 this->task_tracker_
->WaitForCompletedTasks(kTaskCount
);
250 this->delegate_
.StopTaskRunner();
252 EXPECT_TRUE(CheckNonNestableInvariants(this->task_tracker_
->GetTaskEvents(),
256 // This test posts a normal task and a delayed task, and checks that the
257 // delayed task runs after the normal task even if the normal task takes
258 // a long time to run.
259 TYPED_TEST_P(SequencedTaskRunnerTest
, DelayedTaskAfterLongTask
) {
260 const int kTaskCount
= 2;
262 this->delegate_
.StartTaskRunner();
263 const scoped_refptr
<SequencedTaskRunner
> task_runner
=
264 this->delegate_
.GetTaskRunner();
266 this->task_tracker_
->PostWrappedNonNestableTask(
267 task_runner
, base::Bind(&PlatformThread::Sleep
,
268 TimeDelta::FromMilliseconds(50)));
269 this->task_tracker_
->PostWrappedDelayedNonNestableTask(
270 task_runner
, Closure(), TimeDelta::FromMilliseconds(10));
271 this->task_tracker_
->WaitForCompletedTasks(kTaskCount
);
272 this->delegate_
.StopTaskRunner();
274 EXPECT_TRUE(CheckNonNestableInvariants(this->task_tracker_
->GetTaskEvents(),
278 // Test that a pile of normal tasks and a delayed task run in the
279 // time-to-run order.
280 TYPED_TEST_P(SequencedTaskRunnerTest
, DelayedTaskAfterManyLongTasks
) {
281 const int kTaskCount
= 11;
283 this->delegate_
.StartTaskRunner();
284 const scoped_refptr
<SequencedTaskRunner
> task_runner
=
285 this->delegate_
.GetTaskRunner();
287 for (int i
= 0; i
< kTaskCount
- 1; i
++) {
288 this->task_tracker_
->PostWrappedNonNestableTask(
289 task_runner
, base::Bind(&PlatformThread::Sleep
,
290 TimeDelta::FromMilliseconds(50)));
292 this->task_tracker_
->PostWrappedDelayedNonNestableTask(
293 task_runner
, Closure(), TimeDelta::FromMilliseconds(10));
294 this->task_tracker_
->WaitForCompletedTasks(kTaskCount
);
295 this->delegate_
.StopTaskRunner();
297 EXPECT_TRUE(CheckNonNestableInvariants(this->task_tracker_
->GetTaskEvents(),
302 // TODO(francoisk777@gmail.com) Add a test, similiar to the above, which runs
303 // some tasked nestedly (which should be implemented in the test
304 // delegate). Also add, to the the test delegate, a predicate which checks
305 // whether the implementation supports nested tasks.
308 // The SequencedTaskRunnerTest test case verifies behaviour that is expected
309 // from a sequenced task runner in order to be conformant.
310 REGISTER_TYPED_TEST_CASE_P(SequencedTaskRunnerTest
,
311 SequentialNonNestable
,
313 SequentialDelayedNonNestable
,
314 NonNestablePostFromNonNestableTask
,
315 DelayedTasksSameDelay
,
316 DelayedTaskAfterLongTask
,
317 DelayedTaskAfterManyLongTasks
);
319 template <typename TaskRunnerTestDelegate
>
320 class SequencedTaskRunnerDelayedTest
321 : public SequencedTaskRunnerTest
<TaskRunnerTestDelegate
> {};
323 TYPED_TEST_CASE_P(SequencedTaskRunnerDelayedTest
);
325 // This test posts a delayed task, and checks that the task is run later than
326 // the specified time.
327 TYPED_TEST_P(SequencedTaskRunnerDelayedTest
, DelayedTaskBasic
) {
328 const int kTaskCount
= 1;
329 const TimeDelta kDelay
= TimeDelta::FromMilliseconds(100);
331 this->delegate_
.StartTaskRunner();
332 const scoped_refptr
<SequencedTaskRunner
> task_runner
=
333 this->delegate_
.GetTaskRunner();
335 Time time_before_run
= Time::Now();
336 this->task_tracker_
->PostWrappedDelayedNonNestableTask(task_runner
, Closure(),
338 this->task_tracker_
->WaitForCompletedTasks(kTaskCount
);
339 this->delegate_
.StopTaskRunner();
340 Time time_after_run
= Time::Now();
342 EXPECT_TRUE(CheckNonNestableInvariants(this->task_tracker_
->GetTaskEvents(),
344 EXPECT_LE(kDelay
, time_after_run
- time_before_run
);
347 // SequencedTaskRunnerDelayedTest tests that the |delay| parameter of
348 // is used to actually wait for |delay| ms before executing the task.
349 // This is not mandatory for a SequencedTaskRunner to be compliant.
350 REGISTER_TYPED_TEST_CASE_P(SequencedTaskRunnerDelayedTest
, DelayedTaskBasic
);
354 #endif // BASE_TEST_SEQUENCED_TASK_RUNNER_TEST_TEMPLATE_H_