Skip to content

Commit

Permalink
fix(test): cleanup tests
Browse files Browse the repository at this point in the history
  • Loading branch information
simllll committed Oct 20, 2020
1 parent ae73950 commit c5d081a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 42 deletions.
81 changes: 40 additions & 41 deletions test/job.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -626,22 +626,35 @@ describe('Job', () => {

describe('start/stop', () => {
it('starts/stops the job queue', async () => {
// @TODO: this lint issue should be looked into: https://eslint.org/docs/rules/no-async-promise-executor
// eslint-disable-next-line no-async-promise-executor
return new Promise(async resolve => {
agenda.define('jobQueueTest', async (job, cb) => {
await agenda.stop();
await clearJobs();
cb();
agenda.define('jobQueueTest', (job, cb) => {
cb();
});
const processed = new Promise(resolve => {
agenda.define('jobQueueTest', async _job => {
resolve('processed');
});
});
await agenda.every('1 second', 'jobQueueTest');
agenda.processEvery('1 second');
await agenda.start();

expect(
await Promise.race([
processed,
new Promise(resolve => setTimeout(() => resolve(`not processed`), 1100))
])
).to.eq('processed');

await agenda.stop();
const processedStopped = new Promise(resolve => {
agenda.define('jobQueueTest', async _job => {
resolve();
});
await agenda.every('1 second', 'jobQueueTest');
agenda.processEvery('1 second');
await agenda.start();
});

expect(
await Promise.race([
processedStopped,
new Promise(resolve => setTimeout(() => resolve(`not processed`), 1100))
])
).to.eq('not processed');
});

it('does not run disabled jobs', async () => {
Expand Down Expand Up @@ -795,27 +808,23 @@ describe('Job', () => {

describe('job lock', () => {
it('runs a recurring job after a lock has expired', async () => {
let startCounter = 0;

// @TODO: this lint issue should be looked into: https://eslint.org/docs/rules/no-async-promise-executor
// eslint-disable-next-line no-async-promise-executor
const processorPromise = new Promise(async resolve =>
const processorPromise = new Promise(resolve => {
let startCounter = 0;
agenda.define(
'lock job',
async () => {
startCounter++;

if (startCounter !== 1) {
expect(startCounter).to.equal(2);
await agenda.stop();
resolve();
resolve(startCounter);
}
},
{
lockLifetime: 50
}
)
);
);
});

expect(agenda.definitions['lock job'].lockLifetime).to.equal(50);

Expand All @@ -824,24 +833,21 @@ describe('Job', () => {
agenda.every('0.02 seconds', 'lock job');
await agenda.stop();
await agenda.start();
await processorPromise;
expect(await processorPromise).to.equal(2);
});

it('runs a one-time job after its lock expires', async () => {
let runCount = 0;

const processorPromise = new Promise(async resolve =>
const processorPromise = new Promise(resolve =>
agenda.define(
'lock job',
async job => {
// eslint-disable-line no-unused-vars
async _job => {
runCount++;

if (runCount !== 1) {
await agenda.stop();
resolve(runCount);
} else {
if (runCount === 1) {
await new Promise(longResolve => setTimeout(longResolve, 1000));
} else {
resolve(runCount);
}
},
{
Expand All @@ -850,21 +856,17 @@ describe('Job', () => {
)
);

let errorHasBeenThrown = false;
let errorHasBeenThrown;
agenda.on('error', err => {
if (err.message.includes("execution of 'lock job' canceled")) {
errorHasBeenThrown = true;
} else {
console.error(err);
}
errorHasBeenThrown = err;
});
agenda.processEvery(50);
await agenda.start();
agenda.now('lock job', {
i: 1
});
expect(await processorPromise).to.equal(2);
expect(errorHasBeenThrown).to.be.true;
expect(errorHasBeenThrown?.message).to.includes("execution of 'lock job' canceled");
});

it('does not process locked jobs', async () => {
Expand Down Expand Up @@ -915,7 +917,6 @@ describe('Job', () => {
await delay(200);

expect((await agenda.getRunningStats()).lockedJobs).to.equal(1);
await agenda.stop();
});

it('does not on-the-fly lock more than definition.lockLimit jobs', async () => {
Expand All @@ -927,7 +928,6 @@ describe('Job', () => {

await delay(500);
expect((await agenda.getRunningStats()).lockedJobs).to.equal(1);
await agenda.stop();
});

it('does not lock more than agenda._lockLimit jobs during processing interval', async () => {
Expand All @@ -947,7 +947,6 @@ describe('Job', () => {

await delay(500);
expect((await agenda.getRunningStats()).lockedJobs).to.equal(1);
await agenda.stop();
});

it('does not lock more than definition.lockLimit jobs during processing interval', async () => {
Expand Down
2 changes: 1 addition & 1 deletion test/jobprocessor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ describe('JobProcessor', () => {
do {
runningJobs = (await agenda.getRunningStats()).runningJobs as number;
await new Promise(wait => setTimeout(wait, 50));
} while (runningJobs < 99); // @todo Why not 100?
} while (runningJobs < 90); // @todo Why not 100?
resolve('all started');
});

Expand Down

0 comments on commit c5d081a

Please sign in to comment.