forked from facebook/react
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathReactExpiration-test.js
801 lines (705 loc) · 24.8 KB
/
ReactExpiration-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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @jest-environment node
*/
'use strict';
let React;
let ReactNoop;
let Scheduler;
let readText;
let resolveText;
describe('ReactExpiration', () => {
beforeEach(() => {
jest.resetModules();
React = require('react');
ReactNoop = require('react-noop-renderer');
Scheduler = require('scheduler');
const textCache = new Map();
readText = text => {
const record = textCache.get(text);
if (record !== undefined) {
switch (record.status) {
case 'pending':
throw record.promise;
case 'rejected':
throw Error('Failed to load: ' + text);
case 'resolved':
return text;
}
} else {
let ping;
const promise = new Promise(resolve => (ping = resolve));
const newRecord = {
status: 'pending',
ping: ping,
promise,
};
textCache.set(text, newRecord);
throw promise;
}
};
resolveText = text => {
const record = textCache.get(text);
if (record !== undefined) {
if (record.status === 'pending') {
Scheduler.unstable_yieldValue(`Promise resolved [${text}]`);
record.ping();
record.ping = null;
record.status = 'resolved';
clearTimeout(record.promise._timer);
record.promise = null;
}
} else {
const newRecord = {
ping: null,
status: 'resolved',
promise: null,
};
textCache.set(text, newRecord);
}
};
});
function Text(props) {
Scheduler.unstable_yieldValue(props.text);
return props.text;
}
function AsyncText(props) {
const text = props.text;
try {
readText(text);
Scheduler.unstable_yieldValue(text);
return text;
} catch (promise) {
if (typeof promise.then === 'function') {
Scheduler.unstable_yieldValue(`Suspend! [${text}]`);
if (typeof props.ms === 'number' && promise._timer === undefined) {
promise._timer = setTimeout(() => {
resolveText(text);
}, props.ms);
}
} else {
Scheduler.unstable_yieldValue(`Error! [${text}]`);
}
throw promise;
}
}
function span(prop) {
return {type: 'span', children: [], prop, hidden: false};
}
it('increases priority of updates as time progresses', () => {
ReactNoop.render(<span prop="done" />);
expect(ReactNoop.getChildren()).toEqual([]);
// Nothing has expired yet because time hasn't advanced.
ReactNoop.flushExpired();
expect(ReactNoop.getChildren()).toEqual([]);
// Advance time a bit, but not enough to expire the low pri update.
ReactNoop.expire(4500);
ReactNoop.flushExpired();
expect(ReactNoop.getChildren()).toEqual([]);
// Advance by another second. Now the update should expire and flush.
ReactNoop.expire(1000);
ReactNoop.flushExpired();
expect(ReactNoop.getChildren()).toEqual([span('done')]);
});
it('two updates of like priority in the same event always flush within the same batch', () => {
class TextClass extends React.Component {
componentDidMount() {
Scheduler.unstable_yieldValue(`${this.props.text} [commit]`);
}
componentDidUpdate() {
Scheduler.unstable_yieldValue(`${this.props.text} [commit]`);
}
render() {
Scheduler.unstable_yieldValue(`${this.props.text} [render]`);
return <span prop={this.props.text} />;
}
}
function interrupt() {
ReactNoop.flushSync(() => {
ReactNoop.renderToRootWithID(null, 'other-root');
});
}
// First, show what happens for updates in two separate events.
// Schedule an update.
ReactNoop.render(<TextClass text="A" />);
// Advance the timer.
Scheduler.unstable_advanceTime(2000);
// Partially flush the first update, then interrupt it.
expect(Scheduler).toFlushAndYieldThrough(['A [render]']);
interrupt();
// Don't advance time by enough to expire the first update.
expect(Scheduler).toHaveYielded([]);
expect(ReactNoop.getChildren()).toEqual([]);
// Schedule another update.
ReactNoop.render(<TextClass text="B" />);
// Both updates are batched
expect(Scheduler).toFlushAndYield(['B [render]', 'B [commit]']);
expect(ReactNoop.getChildren()).toEqual([span('B')]);
// Now do the same thing again, except this time don't flush any work in
// between the two updates.
ReactNoop.render(<TextClass text="A" />);
Scheduler.unstable_advanceTime(2000);
expect(Scheduler).toHaveYielded([]);
expect(ReactNoop.getChildren()).toEqual([span('B')]);
// Schedule another update.
ReactNoop.render(<TextClass text="B" />);
// The updates should flush in the same batch, since as far as the scheduler
// knows, they may have occurred inside the same event.
expect(Scheduler).toFlushAndYield(['B [render]', 'B [commit]']);
});
it(
'two updates of like priority in the same event always flush within the ' +
"same batch, even if there's a sync update in between",
() => {
class TextClass extends React.Component {
componentDidMount() {
Scheduler.unstable_yieldValue(`${this.props.text} [commit]`);
}
componentDidUpdate() {
Scheduler.unstable_yieldValue(`${this.props.text} [commit]`);
}
render() {
Scheduler.unstable_yieldValue(`${this.props.text} [render]`);
return <span prop={this.props.text} />;
}
}
function interrupt() {
ReactNoop.flushSync(() => {
ReactNoop.renderToRootWithID(null, 'other-root');
});
}
// First, show what happens for updates in two separate events.
// Schedule an update.
ReactNoop.render(<TextClass text="A" />);
// Advance the timer.
Scheduler.unstable_advanceTime(2000);
// Partially flush the first update, then interrupt it.
expect(Scheduler).toFlushAndYieldThrough(['A [render]']);
interrupt();
// Don't advance time by enough to expire the first update.
expect(Scheduler).toHaveYielded([]);
expect(ReactNoop.getChildren()).toEqual([]);
// Schedule another update.
ReactNoop.render(<TextClass text="B" />);
// Both updates are batched
expect(Scheduler).toFlushAndYield(['B [render]', 'B [commit]']);
expect(ReactNoop.getChildren()).toEqual([span('B')]);
// Now do the same thing again, except this time don't flush any work in
// between the two updates.
ReactNoop.render(<TextClass text="A" />);
Scheduler.unstable_advanceTime(2000);
expect(Scheduler).toHaveYielded([]);
expect(ReactNoop.getChildren()).toEqual([span('B')]);
// Perform some synchronous work. The scheduler must assume we're inside
// the same event.
interrupt();
// Schedule another update.
ReactNoop.render(<TextClass text="B" />);
// The updates should flush in the same batch, since as far as the scheduler
// knows, they may have occurred inside the same event.
expect(Scheduler).toFlushAndYield(['B [render]', 'B [commit]']);
},
);
it('cannot update at the same expiration time that is already rendering', () => {
const store = {text: 'initial'};
const subscribers = [];
class Connected extends React.Component {
state = {text: store.text};
componentDidMount() {
subscribers.push(this);
Scheduler.unstable_yieldValue(
`${this.state.text} [${this.props.label}] [commit]`,
);
}
componentDidUpdate() {
Scheduler.unstable_yieldValue(
`${this.state.text} [${this.props.label}] [commit]`,
);
}
render() {
Scheduler.unstable_yieldValue(
`${this.state.text} [${this.props.label}] [render]`,
);
return <span prop={this.state.text} />;
}
}
function App() {
return (
<>
<Connected label="A" />
<Connected label="B" />
<Connected label="C" />
<Connected label="D" />
</>
);
}
// Initial mount
ReactNoop.render(<App />);
expect(Scheduler).toFlushAndYield([
'initial [A] [render]',
'initial [B] [render]',
'initial [C] [render]',
'initial [D] [render]',
'initial [A] [commit]',
'initial [B] [commit]',
'initial [C] [commit]',
'initial [D] [commit]',
]);
// Partial update
subscribers.forEach(s => s.setState({text: '1'}));
expect(Scheduler).toFlushAndYieldThrough([
'1 [A] [render]',
'1 [B] [render]',
]);
// Before the update can finish, update again. Even though no time has
// advanced, this update should be given a different expiration time than
// the currently rendering one. So, C and D should render with 1, not 2.
subscribers.forEach(s => s.setState({text: '2'}));
expect(Scheduler).toFlushAndYieldThrough([
'1 [C] [render]',
'1 [D] [render]',
]);
});
it('stops yielding if CPU-bound update takes too long to finish', () => {
const root = ReactNoop.createRoot();
function App() {
return (
<>
<Text text="A" />
<Text text="B" />
<Text text="C" />
<Text text="D" />
<Text text="E" />
</>
);
}
root.render(<App />);
expect(Scheduler).toFlushAndYieldThrough(['A']);
expect(Scheduler).toFlushAndYieldThrough(['B']);
expect(Scheduler).toFlushAndYieldThrough(['C']);
Scheduler.unstable_advanceTime(10000);
expect(Scheduler).toFlushExpired(['D', 'E']);
expect(root).toMatchRenderedOutput('ABCDE');
});
it('root expiration is measured from the time of the first update', () => {
Scheduler.unstable_advanceTime(10000);
const root = ReactNoop.createRoot();
function App() {
return (
<>
<Text text="A" />
<Text text="B" />
<Text text="C" />
<Text text="D" />
<Text text="E" />
</>
);
}
root.render(<App />);
expect(Scheduler).toFlushAndYieldThrough(['A']);
expect(Scheduler).toFlushAndYieldThrough(['B']);
expect(Scheduler).toFlushAndYieldThrough(['C']);
Scheduler.unstable_advanceTime(10000);
expect(Scheduler).toFlushExpired(['D', 'E']);
expect(root).toMatchRenderedOutput('ABCDE');
});
it('should measure expiration times relative to module initialization', () => {
// Tests an implementation detail where expiration times are computed using
// bitwise operations.
jest.resetModules();
Scheduler = require('scheduler');
// Before importing the renderer, advance the current time by a number
// larger than the maximum allowed for bitwise operations.
const maxSigned31BitInt = 1073741823;
Scheduler.unstable_advanceTime(maxSigned31BitInt * 100);
// Now import the renderer. On module initialization, it will read the
// current time.
ReactNoop = require('react-noop-renderer');
ReactNoop.render('Hi');
// The update should not have expired yet.
expect(Scheduler).toFlushExpired([]);
expect(ReactNoop).toMatchRenderedOutput(null);
// Advance the time some more to expire the update.
Scheduler.unstable_advanceTime(10000);
expect(Scheduler).toFlushExpired([]);
expect(ReactNoop).toMatchRenderedOutput('Hi');
});
it('should measure callback timeout relative to current time, not start-up time', () => {
// Corresponds to a bugfix: https://github.com/facebook/react/pull/15479
// The bug wasn't caught by other tests because we use virtual times that
// default to 0, and most tests don't advance time.
// Before scheduling an update, advance the current time.
Scheduler.unstable_advanceTime(10000);
ReactNoop.render('Hi');
expect(Scheduler).toFlushExpired([]);
expect(ReactNoop).toMatchRenderedOutput(null);
// Advancing by ~5 seconds should be sufficient to expire the update. (I
// used a slightly larger number to allow for possible rounding.)
Scheduler.unstable_advanceTime(6000);
ReactNoop.render('Hi');
expect(Scheduler).toFlushExpired([]);
expect(ReactNoop).toMatchRenderedOutput('Hi');
});
it('prevents starvation by high priority updates', async () => {
const {useState} = React;
let updateHighPri;
let updateNormalPri;
function App() {
const [highPri, setHighPri] = useState(0);
const [normalPri, setNormalPri] = useState(0);
updateHighPri = () =>
Scheduler.unstable_runWithPriority(
Scheduler.unstable_UserBlockingPriority,
() => setHighPri(n => n + 1),
);
updateNormalPri = () => setNormalPri(n => n + 1);
return (
<>
<Text text={'High pri: ' + highPri} />
{', '}
<Text text={'Normal pri: ' + normalPri} />
</>
);
}
const root = ReactNoop.createRoot();
await ReactNoop.act(async () => {
root.render(<App />);
});
expect(Scheduler).toHaveYielded(['High pri: 0', 'Normal pri: 0']);
expect(root).toMatchRenderedOutput('High pri: 0, Normal pri: 0');
// First demonstrate what happens when there's no starvation
await ReactNoop.act(async () => {
updateNormalPri();
expect(Scheduler).toFlushAndYieldThrough(['High pri: 0']);
updateHighPri();
});
expect(Scheduler).toHaveYielded([
// Interrupt high pri update to render sync update
'High pri: 1',
'Normal pri: 0',
// Now render normal pri
'High pri: 1',
'Normal pri: 1',
]);
expect(root).toMatchRenderedOutput('High pri: 1, Normal pri: 1');
// Do the same thing, but starve the first update
await ReactNoop.act(async () => {
updateNormalPri();
expect(Scheduler).toFlushAndYieldThrough(['High pri: 1']);
// This time, a lot of time has elapsed since the normal pri update
// started rendering. (This should advance time by some number that's
// definitely bigger than the constant heuristic we use to detect
// starvation of normal priority updates.)
Scheduler.unstable_advanceTime(10000);
// So when we get a high pri update, we shouldn't interrupt
updateHighPri();
});
expect(Scheduler).toHaveYielded([
// Finish normal pri update
'Normal pri: 2',
// Then do high pri update
'High pri: 2',
'Normal pri: 2',
]);
expect(root).toMatchRenderedOutput('High pri: 2, Normal pri: 2');
});
it('prevents starvation by sync updates', async () => {
const {useState} = React;
let updateSyncPri;
let updateHighPri;
function App() {
const [syncPri, setSyncPri] = useState(0);
const [highPri, setHighPri] = useState(0);
updateSyncPri = () => ReactNoop.flushSync(() => setSyncPri(n => n + 1));
updateHighPri = () =>
Scheduler.unstable_runWithPriority(
Scheduler.unstable_UserBlockingPriority,
() => setHighPri(n => n + 1),
);
return (
<>
<Text text={'Sync pri: ' + syncPri} />
{', '}
<Text text={'High pri: ' + highPri} />
</>
);
}
const root = ReactNoop.createRoot();
await ReactNoop.act(async () => {
root.render(<App />);
});
expect(Scheduler).toHaveYielded(['Sync pri: 0', 'High pri: 0']);
expect(root).toMatchRenderedOutput('Sync pri: 0, High pri: 0');
// First demonstrate what happens when there's no starvation
await ReactNoop.act(async () => {
updateHighPri();
expect(Scheduler).toFlushAndYieldThrough(['Sync pri: 0']);
updateSyncPri();
});
expect(Scheduler).toHaveYielded([
// Interrupt high pri update to render sync update
'Sync pri: 1',
'High pri: 0',
// Now render high pri
'Sync pri: 1',
'High pri: 1',
]);
expect(root).toMatchRenderedOutput('Sync pri: 1, High pri: 1');
// Do the same thing, but starve the first update
await ReactNoop.act(async () => {
updateHighPri();
expect(Scheduler).toFlushAndYieldThrough(['Sync pri: 1']);
// This time, a lot of time has elapsed since the high pri update started
// rendering. (This should advance time by some number that's definitely
// bigger than the constant heuristic we use to detect starvation of user
// interactions, but not as high as the onse used for normal pri updates.)
Scheduler.unstable_advanceTime(1500);
// So when we get a sync update, we shouldn't interrupt
updateSyncPri();
});
expect(Scheduler).toHaveYielded([
// Finish high pri update
'High pri: 2',
// Then do sync update
'Sync pri: 2',
'High pri: 2',
]);
expect(root).toMatchRenderedOutput('Sync pri: 2, High pri: 2');
});
it('idle work never expires', async () => {
const {useState} = React;
let updateSyncPri;
let updateIdlePri;
function App() {
const [syncPri, setSyncPri] = useState(0);
const [highPri, setIdlePri] = useState(0);
updateSyncPri = () => ReactNoop.flushSync(() => setSyncPri(n => n + 1));
updateIdlePri = () =>
Scheduler.unstable_runWithPriority(
Scheduler.unstable_IdlePriority,
() => setIdlePri(n => n + 1),
);
return (
<>
<Text text={'Sync pri: ' + syncPri} />
{', '}
<Text text={'Idle pri: ' + highPri} />
</>
);
}
const root = ReactNoop.createRoot();
await ReactNoop.act(async () => {
root.render(<App />);
});
expect(Scheduler).toHaveYielded(['Sync pri: 0', 'Idle pri: 0']);
expect(root).toMatchRenderedOutput('Sync pri: 0, Idle pri: 0');
// First demonstrate what happens when there's no starvation
await ReactNoop.act(async () => {
updateIdlePri();
expect(Scheduler).toFlushAndYieldThrough(['Sync pri: 0']);
updateSyncPri();
});
expect(Scheduler).toHaveYielded([
// Interrupt idle update to render sync update
'Sync pri: 1',
'Idle pri: 0',
// Now render idle
'Sync pri: 1',
'Idle pri: 1',
]);
expect(root).toMatchRenderedOutput('Sync pri: 1, Idle pri: 1');
// Do the same thing, but starve the first update
await ReactNoop.act(async () => {
updateIdlePri();
expect(Scheduler).toFlushAndYieldThrough(['Sync pri: 1']);
// Advance a ridiculously large amount of time to demonstrate that the
// idle work never expires
Scheduler.unstable_advanceTime(100000);
updateSyncPri();
});
// Same thing should happen as last time
expect(Scheduler).toHaveYielded([
// Interrupt idle update to render sync update
'Sync pri: 2',
'Idle pri: 1',
// Now render idle
'Sync pri: 2',
'Idle pri: 2',
]);
expect(root).toMatchRenderedOutput('Sync pri: 2, Idle pri: 2');
});
it('a single update can expire without forcing all other updates to expire', async () => {
const {useState} = React;
let updateHighPri;
let updateNormalPri;
function App() {
const [highPri, setHighPri] = useState(0);
const [normalPri, setNormalPri] = useState(0);
updateHighPri = () =>
Scheduler.unstable_runWithPriority(
Scheduler.unstable_UserBlockingPriority,
() => setHighPri(n => n + 1),
);
updateNormalPri = () => setNormalPri(n => n + 1);
return (
<>
<Text text={'High pri: ' + highPri} />
{', '}
<Text text={'Normal pri: ' + normalPri} />
{', '}
<Text text="Sibling" />
</>
);
}
const root = ReactNoop.createRoot();
await ReactNoop.act(async () => {
root.render(<App />);
});
expect(Scheduler).toHaveYielded([
'High pri: 0',
'Normal pri: 0',
'Sibling',
]);
expect(root).toMatchRenderedOutput('High pri: 0, Normal pri: 0, Sibling');
await ReactNoop.act(async () => {
// Partially render an update
updateNormalPri();
expect(Scheduler).toFlushAndYieldThrough(['High pri: 0']);
// Some time goes by. In an interleaved event, schedule another update.
// This will be placed into a separate batch.
Scheduler.unstable_advanceTime(4000);
updateNormalPri();
// Keep rendering the first update
expect(Scheduler).toFlushAndYieldThrough(['Normal pri: 1']);
// More time goes by. Enough to expire the first batch, but not the
// second one.
Scheduler.unstable_advanceTime(1000);
// Attempt to interrupt with a high pri update.
updateHighPri();
// The first update expired, so first will finish it without interrupting.
// But not the second update, which hasn't expired yet.
expect(Scheduler).toFlushExpired(['Sibling']);
});
expect(Scheduler).toHaveYielded([
// Then render the high pri update
'High pri: 1',
'Normal pri: 1',
'Sibling',
// Then the second normal pri update
'High pri: 1',
'Normal pri: 2',
'Sibling',
]);
});
it('detects starvation in multiple batches', async () => {
const {useState} = React;
let updateHighPri;
let updateNormalPri;
function App() {
const [highPri, setHighPri] = useState(0);
const [normalPri, setNormalPri] = useState(0);
updateHighPri = () =>
Scheduler.unstable_runWithPriority(
Scheduler.unstable_UserBlockingPriority,
() => setHighPri(n => n + 1),
);
updateNormalPri = () => setNormalPri(n => n + 1);
return (
<>
<Text text={'High pri: ' + highPri} />
{', '}
<Text text={'Normal pri: ' + normalPri} />
{', '}
<Text text="Sibling" />
</>
);
}
const root = ReactNoop.createRoot();
await ReactNoop.act(async () => {
root.render(<App />);
});
expect(Scheduler).toHaveYielded([
'High pri: 0',
'Normal pri: 0',
'Sibling',
]);
expect(root).toMatchRenderedOutput('High pri: 0, Normal pri: 0, Sibling');
await ReactNoop.act(async () => {
// Partially render an update
updateNormalPri();
expect(Scheduler).toFlushAndYieldThrough(['High pri: 0']);
// Some time goes by. In an interleaved event, schedule another update.
// This will be placed into a separate batch.
Scheduler.unstable_advanceTime(4000);
updateNormalPri();
// Keep rendering the first update
expect(Scheduler).toFlushAndYieldThrough(['Normal pri: 1']);
// More time goes by. This expires both of the updates just scheduled.
Scheduler.unstable_advanceTime(10000);
// Attempt to interrupt with a high pri update.
updateHighPri();
// Both normal pri updates should have expired.
expect(Scheduler).toFlushExpired([
'Sibling',
// Notice that the high pri update didn't flush yet. Expiring one lane
// doesn't affect other lanes. (Unless they are intentionally entangled,
// like we do for overlapping transitions that affect the same state.)
'High pri: 0',
'Normal pri: 2',
'Sibling',
]);
});
});
it('updates do not expire while they are IO-bound', async () => {
const {Suspense} = React;
function App({text}) {
return (
<Suspense fallback={<Text text="Loading..." />}>
<AsyncText text={text} />
{', '}
<Text text="Sibling" />
</Suspense>
);
}
const root = ReactNoop.createRoot();
await ReactNoop.act(async () => {
await resolveText('A');
root.render(<App text="A" />);
});
expect(Scheduler).toHaveYielded(['A', 'Sibling']);
expect(root).toMatchRenderedOutput('A, Sibling');
await ReactNoop.act(async () => {
root.render(<App text="B" />);
expect(Scheduler).toFlushAndYield([
'Suspend! [B]',
'Sibling',
'Loading...',
]);
// Lots of time elapses before the promise resolves
Scheduler.unstable_advanceTime(10000);
await resolveText('B');
expect(Scheduler).toHaveYielded(['Promise resolved [B]']);
// But the update doesn't expire, because it was IO bound. So we can
// partially rendering without finishing.
expect(Scheduler).toFlushAndYieldThrough(['B']);
expect(root).toMatchRenderedOutput('A, Sibling');
// Lots more time elapses. We're CPU-bound now, so we should treat this
// as starvation.
Scheduler.unstable_advanceTime(10000);
// Attempt to interrupt with a sync update.
ReactNoop.flushSync(() => root.render(<App text="A" />));
expect(Scheduler).toHaveYielded([
// Because the previous update had already expired, we don't interrupt
// it. Finish rendering it first.
'Sibling',
// Then do the sync update.
'A',
'Sibling',
]);
});
});
});