-
-
Notifications
You must be signed in to change notification settings - Fork 337
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(stalltracking): Ignore FrameStalling when app goes to background (#…
…3211) Co-authored-by: Kryštof Woldřich <[email protected]>
- Loading branch information
1 parent
a76d821
commit 43dbc9e
Showing
5 changed files
with
126 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
/test/react-native/versions | ||
CHANGELOG.md | ||
|
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import type { AppStateStatus } from 'react-native'; | ||
|
||
import { StallTrackingInstrumentation } from '../../src/js/tracing/stalltracking'; | ||
|
||
describe('BackgroundEventListener', () => { | ||
it('Stall tracking should set _isBackground to false, update _lastIntervalMs, and call _iteration when state is active and _timeout is not null', () => { | ||
const stallTracking = new StallTrackingInstrumentation(); | ||
const LOOP_TIMEOUT_INTERVAL_MS = 500; // Change this value based on your actual interval value | ||
const currentTime = Date.now(); | ||
stallTracking['_lastIntervalMs'] = currentTime; | ||
stallTracking['_timeout'] = setTimeout(() => {}, LOOP_TIMEOUT_INTERVAL_MS); // Create a fake timeout to simulate a running interval | ||
stallTracking['_isBackground'] = true; | ||
jest.useFakeTimers(); // Enable fake timers to control timeouts | ||
stallTracking['_backgroundEventListener']('active' as AppStateStatus); | ||
// Check if _isBackground is set to false and _lastIntervalMs is updated correctly | ||
expect(stallTracking['_isBackground']).toBe(false); | ||
expect(stallTracking['_lastIntervalMs']).toBeGreaterThanOrEqual(currentTime); | ||
jest.runOnlyPendingTimers(); // Fast-forward the timer to execute the timeout function | ||
}); | ||
it('Stall tracking should set _isBackground to true when state is not active', () => { | ||
const stallTracking = new StallTrackingInstrumentation(); | ||
stallTracking['_isBackground'] = false; | ||
stallTracking['_backgroundEventListener']('background' as AppStateStatus); | ||
// Check if _isBackground is set to true | ||
expect(stallTracking['_isBackground']).toBe(true); | ||
}); | ||
it('Stall tracking should not call _iteration when state is active but _timeout is null', () => { | ||
const stallTracking = new StallTrackingInstrumentation(); | ||
stallTracking['_timeout'] = null; | ||
// Mock _iteration | ||
stallTracking['_iteration'] = jest.fn(); | ||
jest.useFakeTimers(); // Enable fake timers to control timeouts | ||
stallTracking['_backgroundEventListener']('active' as AppStateStatus); | ||
|
||
expect(stallTracking['_iteration']).not.toBeCalled(); | ||
}); | ||
it('Stall tracking should call _iteration when state is active and _timeout is defined', () => { | ||
const stallTracking = new StallTrackingInstrumentation(); | ||
stallTracking['_timeout'] = setTimeout(() => {}, 500); | ||
// Mock _iteration | ||
stallTracking['_iteration'] = jest.fn(); // Create a fake timeout to simulate a running interval | ||
jest.useFakeTimers(); // Enable fake timers to control timeouts | ||
stallTracking['_backgroundEventListener']('active' as AppStateStatus); | ||
expect(stallTracking['_iteration']).toBeCalled(); | ||
}); | ||
}); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { StallTrackingInstrumentation } from '../../src/js/tracing/stalltracking'; | ||
|
||
describe('Iteration', () => { | ||
it('Stall tracking does not set _timeout when isTracking is false', () => { | ||
const stallTracking = new StallTrackingInstrumentation(); | ||
stallTracking['isTracking'] = false; | ||
stallTracking['_isBackground'] = false; | ||
stallTracking['_lastIntervalMs'] = Date.now() - 1000; // Force a timeout | ||
jest.useFakeTimers(); | ||
// Invokes the private _interaction function. | ||
stallTracking['_iteration'](); | ||
expect(stallTracking['_timeout']).toBeNull(); | ||
}); | ||
it('Stall tracking does not set _timeout when isBackground is true', () => { | ||
const stallTracking = new StallTrackingInstrumentation(); | ||
stallTracking['isTracking'] = true; | ||
stallTracking['_isBackground'] = true; | ||
stallTracking['_lastIntervalMs'] = Date.now() - 1000; // Force a timeout | ||
jest.useFakeTimers(); | ||
// Invokes the private _interaction function. | ||
stallTracking['_iteration'](); | ||
expect(stallTracking['_timeout']).toBeNull(); | ||
}); | ||
it('Stall tracking should set _timeout when isTracking is true and isBackground false', () => { | ||
const stallTracking = new StallTrackingInstrumentation(); | ||
stallTracking['isTracking'] = true; | ||
stallTracking['_isBackground'] = false; | ||
jest.useFakeTimers(); | ||
stallTracking['_lastIntervalMs'] = Date.now(); // Force a timeout | ||
// Invokes the private _interaction function. | ||
stallTracking['_iteration'](); | ||
expect(stallTracking['_timeout']).toBeDefined(); | ||
}); | ||
it('Stall tracking should update _stallCount and _totalStallTime when timeout condition is met', () => { | ||
const stallTracking = new StallTrackingInstrumentation(); | ||
const LOOP_TIMEOUT_INTERVAL_MS = 50; | ||
const _minimumStallThreshold = 100; | ||
// Call _iteration with totalTimeTaken >= LOOP_TIMEOUT_INTERVAL_MS + _minimumStallThreshold | ||
const totalTimeTaken = LOOP_TIMEOUT_INTERVAL_MS + _minimumStallThreshold; | ||
jest.useFakeTimers(); | ||
stallTracking['_lastIntervalMs'] = Date.now() - totalTimeTaken; | ||
stallTracking['_statsByTransaction'] = new Map(); | ||
stallTracking['_iteration'](); | ||
// Check if _stallCount and _totalStallTime have been updated as expected. | ||
expect(stallTracking['_stallCount']).toBe(1); | ||
expect(stallTracking['_totalStallTime']).toBeGreaterThanOrEqual( | ||
Math.round(totalTimeTaken - LOOP_TIMEOUT_INTERVAL_MS), | ||
); | ||
}); | ||
}); |