Skip to content

Commit a8e9909

Browse files
committed
Use seeded random number generator
So if there's a failure, we can bisect.
1 parent b0e538b commit a8e9909

File tree

1 file changed

+25
-22
lines changed

1 file changed

+25
-22
lines changed

packages/react-reconciler/src/__tests__/ReactSuspenseFuzz-test.internal.js

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ let React;
22
let Suspense;
33
let ReactTestRenderer;
44
let ReactFeatureFlags;
5+
let Random;
6+
7+
const SEED = 0;
58

69
const prettyFormatPkg = require('pretty-format');
710

@@ -24,6 +27,7 @@ describe('ReactSuspenseFuzz', () => {
2427
React = require('react');
2528
Suspense = React.Suspense;
2629
ReactTestRenderer = require('react-test-renderer');
30+
Random = require('random-seed');
2731
});
2832

2933
function createFuzzer() {
@@ -171,29 +175,32 @@ describe('ReactSuspenseFuzz', () => {
171175
const expectedOutput = renderToRoot(expectedRoot, children, {
172176
shouldSuspend: false,
173177
});
178+
expectedRoot.unmount();
174179

175180
resetCache();
176181
const syncRoot = ReactTestRenderer.create(null);
177182
const syncOutput = renderToRoot(syncRoot, children);
178183
expect(syncOutput).toEqual(expectedOutput);
184+
syncRoot.unmount();
179185

180186
resetCache();
181187
const concurrentRoot = ReactTestRenderer.create(null, {
182188
unstable_isConcurrent: true,
183189
});
184190
const concurrentOutput = renderToRoot(concurrentRoot, children);
185191
expect(concurrentOutput).toEqual(expectedOutput);
192+
concurrentRoot.unmount();
193+
concurrentRoot.unstable_flushAll();
186194

187195
ReactTestRenderer.unstable_clearYields();
188196
}
189197

190-
function pickRandomWeighted(options) {
198+
function pickRandomWeighted(rand, options) {
191199
let totalWeight = 0;
192200
for (let i = 0; i < options.length; i++) {
193201
totalWeight += options[i].weight;
194202
}
195-
const randomNumber = Math.random() * totalWeight;
196-
let remainingWeight = randomNumber;
203+
let remainingWeight = rand.floatBetween(0, totalWeight);
197204
for (let i = 0; i < options.length; i++) {
198205
const {value, weight} = options[i];
199206
remainingWeight -= weight;
@@ -203,13 +210,7 @@ describe('ReactSuspenseFuzz', () => {
203210
}
204211
}
205212

206-
function randomInteger(min, max) {
207-
min = Math.ceil(min);
208-
max = Math.floor(max);
209-
return Math.floor(Math.random() * (max - min)) + min;
210-
}
211-
212-
function generateTestCase(numberOfElements) {
213+
function generateTestCase(rand, numberOfElements) {
213214
let remainingElements = numberOfElements;
214215

215216
function createRandomChild(hasSibling) {
@@ -223,13 +224,13 @@ describe('ReactSuspenseFuzz', () => {
223224
possibleActions.push({value: 'suspense', weight: 1});
224225
}
225226

226-
const action = pickRandomWeighted(possibleActions);
227+
const action = pickRandomWeighted(rand, possibleActions);
227228

228229
switch (action) {
229230
case 'text': {
230231
remainingElements--;
231232

232-
const numberOfUpdates = pickRandomWeighted([
233+
const numberOfUpdates = pickRandomWeighted(rand, [
233234
{value: 0, weight: 8},
234235
{value: 1, weight: 4},
235236
{value: 2, weight: 1},
@@ -238,21 +239,21 @@ describe('ReactSuspenseFuzz', () => {
238239
let updates = [];
239240
for (let i = 0; i < numberOfUpdates; i++) {
240241
updates.push({
241-
beginAfter: randomInteger(0, 10000),
242-
suspendFor: randomInteger(0, 10000),
242+
beginAfter: rand.intBetween(0, 10000),
243+
suspendFor: rand.intBetween(0, 10000),
243244
});
244245
}
245246

246247
return (
247248
<Text
248249
text={(remainingElements + 9).toString(36).toUpperCase()}
249-
initialDelay={randomInteger(0, 10000)}
250+
initialDelay={rand.intBetween(0, 10000)}
250251
updates={updates}
251252
/>
252253
);
253254
}
254255
case 'container': {
255-
const numberOfUpdates = pickRandomWeighted([
256+
const numberOfUpdates = pickRandomWeighted(rand, [
256257
{value: 0, weight: 8},
257258
{value: 1, weight: 4},
258259
{value: 2, weight: 1},
@@ -261,7 +262,7 @@ describe('ReactSuspenseFuzz', () => {
261262
let updates = [];
262263
for (let i = 0; i < numberOfUpdates; i++) {
263264
updates.push({
264-
remountAfter: randomInteger(0, 10000),
265+
remountAfter: rand.intBetween(0, 10000),
265266
});
266267
}
267268

@@ -273,12 +274,12 @@ describe('ReactSuspenseFuzz', () => {
273274
remainingElements--;
274275
const children = createRandomChildren(3);
275276

276-
const maxDuration = pickRandomWeighted([
277+
const maxDuration = pickRandomWeighted(rand, [
277278
{value: undefined, weight: 1},
278-
{value: randomInteger(0, 5000), weight: 1},
279+
{value: rand.intBetween(0, 5000), weight: 1},
279280
]);
280281

281-
const fallbackType = pickRandomWeighted([
282+
const fallbackType = pickRandomWeighted(rand, [
282283
{value: 'none', weight: 1},
283284
{value: 'normal', weight: 1},
284285
{value: 'nested suspense', weight: 1},
@@ -361,11 +362,13 @@ describe('ReactSuspenseFuzz', () => {
361362
it('generative tests', () => {
362363
const {generateTestCase, testResolvedOutput} = createFuzzer();
363364

365+
const rand = Random.create(SEED);
366+
364367
const NUMBER_OF_TEST_CASES = 500;
365-
const ELEMENTS_PER_CASE = 8;
368+
const ELEMENTS_PER_CASE = 12;
366369

367370
for (let i = 0; i < NUMBER_OF_TEST_CASES; i++) {
368-
const randomTestCase = generateTestCase(ELEMENTS_PER_CASE);
371+
const randomTestCase = generateTestCase(rand, ELEMENTS_PER_CASE);
369372
try {
370373
testResolvedOutput(randomTestCase);
371374
} catch (e) {

0 commit comments

Comments
 (0)