-
-
Notifications
You must be signed in to change notification settings - Fork 317
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Remove subclass of NSTimer (#2904)
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
1 parent
8c50edb
commit 326b7eb
Showing
2 changed files
with
14 additions
and
19 deletions.
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,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() | ||
} | ||
} |
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