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

test: Fix TestDisplayLink rounding issues #3482

Merged
merged 1 commit into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions SentryTestUtils/TestDisplayLinkWrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,19 @@ public class TestDisplayLinkWrapper: SentryDisplayLinkWrapper {
public var target: AnyObject!
public var selector: Selector!
public var currentFrameRate: FrameRate = .low
let frozenFrameThreshold = 0.7

private let frozenFrameThreshold = 0.7
private let slowestSlowFrameDuration: Double
private let fastestFrozenFrameDuration: Double

public var dateProvider: TestCurrentDateProvider

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

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

public func slowestSlowFrame() -> CFTimeInterval {
dateProvider.advance(by: frozenFrameThreshold)
dateProvider.advance(by: slowestSlowFrameDuration)
call()
return frozenFrameThreshold
return slowestSlowFrameDuration
}

public func fastestFrozenFrame() -> CFTimeInterval {
let duration: Double = frozenFrameThreshold + timeEpsilon
dateProvider.advance(by: duration)
dateProvider.advance(by: fastestFrozenFrameDuration)
call()
return duration
return fastestFrozenFrameDuration
}

/// There's no upper bound for a frozen frame, except maybe for the watchdog time limit.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,20 @@ class SentryFramesTrackerTests: XCTestCase {

try assert(slow: 2, frozen: 0, total: 3)
}

func testMultipleSlowestSlowFrames() throws {
let sut = fixture.sut
sut.start()

fixture.displayLinkWrapper.call()

let slowFramesCount: UInt = 20
for _ in 0..<slowFramesCount {
_ = fixture.displayLinkWrapper.slowestSlowFrame()
}

try assert(slow: slowFramesCount, frozen: 0, total: slowFramesCount)
}

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

try assert(slow: 1, frozen: 1, total: 2)
}

func testMultipleFastestFrozenFrames() throws {
let sut = fixture.sut
sut.start()

fixture.displayLinkWrapper.call()

let frozenFramesCount: UInt = 20
for _ in 0..<frozenFramesCount {
_ = fixture.displayLinkWrapper.fastestFrozenFrame()
}

try assert(slow: 0, frozen: frozenFramesCount, total: frozenFramesCount)
}

func testFrameRateChange() throws {
let sut = fixture.sut
Expand Down
Loading