2019-02-07 19:31:36 -05:00
|
|
|
const test = require('tap').test;
|
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
|
|
|
|
2019-02-07 19:31:36 -05:00
|
|
|
const MockTimer = require('../fixtures/mock-timer');
|
2018-08-31 15:38:06 -04:00
|
|
|
const testCompare = require('../fixtures/test-compare');
|
|
|
|
|
2019-02-11 15:46:48 -05:00
|
|
|
// Max tokens = 1000
|
|
|
|
// Refill 1000 tokens per second (1 per millisecond)
|
|
|
|
// Token bucket starts empty
|
2019-02-12 21:57:58 -05:00
|
|
|
// Max total cost of queued tasks = 10000 tokens = 10 seconds
|
2019-02-11 15:46:48 -05:00
|
|
|
const makeTestQueue = () => {
|
2019-02-12 21:57:58 -05:00
|
|
|
const bukkit = new TaskQueue(1000, 1000, {
|
|
|
|
startingTokens: 0,
|
|
|
|
maxTotalCost: 10000
|
|
|
|
});
|
2018-08-31 15:38:06 -04:00
|
|
|
|
2019-02-07 19:31:36 -05:00
|
|
|
const mockTimer = new MockTimer();
|
|
|
|
bukkit._timer = mockTimer;
|
|
|
|
mockTimer.start();
|
2018-08-31 15:38:06 -04:00
|
|
|
|
2019-02-11 15:46:48 -05:00
|
|
|
return bukkit;
|
|
|
|
};
|
|
|
|
|
|
|
|
test('spec', t => {
|
|
|
|
t.type(TaskQueue, 'function');
|
|
|
|
const bukkit = makeTestQueue();
|
|
|
|
|
|
|
|
t.type(bukkit, 'object');
|
|
|
|
|
|
|
|
t.type(bukkit.length, 'number');
|
|
|
|
t.type(bukkit.do, 'function');
|
2019-02-12 20:37:04 -05:00
|
|
|
t.type(bukkit.cancel, 'function');
|
2019-02-11 15:46:48 -05:00
|
|
|
t.type(bukkit.cancelAll, 'function');
|
|
|
|
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2019-02-15 18:20:38 -05:00
|
|
|
test('constructor', t => {
|
|
|
|
t.ok(new TaskQueue(1, 1));
|
|
|
|
t.ok(new TaskQueue(1, 1, {}));
|
|
|
|
t.ok(new TaskQueue(1, 1, {startingTokens: 0}));
|
|
|
|
t.ok(new TaskQueue(1, 1, {maxTotalCost: 999}));
|
|
|
|
t.ok(new TaskQueue(1, 1, {startingTokens: 0, maxTotalCost: 999}));
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2019-02-12 20:37:04 -05:00
|
|
|
test('run tasks', async t => {
|
|
|
|
const bukkit = makeTestQueue();
|
|
|
|
|
|
|
|
const taskResults = [];
|
|
|
|
|
|
|
|
const promises = [
|
|
|
|
bukkit.do(() => {
|
|
|
|
taskResults.push('a');
|
|
|
|
testCompare(t, bukkit._timer.timeElapsed(), '>=', 50, 'Costly task must wait');
|
|
|
|
}, 50),
|
|
|
|
bukkit.do(() => {
|
|
|
|
taskResults.push('b');
|
|
|
|
testCompare(t, bukkit._timer.timeElapsed(), '>=', 60, 'Tasks must run in serial');
|
|
|
|
}, 10),
|
|
|
|
bukkit.do(() => {
|
|
|
|
taskResults.push('c');
|
|
|
|
testCompare(t, bukkit._timer.timeElapsed(), '<=', 70, 'Cheap task should run soon');
|
|
|
|
}, 1)
|
|
|
|
];
|
|
|
|
|
|
|
|
// advance 10 simulated milliseconds per JS tick
|
|
|
|
while (bukkit.length > 0) {
|
|
|
|
await bukkit._timer.advanceMockTimeAsync(10);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Promise.all(promises).then(() => {
|
|
|
|
t.deepEqual(taskResults, ['a', 'b', 'c'], 'All tasks must run in correct order');
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('cancel', async t => {
|
|
|
|
const bukkit = makeTestQueue();
|
|
|
|
|
|
|
|
const taskResults = [];
|
|
|
|
const goodCancelMessage = 'Task was canceled correctly';
|
|
|
|
const afterCancelMessage = 'Task was run correctly';
|
|
|
|
const cancelTaskPromise = bukkit.do(
|
|
|
|
() => {
|
|
|
|
taskResults.push('nope');
|
|
|
|
}, 999);
|
|
|
|
const cancelCheckPromise = cancelTaskPromise.then(
|
|
|
|
() => {
|
|
|
|
t.fail('Task should have been canceled');
|
|
|
|
},
|
|
|
|
() => {
|
|
|
|
taskResults.push(goodCancelMessage);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
const keepTaskPromise = bukkit.do(
|
|
|
|
() => {
|
|
|
|
taskResults.push(afterCancelMessage);
|
|
|
|
testCompare(t, bukkit._timer.timeElapsed(), '<', 10, 'Canceled task must not delay other tasks');
|
|
|
|
}, 5);
|
|
|
|
|
|
|
|
// give the bucket a chance to make a mistake
|
|
|
|
await bukkit._timer.advanceMockTimeAsync(1);
|
|
|
|
|
|
|
|
t.equal(bukkit.length, 2);
|
|
|
|
const taskWasCanceled = bukkit.cancel(cancelTaskPromise);
|
|
|
|
t.ok(taskWasCanceled);
|
|
|
|
t.equal(bukkit.length, 1);
|
|
|
|
|
|
|
|
while (bukkit.length > 0) {
|
|
|
|
await bukkit._timer.advanceMockTimeAsync(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Promise.all([cancelCheckPromise, keepTaskPromise]).then(() => {
|
|
|
|
t.deepEqual(taskResults, [goodCancelMessage, afterCancelMessage]);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('cancelAll', async t => {
|
2019-02-11 15:46:48 -05:00
|
|
|
const bukkit = makeTestQueue();
|
|
|
|
|
2018-08-31 15:38:06 -04:00
|
|
|
const taskResults = [];
|
2019-02-11 15:46:48 -05:00
|
|
|
const goodCancelMessage1 = 'Task1 was canceled correctly';
|
|
|
|
const goodCancelMessage2 = 'Task2 was canceled correctly';
|
|
|
|
|
|
|
|
const promises = [
|
|
|
|
bukkit.do(() => taskResults.push('nope'), 999).then(
|
|
|
|
() => {
|
|
|
|
t.fail('Task1 should have been canceled');
|
|
|
|
},
|
|
|
|
() => {
|
|
|
|
taskResults.push(goodCancelMessage1);
|
|
|
|
}
|
|
|
|
),
|
|
|
|
bukkit.do(() => taskResults.push('nah'), 999).then(
|
|
|
|
() => {
|
|
|
|
t.fail('Task2 should have been canceled');
|
|
|
|
},
|
|
|
|
() => {
|
|
|
|
taskResults.push(goodCancelMessage2);
|
|
|
|
}
|
|
|
|
)
|
|
|
|
];
|
|
|
|
|
|
|
|
// advance time, but not enough that any task should run
|
2019-02-12 20:37:04 -05:00
|
|
|
await bukkit._timer.advanceMockTimeAsync(100);
|
2019-02-11 15:46:48 -05:00
|
|
|
|
2018-09-14 22:10:17 -04:00
|
|
|
bukkit.cancelAll();
|
2019-02-11 15:46:48 -05:00
|
|
|
|
|
|
|
// advance enough that both tasks would run if they hadn't been canceled
|
2019-02-12 20:37:04 -05:00
|
|
|
await bukkit._timer.advanceMockTimeAsync(10000);
|
2019-02-11 15:46:48 -05:00
|
|
|
|
|
|
|
return Promise.all(promises).then(() => {
|
|
|
|
t.deepEqual(taskResults, [goodCancelMessage1, goodCancelMessage2], 'Tasks should cancel in order');
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-02-12 21:57:58 -05:00
|
|
|
test('max total cost', async t => {
|
|
|
|
const bukkit = makeTestQueue();
|
|
|
|
|
|
|
|
let numTasks = 0;
|
|
|
|
|
|
|
|
const task = () => ++numTasks;
|
|
|
|
|
|
|
|
// Fill the queue
|
|
|
|
for (let i = 0; i < 10; ++i) {
|
|
|
|
bukkit.do(task, 1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
// This one should be rejected because the queue is full
|
|
|
|
bukkit
|
|
|
|
.do(task, 1000)
|
|
|
|
.then(
|
|
|
|
() => {
|
|
|
|
t.fail('Full queue did not reject task');
|
|
|
|
},
|
|
|
|
() => {
|
|
|
|
t.pass();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
while (bukkit.length > 0) {
|
|
|
|
await bukkit._timer.advanceMockTimeAsync(1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
// this should be 10 if the last task is rejected or 11 if it runs
|
|
|
|
t.equal(numTasks, 10);
|
|
|
|
t.end();
|
|
|
|
});
|