mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2024-12-22 22:12:28 -05:00
Adding a simulated timer to the task queue test instead of using a Timer object.
This commit is contained in:
parent
2bc8778f00
commit
16e45957fa
1 changed files with 6 additions and 8 deletions
|
@ -1,18 +1,16 @@
|
||||||
const test = require('tap').test;
|
const test = require('tap').test;
|
||||||
|
|
||||||
const Timer = require('../../src/util/timer');
|
|
||||||
const TaskQueue = require('../../src/util/task-queue');
|
const TaskQueue = require('../../src/util/task-queue');
|
||||||
|
|
||||||
const testCompare = require('../fixtures/test-compare');
|
const testCompare = require('../fixtures/test-compare');
|
||||||
|
|
||||||
// TODO: temporary comment to trigger git
|
|
||||||
|
|
||||||
test('constructor', t => {
|
test('constructor', t => {
|
||||||
// Max tokens = 1000, refill 1000 tokens per second (1 per millisecond), and start with 0 tokens
|
// Max tokens = 1000, refill 1000 tokens per second (1 per millisecond), and start with 0 tokens
|
||||||
const bukkit = new TaskQueue(1000, 1000, 0);
|
const bukkit = new TaskQueue(1000, 1000, 0);
|
||||||
|
|
||||||
const timer = new Timer();
|
// Simulate time passing with a stubbed timer
|
||||||
timer.start();
|
const simulatedTimeStart = Date.now();
|
||||||
|
bukkit._timer = {timeElapsed: () => Date.now() - simulatedTimeStart};
|
||||||
|
|
||||||
const taskResults = [];
|
const taskResults = [];
|
||||||
const promises = [];
|
const promises = [];
|
||||||
|
@ -28,13 +26,13 @@ test('constructor', t => {
|
||||||
bukkit.cancelAll();
|
bukkit.cancelAll();
|
||||||
promises.push(
|
promises.push(
|
||||||
bukkit.do(() => taskResults.push('a'), 50).then(() =>
|
bukkit.do(() => taskResults.push('a'), 50).then(() =>
|
||||||
testCompare(t, timer.timeElapsed(), '>=', 50, 'Costly task must wait')
|
testCompare(t, bukkit._timer.timeElapsed(), '>=', 50, 'Costly task must wait')
|
||||||
),
|
),
|
||||||
bukkit.do(() => taskResults.push('b'), 10).then(() =>
|
bukkit.do(() => taskResults.push('b'), 10).then(() =>
|
||||||
testCompare(t, timer.timeElapsed(), '>=', 60, 'Tasks must run in serial')
|
testCompare(t, bukkit._timer.timeElapsed(), '>=', 60, 'Tasks must run in serial')
|
||||||
),
|
),
|
||||||
bukkit.do(() => taskResults.push('c'), 1).then(() =>
|
bukkit.do(() => taskResults.push('c'), 1).then(() =>
|
||||||
testCompare(t, timer.timeElapsed(), '<', 80, 'Cheap task should run soon')
|
testCompare(t, bukkit._timer.timeElapsed(), '<', 80, 'Cheap task should run soon')
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
return Promise.all(promises).then(() => {
|
return Promise.all(promises).then(() => {
|
||||||
|
|
Loading…
Reference in a new issue