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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
|
// Copyright 2015 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 "content/child/scheduler/worker_scheduler_impl.h"
#include "base/callback.h"
#include "base/strings/stringprintf.h"
#include "cc/test/ordered_simple_task_runner.h"
#include "content/child/scheduler/nestable_task_runner_for_test.h"
#include "content/child/scheduler/scheduler_message_loop_delegate.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using testing::ElementsAreArray;
namespace content {
namespace {
void NopTask() {
}
int TimeTicksToIntMs(const base::TimeTicks& time) {
return static_cast<int>((time - base::TimeTicks()).InMilliseconds());
}
void WakeUpTask(std::vector<std::string>* timeline, cc::TestNowSource* clock) {
if (timeline) {
timeline->push_back(base::StringPrintf(
"run WakeUpTask @ %d", TimeTicksToIntMs(clock->Now())));
}
}
void RecordTimelineTask(std::vector<std::string>* timeline,
cc::TestNowSource* clock) {
timeline->push_back(base::StringPrintf(
"run RecordTimelineTask @ %d", TimeTicksToIntMs(clock->Now())));
}
void AppendToVectorTestTask(std::vector<std::string>* vector,
std::string value) {
vector->push_back(value);
}
void AppendToVectorIdleTestTask(std::vector<std::string>* vector,
std::string value,
base::TimeTicks deadline) {
AppendToVectorTestTask(vector, value);
}
void TimelineIdleTestTask(std::vector<std::string>* timeline,
base::TimeTicks deadline) {
timeline->push_back(base::StringPrintf("run TimelineIdleTestTask deadline %d",
TimeTicksToIntMs(deadline)));
}
}; // namespace
class WorkerSchedulerImplForTest : public WorkerSchedulerImpl {
public:
WorkerSchedulerImplForTest(
scoped_refptr<NestableSingleThreadTaskRunner> main_task_runner,
scoped_refptr<cc::TestNowSource> clock_)
: WorkerSchedulerImpl(main_task_runner),
clock_(clock_),
timeline_(nullptr) {}
void RecordTimelineEvents(std::vector<std::string>* timeline) {
timeline_ = timeline;
}
private:
bool CanEnterLongIdlePeriod(
base::TimeTicks now,
base::TimeDelta* next_long_idle_period_delay_out) override {
if (timeline_) {
timeline_->push_back(base::StringPrintf("CanEnterLongIdlePeriod @ %d",
TimeTicksToIntMs(now)));
}
return WorkerSchedulerImpl::CanEnterLongIdlePeriod(
now, next_long_idle_period_delay_out);
}
void IsNotQuiescent() override {
if (timeline_) {
timeline_->push_back(base::StringPrintf("IsNotQuiescent @ %d",
TimeTicksToIntMs(clock_->Now())));
}
WorkerSchedulerImpl::IsNotQuiescent();
}
scoped_refptr<cc::TestNowSource> clock_;
std::vector<std::string>* timeline_; // NOT OWNED
};
class WorkerSchedulerImplTest : public testing::Test {
public:
WorkerSchedulerImplTest()
: clock_(cc::TestNowSource::Create(5000)),
mock_task_runner_(new cc::OrderedSimpleTaskRunner(clock_, true)),
nestable_task_runner_(
NestableTaskRunnerForTest::Create(mock_task_runner_)),
scheduler_(
new WorkerSchedulerImplForTest(nestable_task_runner_, clock_)),
timeline_(nullptr) {
scheduler_->SetTimeSourceForTesting(clock_);
}
~WorkerSchedulerImplTest() override {}
void TearDown() override {
// Check that all tests stop posting tasks.
while (mock_task_runner_->RunUntilIdle()) {
}
}
void Init() {
scheduler_->Init();
default_task_runner_ = scheduler_->DefaultTaskRunner();
idle_task_runner_ = scheduler_->IdleTaskRunner();
timeline_ = nullptr;
}
void RecordTimelineEvents(std::vector<std::string>* timeline) {
timeline_ = timeline;
scheduler_->RecordTimelineEvents(timeline);
}
void RunUntilIdle() {
if (timeline_) {
timeline_->push_back(base::StringPrintf("RunUntilIdle begin @ %d",
TimeTicksToIntMs(clock_->Now())));
}
mock_task_runner_->RunUntilIdle();
if (timeline_) {
timeline_->push_back(base::StringPrintf("RunUntilIdle end @ %d",
TimeTicksToIntMs(clock_->Now())));
}
}
void InitAndPostDelayedWakeupTask() {
Init();
// WorkerSchedulerImpl::Init causes a delayed task to be posted on the
// after wakeup control runner. We need a task to wake the system up
// AFTER the delay for this has expired.
default_task_runner_->PostDelayedTask(
FROM_HERE, base::Bind(&WakeUpTask, base::Unretained(timeline_),
base::Unretained(clock_.get())),
base::TimeDelta::FromMilliseconds(100));
}
// Helper for posting several tasks of specific types. |task_descriptor| is a
// string with space delimited task identifiers. The first letter of each
// task identifier specifies the task type:
// - 'D': Default task
// - 'I': Idle task
void PostTestTasks(std::vector<std::string>* run_order,
const std::string& task_descriptor) {
std::istringstream stream(task_descriptor);
while (!stream.eof()) {
std::string task;
stream >> task;
switch (task[0]) {
case 'D':
default_task_runner_->PostTask(
FROM_HERE, base::Bind(&AppendToVectorTestTask, run_order, task));
break;
case 'I':
idle_task_runner_->PostIdleTask(
FROM_HERE,
base::Bind(&AppendToVectorIdleTestTask, run_order, task));
break;
default:
NOTREACHED();
}
}
}
static base::TimeDelta maximum_idle_period_duration() {
return base::TimeDelta::FromMilliseconds(
SchedulerHelper::kMaximumIdlePeriodMillis);
}
protected:
scoped_refptr<cc::TestNowSource> clock_;
// Only one of mock_task_runner_ or message_loop_ will be set.
scoped_refptr<cc::OrderedSimpleTaskRunner> mock_task_runner_;
scoped_refptr<NestableSingleThreadTaskRunner> nestable_task_runner_;
scoped_ptr<WorkerSchedulerImplForTest> scheduler_;
scoped_refptr<base::SingleThreadTaskRunner> default_task_runner_;
scoped_refptr<SingleThreadIdleTaskRunner> idle_task_runner_;
std::vector<std::string>* timeline_; // NOT OWNED
DISALLOW_COPY_AND_ASSIGN(WorkerSchedulerImplTest);
};
TEST_F(WorkerSchedulerImplTest, TestPostDefaultTask) {
InitAndPostDelayedWakeupTask();
std::vector<std::string> run_order;
PostTestTasks(&run_order, "D1 D2 D3 D4");
RunUntilIdle();
EXPECT_THAT(run_order,
testing::ElementsAre(std::string("D1"), std::string("D2"),
std::string("D3"), std::string("D4")));
}
TEST_F(WorkerSchedulerImplTest, TestPostIdleTask) {
InitAndPostDelayedWakeupTask();
std::vector<std::string> run_order;
PostTestTasks(&run_order, "I1");
RunUntilIdle();
EXPECT_THAT(run_order, testing::ElementsAre(std::string("I1")));
}
TEST_F(WorkerSchedulerImplTest, TestPostIdleTask_NoWakeup) {
Init();
std::vector<std::string> run_order;
PostTestTasks(&run_order, "I1");
RunUntilIdle();
EXPECT_TRUE(run_order.empty());
}
TEST_F(WorkerSchedulerImplTest, TestPostDefaultAndIdleTasks) {
InitAndPostDelayedWakeupTask();
std::vector<std::string> run_order;
PostTestTasks(&run_order, "I1 D2 D3 D4");
RunUntilIdle();
EXPECT_THAT(run_order,
testing::ElementsAre(std::string("D2"), std::string("D3"),
std::string("D4"), std::string("I1")));
}
TEST_F(WorkerSchedulerImplTest, TestPostIdleTaskWithWakeupNeeded_NoWakeup) {
InitAndPostDelayedWakeupTask();
RunUntilIdle();
// The delayed call to EnableLongIdlePeriod happened and it posted a call to
// EnableLongIdlePeriod on the after wakeup control queue.
std::vector<std::string> run_order;
PostTestTasks(&run_order, "I1");
RunUntilIdle();
EXPECT_TRUE(run_order.empty());
}
TEST_F(WorkerSchedulerImplTest, TestPostDefaultDelayedAndIdleTasks) {
InitAndPostDelayedWakeupTask();
std::vector<std::string> run_order;
PostTestTasks(&run_order, "I1 D2 D3 D4");
default_task_runner_->PostDelayedTask(
FROM_HERE, base::Bind(&AppendToVectorTestTask, &run_order, "DELAYED"),
base::TimeDelta::FromMilliseconds(1000));
RunUntilIdle();
EXPECT_THAT(run_order,
testing::ElementsAre(std::string("D2"), std::string("D3"),
std::string("D4"), std::string("I1"),
std::string("DELAYED")));
}
TEST_F(WorkerSchedulerImplTest, TestIdleDeadlineWithPendingDelayedTask) {
std::vector<std::string> timeline;
RecordTimelineEvents(&timeline);
InitAndPostDelayedWakeupTask();
timeline.push_back("Post delayed and idle tasks");
// Post a delayed task timed to occur mid way during the long idle period.
default_task_runner_->PostDelayedTask(
FROM_HERE, base::Bind(&RecordTimelineTask, base::Unretained(&timeline),
base::Unretained(clock_.get())),
base::TimeDelta::FromMilliseconds(420));
idle_task_runner_->PostIdleTask(FROM_HERE,
base::Bind(&TimelineIdleTestTask, &timeline));
RunUntilIdle();
std::string expected_timeline[] = {
"CanEnterLongIdlePeriod @ 5",
"Post delayed and idle tasks",
"IsNotQuiescent @ 105",
"CanEnterLongIdlePeriod @ 405",
"run TimelineIdleTestTask deadline 425", // Note the short 20ms deadline.
"CanEnterLongIdlePeriod @ 425",
"run RecordTimelineTask @ 425"};
EXPECT_THAT(timeline, ElementsAreArray(expected_timeline));
}
TEST_F(WorkerSchedulerImplTest,
TestIdleDeadlineWithPendingDelayedTaskFarInTheFuture) {
std::vector<std::string> timeline;
RecordTimelineEvents(&timeline);
InitAndPostDelayedWakeupTask();
timeline.push_back("Post delayed and idle tasks");
// Post a delayed task timed to occur well after the long idle period.
default_task_runner_->PostDelayedTask(
FROM_HERE, base::Bind(&RecordTimelineTask, base::Unretained(&timeline),
base::Unretained(clock_.get())),
base::TimeDelta::FromMilliseconds(1000));
idle_task_runner_->PostIdleTask(FROM_HERE,
base::Bind(&TimelineIdleTestTask, &timeline));
RunUntilIdle();
std::string expected_timeline[] = {
"CanEnterLongIdlePeriod @ 5",
"Post delayed and idle tasks",
"IsNotQuiescent @ 105",
"CanEnterLongIdlePeriod @ 405",
"run TimelineIdleTestTask deadline 455", // Note the full 50ms deadline.
"CanEnterLongIdlePeriod @ 455",
"run RecordTimelineTask @ 1005"};
EXPECT_THAT(timeline, ElementsAreArray(expected_timeline));
}
TEST_F(WorkerSchedulerImplTest,
TestPostIdleTaskAfterRunningUntilIdle_NoWakeUp) {
InitAndPostDelayedWakeupTask();
default_task_runner_->PostDelayedTask(
FROM_HERE, base::Bind(&NopTask), base::TimeDelta::FromMilliseconds(1000));
RunUntilIdle();
// The delayed call to EnableLongIdlePeriod happened and it posted a call to
// EnableLongIdlePeriod on the after wakeup control queue. Without an other
// non-idle task posted, the idle tasks won't run.
std::vector<std::string> run_order;
PostTestTasks(&run_order, "I1 I2");
RunUntilIdle();
EXPECT_TRUE(run_order.empty());
}
TEST_F(WorkerSchedulerImplTest,
TestPostIdleTaskAfterRunningUntilIdle_WithWakeUp) {
InitAndPostDelayedWakeupTask();
default_task_runner_->PostDelayedTask(
FROM_HERE, base::Bind(&NopTask), base::TimeDelta::FromMilliseconds(1000));
RunUntilIdle();
// The delayed call to EnableLongIdlePeriod happened and it posted a call to
// EnableLongIdlePeriod on the after wakeup control queue. Without an other
// non-idle task posted, the idle tasks won't run.
std::vector<std::string> run_order;
PostTestTasks(&run_order, "I1 I2 D3");
RunUntilIdle();
EXPECT_THAT(run_order,
testing::ElementsAre(std::string("D3"), std::string("I1"),
std::string("I2")));
}
TEST_F(WorkerSchedulerImplTest, TestLongIdlePeriodTimeline) {
Init();
std::vector<std::string> timeline;
RecordTimelineEvents(&timeline);
// The scheduler should not run the initiate_next_long_idle_period task if
// there are no idle tasks and no other task woke up the scheduler, thus
// the idle period deadline shouldn't update at the end of the current long
// idle period.
base::TimeTicks idle_period_deadline =
scheduler_->CurrentIdleTaskDeadlineForTesting();
clock_->AdvanceNow(maximum_idle_period_duration());
RunUntilIdle();
base::TimeTicks new_idle_period_deadline =
scheduler_->CurrentIdleTaskDeadlineForTesting();
EXPECT_EQ(idle_period_deadline, new_idle_period_deadline);
// Posting a after-wakeup idle task also shouldn't wake the scheduler or
// initiate the next long idle period.
timeline.push_back("PostIdleTaskAfterWakeup");
idle_task_runner_->PostIdleTaskAfterWakeup(
FROM_HERE, base::Bind(&TimelineIdleTestTask, &timeline));
RunUntilIdle();
new_idle_period_deadline = scheduler_->CurrentIdleTaskDeadlineForTesting();
// Running a normal task should initiate a new long idle period after waiting
// 300ms for quiescence.
timeline.push_back("Post RecordTimelineTask");
default_task_runner_->PostTask(
FROM_HERE, base::Bind(&RecordTimelineTask, base::Unretained(&timeline),
base::Unretained(clock_.get())));
RunUntilIdle();
std::string expected_timeline[] = {
"RunUntilIdle begin @ 55",
"RunUntilIdle end @ 55",
"PostIdleTaskAfterWakeup",
"RunUntilIdle begin @ 55", // NOTE idle task doesn't run till later.
"RunUntilIdle end @ 55",
"Post RecordTimelineTask",
"RunUntilIdle begin @ 55",
"run RecordTimelineTask @ 55",
"IsNotQuiescent @ 55", // NOTE we have to wait for quiescence.
"CanEnterLongIdlePeriod @ 355",
"run TimelineIdleTestTask deadline 405",
"CanEnterLongIdlePeriod @ 405",
"RunUntilIdle end @ 455"};
EXPECT_THAT(timeline, ElementsAreArray(expected_timeline));
}
} // namespace content
|