1 // Copyright (c) 2013 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 #include "base/deferred_sequenced_task_runner.h"
7 #include "base/basictypes.h"
9 #include "base/bind_helpers.h"
10 #include "base/location.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/single_thread_task_runner.h"
13 #include "base/threading/non_thread_safe.h"
14 #include "base/threading/thread.h"
15 #include "testing/gmock/include/gmock/gmock.h"
16 #include "testing/gtest/include/gtest/gtest.h"
20 class DeferredSequencedTaskRunnerTest
: public testing::Test
,
21 public base::NonThreadSafe
{
23 class ExecuteTaskOnDestructor
:
24 public base::RefCounted
<ExecuteTaskOnDestructor
> {
26 ExecuteTaskOnDestructor(
27 DeferredSequencedTaskRunnerTest
* executor
,
29 : executor_(executor
),
33 friend class base::RefCounted
<ExecuteTaskOnDestructor
>;
34 virtual ~ExecuteTaskOnDestructor() {
35 executor_
->ExecuteTask(task_id_
);
37 DeferredSequencedTaskRunnerTest
* executor_
;
41 void ExecuteTask(int task_id
) {
42 base::AutoLock
lock(lock_
);
43 executed_task_ids_
.push_back(task_id
);
46 void PostExecuteTask(int task_id
) {
47 runner_
->PostTask(FROM_HERE
,
48 base::Bind(&DeferredSequencedTaskRunnerTest::ExecuteTask
,
49 base::Unretained(this),
57 void DoNothing(ExecuteTaskOnDestructor
* object
) {
61 DeferredSequencedTaskRunnerTest()
63 runner_(new base::DeferredSequencedTaskRunner(loop_
.task_runner())) {}
65 base::MessageLoop loop_
;
66 scoped_refptr
<base::DeferredSequencedTaskRunner
> runner_
;
67 mutable base::Lock lock_
;
68 std::vector
<int> executed_task_ids_
;
71 TEST_F(DeferredSequencedTaskRunnerTest
, Stopped
) {
74 EXPECT_THAT(executed_task_ids_
, testing::ElementsAre());
77 TEST_F(DeferredSequencedTaskRunnerTest
, Start
) {
81 EXPECT_THAT(executed_task_ids_
, testing::ElementsAre(1));
84 TEST_F(DeferredSequencedTaskRunnerTest
, StartWithMultipleElements
) {
86 for (int i
= 1; i
< 5; ++i
)
90 EXPECT_THAT(executed_task_ids_
, testing::ElementsAre(1, 2, 3, 4));
93 TEST_F(DeferredSequencedTaskRunnerTest
, DeferredStart
) {
96 EXPECT_THAT(executed_task_ids_
, testing::ElementsAre());
100 EXPECT_THAT(executed_task_ids_
, testing::ElementsAre(1));
103 loop_
.RunUntilIdle();
104 EXPECT_THAT(executed_task_ids_
, testing::ElementsAre(1, 2));
107 TEST_F(DeferredSequencedTaskRunnerTest
, DeferredStartWithMultipleElements
) {
108 for (int i
= 1; i
< 5; ++i
)
110 loop_
.RunUntilIdle();
111 EXPECT_THAT(executed_task_ids_
, testing::ElementsAre());
114 for (int i
= 5; i
< 9; ++i
)
116 loop_
.RunUntilIdle();
117 EXPECT_THAT(executed_task_ids_
, testing::ElementsAre(1, 2, 3, 4, 5, 6, 7, 8));
120 TEST_F(DeferredSequencedTaskRunnerTest
, DeferredStartWithMultipleThreads
) {
122 base::Thread
thread1("DeferredSequencedTaskRunnerTestThread1");
123 base::Thread
thread2("DeferredSequencedTaskRunnerTestThread2");
126 for (int i
= 0; i
< 5; ++i
) {
127 thread1
.task_runner()->PostTask(
129 base::Bind(&DeferredSequencedTaskRunnerTest::PostExecuteTask
,
130 base::Unretained(this), 2 * i
));
131 thread2
.task_runner()->PostTask(
133 base::Bind(&DeferredSequencedTaskRunnerTest::PostExecuteTask
,
134 base::Unretained(this), 2 * i
+ 1));
136 thread1
.task_runner()->PostTask(
137 FROM_HERE
, base::Bind(&DeferredSequencedTaskRunnerTest::StartRunner
,
138 base::Unretained(this)));
143 loop_
.RunUntilIdle();
144 EXPECT_THAT(executed_task_ids_
,
145 testing::WhenSorted(testing::ElementsAre(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)));
148 TEST_F(DeferredSequencedTaskRunnerTest
, ObjectDestructionOrder
) {
150 base::Thread
thread("DeferredSequencedTaskRunnerTestThread");
152 runner_
= new base::DeferredSequencedTaskRunner(thread
.task_runner());
153 for (int i
= 0; i
< 5; ++i
) {
155 // Use a block to ensure that no reference to |short_lived_object|
156 // is kept on the main thread after it is posted to |runner_|.
157 scoped_refptr
<ExecuteTaskOnDestructor
> short_lived_object
=
158 new ExecuteTaskOnDestructor(this, 2 * i
);
161 base::Bind(&DeferredSequencedTaskRunnerTest::DoNothing
,
162 base::Unretained(this),
163 short_lived_object
));
165 // |short_lived_object| with id |2 * i| should be destroyed before the
166 // task |2 * i + 1| is executed.
167 PostExecuteTask(2 * i
+ 1);
172 // All |short_lived_object| with id |2 * i| are destroyed before the task
173 // |2 * i + 1| is executed.
174 EXPECT_THAT(executed_task_ids_
,
175 testing::ElementsAre(0, 1, 2, 3, 4, 5, 6, 7, 8, 9));