-
-
Notifications
You must be signed in to change notification settings - Fork 341
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
128 additions
and
55 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
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,52 +1,101 @@ | ||
import Foundation | ||
import Sentry | ||
@testable import Sentry | ||
|
||
// 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 TestSentryNSTimerFactory: SentryNSTimerFactory { | ||
public struct Overrides { | ||
private var _timer: Timer? | ||
|
||
public var timer: Timer { | ||
get { | ||
_timer ?? Timer() | ||
} | ||
private var _timer: Timer? | ||
private var _interval: TimeInterval? | ||
|
||
public var timer: Timer { | ||
get { | ||
_timer ?? Timer() | ||
} | ||
set(newValue) { | ||
_timer = newValue | ||
} | ||
} | ||
|
||
} | ||
var block: ((Timer) -> Void)? | ||
|
||
|
||
var interval: TimeInterval { | ||
get { | ||
_interval ?? TimeInterval.infinity | ||
} | ||
set { | ||
_interval = newValue | ||
} | ||
} | ||
|
||
var lastFireDate: Date | ||
|
||
struct InvocationInfo { | ||
var target: NSObject | ||
var selector: Selector | ||
} | ||
var invocationInfo: InvocationInfo? | ||
|
||
init(timer: Timer, interval: TimeInterval? = nil, block: ((Timer) -> Void)? = nil, lastFireDate: Date, invocationInfo: InvocationInfo? = nil) { | ||
self._timer = timer | ||
self._interval = interval | ||
self.block = block | ||
self.lastFireDate = lastFireDate | ||
self.invocationInfo = invocationInfo | ||
} | ||
} | ||
|
||
public var overrides: Overrides? | ||
|
||
private var currentDateProvider: SentryCurrentDateProvider | ||
|
||
public init(currentDateProvider: SentryCurrentDateProvider) { | ||
self.currentDateProvider = currentDateProvider | ||
super.init() | ||
} | ||
} | ||
|
||
public var overrides = Overrides() | ||
|
||
public override func scheduledTimer(withTimeInterval interval: TimeInterval, repeats: Bool, block: @escaping (Timer) -> Void) -> Timer { | ||
// MARK: Superclass overrides | ||
public extension TestSentryNSTimerFactory { | ||
override func scheduledTimer(withTimeInterval interval: TimeInterval, repeats: Bool, block: @escaping (Timer) -> Void) -> Timer { | ||
let timer = Timer.scheduledTimer(withTimeInterval: TimeInterval.infinity, repeats: repeats, block: block) | ||
overrides.timer = timer | ||
overrides.block = block | ||
overrides = Overrides(timer: timer, interval: interval, block: block, lastFireDate: currentDateProvider.date()) | ||
return timer | ||
} | ||
|
||
public override func scheduledTimer(withTimeInterval ti: TimeInterval, target aTarget: Any, selector aSelector: Selector, userInfo: Any?, repeats yesOrNo: Bool) -> Timer { | ||
override func scheduledTimer(withTimeInterval ti: TimeInterval, target aTarget: Any, selector aSelector: Selector, userInfo: Any?, repeats yesOrNo: Bool) -> Timer { | ||
let timer = Timer.scheduledTimer(timeInterval: ti, target: aTarget, selector: aSelector, userInfo: userInfo, repeats: yesOrNo) | ||
//swiftlint:disable force_cast | ||
overrides.invocationInfo = Overrides.InvocationInfo(target: aTarget as! NSObject, selector: aSelector) | ||
let invocationInfo = Overrides.InvocationInfo(target: aTarget as! NSObject, selector: aSelector) | ||
//swiftlint:enable force_cast | ||
overrides = Overrides(timer: timer, interval: ti, lastFireDate: currentDateProvider.date(), invocationInfo: invocationInfo) | ||
return timer | ||
} | ||
} | ||
|
||
// MARK: Extensions | ||
public extension TestSentryNSTimerFactory { | ||
enum TestTimerError: Error { | ||
case timerNotInitialized | ||
} | ||
|
||
// check the current time against the last fire time and interval, and if enough time has elapsed, execute any block/invocation registered with the timer | ||
func check() throws { | ||
guard let overrides = overrides else { throw TestTimerError.timerNotInitialized } | ||
let currentDate = currentDateProvider.date() | ||
if currentDate.timeIntervalSince(overrides.lastFireDate) >= overrides.interval { | ||
try fire() | ||
} | ||
} | ||
|
||
public func fire() { | ||
// immediately execute any block/invocation registered with the timer | ||
func fire() throws { | ||
guard var overrides = overrides else { throw TestTimerError.timerNotInitialized } | ||
overrides.lastFireDate = currentDateProvider.date() | ||
if let block = overrides.block { | ||
block(overrides.timer) | ||
} else if let invocationInfo = overrides.invocationInfo { | ||
try! Invocation(target: invocationInfo.target, selector: invocationInfo.selector).invoke() | ||
try Invocation(target: invocationInfo.target, selector: invocationInfo.selector).invoke() | ||
} | ||
} | ||
} |
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
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
Oops, something went wrong.