|
|
@ -17,7 +17,7 @@ describe('JobQueue', () => {
|
|
|
|
const id = 'jobId';
|
|
|
|
const id = 'jobId';
|
|
|
|
|
|
|
|
|
|
|
|
assert.isFalse(queue.has(id));
|
|
|
|
assert.isFalse(queue.has(id));
|
|
|
|
const promise = queue.addWithId(id, async () => TestUtils.timeout(100));
|
|
|
|
const promise = queue.addWithId(id, async () => TestUtils.timeout(30));
|
|
|
|
assert.isTrue(queue.has(id));
|
|
|
|
assert.isTrue(queue.has(id));
|
|
|
|
await promise;
|
|
|
|
await promise;
|
|
|
|
assert.isFalse(queue.has(id));
|
|
|
|
assert.isFalse(queue.has(id));
|
|
|
@ -27,9 +27,9 @@ describe('JobQueue', () => {
|
|
|
|
describe('addWithId', () => {
|
|
|
|
describe('addWithId', () => {
|
|
|
|
it('should run the jobs concurrently', async () => {
|
|
|
|
it('should run the jobs concurrently', async () => {
|
|
|
|
const input = [
|
|
|
|
const input = [
|
|
|
|
[10, 300],
|
|
|
|
[10, 10],
|
|
|
|
[20, 200],
|
|
|
|
[20, 8],
|
|
|
|
[30, 100],
|
|
|
|
[30, 2],
|
|
|
|
];
|
|
|
|
];
|
|
|
|
const queue = new JobQueue();
|
|
|
|
const queue = new JobQueue();
|
|
|
|
const mapper = async ([value, ms]: Array<number>): Promise<number> =>
|
|
|
|
const mapper = async ([value, ms]: Array<number>): Promise<number> =>
|
|
|
@ -48,20 +48,20 @@ describe('JobQueue', () => {
|
|
|
|
const timeTaken = Date.now() - start;
|
|
|
|
const timeTaken = Date.now() - start;
|
|
|
|
assert.isAtLeast(
|
|
|
|
assert.isAtLeast(
|
|
|
|
timeTaken,
|
|
|
|
timeTaken,
|
|
|
|
600,
|
|
|
|
20,
|
|
|
|
'Queue should take atleast 600ms to run.'
|
|
|
|
'Queue should take atleast 100ms to run.'
|
|
|
|
);
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it('should return the result of the job', async () => {
|
|
|
|
it('should return the result of the job', async () => {
|
|
|
|
const queue = new JobQueue();
|
|
|
|
const queue = new JobQueue();
|
|
|
|
const success = queue.addWithId(uuid(), async () => {
|
|
|
|
const success = queue.addWithId(uuid(), async () => {
|
|
|
|
await TestUtils.timeout(100);
|
|
|
|
await TestUtils.timeout(10);
|
|
|
|
|
|
|
|
|
|
|
|
return 'success';
|
|
|
|
return 'success';
|
|
|
|
});
|
|
|
|
});
|
|
|
|
const failure = queue.addWithId(uuid(), async () => {
|
|
|
|
const failure = queue.addWithId(uuid(), async () => {
|
|
|
|
await TestUtils.timeout(100);
|
|
|
|
await TestUtils.timeout(10);
|
|
|
|
throw new Error('failed');
|
|
|
|
throw new Error('failed');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
@ -73,7 +73,7 @@ describe('JobQueue', () => {
|
|
|
|
const queue = new JobQueue();
|
|
|
|
const queue = new JobQueue();
|
|
|
|
const first = queue.addWithId(uuid(), () => 'first');
|
|
|
|
const first = queue.addWithId(uuid(), () => 'first');
|
|
|
|
const second = queue.addWithId(uuid(), async () => {
|
|
|
|
const second = queue.addWithId(uuid(), async () => {
|
|
|
|
await TestUtils.timeout(100);
|
|
|
|
await TestUtils.timeout(10);
|
|
|
|
|
|
|
|
|
|
|
|
return 'second';
|
|
|
|
return 'second';
|
|
|
|
});
|
|
|
|
});
|
|
|
@ -90,7 +90,7 @@ describe('JobQueue', () => {
|
|
|
|
const queue = new JobQueue();
|
|
|
|
const queue = new JobQueue();
|
|
|
|
const id = uuid();
|
|
|
|
const id = uuid();
|
|
|
|
const job = async () => {
|
|
|
|
const job = async () => {
|
|
|
|
await TestUtils.timeout(100);
|
|
|
|
await TestUtils.timeout(10);
|
|
|
|
|
|
|
|
|
|
|
|
return 'job1';
|
|
|
|
return 'job1';
|
|
|
|
};
|
|
|
|
};
|
|
|
@ -106,14 +106,14 @@ describe('JobQueue', () => {
|
|
|
|
const id = uuid();
|
|
|
|
const id = uuid();
|
|
|
|
|
|
|
|
|
|
|
|
const successfullJob = queue.addWithId(id, async () =>
|
|
|
|
const successfullJob = queue.addWithId(id, async () =>
|
|
|
|
TestUtils.timeout(100)
|
|
|
|
TestUtils.timeout(10)
|
|
|
|
);
|
|
|
|
);
|
|
|
|
assert.isTrue(queue.has(id));
|
|
|
|
assert.isTrue(queue.has(id));
|
|
|
|
await successfullJob;
|
|
|
|
await successfullJob;
|
|
|
|
assert.isFalse(queue.has(id));
|
|
|
|
assert.isFalse(queue.has(id));
|
|
|
|
|
|
|
|
|
|
|
|
const failJob = queue.addWithId(id, async () => {
|
|
|
|
const failJob = queue.addWithId(id, async () => {
|
|
|
|
await TestUtils.timeout(100);
|
|
|
|
await TestUtils.timeout(10);
|
|
|
|
throw new Error('failed');
|
|
|
|
throw new Error('failed');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
assert.isTrue(queue.has(id));
|
|
|
|
assert.isTrue(queue.has(id));
|
|
|
|