Merge pull request #1804 from evhan55/test/task-queue

Task Queue unit test stubbed timer
This commit is contained in:
Eric Rosenbaum 2018-12-04 16:15:49 -05:00 committed by GitHub
commit 19069998c0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,6 +1,5 @@
const test = require('tap').test;
const Timer = require('../../src/util/timer');
const TaskQueue = require('../../src/util/task-queue');
const testCompare = require('../fixtures/test-compare');
@ -9,8 +8,9 @@ test('constructor', t => {
// Max tokens = 1000, refill 1000 tokens per second (1 per millisecond), and start with 0 tokens
const bukkit = new TaskQueue(1000, 1000, 0);
const timer = new Timer();
timer.start();
// Simulate time passing with a stubbed timer
const simulatedTimeStart = Date.now();
bukkit._timer = {timeElapsed: () => Date.now() - simulatedTimeStart};
const taskResults = [];
const promises = [];
@ -26,13 +26,13 @@ test('constructor', t => {
bukkit.cancelAll();
promises.push(
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(() =>
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(() =>
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(() => {