Skip to content

Commit ddb4778

Browse files
test: Fix TestDisplayLink rounding issues (#3482)
Calling the TestDisplayLinkWrapper with multiple slowestSlowFrames or fastestFrozenFrames in tests leads to wrong results. This is fixed now by adding and subtracting the timeEpsilon for these.
1 parent 0288d6c commit ddb4778

File tree

2 files changed

+41
-6
lines changed

2 files changed

+41
-6
lines changed

SentryTestUtils/TestDisplayLinkWrapper.swift

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,19 @@ public class TestDisplayLinkWrapper: SentryDisplayLinkWrapper {
2323
public var target: AnyObject!
2424
public var selector: Selector!
2525
public var currentFrameRate: FrameRate = .low
26-
let frozenFrameThreshold = 0.7
26+
27+
private let frozenFrameThreshold = 0.7
28+
private let slowestSlowFrameDuration: Double
29+
private let fastestFrozenFrameDuration: Double
30+
2731
public var dateProvider: TestCurrentDateProvider
2832

2933
public init(dateProvider: TestCurrentDateProvider = TestCurrentDateProvider()) {
3034
self.dateProvider = dateProvider
35+
// The test date provider converts the duration from UInt64 to a double back and forth.
36+
// Therefore we have rounding issues, and subtract or add the timeEpsilon.
37+
slowestSlowFrameDuration = frozenFrameThreshold - timeEpsilon
38+
fastestFrozenFrameDuration = frozenFrameThreshold + timeEpsilon
3139
}
3240

3341
public var linkInvocations = Invocations<Void>()
@@ -80,16 +88,15 @@ public class TestDisplayLinkWrapper: SentryDisplayLinkWrapper {
8088
}
8189

8290
public func slowestSlowFrame() -> CFTimeInterval {
83-
dateProvider.advance(by: frozenFrameThreshold)
91+
dateProvider.advance(by: slowestSlowFrameDuration)
8492
call()
85-
return frozenFrameThreshold
93+
return slowestSlowFrameDuration
8694
}
8795

8896
public func fastestFrozenFrame() -> CFTimeInterval {
89-
let duration: Double = frozenFrameThreshold + timeEpsilon
90-
dateProvider.advance(by: duration)
97+
dateProvider.advance(by: fastestFrozenFrameDuration)
9198
call()
92-
return duration
99+
return fastestFrozenFrameDuration
93100
}
94101

95102
/// There's no upper bound for a frozen frame, except maybe for the watchdog time limit.

Tests/SentryTests/Integrations/Performance/FramesTracking/SentryFramesTrackerTests.swift

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,20 @@ class SentryFramesTrackerTests: XCTestCase {
8484

8585
try assert(slow: 2, frozen: 0, total: 3)
8686
}
87+
88+
func testMultipleSlowestSlowFrames() throws {
89+
let sut = fixture.sut
90+
sut.start()
91+
92+
fixture.displayLinkWrapper.call()
93+
94+
let slowFramesCount: UInt = 20
95+
for _ in 0..<slowFramesCount {
96+
_ = fixture.displayLinkWrapper.slowestSlowFrame()
97+
}
98+
99+
try assert(slow: slowFramesCount, frozen: 0, total: slowFramesCount)
100+
}
87101

88102
func testFrozenFrame() throws {
89103
let sut = fixture.sut
@@ -95,6 +109,20 @@ class SentryFramesTrackerTests: XCTestCase {
95109

96110
try assert(slow: 1, frozen: 1, total: 2)
97111
}
112+
113+
func testMultipleFastestFrozenFrames() throws {
114+
let sut = fixture.sut
115+
sut.start()
116+
117+
fixture.displayLinkWrapper.call()
118+
119+
let frozenFramesCount: UInt = 20
120+
for _ in 0..<frozenFramesCount {
121+
_ = fixture.displayLinkWrapper.fastestFrozenFrame()
122+
}
123+
124+
try assert(slow: 0, frozen: frozenFramesCount, total: frozenFramesCount)
125+
}
98126

99127
func testFrameRateChange() throws {
100128
let sut = fixture.sut

0 commit comments

Comments
 (0)