Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Scheduler] Test browser implementation details #16198

Merged
merged 1 commit into from
Jul 25, 2019

Conversation

acdlite
Copy link
Collaborator

@acdlite acdlite commented Jul 25, 2019

The Scheduler implementation uses browser APIs like MessageChannel, requestAnimationFrame, and setTimeout to schedule work on the main thread. Most of our tests treat these as implementation details; however, the sequence and timing of these APIs are not precisely specified, and can vary wildly across browsers.

To prevent regressions, we need the ability to simulate specific edge cases that we may encounter in various browsers.

This adds a new test suite that mocks all browser methods used in our implementation. It assumes as little as possible about the order and timing of events. The only thing it assumes is that requestAnimationFrame is passed a frame time that is equal to or less than the time returned by performance.now. Everything else can be controlled at will.

It also includes Scheduler-specific invariants, e.g. only one rAF callback can be scheduled at a time.

It overlaps slightly with the existing SchedulerDOM-test suite, which also mocks the browser APIs, but exposes a higher-level set of testing primitives. I will consolidate the two suites in a follow-up.

The Scheduler implementation uses browser APIs like `MessageChannel`,
`requestAnimationFrame`, and `setTimeout` to schedule work on the main
thread. Most of our tests treat these as implementation details;
however, the sequence and timing of these APIs are not precisely
specified, and can vary wildly across browsers.

To prevent regressions, we need the ability to simulate specific edge
cases that we may encounter in various browsers.

This adds a new test suite that mocks all browser methods used in our
implementation. It assumes as little as possible about the order and
timing of events. The only thing it assumes is that
requestAnimationFrame is passed a frame time that is equal to or less
than the time returned by performance.now. Everything else can be
controlled at will.

It also includes Scheduler-specific invariants, e.g. only one rAF
callback can be scheduled at a time.

It overlaps slightly with the existing SchedulerDOM-test suite, which
also mocks the browser APIs, but exposes a higher-level set of testing
primitives. I will consolidate the two suites in a follow-up.
prevRAFTime !== -1 &&
// Make sure this rAF time is different from the previous one. This check
// could fail if two rAFs fire in the same frame.
rAFTime - prevRAFTime > 0.1
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This particular change fixes a bug in Safari where we post a rAF from inside another rAF, and that rAF fires within the same frame. One of the tests I added covers this scenario.

@sizebot
Copy link

sizebot commented Jul 25, 2019

React: size: -0.0%, gzip: -0.0%

Details of bundled changes.

Comparing: 19354db...8c1adfa

react

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react.development.js -0.3% -0.4% 114.5 KB 114.15 KB 28.79 KB 28.69 KB UMD_DEV
react.production.min.js -0.0% -0.0% 13.04 KB 13.04 KB 5.08 KB 5.08 KB UMD_PROD
react.profiling.min.js -0.0% -0.1% 15.24 KB 15.23 KB 5.62 KB 5.62 KB UMD_PROFILING
react.development.js 0.0% -0.0% 70.99 KB 70.99 KB 18.53 KB 18.53 KB NODE_DEV
react.production.min.js 0.0% -0.0% 6.69 KB 6.69 KB 2.75 KB 2.75 KB NODE_PROD
React-dev.js 0.0% -0.0% 69.18 KB 69.18 KB 17.8 KB 17.8 KB FB_WWW_DEV
React-prod.js 0.0% -0.1% 17.21 KB 17.21 KB 4.46 KB 4.46 KB FB_WWW_PROD
React-profiling.js 0.0% -0.1% 17.21 KB 17.21 KB 4.46 KB 4.46 KB FB_WWW_PROFILING

scheduler

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
scheduler.development.js -1.2% -1.3% 28.49 KB 28.14 KB 7.13 KB 7.03 KB NODE_DEV
scheduler.production.min.js -0.1% -0.2% 5.69 KB 5.68 KB 2.18 KB 2.18 KB NODE_PROD
Scheduler-dev.js -1.1% -1.3% 29.17 KB 28.84 KB 7.24 KB 7.15 KB FB_WWW_DEV
Scheduler-prod.js 0.0% 🔺+0.1% 17.17 KB 17.17 KB 3.58 KB 3.59 KB FB_WWW_PROD
scheduler-tracing.development.js 0.0% -0.1% 11.15 KB 11.15 KB 2.79 KB 2.79 KB NODE_DEV
scheduler-tracing.production.min.js 0.0% -1.0% 728 B 728 B 384 B 380 B NODE_PROD
scheduler-unstable_mock.development.js 0.0% -0.1% 20.8 KB 20.8 KB 4.56 KB 4.56 KB UMD_DEV
scheduler-tracing.profiling.min.js 0.0% -0.3% 3.26 KB 3.26 KB 1000 B 997 B NODE_PROFILING
scheduler-unstable_mock.production.min.js 0.0% -0.1% 5.08 KB 5.08 KB 2 KB 2 KB UMD_PROD
scheduler-unstable_mock.development.js 0.0% -0.0% 20.61 KB 20.61 KB 4.5 KB 4.5 KB NODE_DEV
scheduler-unstable_mock.production.min.js 0.0% -0.1% 5.07 KB 5.07 KB 1.94 KB 1.94 KB NODE_PROD

Generated by 🚫 dangerJS

currentTime = targetTime;
}
}
function getMostRecentFrameNumber() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

trivial naming nit: getCurrentFrameNumber? "Most recent" feels like an odd name to use when currentTime is exactly at a frame boundary.

Edit I see how you're using this method in the tests below, and getMostRecentFrameNumber makes more sense as a name. I think the usage in fireAnimationFrame feels more like a "current" value but I don't feel strongly about this. 😄

@acdlite acdlite merged commit c0830a0 into facebook:master Jul 25, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants