Chris Poucet | 93fa7ad | 2022-01-20 21:37:16 +0100 | [diff] [blame] | 1 | /** |
| 2 | * @license |
| 3 | * Copyright 2022 Google LLC |
| 4 | * SPDX-License-Identifier: Apache-2.0 |
| 5 | */ |
Frank Borden | be9451a | 2022-09-12 15:44:29 +0200 | [diff] [blame] | 6 | import '../../test/common-test-setup'; |
Frank Borden | 7b24f73 | 2022-09-12 14:29:32 +0200 | [diff] [blame] | 7 | import {assertFails, waitEventLoop} from '../../test/test-utils'; |
Chris Poucet | 93fa7ad | 2022-01-20 21:37:16 +0100 | [diff] [blame] | 8 | import {Scheduler} from './scheduler'; |
| 9 | import {MaxInFlightScheduler} from './max-in-flight-scheduler'; |
| 10 | import {FakeScheduler} from './fake-scheduler'; |
Frank Borden | e1ba821 | 2022-08-29 15:20:01 +0200 | [diff] [blame] | 11 | import {assert} from '@open-wc/testing'; |
Chris Poucet | 93fa7ad | 2022-01-20 21:37:16 +0100 | [diff] [blame] | 12 | |
| 13 | suite('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 Borden | 7b24f73 | 2022-09-12 14:29:32 +0200 | [diff] [blame] | 70 | await waitEventLoop(); |
Chris Poucet | 93fa7ad | 2022-01-20 21:37:16 +0100 | [diff] [blame] | 71 | 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 Borden | 7b24f73 | 2022-09-12 14:29:32 +0200 | [diff] [blame] | 81 | await waitEventLoop(); |
Chris Poucet | 93fa7ad | 2022-01-20 21:37:16 +0100 | [diff] [blame] | 82 | 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 Borden | 7b24f73 | 2022-09-12 14:29:32 +0200 | [diff] [blame] | 92 | await waitEventLoop(); |
Chris Poucet | 93fa7ad | 2022-01-20 21:37:16 +0100 | [diff] [blame] | 93 | } |
| 94 | const res = await Promise.all(promises); |
| 95 | assert.deepEqual(res, [0, 1, 2]); |
| 96 | }); |
| 97 | }); |