[scheduler] add a test documenting current behavior (#13687)
* [scheduler] add a test documenting current behavior * Update with latest changes from master and confirm fixed behavior
plievone committed
Oct 5, 2018 at 21:25 UTC
e2e7cb9f4cb3b415775688fb118846a9927acaf3
1 file changed
+56
-4
packages/scheduler/src/__tests__/Scheduler-test.internal.js
+56
-4
@@ -33,6 +33,7 @@ describe('Scheduler', () => {
33
let isFlushing = false;
34
let timeoutID = -1;
35
let endOfFrame = -1;
36
+ let hasMicrotask = false;
37
38
let currentTime = 0;
39
@@ -54,6 +55,9 @@ describe('Scheduler', () => {
55
} finally {
56
isFlushing = false;
57
endOfFrame = -1;
58
+ if (hasMicrotask) {
59
+ onTimeout();
60
+ }
61
}
62
const yields = yieldedValues;
63
yieldedValues = [];
@@ -89,14 +93,13 @@ describe('Scheduler', () => {
93
return;
94
}
95
if (isFlushing) {
92
- // Jest fires timers synchronously when jest.advanceTimersByTime is
93
- // called. Use setImmediate to prevent re-entrancy.
94
- setImmediate(onTimeout);
96
+ hasMicrotask = true;
97
} else {
98
try {
99
isFlushing = true;
100
_flushWork(true);
101
} finally {
102
+ hasMicrotask = false;
103
isFlushing = false;
104
}
105
}
@@ -153,6 +156,55 @@ describe('Scheduler', () => {
156
expect(flushWork(400)).toEqual(['D']);
157
});
158
159
+ it('flushes work until framesize reached', () => {
160
+ scheduleCallback(() => doWork('A1_100', 100));
161
+ scheduleCallback(() => doWork('A2_200', 200));
162
+ scheduleCallback(() => doWork('B1_100', 100));
163
+ scheduleCallback(() => doWork('B2_200', 200));
164
+ scheduleCallback(() => doWork('C1_300', 300));
165
+ scheduleCallback(() => doWork('C2_300', 300));
166
+ scheduleCallback(() => doWork('D_3000', 3000));
167
+ scheduleCallback(() => doWork('E1_300', 300));
168
+ scheduleCallback(() => doWork('E2_200', 200));
169
+ scheduleCallback(() => doWork('F1_200', 200));
170
+ scheduleCallback(() => doWork('F2_200', 200));
171
+ scheduleCallback(() => doWork('F3_300', 300));
172
+ scheduleCallback(() => doWork('F4_500', 500));
173
+ scheduleCallback(() => doWork('F5_200', 200));
174
+ scheduleCallback(() => doWork('F6_20', 20));
175
+
176
+ expect(Date.now()).toEqual(0);
177
+ // No time left after A1_100 and A2_200 are run
178
+ expect(flushWork(300)).toEqual(['A1_100', 'A2_200']);
179
+ expect(Date.now()).toEqual(300);
180
+ // B2_200 is started as there is still time left after B1_100
181
+ expect(flushWork(101)).toEqual(['B1_100', 'B2_200']);
182
+ expect(Date.now()).toEqual(600);
183
+ // C1_300 is started as there is even a little frame time
184
+ expect(flushWork(1)).toEqual(['C1_300']);
185
+ expect(Date.now()).toEqual(900);
186
+ // C2_300 is started even though there is no frame time
187
+ expect(flushWork(0)).toEqual(['C2_300']);
188
+ expect(Date.now()).toEqual(1200);
189
+ // D_3000 is very slow, but won't affect next flushes (if no
190
+ // timeouts happen)
191
+ expect(flushWork(100)).toEqual(['D_3000']);
192
+ expect(Date.now()).toEqual(4200);
193
+ expect(flushWork(400)).toEqual(['E1_300', 'E2_200']);
194
+ expect(Date.now()).toEqual(4700);
195
+ // Default timeout is 5000, so during F2_200, work will timeout and are done
196
+ // in reverse, including F2_200
197
+ expect(flushWork(1000)).toEqual([
198
+ 'F1_200',
199
+ 'F2_200',
200
+ 'F3_300',
201
+ 'F4_500',
202
+ 'F5_200',
203
+ 'F6_20',
204
+ ]);
205
+ expect(Date.now()).toEqual(6120);
206
+ });
207
+
208
it('cancels work', () => {
209
scheduleCallback(() => doWork('A', 100));
210
const callbackHandleB = scheduleCallback(() => doWork('B', 200));
@@ -246,7 +298,7 @@ describe('Scheduler', () => {
298
});
299
300
it('continuation callbacks inherit the expiration of the previous callback', () => {
249
- const tasks = [['A', 125], ['B', 125], ['C', 125], ['D', 125]];
301
+ const tasks = [['A', 125], ['B', 124], ['C', 100], ['D', 100]];
302
const work = deadline => {
303
while (tasks.length > 0) {
304
doWork(...tasks.shift());