File: scheduler_impl_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (476 lines) | stat: -rw-r--r-- 18,687 bytes parent folder | download | duplicates (6)
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/download/internal/background_service/scheduler/scheduler_impl.h"

#include <stdint.h>
#include <memory>

#include "base/strings/string_number_conversions.h"
#include "components/download/internal/background_service/config.h"
#include "components/download/internal/background_service/entry.h"
#include "components/download/internal/background_service/scheduler/device_status.h"
#include "components/download/public/task/task_scheduler.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

using testing::_;
using testing::InSequence;

namespace download {
namespace {

class MockTaskScheduler : public TaskScheduler {
 public:
  MockTaskScheduler() = default;
  ~MockTaskScheduler() override = default;

  MOCK_METHOD6(ScheduleTask,
               void(DownloadTaskType, bool, bool, int, int64_t, int64_t));
  MOCK_METHOD1(CancelTask, void(DownloadTaskType));
};

class DownloadSchedulerImplTest : public testing::Test {
 public:
  DownloadSchedulerImplTest() = default;

  DownloadSchedulerImplTest(const DownloadSchedulerImplTest&) = delete;
  DownloadSchedulerImplTest& operator=(const DownloadSchedulerImplTest&) =
      delete;

  ~DownloadSchedulerImplTest() override = default;

  void TearDown() override { DestroyScheduler(); }

  void BuildScheduler(const std::vector<DownloadClient> clients) {
    scheduler_ =
        std::make_unique<SchedulerImpl>(&task_scheduler_, &config_, clients);
  }
  void DestroyScheduler() { scheduler_.reset(); }

  // Helper function to create a list of entries for the scheduler to query the
  // next entry.
  void BuildDataEntries(size_t size) {
    entries_ = std::vector<Entry>(size, Entry());
    for (size_t i = 0; i < size; ++i) {
      entries_[i].guid = base::NumberToString(i);
      entries_[i].scheduling_params.battery_requirements =
          SchedulingParams::BatteryRequirements::BATTERY_SENSITIVE;
      entries_[i].scheduling_params.network_requirements =
          SchedulingParams::NetworkRequirements::UNMETERED;
      entries_[i].state = Entry::State::AVAILABLE;
    }
  }

  // Returns list of entry pointers to feed to the scheduler.
  Model::EntryList entries() {
    Model::EntryList entry_list;
    for (auto& entry : entries_) {
      entry_list.emplace_back(&entry);
    }
    return entry_list;
  }

  // Simulates the entry has been processed by the download service and the
  // state has changed.
  void MakeEntryActive(Entry* entry) {
    if (entry)
      entry->state = Entry::State::ACTIVE;
  }

  // Reverts the states of entry so that the scheduler can poll it again.
  void MakeEntryAvailable(Entry* entry) {
    entry->state = Entry::State::AVAILABLE;
  }

  // Helper function to build a device status.
  DeviceStatus BuildDeviceStatus(BatteryStatus battery, NetworkStatus network) {
    DeviceStatus device_status;
    device_status.battery_status = battery;
    device_status.network_status = network;
    return device_status;
  }

 protected:
  std::unique_ptr<SchedulerImpl> scheduler_;
  MockTaskScheduler task_scheduler_;
  Configuration config_;

