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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
base / test / sequenced_task_runner_test_template.cc [blame]
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/test/sequenced_task_runner_test_template.h"
#include <ostream>
#include "base/location.h"
namespace base {
namespace internal {
TaskEvent::TaskEvent(int i, Type type)
: i(i), type(type) {
}
SequencedTaskTracker::SequencedTaskTracker()
: next_post_i_(0),
task_end_count_(0),
task_end_cv_(&lock_) {
}
void SequencedTaskTracker::PostWrappedNonNestableTask(
SequencedTaskRunner* task_runner,
OnceClosure task) {
AutoLock event_lock(lock_);
const int post_i = next_post_i_++;
auto wrapped_task =
BindOnce(&SequencedTaskTracker::RunTask, this, std::move(task), post_i);
task_runner->PostNonNestableTask(FROM_HERE, std::move(wrapped_task));
TaskPosted(post_i);
}
void SequencedTaskTracker::PostWrappedNestableTask(
SequencedTaskRunner* task_runner,
OnceClosure task) {
AutoLock event_lock(lock_);
const int post_i = next_post_i_++;
auto wrapped_task =
BindOnce(&SequencedTaskTracker::RunTask, this, std::move(task), post_i);
task_runner->PostTask(FROM_HERE, std::move(wrapped_task));
TaskPosted(post_i);
}
void SequencedTaskTracker::PostWrappedDelayedNonNestableTask(
SequencedTaskRunner* task_runner,
OnceClosure task,
TimeDelta delay) {
AutoLock event_lock(lock_);
const int post_i = next_post_i_++;
auto wrapped_task =
BindOnce(&SequencedTaskTracker::RunTask, this, std::move(task), post_i);
task_runner->PostNonNestableDelayedTask(FROM_HERE, std::move(wrapped_task),
delay);
TaskPosted(post_i);
}
void SequencedTaskTracker::PostNonNestableTasks(
SequencedTaskRunner* task_runner,
int task_count) {
for (int i = 0; i < task_count; ++i) {
PostWrappedNonNestableTask(task_runner, OnceClosure());
}
}
void SequencedTaskTracker::RunTask(OnceClosure task, int task_i) {
TaskStarted(task_i);
if (!task.is_null())
std::move(task).Run();
TaskEnded(task_i);
}
void SequencedTaskTracker::TaskPosted(int i) {
// Caller must own |lock_|.
events_.push_back(TaskEvent(i, TaskEvent::POST));
}
void SequencedTaskTracker::TaskStarted(int i) {
AutoLock lock(lock_);
events_.push_back(TaskEvent(i, TaskEvent::START));
}
void SequencedTaskTracker::TaskEnded(int i) {
AutoLock lock(lock_);
events_.push_back(TaskEvent(i, TaskEvent::END));
++task_end_count_;
task_end_cv_.Signal();
}
const std::vector<TaskEvent>&
SequencedTaskTracker::GetTaskEvents() const {
return events_;
}
void SequencedTaskTracker::WaitForCompletedTasks(int count) {
AutoLock lock(lock_);
while (task_end_count_ < count)
task_end_cv_.Wait();
}
SequencedTaskTracker::~SequencedTaskTracker() = default;
void PrintTo(const TaskEvent& event, std::ostream* os) {
*os << "(i=" << event.i << ", type=";
switch (event.type) {
case TaskEvent::POST: *os << "POST"; break;
case TaskEvent::START: *os << "START"; break;
case TaskEvent::END: *os << "END"; break;
}
*os << ")";
}
namespace {
// Returns the task ordinals for the task event type |type| in the order that
// they were recorded.
std::vector<int> GetEventTypeOrder(const std::vector<TaskEvent>& events,
TaskEvent::Type type) {
std::vector<int> tasks;
std::vector<TaskEvent>::const_iterator event;
for (event = events.begin(); event != events.end(); ++event) {
if (event->type == type)
tasks.push_back(event->i);
}
return tasks;
}
// Returns all task events for task |task_i|.
std::vector<TaskEvent::Type> GetEventsForTask(
const std::vector<TaskEvent>& events,
int task_i) {
std::vector<TaskEvent::Type> task_event_orders;
std::vector<TaskEvent>::const_iterator event;
for (event = events.begin(); event != events.end(); ++event) {
if (event->i == task_i)
task_event_orders.push_back(event->type);
}
return task_event_orders;
}
// Checks that the task events for each task in |events| occur in the order
// {POST, START, END}, and that there is only one instance of each event type
// per task.
::testing::AssertionResult CheckEventOrdersForEachTask(
const std::vector<TaskEvent>& events,
int task_count) {
std::vector<TaskEvent::Type> expected_order;
expected_order.push_back(TaskEvent::POST);
expected_order.push_back(TaskEvent::START);
expected_order.push_back(TaskEvent::END);
// This is O(n^2), but it runs fast enough currently so is not worth
// optimizing.
for (int i = 0; i < task_count; ++i) {
const std::vector<TaskEvent::Type> task_events =
GetEventsForTask(events, i);
if (task_events != expected_order) {
return ::testing::AssertionFailure()
<< "Events for task " << i << " are out of order; expected: "
<< ::testing::PrintToString(expected_order) << "; actual: "
<< ::testing::PrintToString(task_events);
}
}
return ::testing::AssertionSuccess();
}
// Checks that no two tasks were running at the same time. I.e. the only
// events allowed between the START and END of a task are the POSTs of other
// tasks.
::testing::AssertionResult CheckNoTaskRunsOverlap(
const std::vector<TaskEvent>& events) {
// If > -1, we're currently inside a START, END pair.
int current_task_i = -1;
std::vector<TaskEvent>::const_iterator event;
for (event = events.begin(); event != events.end(); ++event) {
bool spurious_event_found = false;
if (current_task_i == -1) { // Not inside a START, END pair.
switch (event->type) {
case TaskEvent::POST:
break;
case TaskEvent::START:
current_task_i = event->i;
break;
case TaskEvent::END:
spurious_event_found = true;
break;
}
} else { // Inside a START, END pair.
bool interleaved_task_detected = false;
switch (event->type) {
case TaskEvent::POST:
if (event->i == current_task_i)
spurious_event_found = true;
break;
case TaskEvent::START:
interleaved_task_detected = true;
break;
case TaskEvent::END:
if (event->i != current_task_i)
interleaved_task_detected = true;
else
current_task_i = -1;
break;
}
if (interleaved_task_detected) {
return ::testing::AssertionFailure()
<< "Found event " << ::testing::PrintToString(*event)
<< " between START and END events for task " << current_task_i
<< "; event dump: " << ::testing::PrintToString(events);
}
}
if (spurious_event_found) {
const int event_i = event - events.begin();
return ::testing::AssertionFailure()
<< "Spurious event " << ::testing::PrintToString(*event)
<< " at position " << event_i << "; event dump: "
<< ::testing::PrintToString(events);
}
}
return ::testing::AssertionSuccess();
}
} // namespace
::testing::AssertionResult CheckNonNestableInvariants(
const std::vector<TaskEvent>& events,
int task_count) {
const std::vector<int> post_order =
GetEventTypeOrder(events, TaskEvent::POST);
const std::vector<int> start_order =
GetEventTypeOrder(events, TaskEvent::START);
const std::vector<int> end_order =
GetEventTypeOrder(events, TaskEvent::END);
if (start_order != post_order) {
return ::testing::AssertionFailure()
<< "Expected START order (which equals actual POST order): \n"
<< ::testing::PrintToString(post_order)
<< "\n Actual START order:\n"
<< ::testing::PrintToString(start_order);
}
if (end_order != post_order) {
return ::testing::AssertionFailure()
<< "Expected END order (which equals actual POST order): \n"
<< ::testing::PrintToString(post_order)
<< "\n Actual END order:\n"
<< ::testing::PrintToString(end_order);
}
const ::testing::AssertionResult result =
CheckEventOrdersForEachTask(events, task_count);
if (!result)
return result;
return CheckNoTaskRunsOverlap(events);
}
} // namespace internal
// This suite is instantiated in binaries that use //base:test_support.
GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(SequencedTaskRunnerTest);
} // namespace base