blob: a96befacda27eafa8bd44cd7399dda48d71a1fc7 [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/thread.h"
15#include "testing/gmock/include/gmock/gmock.h"
16#include "testing/gtest/include/gtest/gtest.h"
17
18namespace {
19
gab7d2fae42017-06-01 14:02:5520class DeferredSequencedTaskRunnerTest : public testing::Test {
[email protected]afecfb72013-04-18 17:17:3321 public:
22 class ExecuteTaskOnDestructor :
23 public base::RefCounted<ExecuteTaskOnDestructor> {
24 public:
25 ExecuteTaskOnDestructor(
26 DeferredSequencedTaskRunnerTest* executor,
27 int task_id)
28 : executor_(executor),
29 task_id_(task_id) {
30 }
31 private:
32 friend class base::RefCounted<ExecuteTaskOnDestructor>;
33 virtual ~ExecuteTaskOnDestructor() {
34 executor_->ExecuteTask(task_id_);
35 }
36 DeferredSequencedTaskRunnerTest* executor_;
37 int task_id_;
38 };
39
40 void ExecuteTask(int task_id) {
41 base::AutoLock lock(lock_);
42 executed_task_ids_.push_back(task_id);
43 }
44
45 void PostExecuteTask(int task_id) {
tzik92b7a422017-04-11 15:00:4446 runner_->PostTask(
47 FROM_HERE, base::BindOnce(&DeferredSequencedTaskRunnerTest::ExecuteTask,
48 base::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()
60 : loop_(),
61 runner_(new base::DeferredSequencedTaskRunner(loop_.task_runner())) {}
[email protected]afecfb72013-04-18 17:17:3362
[email protected]9e7154122013-05-30 23:11:0463 base::MessageLoop loop_;
[email protected]afecfb72013-04-18 17:17:3364 scoped_refptr<base::DeferredSequencedTaskRunner> runner_;
65 mutable base::Lock lock_;
66 std::vector<int> executed_task_ids_;
67};
68
69TEST_F(DeferredSequencedTaskRunnerTest, Stopped) {
70 PostExecuteTask(1);
fdoray10224582016-06-30 18:17:3971 base::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);
fdoray10224582016-06-30 18:17:3978 base::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
fdoray10224582016-06-30 18:17:3987 base::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);
fdoray10224582016-06-30 18:17:3993 base::RunLoop().RunUntilIdle();
[email protected]afecfb72013-04-18 17:17:3394 EXPECT_THAT(executed_task_ids_, testing::ElementsAre());
95
96 StartRunner();
fdoray10224582016-06-30 18:17:3997 base::RunLoop().RunUntilIdle();
[email protected]afecfb72013-04-18 17:17:3398 EXPECT_THAT(executed_task_ids_, testing::ElementsAre(1));
99
100 PostExecuteTask(2);
fdoray10224582016-06-30 18:17:39101 base::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);
fdoray10224582016-06-30 18:17:39108 base::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);
fdoray10224582016-06-30 18:17:39114 base::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 {
120 base::Thread thread1("DeferredSequencedTaskRunnerTestThread1");
121 base::Thread thread2("DeferredSequencedTaskRunnerTestThread2");
122 thread1.Start();
123 thread2.Start();
124 for (int i = 0; i < 5; ++i) {
skyostil054861d2015-04-30 19:06:15125 thread1.task_runner()->PostTask(
[email protected]afecfb72013-04-18 17:17:33126 FROM_HERE,
tzik92b7a422017-04-11 15:00:44127 base::BindOnce(&DeferredSequencedTaskRunnerTest::PostExecuteTask,
128 base::Unretained(this), 2 * i));
skyostil054861d2015-04-30 19:06:15129 thread2.task_runner()->PostTask(
[email protected]afecfb72013-04-18 17:17:33130 FROM_HERE,
tzik92b7a422017-04-11 15:00:44131 base::BindOnce(&DeferredSequencedTaskRunnerTest::PostExecuteTask,
132 base::Unretained(this), 2 * i + 1));
[email protected]afecfb72013-04-18 17:17:33133 if (i == 2) {
skyostil054861d2015-04-30 19:06:15134 thread1.task_runner()->PostTask(
tzik92b7a422017-04-11 15:00:44135 FROM_HERE,
136 base::BindOnce(&DeferredSequencedTaskRunnerTest::StartRunner,
137 base::Unretained(this)));
[email protected]afecfb72013-04-18 17:17:33138 }
139 }
140 }
141
fdoray10224582016-06-30 18:17:39142 base::RunLoop().RunUntilIdle();
[email protected]afecfb72013-04-18 17:17:33143 EXPECT_THAT(executed_task_ids_,
144 testing::WhenSorted(testing::ElementsAre(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)));
145}
146
147TEST_F(DeferredSequencedTaskRunnerTest, ObjectDestructionOrder) {
148 {
149 base::Thread thread("DeferredSequencedTaskRunnerTestThread");
150 thread.Start();
skyostil054861d2015-04-30 19:06:15151 runner_ = new base::DeferredSequencedTaskRunner(thread.task_runner());
[email protected]afecfb72013-04-18 17:17:33152 for (int i = 0; i < 5; ++i) {
153 {
154 // Use a block to ensure that no reference to |short_lived_object|
155 // is kept on the main thread after it is posted to |runner_|.
156 scoped_refptr<ExecuteTaskOnDestructor> short_lived_object =
157 new ExecuteTaskOnDestructor(this, 2 * i);
158 runner_->PostTask(
tzik92b7a422017-04-11 15:00:44159 FROM_HERE,
160 base::BindOnce(&DeferredSequencedTaskRunnerTest::DoNothing,
161 base::Unretained(this),
162 base::RetainedRef(short_lived_object)));
[email protected]afecfb72013-04-18 17:17:33163 }
164 // |short_lived_object| with id |2 * i| should be destroyed before the
165 // task |2 * i + 1| is executed.
166 PostExecuteTask(2 * i + 1);
167 }
168 StartRunner();
169 }
170
171 // All |short_lived_object| with id |2 * i| are destroyed before the task
172 // |2 * i + 1| is executed.
173 EXPECT_THAT(executed_task_ids_,
174 testing::ElementsAre(0, 1, 2, 3, 4, 5, 6, 7, 8, 9));
175}
176
177} // namespace