  // Entries owned by the test fixture.
  std::vector<Entry> entries_;
};

// Ensures normal polling logic is correct.
TEST_F(DownloadSchedulerImplTest, BasicPolling) {
  BuildScheduler(std::vector<DownloadClient>{DownloadClient::TEST_2,
                                             DownloadClient::TEST});

  // Client TEST: entry 0.
  // Client TEST_2: entry 1.
  // Poll sequence: 1 -> 0.
  BuildDataEntries(2);
  entries_[0].client = DownloadClient::TEST;
  entries_[1].client = DownloadClient::TEST_2;

  // First download belongs to first client.
  Entry* next = scheduler_->Next(
      entries(),
      BuildDeviceStatus(BatteryStatus::CHARGING, NetworkStatus::UNMETERED));
  EXPECT_EQ(next, &entries_[1]);
  MakeEntryActive(next);

  // If the first one is processed, the next should be the other entry.
  next = scheduler_->Next(
      entries(),
      BuildDeviceStatus(BatteryStatus::CHARGING, NetworkStatus::UNMETERED));
  EXPECT_EQ(next, &entries_[0]);
  MakeEntryActive(next);
}

// Tests the load balancing and polling downloads based on cancel time.
TEST_F(DownloadSchedulerImplTest, BasicLoadBalancing) {
  BuildScheduler(std::vector<DownloadClient>{
      DownloadClient::TEST, DownloadClient::TEST_2, DownloadClient::TEST_3});

  // Client TEST: entry 0, entry 1 (earlier cancel time).
  // Client TEST_2: entry 2.
  // Client TEST_3: No entries.
  // Poll sequence: 1 -> 2 -> 0.
  BuildDataEntries(3);
  entries_[0].client = DownloadClient::TEST;
  entries_[0].scheduling_params.cancel_time = base::Time::FromInternalValue(20);
  entries_[1].client = DownloadClient::TEST;
  entries_[1].scheduling_params.cancel_time = base::Time::FromInternalValue(10);
  entries_[2].client = DownloadClient::TEST_2;
  entries_[2].scheduling_params.cancel_time = base::Time::FromInternalValue(30);

  // There are 2 downloads for client 0, the one with earlier create time will
  // be the next download.
  Entry* next = scheduler_->Next(
      entries(),
      BuildDeviceStatus(BatteryStatus::CHARGING, NetworkStatus::UNMETERED));
  EXPECT_EQ(&entries_[1], next);
  MakeEntryActive(next);

  // The second download should belongs to client 1, because of the round robin
  // load balancing.
  next = scheduler_->Next(
      entries(),
      BuildDeviceStatus(BatteryStatus::CHARGING, NetworkStatus::UNMETERED));
  EXPECT_EQ(&entries_[2], next);
  MakeEntryActive(next);

  // Only one entry left, which will be the next.
  next = scheduler_->Next(
      entries(),
      BuildDeviceStatus(BatteryStatus::CHARGING, NetworkStatus::UNMETERED));
  EXPECT_EQ(&entries_[0], next);
  MakeEntryActive(next);

  // Keep polling twice, since no available downloads, both will return nullptr.
  next = scheduler_->Next(
      entries(),
      BuildDeviceStatus(BatteryStatus::CHARGING, NetworkStatus::UNMETERED));
  EXPECT_EQ(nullptr, next);
  MakeEntryActive(next);

  next = scheduler_->Next(
      entries(),
      BuildDeviceStatus(BatteryStatus::CHARGING, NetworkStatus::UNMETERED));
  EXPECT_EQ(nullptr, next);
  MakeEntryActive(next);
}

// Ensures downloads are polled based on scheduling parameters and device
// status.
TEST_F(DownloadSchedulerImplTest, SchedulingParams) {
  BuildScheduler(std::vector<DownloadClient>{DownloadClient::TEST});
  BuildDataEntries(1);
  entries_[0].client = DownloadClient::TEST;
  entries_[0].scheduling_params.battery_requirements =
      SchedulingParams::BatteryRequirements::BATTERY_SENSITIVE;
  entries_[0].scheduling_params.network_requirements =
      SchedulingParams::NetworkRequirements::UNMETERED;

  Entry* next = nullptr;

  // Tests network scheduling parameter.
  // No downloads can be polled when network disconnected.
  next = scheduler_->Next(
      entries(),
      BuildDeviceStatus(BatteryStatus::CHARGING, NetworkStatus::DISCONNECTED));
  EXPECT_EQ(nullptr, next);

  // If the network is metered, and scheduling parameter requires unmetered
  // network, the download should not be polled.
  next = scheduler_->Next(entries(), BuildDeviceStatus(BatteryStatus::CHARGING,
                                                       NetworkStatus::METERED));
  EXPECT_EQ(nullptr, next);

  // If the network requirement is none, the download can happen under metered
  // network. However, download won't happen when network is disconnected.
  entries_[0].scheduling_params.network_requirements =
      SchedulingParams::NetworkRequirements::NONE;
  next = scheduler_->Next(entries(), BuildDeviceStatus(BatteryStatus::CHARGING,
                                                       NetworkStatus::METERED));
  EXPECT_EQ(&entries_[0], next);
  next = scheduler_->Next(
      entries(),
      BuildDeviceStatus(BatteryStatus::CHARGING, NetworkStatus::DISCONNECTED));
  EXPECT_EQ(nullptr, next);
  MakeEntryActive(next);

  // Tests battery sensitive scheduling parameter.
  entries_[0].scheduling_params.battery_requirements =
      SchedulingParams::BatteryRequirements::BATTERY_SENSITIVE;

  next = scheduler_->Next(
      entries(),
      BuildDeviceStatus(BatteryStatus::CHARGING, NetworkStatus::UNMETERED));
  EXPECT_EQ(&entries_[0], next);

  // Battery sensitive with low battery level.
  DeviceStatus status =
      BuildDeviceStatus(BatteryStatus::NOT_CHARGING, NetworkStatus::UNMETERED);
  DCHECK_EQ(status.battery_percentage, 0);
  next = scheduler_->Next(entries(), status);
  EXPECT_EQ(nullptr, next);

  status.battery_percentage = config_.download_battery_percentage - 1;
  next = scheduler_->Next(entries(), status);
  EXPECT_EQ(nullptr, next);

  // Battery sensitive with high battery level.
  status.battery_percentage = config_.download_battery_percentage;
  next = scheduler_->Next(entries(), status);
  EXPECT_EQ(&entries_[0], next);

  // Tests battery insensitive scheduling parameter.
  entries_[0].scheduling_params.battery_requirements =
      SchedulingParams::BatteryRequirements::BATTERY_INSENSITIVE;
  next = scheduler_->Next(
      entries(),
      BuildDeviceStatus(BatteryStatus::NOT_CHARGING, NetworkStatus::UNMETERED));
  EXPECT_EQ(&entries_[0], next);
  next = scheduler_->Next(
      entries(),
      BuildDeviceStatus(BatteryStatus::CHARGING, NetworkStatus::UNMETERED));
  EXPECT_EQ(&entries_[0], next);
  MakeEntryActive(next);
}

// Ensures higher priority will be scheduled first.
TEST_F(DownloadSchedulerImplTest, Priority) {
  BuildScheduler(std::vector<DownloadClient>{DownloadClient::TEST});

  // The second entry has higher priority but is created later than the first
  // entry. This ensures priority is checked before the create time.
  BuildDataEntries(2);
  entries_[0].client = DownloadClient::TEST;
  entries_[0].scheduling_params.priority = SchedulingParams::Priority::LOW;
  entries_[0].scheduling_params.cancel_time = base::Time::FromInternalValue(20);
  entries_[1].client = DownloadClient::TEST;
  entries_[1].scheduling_params.priority = SchedulingParams::Priority::HIGH;
  entries_[1].scheduling_params.cancel_time = base::Time::FromInternalValue(40);

  // Download with higher priority should be polled first, even if there is
  // another download created earlier.
  Entry* next = scheduler_->Next(
      entries(),
      BuildDeviceStatus(BatteryStatus::CHARGING, NetworkStatus::UNMETERED));
  EXPECT_EQ(&entries_[1], next);

  // Download with non UI priority should be subject to network and battery
  // scheduling parameters. The higher priority one will be ignored because of
  // mismatching battery condition.
  entries_[1].scheduling_params.battery_requirements =
      SchedulingParams::BatteryRequirements::BATTERY_SENSITIVE;
  entries_[0].scheduling_params.battery_requirements =
      SchedulingParams::BatteryRequirements::BATTERY_INSENSITIVE;

  next = scheduler_->Next(
      entries(),
      BuildDeviceStatus(BatteryStatus::NOT_CHARGING, NetworkStatus::UNMETERED));
  EXPECT_EQ(&entries_[0], next);
  MakeEntryActive(next);
}

// Ensures UI priority entries are subject to device status check.
TEST_F(DownloadSchedulerImplTest, UIPrioritySubjectToDeviceStatus) {
  BuildScheduler(std::vector<DownloadClient>{DownloadClient::TEST,
                                             DownloadClient::TEST_2});

  // Client TEST: entry 0.
  // Client TEST_2: entry 1 (UI priority, cancel later).
  BuildDataEntries(2);
  entries_[0].client = DownloadClient::TEST;
  entries_[0].scheduling_params.priority = SchedulingParams::Priority::LOW;
  entries_[1].client = DownloadClient::TEST_2;
  entries_[1].scheduling_params.priority = SchedulingParams::Priority::UI;

  // UI priority is also subject to device status validation.
  Entry* next = scheduler_->Next(
      entries(),
      BuildDeviceStatus(BatteryStatus::NOT_CHARGING, NetworkStatus::METERED));
  EXPECT_EQ(nullptr, next);
  MakeEntryActive(next);
}

// UI priority entries will be processed first even if they doesn't belong to
// the current client in load balancing.
TEST_F(DownloadSchedulerImplTest, UIPriorityLoadBalancing) {
  BuildScheduler(std::vector<DownloadClient>{DownloadClient::TEST,
                                             DownloadClient::TEST_2});

  // Client TEST: entry 0(Low priority).
  // Client TEST_2: entry 1(UI priority).
  BuildDataEntries(2);
  entries_[0].client = DownloadClient::TEST;
  entries_[0].scheduling_params.priority = SchedulingParams::Priority::LOW;
  entries_[1].client = DownloadClient::TEST_2;
  entries_[1].scheduling_params.priority = SchedulingParams::Priority::UI;

  Entry* next = scheduler_->Next(
      entries(),
      BuildDeviceStatus(BatteryStatus::CHARGING, NetworkStatus::UNMETERED));
  EXPECT_EQ(&entries_[1], next);
  MakeEntryActive(next);
}

TEST_F(DownloadSchedulerImplTest, PickOlderDownloadIfSameParameters) {
  BuildScheduler(std::vector<DownloadClient>{DownloadClient::TEST,
                                             DownloadClient::TEST_2});

  // Client TEST: entry 0(Low priority, No Cancel Time, Newer).
  // Client TEST: entry 1(Low priority, No Cancel Time, Older).
  // Client TEST: entry 2(Low priority, No Cancel Time, Newer).
  BuildDataEntries(3);
  entries_[0].client = DownloadClient::TEST;
  entries_[0].scheduling_params.priority = SchedulingParams::Priority::LOW;
  entries_[0].create_time = base::Time::Now();
  entries_[1].client = DownloadClient::TEST;
  entries_[1].scheduling_params.priority = SchedulingParams::Priority::LOW;
  entries_[1].create_time = base::Time::Now() - base::Days(1);
  entries_[2].client = DownloadClient::TEST;
  entries_[2].scheduling_params.priority = SchedulingParams::Priority::LOW;
  entries_[2].create_time = base::Time::Now();

  Entry* next = scheduler_->Next(
      entries(),
      BuildDeviceStatus(BatteryStatus::CHARGING, NetworkStatus::UNMETERED));
  EXPECT_EQ(&entries_[1], next);
  MakeEntryActive(next);
}

// When multiple UI priority entries exist, the next entry is selected based on
// cancel time and load balancing.
TEST_F(DownloadSchedulerImplTest, MultipleUIPriorityEntries) {
  BuildScheduler(std::vector<DownloadClient>{DownloadClient::TEST,
                                             DownloadClient::TEST_2});
  BuildDataEntries(4);

  // Client TEST: entry 0(UI priority), entry 1(UI priority, early cancel time).
  // Client TEST_2: entry 2(UI priority), entry 3(high priority, early cancel
  // time). Poll sequence: 1 -> 2 -> 0 -> 3.
  for (auto& entry : entries_) {
    entry.scheduling_params.priority = SchedulingParams::Priority::UI;
  }
  entries_[0].client = DownloadClient::TEST;
  entries_[0].scheduling_params.cancel_time = base::Time::FromInternalValue(40);
  entries_[1].client = DownloadClient::TEST;
  entries_[1].scheduling_params.cancel_time = base::Time::FromInternalValue(20);
  entries_[2].client = DownloadClient::TEST_2;
  entries_[2].scheduling_params.cancel_time = base::Time::FromInternalValue(50);
  entries_[3].client = DownloadClient::TEST_2;
  entries_[3].scheduling_params.cancel_time = base::Time::FromInternalValue(20);
  entries_[3].scheduling_params.priority = SchedulingParams::Priority::HIGH;

  // When device conditions are meet, UI priority entry with the earliest cancel
  // time will be processed first.
  Entry* next = scheduler_->Next(
      entries(),
      BuildDeviceStatus(BatteryStatus::CHARGING, NetworkStatus::UNMETERED));
  EXPECT_EQ(&entries_[1], next);
  MakeEntryActive(next);

  // Next entry will be UI priority entry from another client.
  next = scheduler_->Next(
      entries(),
      BuildDeviceStatus(BatteryStatus::CHARGING, NetworkStatus::UNMETERED));
  EXPECT_EQ(&entries_[2], next);
  MakeEntryActive(next);

  next = scheduler_->Next(
      entries(),
      BuildDeviceStatus(BatteryStatus::CHARGING, NetworkStatus::UNMETERED));
  EXPECT_EQ(&entries_[0], next);
  MakeEntryActive(next);

  next = scheduler_->Next(
      entries(),
      BuildDeviceStatus(BatteryStatus::CHARGING, NetworkStatus::UNMETERED));
  EXPECT_EQ(&entries_[3], next);
  MakeEntryActive(next);
}

// Ensures the reschedule logic works correctly, and we can pass the correct
// criteria to platform task scheduler.
TEST_F(DownloadSchedulerImplTest, Reschedule) {
  InSequence s;

  BuildScheduler(std::vector<DownloadClient>{DownloadClient::TEST});
  BuildDataEntries(2);
  entries_[0].client = DownloadClient::TEST;
  entries_[1].client = DownloadClient::TEST;

  entries_[0].scheduling_params.battery_requirements =
      SchedulingParams::BatteryRequirements::BATTERY_CHARGING;
  entries_[0].scheduling_params.network_requirements =
      SchedulingParams::NetworkRequirements::UNMETERED;
  entries_[1].scheduling_params.battery_requirements =
      SchedulingParams::BatteryRequirements::BATTERY_CHARGING;
  entries_[1].scheduling_params.network_requirements =
      SchedulingParams::NetworkRequirements::UNMETERED;

  Criteria criteria(config_.download_battery_percentage);
  EXPECT_CALL(task_scheduler_, CancelTask(DownloadTaskType::DOWNLOAD_TASK))
      .Times(0);
  EXPECT_CALL(task_scheduler_,
              ScheduleTask(DownloadTaskType::DOWNLOAD_TASK,
                           criteria.requires_unmetered_network,
                           criteria.requires_battery_charging, _, _, _))
      .RetiresOnSaturation();
  scheduler_->Reschedule(entries());

  entries_[0].scheduling_params.battery_requirements =
      SchedulingParams::BatteryRequirements::BATTERY_INSENSITIVE;
  criteria.requires_battery_charging = false;
  EXPECT_CALL(task_scheduler_, CancelTask(DownloadTaskType::DOWNLOAD_TASK))
      .Times(0);
  EXPECT_CALL(task_scheduler_,
              ScheduleTask(DownloadTaskType::DOWNLOAD_TASK,
                           criteria.requires_unmetered_network,
                           criteria.requires_battery_charging, _, _, _))
      .RetiresOnSaturation();
  scheduler_->Reschedule(entries());

  entries_[0].scheduling_params.network_requirements =
      SchedulingParams::NetworkRequirements::NONE;
  criteria.requires_unmetered_network = false;
  EXPECT_CALL(task_scheduler_, CancelTask(DownloadTaskType::DOWNLOAD_TASK))
      .Times(0);
  EXPECT_CALL(task_scheduler_,
              ScheduleTask(DownloadTaskType::DOWNLOAD_TASK,
                           criteria.requires_unmetered_network,
                           criteria.requires_battery_charging, _, _, _))
      .RetiresOnSaturation();
  scheduler_->Reschedule(entries());

  EXPECT_CALL(task_scheduler_, CancelTask(DownloadTaskType::DOWNLOAD_TASK))
      .RetiresOnSaturation();
  scheduler_->Reschedule(Model::EntryList());
}

}  // namespace
}  // namespace download