| b69ab31 | | | 1 | /** |
| b69ab31 | | | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| b69ab31 | | | 3 | * |
| b69ab31 | | | 4 | * This source code is licensed under the MIT license found in the |
| b69ab31 | | | 5 | * LICENSE file in the root directory of this source tree. |
| b69ab31 | | | 6 | */ |
| b69ab31 | | | 7 | |
| b69ab31 | | | 8 | import {serializeAsyncCall, sleep} from '../utils'; |
| b69ab31 | | | 9 | |
| b69ab31 | | | 10 | function flushPromises() { |
| b69ab31 | | | 11 | return new Promise(res => jest.requireActual('timers').setTimeout(res, 0)); |
| b69ab31 | | | 12 | } |
| b69ab31 | | | 13 | |
| b69ab31 | | | 14 | describe('serializeAsyncCall', () => { |
| b69ab31 | | | 15 | it('only runs one at a time', async () => { |
| b69ab31 | | | 16 | jest.useFakeTimers(); |
| b69ab31 | | | 17 | const runTime = async (t: number) => { |
| b69ab31 | | | 18 | jest.advanceTimersByTime(t); |
| b69ab31 | | | 19 | await flushPromises(); |
| b69ab31 | | | 20 | }; |
| b69ab31 | | | 21 | let nextId = 0; |
| b69ab31 | | | 22 | const started: Array<number> = []; |
| b69ab31 | | | 23 | const finished: Array<number> = []; |
| b69ab31 | | | 24 | const testFn = serializeAsyncCall(async () => { |
| b69ab31 | | | 25 | const id = nextId++; |
| b69ab31 | | | 26 | started.push(id); |
| b69ab31 | | | 27 | await sleep(40); |
| b69ab31 | | | 28 | finished.push(id); |
| b69ab31 | | | 29 | }); |
| b69ab31 | | | 30 | |
| b69ab31 | | | 31 | testFn(); // This one is run immediately |
| b69ab31 | | | 32 | expect(started).toEqual([0]); |
| b69ab31 | | | 33 | expect(finished).toEqual([]); // not finished running |
| b69ab31 | | | 34 | await runTime(10); |
| b69ab31 | | | 35 | testFn(); // this one queues up while the first is still running |
| b69ab31 | | | 36 | expect(started).toEqual([0]); // 1 not running yet |
| b69ab31 | | | 37 | expect(finished).toEqual([]); |
| b69ab31 | | | 38 | await runTime(10); |
| b69ab31 | | | 39 | testFn(); // we already have an invocation queued, |
| b69ab31 | | | 40 | expect(started).toEqual([0]); |
| b69ab31 | | | 41 | expect(finished).toEqual([]); |
| b69ab31 | | | 42 | |
| b69ab31 | | | 43 | await runTime(60); |
| b69ab31 | | | 44 | expect(started).toEqual([0, 1]); |
| b69ab31 | | | 45 | expect(finished).toEqual([0]); |
| b69ab31 | | | 46 | |
| b69ab31 | | | 47 | await runTime(60); |
| b69ab31 | | | 48 | expect(started).toEqual([0, 1]); |
| b69ab31 | | | 49 | expect(finished).toEqual([0, 1]); |
| b69ab31 | | | 50 | |
| b69ab31 | | | 51 | // nothing more to run |
| b69ab31 | | | 52 | await runTime(100); |
| b69ab31 | | | 53 | expect(started).toEqual([0, 1]); |
| b69ab31 | | | 54 | expect(finished).toEqual([0, 1]); |
| b69ab31 | | | 55 | }); |
| b69ab31 | | | 56 | |
| b69ab31 | | | 57 | it('returns the same result beyond being queued once', async () => { |
| b69ab31 | | | 58 | jest.useFakeTimers(); |
| b69ab31 | | | 59 | let callNumber = 1; |
| b69ab31 | | | 60 | const testFn = serializeAsyncCall(() => { |
| b69ab31 | | | 61 | return Promise.resolve(callNumber++); |
| b69ab31 | | | 62 | }); |
| b69ab31 | | | 63 | |
| b69ab31 | | | 64 | const promise1 = testFn(); |
| b69ab31 | | | 65 | const promise2 = testFn(); |
| b69ab31 | | | 66 | const promise3 = testFn(); |
| b69ab31 | | | 67 | |
| b69ab31 | | | 68 | const values = await Promise.all([promise1, promise2, promise3]); |
| b69ab31 | | | 69 | expect(values).toEqual([1, 2, 2]); |
| b69ab31 | | | 70 | }); |
| b69ab31 | | | 71 | }); |