-
Notifications
You must be signed in to change notification settings - Fork 6
/
use-async-queue.test.js
248 lines (232 loc) · 8.38 KB
/
use-async-queue.test.js
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import { renderHook, act } from '@testing-library/react-hooks';
import useAsyncQueue from './dist/use-async-queue';
describe('useConcurrentQueue', () => {
describe('real timers', () => {
it('should initialize it', () => {
const { result } = renderHook(() => useAsyncQueue({ concurrency: 1 }));
expect(typeof result.current.add).toBe('function');
});
it('should run one immediate task', async () => {
const inflight = jest.fn();
const done = jest.fn();
const task = {
id: 0,
task: () => {
return Promise.resolve('0 is done');
},
};
const { result, waitForNextUpdate } = renderHook(() =>
useAsyncQueue({ concurrency: 1, inflight, done })
);
expect(done).not.toHaveBeenCalled();
expect(result.current.stats.numInFlight).toBe(0);
expect(result.current.stats.numPending).toBe(0);
expect(result.current.stats.numDone).toBe(0);
act(() => result.current.add(task));
await waitForNextUpdate();
expect(inflight).toHaveBeenCalledTimes(1);
expect(inflight.mock.calls[0][0]).toMatchObject({
id: 0,
task: expect.any(Function),
});
expect(done).toHaveBeenCalledTimes(1);
expect(done.mock.calls[0][0]).toMatchObject({
id: 0,
task: expect.any(Function),
result: expect.any(Promise),
});
expect(done.mock.calls[0][0].result).resolves.toBe('0 is done');
expect(result.current.stats.numInFlight).toBe(0);
expect(result.current.stats.numPending).toBe(0);
expect(result.current.stats.numDone).toBe(1);
});
it('should run two immediate tasks, both resolve', async () => {
const done = jest.fn();
const makeTask = (id) => {
return {
id,
task: () => {
return Promise.resolve(`${id} is done`);
},
};
};
const { result, waitForNextUpdate } = renderHook(() =>
useAsyncQueue({ concurrency: 1, done })
);
expect(done).not.toHaveBeenCalled();
act(() => {
result.current.add(makeTask(0));
});
act(() => {
result.current.add(makeTask(1));
});
await waitForNextUpdate();
expect(done).toHaveBeenCalledTimes(2);
expect(done.mock.calls[0][0].result).resolves.toBe('0 is done');
expect(done.mock.calls[1][0].result).resolves.toBe('1 is done');
});
it('should run two immediate tasks, one resolves, one rejects', async () => {
const done = jest.fn();
const drain = jest.fn();
const makeTask = (id) => {
return {
id,
task: () => {
if (id === 0) {
return Promise.resolve(`${id} is done`);
} else {
return Promise.reject(`${id} rejected`);
}
},
};
};
const { result, rerender, waitForNextUpdate } = renderHook(
({ concurrency, done, drain }) =>
useAsyncQueue({ concurrency, done, drain }),
{ initialProps: { concurrency: 1, done, drain } }
);
// TODO: separate drain testing into its own test case.
expect(done).not.toHaveBeenCalled();
expect(drain).not.toHaveBeenCalled();
act(() => {
result.current.add(makeTask(0));
});
act(() => {
result.current.add(makeTask(1));
});
await waitForNextUpdate();
expect(done).toHaveBeenCalledTimes(2);
expect(done.mock.calls[0][0].result).resolves.toBe('0 is done');
expect(done.mock.calls[1][0].result).rejects.toBe('1 rejected');
// TODO: test that drain is not called until next tick.
expect(drain).toHaveBeenCalledTimes(1);
// Force re-calcuation by changing a prop.
rerender({ concurrency: 2, done, drain });
// Ensure drain isn't called again.
expect(drain).toHaveBeenCalledTimes(1);
});
// This test uses a real timeout, but the call to useFakeTimers messes with
// it.
it.skip('should run one deferred task', async () => {
const done = jest.fn();
const makeTask = (id) => {
return {
id,
task: () => {
return new Promise((resolve) =>
setTimeout(() => resolve(`${id} is done`), 1000 * (id + 1))
);
},
};
};
const { result, waitForNextUpdate } = renderHook(() =>
useAsyncQueue({ concurrency: 1, done })
);
expect(done).not.toHaveBeenCalled();
act(() => result.current.add(makeTask(0)));
await waitForNextUpdate();
expect(done).toHaveBeenCalledTimes(1);
expect(done.mock.calls[0][0]).toMatchObject({
id: 0,
task: expect.any(Function),
result: expect.any(Promise),
});
expect(done.mock.calls[0][0].result).resolves.toBe('0 is done');
});
});
describe('fake timers', () => {
beforeEach(() => {
jest.useFakeTimers();
});
it('should run one deferred task at a time with concurrency 1', async () => {
const inflight = jest.fn();
const done = jest.fn();
const drain = jest.fn();
const makeTask = (id) => {
return {
id,
task: () => {
return new Promise((resolve) =>
setTimeout(() => {
resolve(`${id} is done`);
}, 1000)
);
},
};
};
const { result, waitForNextUpdate } = renderHook(() =>
useAsyncQueue({ concurrency: 1, inflight, done, drain })
);
expect(done).not.toHaveBeenCalled();
expect(inflight).not.toHaveBeenCalled();
expect(drain).not.toHaveBeenCalled();
expect(result.current.stats.numInFlight).toBe(0);
expect(result.current.stats.numPending).toBe(0);
expect(result.current.stats.numDone).toBe(0);
act(() => result.current.add(makeTask(0)));
act(() => result.current.add(makeTask(1)));
expect(result.current.stats.numPending).toBe(1);
expect(result.current.stats.numInFlight).toBe(1);
expect(inflight).toHaveBeenCalledTimes(1);
jest.advanceTimersByTime(900);
expect(done).not.toHaveBeenCalled();
expect(result.current.stats.numInFlight).toBe(1);
expect(result.current.stats.numPending).toBe(1);
expect(result.current.stats.numDone).toBe(0);
jest.advanceTimersByTime(100);
await waitForNextUpdate();
expect(done).toHaveBeenCalledTimes(1);
expect(inflight).toHaveBeenCalledTimes(2);
expect(drain).not.toHaveBeenCalled();
expect(result.current.stats.numInFlight).toBe(1);
expect(result.current.stats.numPending).toBe(0);
expect(result.current.stats.numDone).toBe(1);
jest.advanceTimersByTime(900);
expect(done).toHaveBeenCalledTimes(1);
jest.advanceTimersByTime(100);
await waitForNextUpdate();
expect(done).toHaveBeenCalledTimes(2);
expect(result.current.stats.numInFlight).toBe(0);
expect(result.current.stats.numPending).toBe(0);
expect(result.current.stats.numDone).toBe(2);
expect(drain).toHaveBeenCalledTimes(1);
});
it('should run two deferred tasks at a time with concurrency 2', async () => {
const inflight = jest.fn();
const done = jest.fn();
const makeTask = (id) => {
return {
id,
task: () => {
return new Promise((resolve) =>
setTimeout(() => {
resolve(`${id} is done`);
}, 1000)
);
},
};
};
const { result, waitForNextUpdate } = renderHook(() =>
useAsyncQueue({ concurrency: 2, inflight, done })
);
expect(done).not.toHaveBeenCalled();
act(() => result.current.add(makeTask(0)));
act(() => result.current.add(makeTask(1)));
expect(result.current.stats.numPending).toBe(0);
expect(result.current.stats.numInFlight).toBe(2);
expect(inflight).toHaveBeenCalledTimes(2);
expect(done).toHaveBeenCalledTimes(0);
jest.advanceTimersByTime(900);
expect(done).not.toHaveBeenCalled();
expect(result.current.stats.numInFlight).toBe(2);
expect(result.current.stats.numPending).toBe(0);
expect(result.current.stats.numDone).toBe(0);
jest.advanceTimersByTime(100);
await waitForNextUpdate();
expect(done).toHaveBeenCalledTimes(2);
expect(result.current.stats.numInFlight).toBe(0);
expect(result.current.stats.numPending).toBe(0);
expect(result.current.stats.numDone).toBe(2);
});
});
});