Skip to content

Commit d06fb3e

Browse files
committed
test: use fake timer on executeJob timeout tests
1 parent d8bb49f commit d06fb3e

File tree

1 file changed

+41
-36
lines changed

1 file changed

+41
-36
lines changed

tests/Worker.test.js

Lines changed: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Define globals for eslint.
2-
/* global describe it */
2+
/* global describe it, jest */
33

44
// Load dependencies
55
import should from 'should'; // eslint-disable-line no-unused-vars
@@ -111,41 +111,46 @@ describe('Models/Worker', function() {
111111
/**
112112
* TODO: fix this test
113113
*/
114-
// it('#executeJob() timeout logic should work if timeout is set.', async () => {
115-
// const jobTimeout = 100;
116-
117-
// const job = {
118-
// id: 'd21dca87-435c-4533-b0af-ed9844e6b827',
119-
// name: 'test-job-one',
120-
// payload: JSON.stringify({
121-
// key: 'value'
122-
// }),
123-
// data: JSON.stringify({
124-
// attempts: 1
125-
// }),
126-
// priority: 0,
127-
// active: false,
128-
// timeout: jobTimeout,
129-
// created: new Date(),
130-
// failed: null
131-
// };
132-
133-
// const worker = new Worker();
134-
// worker.addWorker('test-job-one', async () => {
135-
// return new Promise((resolve) => {
136-
// setTimeout(() => {
137-
// resolve(true);
138-
// }, 1000);
139-
// });
140-
// });
141-
142-
// try {
143-
// await worker.executeJob(job);
144-
// throw new Error('execute job should have thrown an error due to timeout.');
145-
// } catch (error) {
146-
// error.should.deepEqual(new Error('TIMEOUT: Job id: ' + job.id + ' timed out in ' + jobTimeout + 'ms.'));
147-
// }
148-
// });
114+
it('#executeJob() timeout logic should work if timeout is set.', async () => {
115+
jest.useFakeTimers('modern');
116+
const jobTimeout = 100;
117+
118+
const job = {
119+
id: 'd21dca87-435c-4533-b0af-ed9844e6b827',
120+
name: 'test-job-one',
121+
payload: JSON.stringify({
122+
key: 'value'
123+
}),
124+
data: JSON.stringify({
125+
attempts: 1
126+
}),
127+
priority: 0,
128+
active: false,
129+
timeout: jobTimeout,
130+
created: new Date(),
131+
failed: null
132+
};
133+
134+
const worker = new Worker();
135+
worker.addWorker('test-job-one', async () => {
136+
return new Promise((resolve) => {
137+
setTimeout(() => {
138+
resolve(true);
139+
}, 1000);
140+
141+
// advance timer by 1sec
142+
jest.advanceTimersByTime(1000);
143+
});
144+
});
145+
146+
try {
147+
await worker.executeJob(job);
148+
throw new Error('execute job should have thrown an error due to timeout.');
149+
} catch (error) {
150+
error.should.deepEqual(new Error('TIMEOUT: Job id: ' + job.id + ' timed out in ' + jobTimeout + 'ms.'));
151+
}
152+
jest.useRealTimers('modern');
153+
});
149154

150155
it('#executeJob() should execute a job correctly.', async () => {
151156
let counter = 0;

0 commit comments

Comments
 (0)