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

[Mock Scheduler] Mimic browser's advanceTime #17967

Merged
merged 1 commit into from
Feb 4, 2020

Conversation

acdlite
Copy link
Collaborator

@acdlite acdlite commented Feb 3, 2020

The mock Scheduler that we use in our tests has its own fake timer implementation. The unstable_advanceTime method advances the timeline.

Currently, a call to unstable_advanceTime will also flush any pending expired work. But that's not how it works in the browser: when a timer fires, the corresponding task is added to the Scheduler queue. However, we will still wait until the next message event before flushing it.

This commit changes unstable_advanceTime to more closely resemble the browser behavior, by removing the automatic flushing of expired work.

// Before this commit
Scheduler.unstable_advanceTime(ms);

// Equivalent behavior after this commit
Scheduler.unstable_advanceTime(ms);
Scheduler.unstable_flushExpired();

The general principle is to prefer separate APIs for scheduling tasks and flushing them.

This change does not affect any public APIs. unstable_advanceTime is only used by our own test suite. It is not used by act.

However, we may need to update tests in www, like Relay's.

The mock Scheduler that we use in our tests has its own fake timer
implementation. The `unstable_advanceTime` method advances the timeline.

Currently, a call to `unstable_advanceTime` will also flush any pending
expired work. But that's not how it works in the browser: when a timer
fires, the corresponding task is added to the Scheduler queue. However,
we will still wait until the next message event before flushing it.

This commit changes `unstable_advanceTime` to more closely resemble the
browser behavior, by removing the automatic flushing of expired work.

```js
// Before this commit
Scheduler.unstable_advanceTime(ms);

// Equivalent behavior after this commit
Scheduler.unstable_advanceTime(ms);
Scheduler.unstable_flushExpired();
```

The general principle is to prefer separate APIs for scheduling tasks
and flushing them.

This change does not affect any public APIs. `unstable_advanceTime` is
only used by our own test suite. It is not used by `act`.

However, we may need to update tests in www, like Relay's.
@facebook-github-bot facebook-github-bot added CLA Signed React Core Team Opened by a member of the React Core Team labels Feb 3, 2020
@codesandbox-ci
Copy link

codesandbox-ci bot commented Feb 3, 2020

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit fe17821:

Sandbox Source
awesome-voice-nuib2 Configuration

@sizebot
Copy link

sizebot commented Feb 3, 2020

Warnings
⚠️ Could not find build artifacts for base commit: ace9e81

Size changes (experimental)

Generated by 🚫 dangerJS against fe17821

@sizebot
Copy link

sizebot commented Feb 3, 2020

Warnings
⚠️ Could not find build artifacts for base commit: ace9e81

Size changes (stable)

Generated by 🚫 dangerJS against fe17821

Copy link
Collaborator

@sebmarkbage sebmarkbage left a comment

Choose a reason for hiding this comment

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

This is more in line with the naming of the API but I'm not sure it's actually better. Seems easy to forget to flush. It's not really how I think about testing. I think of it as E2E. Play time forward a bit and see what happens.

I'd prefer an API like expect(Scheduler).toAdvanceTimeByAndToFlushAndYield(1000, ...)

@sebmarkbage
Copy link
Collaborator

I think I'd prefer a helper that plays time forward for a certain time with a reasonable amount of flushes in between. It shouldn't just jump to the end as if the CPU was stalled for the whole time. Seems like we'd test very unrealistic scenarios that way. The order might matter.

@bvaughn
Copy link
Contributor

bvaughn commented Feb 4, 2020

I dig @sebmarkbage's suggestion of a method like toAdvanceTimeByAndToFlushAndYield(number, Array<string>) to avoid the case of forgetting to flush.

@acdlite
Copy link
Collaborator Author

acdlite commented Feb 4, 2020

Yeah I think that API would make sense because it's a test matcher which discourages us from calling it inside another task.

@acdlite acdlite merged commit 9dba218 into facebook:master Feb 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed React Core Team Opened by a member of the React Core Team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants