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.
4
5 #include "base/deferred_sequenced_task_runner.h"
6
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/callback_forward.h"
10 #include "base/location.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/run_loop.h"
14 #include "base/single_thread_task_runner.h"
15 #include "base/threading/thread.h"
16 #include "testing/gmock/include/gmock/gmock.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18
19 namespace base {
20 namespace {
21
22 class DeferredSequencedTaskRunnerTest : public testing::Test {
23 public:
24 class ExecuteTaskOnDestructor : public RefCounted<ExecuteTaskOnDestructor> {
25 public:
ExecuteTaskOnDestructor(DeferredSequencedTaskRunnerTest * executor,int task_id)26 ExecuteTaskOnDestructor(
27 DeferredSequencedTaskRunnerTest* executor,
28 int task_id)
29 : executor_(executor),
30 task_id_(task_id) {
31 }
32 private:
33 friend class RefCounted<ExecuteTaskOnDestructor>;
~ExecuteTaskOnDestructor()34 virtual ~ExecuteTaskOnDestructor() { executor_->ExecuteTask(task_id_); }
35 DeferredSequencedTaskRunnerTest* executor_;
36 int task_id_;
37 };
38
ExecuteTask(int task_id)39 void ExecuteTask(int task_id) {
40 AutoLock lock(lock_);
41 executed_task_ids_.push_back(task_id);
42 }
43
PostExecuteTask(int task_id)44 void PostExecuteTask(int task_id) {
45 runner_->PostTask(FROM_HERE,
46 BindOnce(&DeferredSequencedTaskRunnerTest::ExecuteTask,
47 Unretained(this), task_id));
48 }
49
StartRunner()50 void StartRunner() {
51 runner_->Start();
52 }
53
DoNothing(ExecuteTaskOnDestructor * object)54 void DoNothing(ExecuteTaskOnDestructor* object) {
55 }
56
57 protected:
DeferredSequencedTaskRunnerTest()58 DeferredSequencedTaskRunnerTest()
59 : loop_(),
60 runner_(new DeferredSequencedTaskRunner(loop_.task_runner())) {}
61
62 MessageLoop loop_;
63 scoped_refptr<DeferredSequencedTaskRunner> runner_;
64 mutable Lock lock_;
65 std::vector<int> executed_task_ids_;
66 };
67
TEST_F(DeferredSequencedTaskRunnerTest,Stopped)68 TEST_F(DeferredSequencedTaskRunnerTest, Stopped) {
69 PostExecuteTask(1);
70 RunLoop().RunUntilIdle();
71 EXPECT_THAT(executed_task_ids_, testing::ElementsAre());
72 }
73
TEST_F(DeferredSequencedTaskRunnerTest,Start)74 TEST_F(DeferredSequencedTaskRunnerTest, Start) {
75 StartRunner();
76 PostExecuteTask(1);
77 RunLoop().RunUntilIdle();
78 EXPECT_THAT(executed_task_ids_, testing::ElementsAre(1));
79 }
80
TEST_F(DeferredSequencedTaskRunnerTest,StartWithMultipleElements)81 TEST_F(DeferredSequencedTaskRunnerTest, StartWithMultipleElements) {
82 StartRunner();
83 for (int i = 1; i < 5; ++i)
84 PostExecuteTask(i);
85
86 RunLoop().RunUntilIdle();
87 EXPECT_THAT(executed_task_ids_, testing::ElementsAre(1, 2, 3, 4));
88 }
89
TEST_F(DeferredSequencedTaskRunnerTest,DeferredStart)90 TEST_F(DeferredSequencedTaskRunnerTest, DeferredStart) {
91 PostExecuteTask(1);
92 RunLoop().RunUntilIdle();
93 EXPECT_THAT(executed_task_ids_, testing::ElementsAre());
94
95 StartRunner();
96 RunLoop().RunUntilIdle();
97 EXPECT_THAT(executed_task_ids_, testing::ElementsAre(1));
98
99 PostExecuteTask(2);
100 RunLoop().RunUntilIdle();
101 EXPECT_THAT(executed_task_ids_, testing::ElementsAre(1, 2));
102 }
103
TEST_F(DeferredSequencedTaskRunnerTest,DeferredStartWithMultipleElements)104 TEST_F(DeferredSequencedTaskRunnerTest, DeferredStartWithMultipleElements) {
105 for (int i = 1; i < 5; ++i)
106 PostExecuteTask(i);
107 RunLoop().RunUntilIdle();
108 EXPECT_THAT(executed_task_ids_, testing::ElementsAre());
109
110 StartRunner();
111 for (int i = 5; i < 9; ++i)
112 PostExecuteTask(i);
113 RunLoop().RunUntilIdle();
114 EXPECT_THAT(executed_task_ids_, testing::ElementsAre(1, 2, 3, 4, 5, 6, 7, 8));
115 }
116
TEST_F(DeferredSequencedTaskRunnerTest,DeferredStartWithMultipleThreads)117 TEST_F(DeferredSequencedTaskRunnerTest, DeferredStartWithMultipleThreads) {
118 {
119 Thread thread1("DeferredSequencedTaskRunnerTestThread1");
120 Thread thread2("DeferredSequencedTaskRunnerTestThread2");
121 thread1.Start();
122 thread2.Start();
123 for (int i = 0; i < 5; ++i) {
124 thread1.task_runner()->PostTask(
125 FROM_HERE, BindOnce(&DeferredSequencedTaskRunnerTest::PostExecuteTask,
126 Unretained(this), 2 * i));
127 thread2.task_runner()->PostTask(
128 FROM_HERE, BindOnce(&DeferredSequencedTaskRunnerTest::PostExecuteTask,
129 Unretained(this), 2 * i + 1));
130 if (i == 2) {
131 thread1.task_runner()->PostTask(
132 FROM_HERE, BindOnce(&DeferredSequencedTaskRunnerTest::StartRunner,
133 Unretained(this)));
134 }
135 }
136 }
137
138 RunLoop().RunUntilIdle();
139 EXPECT_THAT(executed_task_ids_,
140 testing::WhenSorted(testing::ElementsAre(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)));
141 }
142
TEST_F(DeferredSequencedTaskRunnerTest,ObjectDestructionOrder)143 TEST_F(DeferredSequencedTaskRunnerTest, ObjectDestructionOrder) {
144 {
145 Thread thread("DeferredSequencedTaskRunnerTestThread");
146 thread.Start();
147 runner_ = new DeferredSequencedTaskRunner(thread.task_runner());
148 for (int i = 0; i < 5; ++i) {
149 {
150 // Use a block to ensure that no reference to |short_lived_object|
151 // is kept on the main thread after it is posted to |runner_|.
152 scoped_refptr<ExecuteTaskOnDestructor> short_lived_object =
153 new ExecuteTaskOnDestructor(this, 2 * i);
154 runner_->PostTask(
155 FROM_HERE,
156 BindOnce(&DeferredSequencedTaskRunnerTest::DoNothing,
157 Unretained(this), RetainedRef(short_lived_object)));
158 }
159 // |short_lived_object| with id |2 * i| should be destroyed before the
160 // task |2 * i + 1| is executed.
161 PostExecuteTask(2 * i + 1);
162 }
163 StartRunner();
164 }
165
166 // All |short_lived_object| with id |2 * i| are destroyed before the task
167 // |2 * i + 1| is executed.
168 EXPECT_THAT(executed_task_ids_,
169 testing::ElementsAre(0, 1, 2, 3, 4, 5, 6, 7, 8, 9));
170 }
171
GetRunsTasksInCurrentSequence(bool * result,scoped_refptr<SequencedTaskRunner> runner,OnceClosure quit)172 void GetRunsTasksInCurrentSequence(bool* result,
173 scoped_refptr<SequencedTaskRunner> runner,
174 OnceClosure quit) {
175 *result = runner->RunsTasksInCurrentSequence();
176 std::move(quit).Run();
177 }
178
TEST_F(DeferredSequencedTaskRunnerTest,RunsTasksInCurrentSequence)179 TEST_F(DeferredSequencedTaskRunnerTest, RunsTasksInCurrentSequence) {
180 scoped_refptr<DeferredSequencedTaskRunner> runner =
181 MakeRefCounted<DeferredSequencedTaskRunner>();
182 EXPECT_TRUE(runner->RunsTasksInCurrentSequence());
183
184 Thread thread1("DeferredSequencedTaskRunnerTestThread1");
185 thread1.Start();
186 bool runs_task_in_current_thread = true;
187 base::RunLoop run_loop;
188 thread1.task_runner()->PostTask(
189 FROM_HERE,
190 BindOnce(&GetRunsTasksInCurrentSequence, &runs_task_in_current_thread,
191 runner, run_loop.QuitClosure()));
192 run_loop.Run();
193 EXPECT_FALSE(runs_task_in_current_thread);
194 }
195
TEST_F(DeferredSequencedTaskRunnerTest,StartWithTaskRunner)196 TEST_F(DeferredSequencedTaskRunnerTest, StartWithTaskRunner) {
197 scoped_refptr<DeferredSequencedTaskRunner> runner =
198 MakeRefCounted<DeferredSequencedTaskRunner>();
199 bool run_called = false;
200 base::RunLoop run_loop;
201 runner->PostTask(FROM_HERE,
202 BindOnce(
203 [](bool* run_called, base::Closure quit_closure) {
204 *run_called = true;
205 std::move(quit_closure).Run();
206 },
207 &run_called, run_loop.QuitClosure()));
208 runner->StartWithTaskRunner(loop_.task_runner());
209 run_loop.Run();
210 EXPECT_TRUE(run_called);
211 }
212
213 } // namespace
214 } // namespace base
215