1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/deferred_sequenced_task_runner.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/location.h"
#include "base/memory/ref_counted.h"
#include "base/single_thread_task_runner.h"
#include "base/threading/non_thread_safe.h"
#include "base/threading/thread.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
class DeferredSequencedTaskRunnerTest : public testing::Test,
public base::NonThreadSafe {
public:
class ExecuteTaskOnDestructor :
public base::RefCounted<ExecuteTaskOnDestructor> {
public:
ExecuteTaskOnDestructor(
DeferredSequencedTaskRunnerTest* executor,
int task_id)
: executor_(executor),
task_id_(task_id) {
}
private:
friend class base::RefCounted<ExecuteTaskOnDestructor>;
virtual ~ExecuteTaskOnDestructor() {
executor_->ExecuteTask(task_id_);
}
DeferredSequencedTaskRunnerTest* executor_;
int task_id_;
};
void ExecuteTask(int task_id) {
base::AutoLock lock(lock_);
executed_task_ids_.push_back(task_id);
}
void PostExecuteTask(int task_id) {
runner_->PostTask(FROM_HERE,
base::Bind(&DeferredSequencedTaskRunnerTest::ExecuteTask,
base::Unretained(this),
task_id));
}
void StartRunner() {
runner_->Start();
}
void DoNothing(ExecuteTaskOnDestructor* object) {
}
protected:
DeferredSequencedTaskRunnerTest()
: loop_(),
runner_(new base::DeferredSequencedTaskRunner(loop_.task_runner())) {}
base::MessageLoop loop_;
scoped_refptr<base::DeferredSequencedTaskRunner> runner_;
mutable base::Lock lock_;
std::vector<int> executed_task_ids_;
};
TEST_F(DeferredSequencedTaskRunnerTest, Stopped) {
PostExecuteTask(1);
loop_.RunUntilIdle();
EXPECT_THAT(executed_task_ids_, testing::ElementsAre());
}
TEST_F(DeferredSequencedTaskRunnerTest, Start) {
StartRunner();
PostExecuteTask(1);
loop_.RunUntilIdle();
EXPECT_THAT(executed_task_ids_, testing::ElementsAre(1));
}
TEST_F(DeferredSequencedTaskRunnerTest, StartWithMultipleElements) {
StartRunner();
for (int i = 1; i < 5; ++i)
PostExecuteTask(i);
loop_.RunUntilIdle();
EXPECT_THAT(executed_task_ids_, testing::ElementsAre(1, 2, 3, 4));
}
TEST_F(DeferredSequencedTaskRunnerTest, DeferredStart) {
PostExecuteTask(1);
loop_.RunUntilIdle();
EXPECT_THAT(executed_task_ids_, testing::ElementsAre());
StartRunner();
loop_.RunUntilIdle();
EXPECT_THAT(executed_task_ids_, testing::ElementsAre(1));
PostExecuteTask(2);
loop_.RunUntilIdle();
EXPECT_THAT(executed_task_ids_, testing::ElementsAre(1, 2));
}
TEST_F(DeferredSequencedTaskRunnerTest, DeferredStartWithMultipleElements) {
for (int i = 1; i < 5; ++i)
PostExecuteTask(i);
loop_.RunUntilIdle();
EXPECT_THAT(executed_task_ids_, testing::ElementsAre());
StartRunner();
for (int i = 5; i < 9; ++i)
PostExecuteTask(i);
loop_.RunUntilIdle();
EXPECT_THAT(executed_task_ids_, testing::ElementsAre(1, 2, 3, 4, 5, 6, 7, 8));
}
TEST_F(DeferredSequencedTaskRunnerTest, DeferredStartWithMultipleThreads) {
{
base::Thread thread1("DeferredSequencedTaskRunnerTestThread1");
base::Thread thread2("DeferredSequencedTaskRunnerTestThread2");
thread1.Start();
thread2.Start();
for (int i = 0; i < 5; ++i) {
thread1.task_runner()->PostTask(
FROM_HERE,
base::Bind(&DeferredSequencedTaskRunnerTest::PostExecuteTask,
base::Unretained(this), 2 * i));
thread2.task_runner()->PostTask(
FROM_HERE,
base::Bind(&DeferredSequencedTaskRunnerTest::PostExecuteTask,
base::Unretained(this), 2 * i + 1));
if (i == 2) {
thread1.task_runner()->PostTask(
FROM_HERE, base::Bind(&DeferredSequencedTaskRunnerTest::StartRunner,
base::Unretained(this)));
}
}
}
loop_.RunUntilIdle();
EXPECT_THAT(executed_task_ids_,
testing::WhenSorted(testing::ElementsAre(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)));
}
TEST_F(DeferredSequencedTaskRunnerTest, ObjectDestructionOrder) {
{
base::Thread thread("DeferredSequencedTaskRunnerTestThread");
thread.Start();
runner_ = new base::DeferredSequencedTaskRunner(thread.task_runner());
for (int i = 0; i < 5; ++i) {
{
// Use a block to ensure that no reference to |short_lived_object|
// is kept on the main thread after it is posted to |runner_|.
scoped_refptr<ExecuteTaskOnDestructor> short_lived_object =
new ExecuteTaskOnDestructor(this, 2 * i);
runner_->PostTask(
FROM_HERE, base::Bind(&DeferredSequencedTaskRunnerTest::DoNothing,
base::Unretained(this),
base::RetainedRef(short_lived_object)));
}
// |short_lived_object| with id |2 * i| should be destroyed before the
// task |2 * i + 1| is executed.
PostExecuteTask(2 * i + 1);
}
StartRunner();
}
// All |short_lived_object| with id |2 * i| are destroyed before the task
// |2 * i + 1| is executed.
EXPECT_THAT(executed_task_ids_,
testing::ElementsAre(0, 1, 2, 3, 4, 5, 6, 7, 8, 9));
}
} // namespace
|