Skip to content

Commit

Permalink
allow preventing delayed dispatches from running, or to execute immed…
Browse files Browse the repository at this point in the history
…iately
  • Loading branch information
armcknight committed Nov 23, 2022
1 parent f4e8dd4 commit 671183f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
1 change: 1 addition & 0 deletions Tests/SentryTests/Helper/SentryFileManagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class SentryFileManagerTests: XCTestCase {
init() {
currentDateProvider = TestCurrentDateProvider()
dispatchQueueWrapper = TestSentryDispatchQueueWrapper()
dispatchQueueWrapper.dispatchAfterExecutesBlock = true

eventIds = (0...(maxCacheItems + 10)).map { _ in SentryId() }

Expand Down
6 changes: 5 additions & 1 deletion Tests/SentryTests/Networking/SentryHttpTransportTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ class SentryHttpTransportTests: XCTestCase {
let requestManager: TestRequestManager
let requestBuilder = TestNSURLRequestBuilder()
let rateLimits: DefaultRateLimits
let dispatchQueueWrapper = TestSentryDispatchQueueWrapper()
let dispatchQueueWrapper: TestSentryDispatchQueueWrapper = {
let dqw = TestSentryDispatchQueueWrapper()
dqw.dispatchAfterExecutesBlock = true
return dqw
}()
let reachability = TestSentryReachability()
let flushTimeout: TimeInterval = 0.5

Expand Down
32 changes: 23 additions & 9 deletions Tests/SentryTests/Networking/TestSentryDispatchQueueWrapper.swift
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
import Foundation

/// A wrapper around `SentryDispatchQueueWrapper` that memoized invocations to its methods and allows customization of async logic, specifically: dispatch-after calls can be made to run immediately, or not at all.
class TestSentryDispatchQueueWrapper: SentryDispatchQueueWrapper {

var dispatchAsyncCalled = 0


/// Whether or not delayed dispatches should execute.
/// - SeeAlso: `delayDispatches`, which controls whether the block should execute immediately or with the requested delay.
var dispatchAfterExecutesBlock = false

/// If `true`, delay dispatched block execution by the specified duration. Be sure to set expectations in test code that relies on delayed dispatches.
var delayDispatches = false

override func dispatchAsync(_ block: @escaping () -> Void) {
dispatchAsyncCalled += 1
block()
}

var blockOnMainInvocations = Invocations<() -> Void>()
var blockBeforeMainBlock: () -> Bool = { true }

override func dispatchAsync(onMainQueue block: @escaping () -> Void) {
blockOnMainInvocations.record(block)
if blockBeforeMainBlock() {
Expand All @@ -25,24 +33,30 @@ class TestSentryDispatchQueueWrapper: SentryDispatchQueueWrapper {
block()
}
}

var dispatchAfterInvocations = Invocations<(interval: TimeInterval, block: () -> Void)>()
override func dispatch(after interval: TimeInterval, block: @escaping () -> Void) {
dispatchAfterInvocations.record((interval, block))
if blockBeforeMainBlock() {
block()
if dispatchAfterExecutesBlock {
if delayDispatches {
DispatchQueue.main.asyncAfter(deadline: .now() + interval, execute: .init(block: block))
} else {
block()
}
}
}
}

func invokeLastDispatchAfter() {
dispatchAfterInvocations.invocations.last?.block()
}

var dispatchCancelInvocations = Invocations<() -> Void>()
override func dispatchCancel(_ block: @escaping () -> Void) {
dispatchCancelInvocations.record(block)
}

override func dispatchOnce(_ predicate: UnsafeMutablePointer<Int>, block: @escaping () -> Void) {
block()
}
Expand Down

0 comments on commit 671183f

Please sign in to comment.