mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2025-01-09 14:32:07 -05:00
stub timer to fix flaky test
This commit is contained in:
parent
2f62773984
commit
d49c7b732c
1 changed files with 21 additions and 13 deletions
|
@ -1,8 +1,18 @@
|
||||||
const test = require('tap').test;
|
const test = require('tap').test;
|
||||||
const Timer = require('../../src/util/timer');
|
const Timer = require('../../src/util/timer');
|
||||||
|
|
||||||
|
// Stubbed current time
|
||||||
|
let NOW = 0;
|
||||||
|
|
||||||
|
const testNow = {
|
||||||
|
now: () => {
|
||||||
|
NOW += 100;
|
||||||
|
return NOW;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
test('spec', t => {
|
test('spec', t => {
|
||||||
const timer = new Timer();
|
const timer = new Timer(testNow);
|
||||||
|
|
||||||
t.type(Timer, 'function');
|
t.type(Timer, 'function');
|
||||||
t.type(timer, 'object');
|
t.type(timer, 'object');
|
||||||
|
@ -18,33 +28,31 @@ test('spec', t => {
|
||||||
});
|
});
|
||||||
|
|
||||||
test('time', t => {
|
test('time', t => {
|
||||||
const timer = new Timer();
|
const timer = new Timer(testNow);
|
||||||
const time = timer.time();
|
const time = timer.time();
|
||||||
|
|
||||||
t.ok(Date.now() >= time);
|
t.ok(testNow.now() >= time);
|
||||||
t.end();
|
t.end();
|
||||||
});
|
});
|
||||||
|
|
||||||
test('start / timeElapsed', t => {
|
test('start / timeElapsed', t => {
|
||||||
const timer = new Timer();
|
const timer = new Timer(testNow);
|
||||||
const delay = 100;
|
const delay = 100;
|
||||||
const threshold = 1000 / 60; // 60 hz
|
const threshold = 1000 / 60; // 60 hz
|
||||||
|
|
||||||
// Start timer
|
// Start timer
|
||||||
timer.start();
|
timer.start();
|
||||||
|
|
||||||
// Wait and measure timer
|
// Measure timer
|
||||||
timer.setTimeout(() => {
|
|
||||||
const timeElapsed = timer.timeElapsed();
|
const timeElapsed = timer.timeElapsed();
|
||||||
t.ok(timeElapsed >= 0);
|
t.ok(timeElapsed >= 0);
|
||||||
t.ok(timeElapsed >= (delay - threshold) &&
|
t.ok(timeElapsed >= (delay - threshold) &&
|
||||||
timeElapsed <= (delay + threshold));
|
timeElapsed <= (delay + threshold));
|
||||||
t.end();
|
t.end();
|
||||||
}, delay);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test('setTimeout / clearTimeout', t => new Promise((resolve, reject) => {
|
test('setTimeout / clearTimeout', t => new Promise((resolve, reject) => {
|
||||||
const timer = new Timer();
|
const timer = new Timer(testNow);
|
||||||
const cancelId = timer.setTimeout(() => {
|
const cancelId = timer.setTimeout(() => {
|
||||||
reject(new Error('Canceled task ran'));
|
reject(new Error('Canceled task ran'));
|
||||||
}, 1);
|
}, 1);
|
||||||
|
|
Loading…
Reference in a new issue