2018-12-04 10:39:59 -05:00
|
|
|
const test = require('tap').skip;
|
2018-08-31 15:38:06 -04:00
|
|
|
|
2018-09-14 22:34:34 -04:00
|
|
|
const TaskQueue = require('../../src/util/task-queue');
|
2018-08-31 15:38:06 -04:00
|
|
|
|
|
|
|
const testCompare = require('../fixtures/test-compare');
|
|
|
|
|
|
|
|
test('constructor', t => {
|
|
|
|
// Max tokens = 1000, refill 1000 tokens per second (1 per millisecond), and start with 0 tokens
|
2018-09-14 22:34:34 -04:00
|
|
|
const bukkit = new TaskQueue(1000, 1000, 0);
|
2018-08-31 15:38:06 -04:00
|
|
|
|
2018-12-04 12:12:06 -05:00
|
|
|
// Simulate time passing with a stubbed timer
|
|
|
|
const simulatedTimeStart = Date.now();
|
|
|
|
bukkit._timer = {timeElapsed: () => Date.now() - simulatedTimeStart};
|
2018-08-31 15:38:06 -04:00
|
|
|
|
|
|
|
const taskResults = [];
|
|
|
|
const promises = [];
|
2018-09-14 22:10:17 -04:00
|
|
|
const goodCancelMessage = 'Task was canceled correctly';
|
|
|
|
bukkit.do(() => taskResults.push('nope'), 999).then(
|
|
|
|
() => {
|
|
|
|
t.fail('Task should have been canceled');
|
|
|
|
},
|
|
|
|
() => {
|
|
|
|
taskResults.push(goodCancelMessage);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
bukkit.cancelAll();
|
2018-08-31 15:38:06 -04:00
|
|
|
promises.push(
|
2018-09-14 22:10:17 -04:00
|
|
|
bukkit.do(() => taskResults.push('a'), 50).then(() =>
|
2018-12-04 12:12:06 -05:00
|
|
|
testCompare(t, bukkit._timer.timeElapsed(), '>=', 50, 'Costly task must wait')
|
2018-08-31 15:38:06 -04:00
|
|
|
),
|
2018-09-14 22:10:17 -04:00
|
|
|
bukkit.do(() => taskResults.push('b'), 10).then(() =>
|
2018-12-04 12:12:06 -05:00
|
|
|
testCompare(t, bukkit._timer.timeElapsed(), '>=', 60, 'Tasks must run in serial')
|
2018-08-31 15:38:06 -04:00
|
|
|
),
|
2018-09-14 22:10:17 -04:00
|
|
|
bukkit.do(() => taskResults.push('c'), 1).then(() =>
|
2018-12-04 12:12:06 -05:00
|
|
|
testCompare(t, bukkit._timer.timeElapsed(), '<', 80, 'Cheap task should run soon')
|
2018-08-31 15:38:06 -04:00
|
|
|
)
|
|
|
|
);
|
|
|
|
return Promise.all(promises).then(() => {
|
2018-09-14 22:10:17 -04:00
|
|
|
t.deepEqual(taskResults, [goodCancelMessage, 'a', 'b', 'c'], 'All tasks must run in correct order');
|
2018-08-31 15:38:06 -04:00
|
|
|
});
|
|
|
|
});
|