|
| 1 | +let React; |
| 2 | +let ReactTestRenderer; |
| 3 | +let ReactFeatureFlags; |
| 4 | +let originalConsoleError; |
| 5 | + |
| 6 | +// const prettyFormatPkg = require('pretty-format'); |
| 7 | +// function prettyFormat(thing) { |
| 8 | +// prettyFormatPkg(thing, { |
| 9 | +// plugins: [ |
| 10 | +// prettyFormatPkg.plugins.ReactElement, |
| 11 | +// prettyFormatPkg.plugins.ReactTestComponent, |
| 12 | +// ], |
| 13 | +// }); |
| 14 | +// } |
| 15 | + |
| 16 | +describe('ReactSuspenseFuzz', () => { |
| 17 | + beforeEach(() => { |
| 18 | + jest.resetModules(); |
| 19 | + ReactFeatureFlags = require('shared/ReactFeatureFlags'); |
| 20 | + ReactFeatureFlags.debugRenderPhaseSideEffectsForStrictMode = false; |
| 21 | + ReactFeatureFlags.replayFailedUnitOfWorkWithInvokeGuardedCallback = false; |
| 22 | + ReactFeatureFlags.enableHooks = true; |
| 23 | + React = require('react'); |
| 24 | + ReactTestRenderer = require('react-test-renderer'); |
| 25 | + |
| 26 | + originalConsoleError = console.error; |
| 27 | + console.error = (msg, ...rest) => { |
| 28 | + if (msg.includes('update on an unmounted component')) { |
| 29 | + // Suppress this warning. I think my components are correct, but there's |
| 30 | + // this thing with Jest timers where if you advance time, then clear a |
| 31 | + // timeout in one of the affected timers, but that timer was already |
| 32 | + // about to fire, it doesn't clear. Regardless, if this warning fires it |
| 33 | + // doesn't affect the correctness of the thing we're actually testing. |
| 34 | + return; |
| 35 | + } |
| 36 | + originalConsoleError(msg, ...rest); |
| 37 | + }; |
| 38 | + }); |
| 39 | + |
| 40 | + afterEach(() => { |
| 41 | + console.error = originalConsoleError; |
| 42 | + }); |
| 43 | + |
| 44 | + function createFuzzer() { |
| 45 | + const {Suspense, useState, useLayoutEffect} = React; |
| 46 | + |
| 47 | + let shouldSuspend; |
| 48 | + let pendingTasks; |
| 49 | + let cache; |
| 50 | + |
| 51 | + function Container({children, updates}) { |
| 52 | + const [step, setStep] = useState(0); |
| 53 | + |
| 54 | + useLayoutEffect( |
| 55 | + () => { |
| 56 | + if (updates !== undefined) { |
| 57 | + const cleanUps = new Set(); |
| 58 | + updates.forEach(({remountAfter}, i) => { |
| 59 | + const task = { |
| 60 | + label: `Remount childen after ${remountAfter}ms`, |
| 61 | + }; |
| 62 | + const timeoutID = setTimeout(() => { |
| 63 | + pendingTasks.delete(task); |
| 64 | + setStep(i + 1); |
| 65 | + }, remountAfter); |
| 66 | + pendingTasks.add(task); |
| 67 | + cleanUps.add(() => { |
| 68 | + pendingTasks.delete(task); |
| 69 | + clearTimeout(timeoutID); |
| 70 | + }); |
| 71 | + }); |
| 72 | + return () => { |
| 73 | + cleanUps.forEach(cleanUp => cleanUp()); |
| 74 | + }; |
| 75 | + } |
| 76 | + }, |
| 77 | + [updates], |
| 78 | + ); |
| 79 | + |
| 80 | + return <React.Fragment key={step}>{children}</React.Fragment>; |
| 81 | + } |
| 82 | + |
| 83 | + function Text({text, initialDelay, updates}) { |
| 84 | + const [[step, delay], setStep] = useState([0, initialDelay]); |
| 85 | + |
| 86 | + useLayoutEffect( |
| 87 | + () => { |
| 88 | + if (updates !== undefined) { |
| 89 | + const cleanUps = new Set(); |
| 90 | + updates.forEach(({beginAfter, suspendFor}, i) => { |
| 91 | + const task = { |
| 92 | + label: `Update ${beginAfter}ms after mount and suspend for ${suspendFor}ms`, |
| 93 | + }; |
| 94 | + const timeoutID = setTimeout(() => { |
| 95 | + pendingTasks.delete(task); |
| 96 | + setStep([i + 1, suspendFor]); |
| 97 | + }, beginAfter); |
| 98 | + pendingTasks.add(task); |
| 99 | + cleanUps.add(() => { |
| 100 | + pendingTasks.delete(task); |
| 101 | + clearTimeout(timeoutID); |
| 102 | + }); |
| 103 | + }); |
| 104 | + return () => { |
| 105 | + cleanUps.forEach(cleanUp => cleanUp()); |
| 106 | + }; |
| 107 | + } |
| 108 | + }, |
| 109 | + [updates], |
| 110 | + ); |
| 111 | + |
| 112 | + const fullText = updates === undefined ? text : `${text} [${step}]`; |
| 113 | + |
| 114 | + if (shouldSuspend) { |
| 115 | + const resolvedText = cache.get(fullText); |
| 116 | + if (resolvedText === undefined) { |
| 117 | + const thenable = { |
| 118 | + then(resolve) { |
| 119 | + const task = {label: `Suspended ${resolvedText}]`}; |
| 120 | + pendingTasks.add(task); |
| 121 | + setTimeout(() => { |
| 122 | + cache.set(fullText, fullText); |
| 123 | + pendingTasks.delete(task); |
| 124 | + resolve(); |
| 125 | + }, delay); |
| 126 | + }, |
| 127 | + }; |
| 128 | + cache.set(fullText, thenable); |
| 129 | + throw thenable; |
| 130 | + } else if (resolvedText.then === 'function') { |
| 131 | + const thenable = resolvedText; |
| 132 | + throw thenable; |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + return fullText; |
| 137 | + } |
| 138 | + |
| 139 | + function renderToRoot(root, children) { |
| 140 | + pendingTasks = new Set(); |
| 141 | + cache = new Map(); |
| 142 | + |
| 143 | + root.update(children); |
| 144 | + root.unstable_flushAll(); |
| 145 | + |
| 146 | + let elapsedTime = 0; |
| 147 | + while (pendingTasks && pendingTasks.size > 0) { |
| 148 | + if ((elapsedTime += 10) > 1000000) { |
| 149 | + throw new Error('Something did not resolve properly.'); |
| 150 | + } |
| 151 | + jest.advanceTimersByTime(10); |
| 152 | + root.unstable_flushAll(); |
| 153 | + } |
| 154 | + |
| 155 | + return root.toJSON(); |
| 156 | + } |
| 157 | + |
| 158 | + function testResolvedOutput(unwrappedChildren) { |
| 159 | + const children = ( |
| 160 | + <Suspense fallback="Loading...">{unwrappedChildren}</Suspense> |
| 161 | + ); |
| 162 | + |
| 163 | + shouldSuspend = false; |
| 164 | + const expectedRoot = ReactTestRenderer.create(null); |
| 165 | + const expectedOutput = renderToRoot(expectedRoot, children); |
| 166 | + |
| 167 | + shouldSuspend = true; |
| 168 | + const syncRoot = ReactTestRenderer.create(null); |
| 169 | + const syncOutput = renderToRoot(syncRoot, children); |
| 170 | + expect(syncOutput).toEqual(expectedOutput); |
| 171 | + |
| 172 | + const concurrentRoot = ReactTestRenderer.create(null, { |
| 173 | + unstable_isConcurrent: true, |
| 174 | + }); |
| 175 | + const concurrentOutput = renderToRoot(concurrentRoot, children); |
| 176 | + expect(concurrentOutput).toEqual(expectedOutput); |
| 177 | + } |
| 178 | + |
| 179 | + function pickRandomWeighted(options) { |
| 180 | + let totalWeight = 0; |
| 181 | + for (let i = 0; i < options.length; i++) { |
| 182 | + totalWeight += options[i].weight; |
| 183 | + } |
| 184 | + const randomNumber = Math.random() * totalWeight; |
| 185 | + let remainingWeight = randomNumber; |
| 186 | + for (let i = 0; i < options.length; i++) { |
| 187 | + const {value, weight} = options[i]; |
| 188 | + remainingWeight -= weight; |
| 189 | + if (remainingWeight <= 0) { |
| 190 | + return value; |
| 191 | + } |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + function randomInteger(min, max) { |
| 196 | + min = Math.ceil(min); |
| 197 | + max = Math.floor(max); |
| 198 | + return Math.floor(Math.random() * (max - min)) + min; |
| 199 | + } |
| 200 | + |
| 201 | + function generateTestCase(numberOfElements) { |
| 202 | + let remainingElements = numberOfElements; |
| 203 | + |
| 204 | + function createRandomChild(hasSibling) { |
| 205 | + const possibleActions = [ |
| 206 | + {value: 'return', weight: 1}, |
| 207 | + {value: 'text', weight: 1}, |
| 208 | + ]; |
| 209 | + |
| 210 | + if (hasSibling) { |
| 211 | + possibleActions.push({value: 'container', weight: 1}); |
| 212 | + possibleActions.push({value: 'suspense', weight: 1}); |
| 213 | + } |
| 214 | + |
| 215 | + const action = pickRandomWeighted(possibleActions); |
| 216 | + |
| 217 | + switch (action) { |
| 218 | + case 'text': { |
| 219 | + remainingElements--; |
| 220 | + |
| 221 | + const numberOfUpdates = pickRandomWeighted([ |
| 222 | + {value: 0, weight: 8}, |
| 223 | + {value: 1, weight: 4}, |
| 224 | + {value: 2, weight: 1}, |
| 225 | + ]); |
| 226 | + |
| 227 | + let updates = []; |
| 228 | + for (let i = 0; i < numberOfUpdates; i++) { |
| 229 | + updates.push({ |
| 230 | + beginAfter: randomInteger(0, 10000), |
| 231 | + suspendFor: randomInteger(0, 10000), |
| 232 | + }); |
| 233 | + } |
| 234 | + |
| 235 | + return ( |
| 236 | + <Text |
| 237 | + text={(remainingElements + 9).toString(36).toUpperCase()} |
| 238 | + initialDelay={randomInteger(0, 10000)} |
| 239 | + updates={updates} |
| 240 | + /> |
| 241 | + ); |
| 242 | + } |
| 243 | + case 'container': { |
| 244 | + const numberOfUpdates = pickRandomWeighted([ |
| 245 | + {value: 0, weight: 8}, |
| 246 | + {value: 1, weight: 4}, |
| 247 | + {value: 2, weight: 1}, |
| 248 | + ]); |
| 249 | + |
| 250 | + let updates = []; |
| 251 | + for (let i = 0; i < numberOfUpdates; i++) { |
| 252 | + updates.push({ |
| 253 | + remountAfter: randomInteger(0, 10000), |
| 254 | + }); |
| 255 | + } |
| 256 | + |
| 257 | + remainingElements--; |
| 258 | + const children = createRandomChildren(3); |
| 259 | + return React.createElement(Container, {updates}, ...children); |
| 260 | + } |
| 261 | + case 'suspense': { |
| 262 | + remainingElements--; |
| 263 | + const children = createRandomChildren(3); |
| 264 | + |
| 265 | + const maxDuration = pickRandomWeighted([ |
| 266 | + {value: undefined, weight: 1}, |
| 267 | + {value: randomInteger(0, 5000), weight: 1}, |
| 268 | + ]); |
| 269 | + |
| 270 | + return React.createElement(Suspense, {maxDuration}, ...children); |
| 271 | + } |
| 272 | + case 'return': |
| 273 | + default: |
| 274 | + return null; |
| 275 | + } |
| 276 | + } |
| 277 | + |
| 278 | + function createRandomChildren(limit) { |
| 279 | + const children = []; |
| 280 | + while (remainingElements > 0 && children.length < limit) { |
| 281 | + children.push(createRandomChild(children.length > 0)); |
| 282 | + } |
| 283 | + return children; |
| 284 | + } |
| 285 | + |
| 286 | + const children = createRandomChildren(Infinity); |
| 287 | + return React.createElement(React.Fragment, null, ...children); |
| 288 | + } |
| 289 | + |
| 290 | + return {Container, Text, testResolvedOutput, generateTestCase}; |
| 291 | + } |
| 292 | + |
| 293 | + it('basic cases', () => { |
| 294 | + const {Container, Text, testResolvedOutput} = createFuzzer(); |
| 295 | + testResolvedOutput( |
| 296 | + <Container updates={[{remountAfter: 150}]}> |
| 297 | + <Text |
| 298 | + text="Hi" |
| 299 | + initialDelay={2000} |
| 300 | + updates={[{beginAfter: 100, suspendFor: 200}]} |
| 301 | + /> |
| 302 | + </Container>, |
| 303 | + ); |
| 304 | + }); |
| 305 | + |
| 306 | + it('generative tests', () => { |
| 307 | + const {generateTestCase, testResolvedOutput} = createFuzzer(); |
| 308 | + |
| 309 | + const NUMBER_OF_TEST_CASES = 500; |
| 310 | + const ELEMENTS_PER_CASE = 8; |
| 311 | + |
| 312 | + for (let i = 0; i < NUMBER_OF_TEST_CASES; i++) { |
| 313 | + const randomTestCase = generateTestCase(ELEMENTS_PER_CASE); |
| 314 | + testResolvedOutput(randomTestCase); |
| 315 | + } |
| 316 | + }); |
| 317 | +}); |
0 commit comments