blob: a39b2d3485c23787e1408c92923e652c733492fa [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"
fdoray10224582016-06-30 18:17:3911#include "base/run_loop.h"
skyostil054861d2015-04-30 19:06:1512#include "base/single_thread_task_runner.h"
[email protected]afecfb72013-04-18 17:17:3313#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"
17
18namespace {
19
20class DeferredSequencedTaskRunnerTest : public testing::Test,
21 public base::NonThreadSafe {
22 public:
23 class ExecuteTaskOnDestructor :
24 public base::RefCounted<ExecuteTaskOnDestructor> {
25 public:
26 ExecuteTaskOnDestructor(
27 DeferredSequencedTaskRunnerTest* executor,
28 int task_id)
29 : executor_(executor),
30 task_id_(task_id) {
31 }
32 private:
33 friend class base::RefCounted<ExecuteTaskOnDestructor>;
34 virtual ~ExecuteTaskOnDestructor() {
35 executor_->ExecuteTask(task_id_);
36 }
37 DeferredSequencedTaskRunnerTest* executor_;
38 int task_id_;
39 };
40
41 void ExecuteTask(int task_id) {
42 base::AutoLock lock(lock_);
43 executed_task_ids_.push_back(task_id);
44 }
45
46 void PostExecuteTask(int task_id) {
tzik92b7a422017-04-11 15:00:4447 runner_->PostTask(
48 FROM_HERE, base::BindOnce(&DeferredSequencedTaskRunnerTest::ExecuteTask,
49 base::Unretained(this), task_id));
[email protected]afecfb72013-04-18 17:17:3350 }
51
52 void StartRunner() {
53 runner_->Start();
54 }
55
56 void DoNothing(ExecuteTaskOnDestructor* object) {
57 }
58
59 protected:
skyostil054861d2015-04-30 19:06:1560 DeferredSequencedTaskRunnerTest()
61 : loop_(),
62 runner_(new base::DeferredSequencedTaskRunner(loop_.task_runner())) {}
[email protected]afecfb72013-04-18 17:17:3363
[email protected]9e7154122013-05-30 23:11:0464 base::MessageLoop loop_;
[email protected]afecfb72013-04-18 17:17:3365 scoped_refptr<base::DeferredSequencedTaskRunner> runner_;
66 mutable base::Lock lock_;
67 std::vector<int> executed_task_ids_;
68};
69
70TEST_F(DeferredSequencedTaskRunnerTest, Stopped) {
71 PostExecuteTask(1);
fdoray10224582016-06-30 18:17:3972 base::RunLoop().RunUntilIdle();
[email protected]afecfb72013-04-18 17:17:3373 EXPECT_THAT(executed_task_ids_, testing::ElementsAre());
74}
75
76TEST_F(DeferredSequencedTaskRunnerTest, Start) {
77 StartRunner();
78 PostExecuteTask(1);
fdoray10224582016-06-30 18:17:3979 base::RunLoop().RunUntilIdle();
[email protected]afecfb72013-04-18 17:17:3380 EXPECT_THAT(executed_task_ids_, testing::ElementsAre(1));
81}
82
83TEST_F(DeferredSequencedTaskRunnerTest, StartWithMultipleElements) {
84 StartRunner();
85 for (int i = 1; i < 5; ++i)
86 PostExecuteTask(i);
87
fdoray10224582016-06-30 18:17:3988 base::RunLoop().RunUntilIdle();
[email protected]afecfb72013-04-18 17:17:3389 EXPECT_THAT(executed_task_ids_, testing::ElementsAre(1, 2, 3, 4));
90}
91
92TEST_F(DeferredSequencedTaskRunnerTest, DeferredStart) {
93 PostExecuteTask(1);
fdoray10224582016-06-30 18:17:3994 base::RunLoop().RunUntilIdle();
[email protected]afecfb72013-04-18 17:17:3395 EXPECT_THAT(executed_task_ids_, testing::ElementsAre());
96
97 StartRunner();
fdoray10224582016-06-30 18:17:3998 base::RunLoop().RunUntilIdle();
[email protected]afecfb72013-04-18 17:17:3399 EXPECT_THAT(executed_task_ids_, testing::ElementsAre(1));
100
101 PostExecuteTask(2);
fdoray10224582016-06-30 18:17:39102 base::RunLoop().RunUntilIdle();
[email protected]afecfb72013-04-18 17:17:33103 EXPECT_THAT(executed_task_ids_, testing::ElementsAre(1, 2));
104}
105
106TEST_F(DeferredSequencedTaskRunnerTest, DeferredStartWithMultipleElements) {
107 for (int i = 1; i < 5; ++i)
108 PostExecuteTask(i);
fdoray10224582016-06-30 18:17:39109 base::RunLoop().RunUntilIdle();
[email protected]afecfb72013-04-18 17:17:33110 EXPECT_THAT(executed_task_ids_, testing::ElementsAre());
111
112 StartRunner();
113 for (int i = 5; i < 9; ++i)
114 PostExecuteTask(i);
fdoray10224582016-06-30 18:17:39115 base::RunLoop().RunUntilIdle();
[email protected]afecfb72013-04-18 17:17:33116 EXPECT_THAT(executed_task_ids_, testing::ElementsAre(1, 2, 3, 4, 5, 6, 7, 8));
117}
118
119TEST_F(DeferredSequencedTaskRunnerTest, DeferredStartWithMultipleThreads) {
120 {
121 base::Thread thread1("DeferredSequencedTaskRunnerTestThread1");
122 base::Thread thread2("DeferredSequencedTaskRunnerTestThread2");
123 thread1.Start();
124 thread2.Start();
125 for (int i = 0; i < 5; ++i) {
skyostil054861d2015-04-30 19:06:15126 thread1.task_runner()->PostTask(
[email protected]afecfb72013-04-18 17:17:33127 FROM_HERE,
tzik92b7a422017-04-11 15:00:44128 base::BindOnce(&DeferredSequencedTaskRunnerTest::PostExecuteTask,
129 base::Unretained(this), 2 * i));
skyostil054861d2015-04-30 19:06:15130 thread2.task_runner()->PostTask(
[email protected]afecfb72013-04-18 17:17:33131 FROM_HERE,
tzik92b7a422017-04-11 15:00:44132 base::BindOnce(&DeferredSequencedTaskRunnerTest::PostExecuteTask,
133 base::Unretained(this), 2 * i + 1));
[email protected]afecfb72013-04-18 17:17:33134 if (i == 2) {
skyostil054861d2015-04-30 19:06:15135 thread1.task_runner()->PostTask(
tzik92b7a422017-04-11 15:00:44136 FROM_HERE,
137 base::BindOnce(&DeferredSequencedTaskRunnerTest::StartRunner,
138 base::Unretained(this)));
[email protected]afecfb72013-04-18 17:17:33139 }
140 }
141 }
142
fdoray10224582016-06-30 18:17:39143 base::RunLoop().RunUntilIdle();
[email protected]afecfb72013-04-18 17:17:33144 EXPECT_THAT(executed_task_ids_,
145 testing::WhenSorted(testing::ElementsAre(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)));
146}
147
148TEST_F(DeferredSequencedTaskRunnerTest, ObjectDestructionOrder) {
149 {
150 base::Thread thread("DeferredSequencedTaskRunnerTestThread");
151 thread.Start();
skyostil054861d2015-04-30 19:06:15152 runner_ = new base::DeferredSequencedTaskRunner(thread.task_runner());
[email protected]afecfb72013-04-18 17:17:33153 for (int i = 0; i < 5; ++i) {
154 {
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);
159 runner_->PostTask(
tzik92b7a422017-04-11 15:00:44160 FROM_HERE,
161 base::BindOnce(&DeferredSequencedTaskRunnerTest::DoNothing,
162 base::Unretained(this),
163 base::RetainedRef(short_lived_object)));
[email protected]afecfb72013-04-18 17:17:33164 }
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);
168 }
169 StartRunner();
170 }
171
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));
176}
177
178} // namespace