blob: 799ddcd13580749b3d89be960843d23dba953d24 [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"
skyostil054861d2015-04-30 19:06:159#include "base/location.h"
[email protected]afecfb72013-04-18 17:17:3310#include "base/memory/ref_counted.h"
gabf64a25e2017-05-12 19:42:5611#include "base/message_loop/message_loop.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"
[email protected]afecfb72013-04-18 17:17:3314#include "base/threading/non_thread_safe.h"
15#include "base/threading/thread.h"
16#include "testing/gmock/include/gmock/gmock.h"
17#include "testing/gtest/include/gtest/gtest.h"
18
19namespace {
20
21class DeferredSequencedTaskRunnerTest : public testing::Test,
22 public base::NonThreadSafe {
23 public:
24 class ExecuteTaskOnDestructor :
25 public base::RefCounted<ExecuteTaskOnDestructor> {
26 public:
27 ExecuteTaskOnDestructor(
28 DeferredSequencedTaskRunnerTest* executor,
29 int task_id)
30 : executor_(executor),
31 task_id_(task_id) {
32 }
33 private:
34 friend class base::RefCounted<ExecuteTaskOnDestructor>;
35 virtual ~ExecuteTaskOnDestructor() {
36 executor_->ExecuteTask(task_id_);
37 }
38 DeferredSequencedTaskRunnerTest* executor_;
39 int task_id_;
40 };
41
42 void ExecuteTask(int task_id) {
43 base::AutoLock lock(lock_);
44 executed_task_ids_.push_back(task_id);
45 }
46
47 void PostExecuteTask(int task_id) {
tzik92b7a422017-04-11 15:00:4448 runner_->PostTask(
49 FROM_HERE, base::BindOnce(&DeferredSequencedTaskRunnerTest::ExecuteTask,
50 base::Unretained(this), task_id));
[email protected]afecfb72013-04-18 17:17:3351 }
52
53 void StartRunner() {
54 runner_->Start();
55 }
56
57 void DoNothing(ExecuteTaskOnDestructor* object) {
58 }
59
60 protected:
skyostil054861d2015-04-30 19:06:1561 DeferredSequencedTaskRunnerTest()
62 : loop_(),
63 runner_(new base::DeferredSequencedTaskRunner(loop_.task_runner())) {}
[email protected]afecfb72013-04-18 17:17:3364
[email protected]9e7154122013-05-30 23:11:0465 base::MessageLoop loop_;
[email protected]afecfb72013-04-18 17:17:3366 scoped_refptr<base::DeferredSequencedTaskRunner> runner_;
67 mutable base::Lock lock_;
68 std::vector<int> executed_task_ids_;
69};
70
71TEST_F(DeferredSequencedTaskRunnerTest, Stopped) {
72 PostExecuteTask(1);
fdoray10224582016-06-30 18:17:3973 base::RunLoop().RunUntilIdle();
[email protected]afecfb72013-04-18 17:17:3374 EXPECT_THAT(executed_task_ids_, testing::ElementsAre());
75}
76
77TEST_F(DeferredSequencedTaskRunnerTest, Start) {
78 StartRunner();
79 PostExecuteTask(1);
fdoray10224582016-06-30 18:17:3980 base::RunLoop().RunUntilIdle();
[email protected]afecfb72013-04-18 17:17:3381 EXPECT_THAT(executed_task_ids_, testing::ElementsAre(1));
82}
83
84TEST_F(DeferredSequencedTaskRunnerTest, StartWithMultipleElements) {
85 StartRunner();
86 for (int i = 1; i < 5; ++i)
87 PostExecuteTask(i);
88
fdoray10224582016-06-30 18:17:3989 base::RunLoop().RunUntilIdle();
[email protected]afecfb72013-04-18 17:17:3390 EXPECT_THAT(executed_task_ids_, testing::ElementsAre(1, 2, 3, 4));
91}
92
93TEST_F(DeferredSequencedTaskRunnerTest, DeferredStart) {
94 PostExecuteTask(1);
fdoray10224582016-06-30 18:17:3995 base::RunLoop().RunUntilIdle();
[email protected]afecfb72013-04-18 17:17:3396 EXPECT_THAT(executed_task_ids_, testing::ElementsAre());
97
98 StartRunner();
fdoray10224582016-06-30 18:17:3999 base::RunLoop().RunUntilIdle();
[email protected]afecfb72013-04-18 17:17:33100 EXPECT_THAT(executed_task_ids_, testing::ElementsAre(1));
101
102 PostExecuteTask(2);
fdoray10224582016-06-30 18:17:39103 base::RunLoop().RunUntilIdle();
[email protected]afecfb72013-04-18 17:17:33104 EXPECT_THAT(executed_task_ids_, testing::ElementsAre(1, 2));
105}
106
107TEST_F(DeferredSequencedTaskRunnerTest, DeferredStartWithMultipleElements) {
108 for (int i = 1; i < 5; ++i)
109 PostExecuteTask(i);
fdoray10224582016-06-30 18:17:39110 base::RunLoop().RunUntilIdle();
[email protected]afecfb72013-04-18 17:17:33111 EXPECT_THAT(executed_task_ids_, testing::ElementsAre());
112
113 StartRunner();
114 for (int i = 5; i < 9; ++i)
115 PostExecuteTask(i);
fdoray10224582016-06-30 18:17:39116 base::RunLoop().RunUntilIdle();
[email protected]afecfb72013-04-18 17:17:33117 EXPECT_THAT(executed_task_ids_, testing::ElementsAre(1, 2, 3, 4, 5, 6, 7, 8));
118}
119
120TEST_F(DeferredSequencedTaskRunnerTest, DeferredStartWithMultipleThreads) {
121 {
122 base::Thread thread1("DeferredSequencedTaskRunnerTestThread1");
123 base::Thread thread2("DeferredSequencedTaskRunnerTestThread2");
124 thread1.Start();
125 thread2.Start();
126 for (int i = 0; i < 5; ++i) {
skyostil054861d2015-04-30 19:06:15127 thread1.task_runner()->PostTask(
[email protected]afecfb72013-04-18 17:17:33128 FROM_HERE,
tzik92b7a422017-04-11 15:00:44129 base::BindOnce(&DeferredSequencedTaskRunnerTest::PostExecuteTask,
130 base::Unretained(this), 2 * i));
skyostil054861d2015-04-30 19:06:15131 thread2.task_runner()->PostTask(
[email protected]afecfb72013-04-18 17:17:33132 FROM_HERE,
tzik92b7a422017-04-11 15:00:44133 base::BindOnce(&DeferredSequencedTaskRunnerTest::PostExecuteTask,
134 base::Unretained(this), 2 * i + 1));
[email protected]afecfb72013-04-18 17:17:33135 if (i == 2) {
skyostil054861d2015-04-30 19:06:15136 thread1.task_runner()->PostTask(
tzik92b7a422017-04-11 15:00:44137 FROM_HERE,
138 base::BindOnce(&DeferredSequencedTaskRunnerTest::StartRunner,
139 base::Unretained(this)));
[email protected]afecfb72013-04-18 17:17:33140 }
141 }
142 }
143
fdoray10224582016-06-30 18:17:39144 base::RunLoop().RunUntilIdle();
[email protected]afecfb72013-04-18 17:17:33145 EXPECT_THAT(executed_task_ids_,
146 testing::WhenSorted(testing::ElementsAre(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)));
147}
148
149TEST_F(DeferredSequencedTaskRunnerTest, ObjectDestructionOrder) {
150 {
151 base::Thread thread("DeferredSequencedTaskRunnerTestThread");
152 thread.Start();
skyostil054861d2015-04-30 19:06:15153 runner_ = new base::DeferredSequencedTaskRunner(thread.task_runner());
[email protected]afecfb72013-04-18 17:17:33154 for (int i = 0; i < 5; ++i) {
155 {
156 // Use a block to ensure that no reference to |short_lived_object|
157 // is kept on the main thread after it is posted to |runner_|.
158 scoped_refptr<ExecuteTaskOnDestructor> short_lived_object =
159 new ExecuteTaskOnDestructor(this, 2 * i);
160 runner_->PostTask(
tzik92b7a422017-04-11 15:00:44161 FROM_HERE,
162 base::BindOnce(&DeferredSequencedTaskRunnerTest::DoNothing,
163 base::Unretained(this),
164 base::RetainedRef(short_lived_object)));
[email protected]afecfb72013-04-18 17:17:33165 }
166 // |short_lived_object| with id |2 * i| should be destroyed before the
167 // task |2 * i + 1| is executed.
168 PostExecuteTask(2 * i + 1);
169 }
170 StartRunner();
171 }
172
173 // All |short_lived_object| with id |2 * i| are destroyed before the task
174 // |2 * i + 1| is executed.
175 EXPECT_THAT(executed_task_ids_,
176 testing::ElementsAre(0, 1, 2, 3, 4, 5, 6, 7, 8, 9));
177}
178
179} // namespace