8
8
// tslint:disable:no-big-function no-non-null-assertion
9
9
import { EMPTY , Observable , of , timer } from 'rxjs' ;
10
10
import { map , take , toArray } from 'rxjs/operators' ;
11
+ import { promisify } from 'util' ;
11
12
import { JobHandlerContext , JobOutboundMessage , JobOutboundMessageKind , JobState } from './api' ;
12
13
import { createJobHandler } from './create-job-handler' ;
13
14
import { SimpleJobRegistry } from './simple-registry' ;
14
15
import { SimpleScheduler } from './simple-scheduler' ;
15
16
17
+ const flush = promisify ( setImmediate ) ;
18
+
16
19
describe ( 'SimpleScheduler' , ( ) => {
17
20
let registry : SimpleJobRegistry ;
18
21
let scheduler : SimpleScheduler ;
@@ -61,9 +64,11 @@ describe('SimpleScheduler', () => {
61
64
expect ( started ) . toBe ( 0 ) ;
62
65
63
66
const p1 = job1 . output . toPromise ( ) ;
67
+ await flush ( ) ;
64
68
expect ( started ) . toBe ( 1 ) ;
65
69
66
70
const p2 = job2 . output . toPromise ( ) ;
71
+ await flush ( ) ;
67
72
expect ( started ) . toBe ( 2 ) ;
68
73
expect ( finished ) . toBe ( 0 ) ;
69
74
@@ -186,10 +191,10 @@ describe('SimpleScheduler', () => {
186
191
createJobHandler < number , number , number > ( ( argument : number ) => {
187
192
started . push ( argument ) ;
188
193
189
- return new Promise ( resolve => setImmediate ( ( ) => {
194
+ return new Promise ( resolve => setTimeout ( ( ) => {
190
195
done . push ( argument ) ;
191
196
resolve ( argument ) ;
192
- } ) ) ;
197
+ } , 10 ) ) ;
193
198
} ) ,
194
199
{ argument : true , output : true } ,
195
200
) ;
@@ -204,29 +209,35 @@ describe('SimpleScheduler', () => {
204
209
205
210
// Just subscribe to the last job in the lot.
206
211
job6 . outboundBus . subscribe ( ) ;
212
+ await flush ( ) ;
207
213
// Expect the first one to start.
208
214
expect ( started ) . toEqual ( [ 1 ] ) ;
209
215
// Wait for the first one to finish.
210
216
await job1 . output . toPromise ( ) ;
217
+ await flush ( ) ;
211
218
// Expect the second one to have started, and the first one to be done.
212
219
expect ( started ) . toEqual ( [ 1 , 2 ] ) ;
213
220
expect ( done ) . toEqual ( [ 1 ] ) ;
214
221
215
222
// Rinse and repeat.
216
223
await job2 . output . toPromise ( ) ;
224
+ await flush ( ) ;
217
225
expect ( started ) . toEqual ( [ 1 , 2 , 3 ] ) ;
218
226
expect ( done ) . toEqual ( [ 1 , 2 ] ) ;
219
227
220
228
await job3 . output . toPromise ( ) ;
229
+ await flush ( ) ;
221
230
expect ( started ) . toEqual ( [ 1 , 2 , 3 , 4 ] ) ;
222
231
expect ( done ) . toEqual ( [ 1 , 2 , 3 ] ) ;
223
232
224
233
await job4 . output . toPromise ( ) ;
234
+ await flush ( ) ;
225
235
expect ( started ) . toEqual ( [ 1 , 2 , 3 , 4 , 5 ] ) ;
226
236
expect ( done ) . toEqual ( [ 1 , 2 , 3 , 4 ] ) ;
227
237
228
238
// Just skip job 5.
229
239
await job6 . output . toPromise ( ) ;
240
+ await flush ( ) ;
230
241
expect ( done ) . toEqual ( started ) ;
231
242
} ) ;
232
243
@@ -293,11 +304,11 @@ describe('SimpleScheduler', () => {
293
304
const p10 = ( scheduler . schedule ( 'jobA' , 10 ) ) . output . toPromise ( ) ;
294
305
const p11 = ( scheduler . schedule ( 'jobA' , 11 ) ) . output . toPromise ( ) ;
295
306
const p12 = ( scheduler . schedule ( 'jobA' , 12 ) ) . output . toPromise ( ) ;
296
- await Promise . resolve ( ) ;
307
+ await flush ( ) ;
297
308
298
309
expect ( done ) . toEqual ( [ ] ) ;
299
310
resume ( ) ;
300
- await Promise . resolve ( ) ;
311
+ await flush ( ) ;
301
312
expect ( done ) . not . toEqual ( [ ] ) ;
302
313
expect ( await p10 ) . toBe ( 10 ) ;
303
314
expect ( await p11 ) . toBe ( 11 ) ;
0 commit comments