blob: 3fbc4e9a50fc11dda3955a25153a692188ec2641 [file] [log] [blame]
Chris Poucet93fa7ad2022-01-20 21:37:16 +01001/**
2 * @license
3 * Copyright 2022 Google LLC
4 * SPDX-License-Identifier: Apache-2.0
5 */
Frank Bordenbe9451a2022-09-12 15:44:29 +02006import '../../test/common-test-setup';
Frank Borden7b24f732022-09-12 14:29:32 +02007import {assertFails, waitEventLoop} from '../../test/test-utils';
Chris Poucet93fa7ad2022-01-20 21:37:16 +01008import {Scheduler} from './scheduler';
9import {MaxInFlightScheduler} from './max-in-flight-scheduler';
10import {FakeScheduler} from './fake-scheduler';
Frank Bordene1ba8212022-08-29 15:20:01 +020011import {assert} from '@open-wc/testing';
Chris Poucet93fa7ad2022-01-20 21:37:16 +010012
13suite('max-in-flight scheduler', () => {
14 let fakeScheduler: FakeScheduler<number>;
15 let scheduler: Scheduler<number>;
16 setup(() => {
17 fakeScheduler = new FakeScheduler<number>();
18 scheduler = new MaxInFlightScheduler<number>(fakeScheduler, 2);
19 });
20
21 test('executes tasks', async () => {
22 const promise = scheduler.schedule(async () => 1);
23 assert.equal(fakeScheduler.scheduled.length, 1);
24 fakeScheduler.resolve();
25 assert.equal(fakeScheduler.scheduled.length, 0);
26 const val = await promise;
27 assert.equal(val, 1);
28 });
29
30 test('propagates errors', async () => {
31 const error = new Error('This is an error');
32 const promise = scheduler.schedule(async () => {
33 throw error;
34 });
35 assert.equal(fakeScheduler.scheduled.length, 1);
36 assertFails(promise, error);
37 fakeScheduler.resolve();
38 assert.equal(fakeScheduler.scheduled.length, 0);
39 await promise.catch((reason: Error) => {
40 assert.equal(reason, error);
41 });
42 });
43
44 test('propagates subscheduler errors', async () => {
45 const error = new Error('This is an error');
46 const promise = scheduler.schedule(async () => 1);
47 assert.equal(fakeScheduler.scheduled.length, 1);
48 assertFails(promise, error);
49 fakeScheduler.reject(error);
50 assert.equal(fakeScheduler.scheduled.length, 0);
51 await promise.catch((reason: Error) => {
52 assert.equal(reason, error);
53 });
54 });
55
56 test('allows up to 2 in flight', async () => {
57 for (let i = 0; i < 3; ++i) {
58 scheduler.schedule(async () => i);
59 }
60 assert.equal(fakeScheduler.scheduled.length, 2);
61 });
62
63 test('resumes when promise resolves', async () => {
64 for (let i = 0; i < 3; ++i) {
65 scheduler.schedule(async () => i);
66 }
67 assert.equal(fakeScheduler.scheduled.length, 2);
68 fakeScheduler.resolve();
69 assert.equal(fakeScheduler.scheduled.length, 1);
Frank Borden7b24f732022-09-12 14:29:32 +020070 await waitEventLoop();
Chris Poucet93fa7ad2022-01-20 21:37:16 +010071 assert.equal(fakeScheduler.scheduled.length, 2);
72 });
73
74 test('resumes when promise fails', async () => {
75 for (let i = 0; i < 3; ++i) {
76 scheduler.schedule(async () => i);
77 }
78 assert.equal(fakeScheduler.scheduled.length, 2);
79 fakeScheduler.reject(new Error('Fake Error'));
80 assert.equal(fakeScheduler.scheduled.length, 1);
Frank Borden7b24f732022-09-12 14:29:32 +020081 await waitEventLoop();
Chris Poucet93fa7ad2022-01-20 21:37:16 +010082 assert.equal(fakeScheduler.scheduled.length, 2);
83 });
84
85 test('eventually resumes all', async () => {
86 const promises = [];
87 for (let i = 0; i < 3; ++i) {
88 promises.push(scheduler.schedule(async () => i));
89 }
90 for (let i = 0; i < 3; ++i) {
91 fakeScheduler.resolve();
Frank Borden7b24f732022-09-12 14:29:32 +020092 await waitEventLoop();
Chris Poucet93fa7ad2022-01-20 21:37:16 +010093 }
94 const res = await Promise.all(promises);
95 assert.deepEqual(res, [0, 1, 2]);
96 });
97});