Skip to content

Commit

Permalink
test: Remove subclass of NSTimer (#2904)
Browse files Browse the repository at this point in the history
We subclassed NSTimer, which is not allowed see:
https://developer.apple.com/documentation/foundation/nstimer#1770465.
This sometimes leads to crashes in TestSentryNSTimerWrapper with EXC_BAD_ACCESS.
The subclass is now removed.
  • Loading branch information
philipphofmann committed Apr 14, 2023
1 parent 8c50edb commit 326b7eb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 19 deletions.
31 changes: 13 additions & 18 deletions SentryTestUtils/TestSentryNSTimerWrapper.swift
Original file line number Diff line number Diff line change
@@ -1,31 +1,26 @@
import Foundation
import Sentry

public class TestTimer: Timer {
public var invalidateCount = 0

public override func invalidate() {
// no-op as this timer doesn't actually schedule anything on a runloop
invalidateCount += 1
}
}

// We must not subclass NSTimer, see https://developer.apple.com/documentation/foundation/nstimer#1770465.
// Therefore we return a NSTimer instance here with TimeInterval.infinity.
public class TestSentryNSTimerWrapper: SentryNSTimerWrapper {
public struct Overrides {
public var timer: TestTimer!
var block: ((Timer) -> Void)?
}

public var overrides = Overrides()
private var _timer: Timer?

public var timer: Timer {
get {
_timer ?? Timer()
}
}

public override func scheduledTimer(withTimeInterval interval: TimeInterval, repeats: Bool, block: @escaping (Timer) -> Void) -> Timer {
let timer = TestTimer()
overrides.timer = timer
overrides.block = block
let timer = Timer.scheduledTimer(withTimeInterval: TimeInterval.infinity, repeats: repeats, block: block)
_timer = timer

return timer
}

public func fire() {
overrides.block?(overrides.timer)
_timer?.fire()
}
}
2 changes: 1 addition & 1 deletion Tests/SentryTests/Transaction/SentryTracerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ class SentryTracerTests: XCTestCase {
let sut = fixture.getSut()
sut.finish()

XCTAssertEqual(fixture.timerWrapper.overrides.timer.invalidateCount, 1)
XCTAssertFalse(fixture.timerWrapper.timer.isValid)
}

func testFinish_CheckDefaultStatus() {
Expand Down

0 comments on commit 326b7eb

Please sign in to comment.