Skip to content

Commit 5fc18ef

Browse files
clydinalan-agius4
authored andcommitted
test(@angular-devkit/core): allow asynchronous jobs to start execution before expect
1 parent 3bb3c6c commit 5fc18ef

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

packages/angular_devkit/core/src/experimental/jobs/simple-scheduler_spec.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@
88
// tslint:disable:no-big-function no-non-null-assertion
99
import { EMPTY, Observable, of, timer } from 'rxjs';
1010
import { map, take, toArray } from 'rxjs/operators';
11+
import { promisify } from 'util';
1112
import { JobHandlerContext, JobOutboundMessage, JobOutboundMessageKind, JobState } from './api';
1213
import { createJobHandler } from './create-job-handler';
1314
import { SimpleJobRegistry } from './simple-registry';
1415
import { SimpleScheduler } from './simple-scheduler';
1516

17+
const flush = promisify(setImmediate);
18+
1619
describe('SimpleScheduler', () => {
1720
let registry: SimpleJobRegistry;
1821
let scheduler: SimpleScheduler;
@@ -61,9 +64,11 @@ describe('SimpleScheduler', () => {
6164
expect(started).toBe(0);
6265

6366
const p1 = job1.output.toPromise();
67+
await flush();
6468
expect(started).toBe(1);
6569

6670
const p2 = job2.output.toPromise();
71+
await flush();
6772
expect(started).toBe(2);
6873
expect(finished).toBe(0);
6974

@@ -186,10 +191,10 @@ describe('SimpleScheduler', () => {
186191
createJobHandler<number, number, number>((argument: number) => {
187192
started.push(argument);
188193

189-
return new Promise(resolve => setImmediate(() => {
194+
return new Promise(resolve => setTimeout(() => {
190195
done.push(argument);
191196
resolve(argument);
192-
}));
197+
}, 10));
193198
}),
194199
{ argument: true, output: true },
195200
);
@@ -204,29 +209,35 @@ describe('SimpleScheduler', () => {
204209

205210
// Just subscribe to the last job in the lot.
206211
job6.outboundBus.subscribe();
212+
await flush();
207213
// Expect the first one to start.
208214
expect(started).toEqual([1]);
209215
// Wait for the first one to finish.
210216
await job1.output.toPromise();
217+
await flush();
211218
// Expect the second one to have started, and the first one to be done.
212219
expect(started).toEqual([1, 2]);
213220
expect(done).toEqual([1]);
214221

215222
// Rinse and repeat.
216223
await job2.output.toPromise();
224+
await flush();
217225
expect(started).toEqual([1, 2, 3]);
218226
expect(done).toEqual([1, 2]);
219227

220228
await job3.output.toPromise();
229+
await flush();
221230
expect(started).toEqual([1, 2, 3, 4]);
222231
expect(done).toEqual([1, 2, 3]);
223232

224233
await job4.output.toPromise();
234+
await flush();
225235
expect(started).toEqual([1, 2, 3, 4, 5]);
226236
expect(done).toEqual([1, 2, 3, 4]);
227237

228238
// Just skip job 5.
229239
await job6.output.toPromise();
240+
await flush();
230241
expect(done).toEqual(started);
231242
});
232243

@@ -293,11 +304,11 @@ describe('SimpleScheduler', () => {
293304
const p10 = (scheduler.schedule('jobA', 10)).output.toPromise();
294305
const p11 = (scheduler.schedule('jobA', 11)).output.toPromise();
295306
const p12 = (scheduler.schedule('jobA', 12)).output.toPromise();
296-
await Promise.resolve();
307+
await flush();
297308

298309
expect(done).toEqual([]);
299310
resume();
300-
await Promise.resolve();
311+
await flush();
301312
expect(done).not.toEqual([]);
302313
expect(await p10).toBe(10);
303314
expect(await p11).toBe(11);

packages/angular_devkit/core/src/experimental/jobs/strategy_spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.io/license
77
*/
8+
import { promisify } from 'util';
89
import { JobState } from './api';
910
import { createJobHandler } from './create-job-handler';
1011
import { SimpleJobRegistry } from './simple-registry';
1112
import { SimpleScheduler } from './simple-scheduler';
1213
import { strategy } from './strategy';
1314

15+
const flush = promisify(setImmediate);
16+
1417
describe('strategy.serialize()', () => {
1518
let registry: SimpleJobRegistry;
1619
let scheduler: SimpleScheduler;
@@ -46,9 +49,11 @@ describe('strategy.serialize()', () => {
4649
expect(finished).toBe(0);
4750

4851
job1.output.subscribe();
52+
await flush();
4953
expect(started).toBe(1);
5054

5155
job2.output.subscribe();
56+
await flush();
5257
expect(started).toBe(1); // Job2 starts when Job1 ends.
5358

5459
expect(finished).toBe(0);
@@ -111,9 +116,11 @@ describe('strategy.serialize()', () => {
111116
expect(finished).toBe(0);
112117

113118
job1.output.subscribe();
119+
await flush();
114120
expect(started).toBe(1);
115121

116122
job2.output.subscribe();
123+
await flush();
117124
expect(started).toBe(1); // Job2 starts when Job1 ends.
118125

119126
expect(finished).toBe(0);
@@ -169,6 +176,7 @@ describe('strategy.reuse()', () => {
169176
expect(finished).toBe(0);
170177

171178
job1.output.subscribe();
179+
await flush();
172180
expect(started).toBe(1);
173181
expect(finished).toBe(0);
174182

@@ -186,6 +194,7 @@ describe('strategy.reuse()', () => {
186194
const job3 = scheduler.schedule('add', [1, 2, 3, 4, 5]);
187195
const job4 = scheduler.schedule('add', []);
188196
job3.output.subscribe();
197+
await flush();
189198
expect(started).toBe(2);
190199
expect(finished).toBe(1);
191200

@@ -239,6 +248,7 @@ describe('strategy.memoize()', () => {
239248
expect(finished).toBe(0);
240249

241250
job1.output.subscribe();
251+
await flush();
242252
expect(started).toBe(1);
243253
expect(finished).toBe(0);
244254

@@ -247,6 +257,7 @@ describe('strategy.memoize()', () => {
247257
expect(finished).toBe(0);
248258

249259
job3.output.subscribe();
260+
await flush();
250261
expect(started).toBe(2);
251262
expect(finished).toBe(0);
252263

0 commit comments

Comments
 (0)