File size: 4,375 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import { delayedValue } from '../test-common';
import * as Timing from '../timing';

jest.useFakeTimers();

describe( 'monotonicNow', () => {
	it( 'should be strictly monotonic', () => {
		const spiedGetTime = jest.spyOn( global.Date, 'now' );
		const lastNow = Timing.monotonicNow();
		spiedGetTime.mockImplementationOnce( () => lastNow );
		expect( Timing.monotonicNow() ).toBe( lastNow + 1 );
		spiedGetTime.mockImplementationOnce( () => lastNow );
		expect( Timing.monotonicNow() ).toBe( lastNow + 2 );
	} );
} );

describe( 'timeoutPromise', () => {
	it( 'should resolve promises below the timeout', async () => {
		const promise1 = Timing.timeoutPromise( Promise.resolve( 123 ), 1 );
		jest.advanceTimersByTime( 2 );
		await expect( promise1 ).resolves.toBe( 123 );
		const promise2 = Timing.timeoutPromise( delayedValue( 123, 1 ), 4 );
		jest.advanceTimersByTime( 5 );
		await expect( promise2 ).resolves.toBe( 123 );
	} );
	it( 'should reject if promises rejected below the timeout', async () => {
		const promise = Timing.timeoutPromise( Promise.reject( new Error( 'error-123' ) ), 1 );
		jest.advanceTimersByTime( 1 );
		await expect( promise ).rejects.toThrow( 'error-123' );
	} );
	it( 'should throw if promise gets timed-out', async () => {
		const promise1 = Timing.timeoutPromise( delayedValue( null, 4 ), 1 );
		jest.advanceTimersByTime( 5 );
		await expect( promise1 ).rejects.toThrow();
		const promise2 = Timing.timeoutPromise( delayedValue( null, 5 ), 2 );
		jest.advanceTimersByTime( 6 );
		await expect( promise2 ).rejects.toThrow();
	} );
} );

describe( 'asyncOneAtATime', () => {
	it( 'should wrap an async function and behave the same', async () => {
		const f = Timing.asyncOneAtATime( async () => delayedValue( 123, 0 ) );
		const promise = f();
		jest.advanceTimersByTime( 1 );
		await expect( promise ).resolves.toBe( 123 );
	} );

	it( 'should wrap an async function and behave the same (rejection)', async () => {
		const f = Timing.asyncOneAtATime( async () => {
			throw new Error( 'error-123' );
		} );
		await expect( f() ).rejects.toThrow( 'error-123' );
	} );

	it( 'should return the same promise when called multiple times, only calling the original function once', async () => {
		const origF = jest.fn( async () => delayedValue( 123, 1 ) );
		const f = Timing.asyncOneAtATime( origF );
		const a = f();
		const b = f();
		const c = f();
		expect( a ).toBe( b );
		expect( b ).toBe( c );
		jest.advanceTimersByTime( 2 );
		await expect( a ).resolves.toBe( 123 );
		expect( origF.mock.calls.length ).toBe( 1 );
	} );

	it( 'should return the same promise when called multiple times (rejection), only calling the original function once', async () => {
		const origF = jest.fn( async () => {
			throw new Error( 'error-123' );
		} );
		const f = Timing.asyncOneAtATime( origF );
		const a = f();
		const b = f();
		const c = f();
		expect( a ).toBe( b );
		expect( b ).toBe( c );
		await expect( a ).rejects.toThrow( 'error-123' );
		expect( origF.mock.calls.length ).toBe( 1 );
	} );

	it( 'should return a different promise after the last has resolved, calling the orignal function twice', async () => {
		const origF = jest.fn( async () => delayedValue( 123, 3 ) );
		const f = Timing.asyncOneAtATime( origF );
		const a = f();
		const b = f();
		expect( a ).toBe( b );
		jest.advanceTimersByTime( 4 );
		await expect( a ).resolves.toBe( 123 );
		expect( origF.mock.calls.length ).toBe( 1 );

		jest.advanceTimersByTime( 4 );
		const c = f();
		const d = f();
		expect( a ).not.toBe( c );
		expect( c ).toBe( d );
		jest.advanceTimersByTime( 4 );
		await expect( c ).resolves.toBe( 123 );
		expect( origF.mock.calls.length ).toBe( 2 );
	} );

	it( 'should return a different promise after the last has resolved (rejection), calling the original function twice', async () => {
		let isReject = true;
		const origF = jest.fn( async () => {
			if ( isReject ) {
				throw new Error( 'error-123' );
			}
			return 123;
		} );
		const f = Timing.asyncOneAtATime( origF );
		const a = f();
		const b = f();
		expect( a ).toBe( b );
		await expect( a ).rejects.toThrow( 'error-123' );
		expect( origF.mock.calls.length ).toBe( 1 );

		isReject = false;
		const c = f();
		const d = f();
		expect( a ).not.toBe( c );
		expect( c ).toBe( d );
		await expect( c ).resolves.toBe( 123 );
		expect( origF.mock.calls.length ).toBe( 2 );
	} );
} );