|
1 | 1 | // Define globals for eslint.
|
2 |
| -/* global describe it */ |
| 2 | +/* global describe it, jest */ |
3 | 3 |
|
4 | 4 | // Load dependencies
|
5 | 5 | import should from 'should'; // eslint-disable-line no-unused-vars
|
@@ -111,41 +111,46 @@ describe('Models/Worker', function() {
|
111 | 111 | /**
|
112 | 112 | * TODO: fix this test
|
113 | 113 | */
|
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 | + }); |
149 | 154 |
|
150 | 155 | it('#executeJob() should execute a job correctly.', async () => {
|
151 | 156 | let counter = 0;
|
|
0 commit comments