scheduler_unittest.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Copyright 2020 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. import {assertEquals} from 'chrome://webui-test/chai_assert.js';
  5. import {ImageRequestTask} from './image_request_task.js';
  6. import {Scheduler} from './scheduler.js';
  7. /**
  8. * Fake global clock used to record the "time" at which a task was run.
  9. */
  10. let globalTime = 0;
  11. export function setUp() {
  12. globalTime = 0;
  13. }
  14. /**
  15. * @typedef{{
  16. * cancelCallCount: number,
  17. * runTime: number,
  18. * }}
  19. */
  20. let FakeImageRequestTask;
  21. /**
  22. * @param {string} taskId
  23. * @return {!FakeImageRequestTask}
  24. */
  25. function newTask(taskId, priority) {
  26. return /** @type !FakeImageRequestTask */ ({
  27. // Counts how many times cancel method was called.
  28. // Used to test multiple cancellation of the same task.
  29. cancelCallCount: 0,
  30. // Records value of globalTime variable at the time the main method,
  31. // loadFromCacheAndProcess is called. Used to test if the task was
  32. // executed and in what orders tasks were executed.
  33. runTime: 0,
  34. getId() {
  35. return taskId;
  36. },
  37. getPriority() {
  38. return priority;
  39. },
  40. cancel() {
  41. ++this.cancelCallCount;
  42. },
  43. loadFromCacheAndProcess(resolve, reject) {
  44. this.runTime = ++globalTime;
  45. setTimeout(resolve);
  46. },
  47. });
  48. }
  49. /**
  50. * Checks that adding and removing tasks before the scheduler is started works.
  51. */
  52. export function testIdleSchedulerAddRemove() {
  53. const scheduler = new Scheduler();
  54. const fakeTask = newTask('task-1', 0);
  55. scheduler.add(/** @type {!ImageRequestTask} */ (fakeTask));
  56. assertEquals(0, fakeTask.cancelCallCount);
  57. scheduler.remove('task-1');
  58. assertEquals(1, fakeTask.cancelCallCount);
  59. scheduler.remove('task-1');
  60. assertEquals(1, fakeTask.cancelCallCount);
  61. }
  62. /**
  63. * Checks that tasks that were in newTasks are correctly copied to pending
  64. * tasks when scheduler is started. They also should be executed in the
  65. * order of their priorities.
  66. */
  67. export function testNewTasksMovedAndRunInPriorityOrder() {
  68. const fakeTask1 = newTask('task-1', 1);
  69. const fakeTask2 = newTask('task-2', 0);
  70. const scheduler = new Scheduler();
  71. scheduler.add(/** @type {!ImageRequestTask} */ (fakeTask1));
  72. scheduler.add(/** @type {!ImageRequestTask} */ (fakeTask2));
  73. scheduler.start();
  74. assertEquals(2, fakeTask1.runTime);
  75. assertEquals(1, fakeTask2.runTime);
  76. }
  77. /**
  78. * Checks that the scheduler only launches MAXIMUM_IN_PARALLEL tasks.
  79. */
  80. export function testParallelTasks() {
  81. const scheduler = new Scheduler();
  82. const taskList = [];
  83. for (let i = 0; i <= Scheduler.MAXIMUM_IN_PARALLEL; ++i) {
  84. taskList.push(newTask(`task-${i}`, 0));
  85. scheduler.add(/** @type {!ImageRequestTask} */ (taskList[i]));
  86. }
  87. scheduler.start();
  88. for (let i = 0; i < Scheduler.MAXIMUM_IN_PARALLEL; ++i) {
  89. assertEquals(i + 1, taskList[i].runTime, `task ${i} did not run`);
  90. }
  91. assertEquals(0, taskList[Scheduler.MAXIMUM_IN_PARALLEL].runTime);
  92. }