Add deterministic timer mock to Fantom (#57274)#57274
Open
rubennorte wants to merge 1 commit into
Open
Conversation
|
@rubennorte has exported this pull request. If you are a Meta employee, you can view the originating Diff in D109017304. |
rubennorte
added a commit
to rubennorte/react-native
that referenced
this pull request
Jun 18, 2026
Summary: Fantom could not deterministically fire delayed JS timers: the timer registry it used scheduled timers on a real background thread with real wall-clock delays, so `setTimeout(fn, 100)`/`setInterval` callbacks never fired within a synchronous test. This adds a mockable timer registry and a public Fantom API to control it from JS, similar to `installHighResTimeStampMock`. - New `Fantom.installTimerMock()` returns a controller with `advanceTimersByTime(ms)`, `runAllTimers()`, `getPendingTimerCount()`, and `uninstall()` (jest fake-timer style). While installed, `setTimeout`/`setInterval` callbacks only fire when the virtual clock is advanced. - New deterministic `FantomTimerRegistry` (no background thread) keyed off a virtual clock, injected via a new optional `platformTimerRegistryFactory` seam on `ReactInstanceConfig` (the default registry is unchanged for all other consumers). - `PlatformTimerRegistry` gains a virtual `setTimerManager` (default no-op) so the registry can be wired polymorphically. - Control flows from JS through new `NativeFantom` methods, the same way the high-res timestamp mock works. Default (non-mock) behavior is preserved: zero-delay `setTimeout` still fires on the next work loop, and existing tests are unaffected. Changelog: [Internal] Differential Revision: D109017304
06e0e12 to
6c481be
Compare
Summary: Fantom could not deterministically fire delayed JS timers: the timer registry it used scheduled timers on a real background thread with real wall-clock delays, so `setTimeout(fn, 100)`/`setInterval` callbacks never fired within a synchronous test. This adds a mockable timer registry and a public Fantom API to control it from JS, similar to `installHighResTimeStampMock`. - New `Fantom.installTimerMock()` returns a controller with `advanceTimersByTime(ms)`, `runAllTimers()`, `getPendingTimerCount()`, and `uninstall()` (jest fake-timer style). While installed, `setTimeout`/`setInterval` callbacks only fire when the virtual clock is advanced. - New deterministic `FantomTimerRegistry` (no background thread) keyed off a virtual clock, injected via a new optional `platformTimerRegistryFactory` seam on `ReactInstanceConfig` (the default registry is unchanged for all other consumers). - `PlatformTimerRegistry` gains a virtual `setTimerManager` (default no-op) so the registry can be wired polymorphically. - Control flows from JS through new `NativeFantom` methods, the same way the high-res timestamp mock works. Default (non-mock) behavior is preserved: zero-delay `setTimeout` still fires on the next work loop, and existing tests are unaffected. Changelog: [Internal] Differential Revision: D109017304
6c481be to
b0494e7
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary:
Fantom could not deterministically fire delayed JS timers: the timer registry it used scheduled timers on a real background thread with real wall-clock delays, so
setTimeout(fn, 100)/setIntervalcallbacks never fired within a synchronous test. This adds a mockable timer registry and a public Fantom API to control it from JS, similar toinstallHighResTimeStampMock.Fantom.installTimerMock()returns a controller withadvanceTimersByTime(ms),runAllTimers(),getPendingTimerCount(), anduninstall()(jest fake-timer style). While installed,setTimeout/setIntervalcallbacks only fire when the virtual clock is advanced.FantomTimerRegistry(no background thread) keyed off a virtual clock, injected via a new optionalplatformTimerRegistryFactoryseam onReactInstanceConfig(the default registry is unchanged for all other consumers).PlatformTimerRegistrygains a virtualsetTimerManager(default no-op) so the registry can be wired polymorphically.NativeFantommethods, the same way the high-res timestamp mock works.Default (non-mock) behavior is preserved: zero-delay
setTimeoutstill fires on the next work loop, and existing tests are unaffected.Changelog: [Internal]
Differential Revision: D109017304