Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it('can be created', function () {
const q = new Queue((animal: QueueItem) => { });
});
it('can tick the queue', async function () {
this.timeout(5000);
const ctx = TestContext.createHTMLTestContext();
const { scheduler } = ctx;
const callback = async (qAnimal: QueueItem) => {
await wait(100);
qAnimal.resolve();
};
const q = new Queue(callback as (item: QueueItem) => void);
q.activate({ allowedExecutionCostWithinTick: 0, scheduler });
let promise = q.enqueue(new Animal('dog', 'Pluto'));
assert.strictEqual(q.pending.length, 1, `q.pending.length`);
await wait(50);
assert.strictEqual(q.pending.length, 0, `q.pending.length`);
await promise;
promise = q.enqueue(new Animal('cat', 'Figaro'));
assert.strictEqual(q.pending.length, 1, `q.pending.length`);
await wait(120);
assert.strictEqual(q.pending.length, 0, `q.pending.length`);
q.deactivate();
});
});
it('adds to queue', async function () {
this.timeout(5000);
const callback = async (qAnimal: QueueItem) => {
const animal = qAnimal as Animal;
await wait(100);
if (animal.name === 'dog') {
qAnimal.reject();
} else {
qAnimal.resolve();
}
};
const q = new Queue(callback as (item: QueueItem) => void);
q.enqueue(new Animal('dog', 'Pluto')).catch((error: Error) => { throw error; });
assert.strictEqual(q.pending.length, 0, `q.pending.length`);
q.enqueue(new Animal('cat', 'Figaro')).catch((error: Error) => { throw error; });
assert.strictEqual(q.pending.length, 1, `q.pending.length`);
await wait(110);
assert.strictEqual(q.pending.length, 0, `q.pending.length`);
});