Adrià Vilanova MartÃnez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 1 | // Copyright 2019 The Chromium Authors |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | import sinon from 'sinon'; |
| 6 | import {assert} from 'chai'; |
| 7 | import {CronTask} from './cron.js'; |
| 8 | |
| 9 | let clock; |
| 10 | |
| 11 | describe('cron', () => { |
| 12 | beforeEach(() => { |
| 13 | clock = sinon.useFakeTimers(); |
| 14 | }); |
| 15 | |
| 16 | afterEach(() => { |
| 17 | clock.restore(); |
| 18 | }); |
| 19 | |
| 20 | it('calls task periodically', () => { |
| 21 | const task = sinon.spy(); |
| 22 | const cronTask = new CronTask(task, 1000); |
| 23 | |
| 24 | // Make sure task is not called until the cron task has been started. |
| 25 | assert.isFalse(task.called); |
| 26 | |
| 27 | cronTask.start(); |
| 28 | assert.isTrue(task.calledOnce); |
| 29 | |
| 30 | clock.tick(1000); |
| 31 | assert.isTrue(task.calledTwice); |
| 32 | |
| 33 | clock.tick(1000); |
| 34 | assert.isTrue(task.calledThrice); |
| 35 | }); |
| 36 | }); |