blob: f1216faebe21573a84379e71682aa6eca7e30424 [file] [log] [blame]
[email protected]afecfb72013-04-18 17:17:331// 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
[email protected]afecfb72013-04-18 17:17:337#include "base/bind.h"
8#include "base/bind_helpers.h"
Scott Violet875789e2018-02-02 07:46:489#include "base/callback_forward.h"
skyostil054861d2015-04-30 19:06:1510#include "base/location.h"
[email protected]afecfb72013-04-18 17:17:3311#include "base/memory/ref_counted.h"
fdoray10224582016-06-30 18:17:3912#include "base/run_loop.h"
skyostil054861d2015-04-30 19:06:1513#include "base/single_thread_task_runner.h"
Gabriel Charettec7108742019-08-23 03:31:4014#include "base/test/task_environment.h"
[email protected]afecfb72013-04-18 17:17:3315#include "base/threading/thread.h"
Sami Kyostila3f49cb572018-11-19 13:01:0916#include "base/threading/thread_task_runner_handle.h"
[email protected]afecfb72013-04-18 17:17:3317#include "testing/gmock/include/gmock/gmock.h"
18#include "testing/gtest/include/gtest/gtest.h"
19
Scott Violet875789e2018-02-02 07:46:4820namespace base {
[email protected]afecfb72013-04-18 17:17:3321namespace {
22
gab7d2fae42017-06-01 14:02:5523class DeferredSequencedTaskRunnerTest : public testing::Test {
[email protected]afecfb72013-04-18 17:17:3324 public:
Yuki Shiinoae6ae092020-04-28 06:20:5125 class ExecuteTaskOnDestructor : public RefCounted<ExecuteTaskOnDestructor> {
[email protected]afecfb72013-04-18 17:17:3326 public:
27 ExecuteTaskOnDestructor(
28 DeferredSequencedTaskRunnerTest* executor,
29 int task_id)
30 : executor_(executor),
31 task_id_(task_id) {
32 }
33 private:
Yuki Shiinoae6ae092020-04-28 06:20:5134 friend class RefCounted<ExecuteTaskOnDestructor>;
Scott Violet875789e2018-02-02 07:46:4835 virtual ~ExecuteTaskOnDestructor() { executor_->ExecuteTask(task_id_); }
36 DeferredSequencedTaskRunnerTest* executor_;
37 int task_id_;
[email protected]afecfb72013-04-18 17:17:3338 };
39
40 void ExecuteTask(int task_id) {
Scott Violet875789e2018-02-02 07:46:4841 AutoLock lock(lock_);
[email protected]afecfb72013-04-18 17:17:3342 executed_task_ids_.push_back(task_id);
43 }
44
45 void PostExecuteTask(int task_id) {
Scott Violet875789e2018-02-02 07:46:4846 runner_->PostTask(FROM_HERE,
47 BindOnce(&DeferredSequencedTaskRunnerTest::ExecuteTask,
48 Unretained(this), task_id));
[email protected]afecfb72013-04-18 17:17:3349 }
50
51 void StartRunner() {
52 runner_->Start();
53 }
54
55 void DoNothing(ExecuteTaskOnDestructor* object) {
56 }
57
58 protected:
skyostil054861d2015-04-30 19:06:1559 DeferredSequencedTaskRunnerTest()
Sami Kyostila3f49cb572018-11-19 13:01:0960 : runner_(
61 new DeferredSequencedTaskRunner(ThreadTaskRunnerHandle::Get())) {}
[email protected]afecfb72013-04-18 17:17:3362
Gabriel Charette694c3c332019-08-19 14:53:0563 test::TaskEnvironment task_environment_;
Scott Violet875789e2018-02-02 07:46:4864 scoped_refptr<DeferredSequencedTaskRunner> runner_;
65 mutable Lock lock_;
[email protected]afecfb72013-04-18 17:17:3366 std::vector<int> executed_task_ids_;
67};
68
69TEST_F(DeferredSequencedTaskRunnerTest, Stopped) {
70 PostExecuteTask(1);
Scott Violet875789e2018-02-02 07:46:4871 RunLoop().RunUntilIdle();
[email protected]afecfb72013-04-18 17:17:3372 EXPECT_THAT(executed_task_ids_, testing::ElementsAre());
73}
74
75TEST_F(DeferredSequencedTaskRunnerTest, Start) {
76 StartRunner();
77 PostExecuteTask(1);
Scott Violet875789e2018-02-02 07:46:4878 RunLoop().RunUntilIdle();
[email protected]afecfb72013-04-18 17:17:3379 EXPECT_THAT(executed_task_ids_, testing::ElementsAre(1));
80}
81
82TEST_F(DeferredSequencedTaskRunnerTest, StartWithMultipleElements) {
83 StartRunner();
84 for (int i = 1; i < 5; ++i)
85 PostExecuteTask(i);
86
Scott Violet875789e2018-02-02 07:46:4887 RunLoop().RunUntilIdle();
[email protected]afecfb72013-04-18 17:17:3388 EXPECT_THAT(executed_task_ids_, testing::ElementsAre(1, 2, 3, 4));
89}
90
91TEST_F(DeferredSequencedTaskRunnerTest, DeferredStart) {
92 PostExecuteTask(1);
Scott Violet875789e2018-02-02 07:46:4893 RunLoop().RunUntilIdle();
[email protected]afecfb72013-04-18 17:17:3394 EXPECT_THAT(executed_task_ids_, testing::ElementsAre());
95
96 StartRunner();
Scott Violet875789e2018-02-02 07:46:4897 RunLoop().RunUntilIdle();
[email protected]afecfb72013-04-18 17:17:3398 EXPECT_THAT(executed_task_ids_, testing::ElementsAre(1));
99
100 PostExecuteTask(2);
Scott Violet875789e2018-02-02 07:46:48101 RunLoop().RunUntilIdle();
[email protected]afecfb72013-04-18 17:17:33102 EXPECT_THAT(executed_task_ids_, testing::ElementsAre(1, 2));
103}
104
105TEST_F(DeferredSequencedTaskRunnerTest, DeferredStartWithMultipleElements) {
106 for (int i = 1; i < 5; ++i)
107 PostExecuteTask(i);
Scott Violet875789e2018-02-02 07:46:48108 RunLoop().RunUntilIdle();
[email protected]afecfb72013-04-18 17:17:33109 EXPECT_THAT(executed_task_ids_, testing::ElementsAre());
110
111 StartRunner();
112 for (int i = 5; i < 9; ++i)
113 PostExecuteTask(i);
Scott Violet875789e2018-02-02 07:46:48114 RunLoop().RunUntilIdle();
[email protected]afecfb72013-04-18 17:17:33115 EXPECT_THAT(executed_task_ids_, testing::ElementsAre(1, 2, 3, 4, 5, 6, 7, 8));
116}
117
118TEST_F(DeferredSequencedTaskRunnerTest, DeferredStartWithMultipleThreads) {
119 {
Scott Violet875789e2018-02-02 07:46:48120 Thread thread1("DeferredSequencedTaskRunnerTestThread1");
121 Thread thread2("DeferredSequencedTaskRunnerTestThread2");
[email protected]afecfb72013-04-18 17:17:33122 thread1.Start();
123 thread2.Start();
124 for (int i = 0; i < 5; ++i) {
skyostil054861d2015-04-30 19:06:15125 thread1.task_runner()->PostTask(
Scott Violet875789e2018-02-02 07:46:48126 FROM_HERE, BindOnce(&DeferredSequencedTaskRunnerTest::PostExecuteTask,
127 Unretained(this), 2 * i));
skyostil054861d2015-04-30 19:06:15128 thread2.task_runner()->PostTask(
Scott Violet875789e2018-02-02 07:46:48129 FROM_HERE, BindOnce(&DeferredSequencedTaskRunnerTest::PostExecuteTask,
130 Unretained(this), 2 * i + 1));
[email protected]afecfb72013-04-18 17:17:33131 if (i == 2) {
skyostil054861d2015-04-30 19:06:15132 thread1.task_runner()->PostTask(
Scott Violet875789e2018-02-02 07:46:48133 FROM_HERE, BindOnce(&DeferredSequencedTaskRunnerTest::StartRunner,
134 Unretained(this)));
[email protected]afecfb72013-04-18 17:17:33135 }
136 }
137 }
138
Scott Violet875789e2018-02-02 07:46:48139 RunLoop().RunUntilIdle();
[email protected]afecfb72013-04-18 17:17:33140 EXPECT_THAT(executed_task_ids_,
141 testing::WhenSorted(testing::ElementsAre(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)));
142}
143
144TEST_F(DeferredSequencedTaskRunnerTest, ObjectDestructionOrder) {
145 {
Scott Violet875789e2018-02-02 07:46:48146 Thread thread("DeferredSequencedTaskRunnerTestThread");
[email protected]afecfb72013-04-18 17:17:33147 thread.Start();
Scott Violet875789e2018-02-02 07:46:48148 runner_ = new DeferredSequencedTaskRunner(thread.task_runner());
[email protected]afecfb72013-04-18 17:17:33149 for (int i = 0; i < 5; ++i) {
150 {
151 // Use a block to ensure that no reference to |short_lived_object|
152 // is kept on the main thread after it is posted to |runner_|.
153 scoped_refptr<ExecuteTaskOnDestructor> short_lived_object =
154 new ExecuteTaskOnDestructor(this, 2 * i);
155 runner_->PostTask(
tzik92b7a422017-04-11 15:00:44156 FROM_HERE,
Scott Violet875789e2018-02-02 07:46:48157 BindOnce(&DeferredSequencedTaskRunnerTest::DoNothing,
158 Unretained(this), RetainedRef(short_lived_object)));
[email protected]afecfb72013-04-18 17:17:33159 }
160 // |short_lived_object| with id |2 * i| should be destroyed before the
161 // task |2 * i + 1| is executed.
162 PostExecuteTask(2 * i + 1);
163 }
164 StartRunner();
165 }
166
167 // All |short_lived_object| with id |2 * i| are destroyed before the task
168 // |2 * i + 1| is executed.
169 EXPECT_THAT(executed_task_ids_,
170 testing::ElementsAre(0, 1, 2, 3, 4, 5, 6, 7, 8, 9));
171}
172
Scott Violet875789e2018-02-02 07:46:48173void GetRunsTasksInCurrentSequence(bool* result,
174 scoped_refptr<SequencedTaskRunner> runner,
175 OnceClosure quit) {
176 *result = runner->RunsTasksInCurrentSequence();
177 std::move(quit).Run();
178}
179
180TEST_F(DeferredSequencedTaskRunnerTest, RunsTasksInCurrentSequence) {
181 scoped_refptr<DeferredSequencedTaskRunner> runner =
182 MakeRefCounted<DeferredSequencedTaskRunner>();
183 EXPECT_TRUE(runner->RunsTasksInCurrentSequence());
184
185 Thread thread1("DeferredSequencedTaskRunnerTestThread1");
186 thread1.Start();
187 bool runs_task_in_current_thread = true;
188 base::RunLoop run_loop;
189 thread1.task_runner()->PostTask(
190 FROM_HERE,
191 BindOnce(&GetRunsTasksInCurrentSequence, &runs_task_in_current_thread,
192 runner, run_loop.QuitClosure()));
193 run_loop.Run();
194 EXPECT_FALSE(runs_task_in_current_thread);
195}
196
197TEST_F(DeferredSequencedTaskRunnerTest, StartWithTaskRunner) {
198 scoped_refptr<DeferredSequencedTaskRunner> runner =
199 MakeRefCounted<DeferredSequencedTaskRunner>();
200 bool run_called = false;
201 base::RunLoop run_loop;
202 runner->PostTask(FROM_HERE,
203 BindOnce(
kylechar01598d72019-05-21 18:35:31204 [](bool* run_called, base::OnceClosure quit_closure) {
Scott Violet875789e2018-02-02 07:46:48205 *run_called = true;
206 std::move(quit_closure).Run();
207 },
208 &run_called, run_loop.QuitClosure()));
Sami Kyostila3f49cb572018-11-19 13:01:09209 runner->StartWithTaskRunner(ThreadTaskRunnerHandle::Get());
Scott Violet875789e2018-02-02 07:46:48210 run_loop.Run();
211 EXPECT_TRUE(run_called);
212}
213
[email protected]afecfb72013-04-18 17:17:33214} // namespace
Scott Violet875789e2018-02-02 07:46:48215} // namespace base