Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
armcknight committed Dec 5, 2023
1 parent 3139eac commit d78ee13
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 37 deletions.
2 changes: 1 addition & 1 deletion Tests/SentryProfilerTests/SentryProfilerSwiftTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class SentryProfilerSwiftTests: XCTestCase {
}
SentryDependencyContainer.sharedInstance().dispatchFactory = dispatchFactory
SentryDependencyContainer.sharedInstance().timerFactory = timeoutTimerFactory
SentryDependencyContainer.sharedInstance().dispatchQueueWrapper = dispatchQueueWrapper

systemWrapper.overrides.cpuUsage = NSNumber(value: mockCPUUsage)
systemWrapper.overrides.memoryFootprintBytes = mockMemoryFootprint
Expand All @@ -76,7 +77,6 @@ class SentryProfilerSwiftTests: XCTestCase {
if let idleTimeout = idleTimeout {
$0.idleTimeout = idleTimeout
}
$0.dispatchQueueWrapper = self.dispatchQueueWrapper
$0.waitForChildren = true
$0.timerFactory = self.timeoutTimerFactory
}))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ class SentryUIEventTrackerTests: XCTestCase {
init () {
dispatchQueue.blockBeforeMainBlock = { false }
SentryDependencyContainer.sharedInstance().swizzleWrapper = swizzleWrapper
uiEventTrackerMode = SentryUIEventTrackerTransactionMode(dispatchQueueWrapper: dispatchQueue, idleTimeout: 3.0)
SentryDependencyContainer.sharedInstance().dispatchQueueWrapper = dispatchQueue
uiEventTrackerMode = SentryUIEventTrackerTransactionMode(idleTimeout: 3.0)
}

func getSut() -> SentryUIEventTracker {
Expand Down
61 changes: 26 additions & 35 deletions Tests/SentryTests/Transaction/SentryTracerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class SentryTracerTests: XCTestCase {
dispatchQueue.blockBeforeMainBlock = { false }

SentryDependencyContainer.sharedInstance().dateProvider = currentDateProvider
SentryDependencyContainer.sharedInstance().dispatchQueueWrapper = dispatchQueue
appStart = currentDateProvider.date()
appStartEnd = appStart.addingTimeInterval(appStartDuration)

Expand Down Expand Up @@ -86,30 +87,16 @@ class SentryTracerTests: XCTestCase {
}
#endif // os(iOS) || os(tvOS) || targetEnvironment(macCatalyst)

func getSut(waitForChildren: Bool = true) -> SentryTracer {
func getSut(waitForChildren: Bool = true, idleTimeout: TimeInterval = 0.0) -> SentryTracer {
let tracer = hub.startTransaction(
with: transactionContext,
bindToScope: false,
customSamplingContext: [:],
configuration: SentryTracerConfiguration(block: {
$0.waitForChildren = waitForChildren
$0.dispatchQueueWrapper = self.dispatchQueue
$0.timerFactory = self.timerFactory
}))
return tracer
}

func getSut(idleTimeout: TimeInterval = 0.0, dispatchQueueWrapper: SentryDispatchQueueWrapper) -> SentryTracer {
let tracer = hub.startTransaction(
with: transactionContext,
bindToScope: false,
customSamplingContext: [:],
configuration: SentryTracerConfiguration(block: {
$0.idleTimeout = idleTimeout
$0.dispatchQueueWrapper = dispatchQueueWrapper
$0.waitForChildren = true
})
)
}))
return tracer
}
}
Expand Down Expand Up @@ -282,7 +269,7 @@ class SentryTracerTests: XCTestCase {
}

func testDeadlineTimer_OnlyForAutoTransactions() {
let sut = fixture.getSut(idleTimeout: fixture.idleTimeout, dispatchQueueWrapper: fixture.dispatchQueue)
let sut = fixture.getSut(idleTimeout: fixture.idleTimeout)
let child1 = sut.startChild(operation: fixture.transactionOperation)
let child2 = sut.startChild(operation: fixture.transactionOperation)
let child3 = sut.startChild(operation: fixture.transactionOperation)
Expand All @@ -305,12 +292,14 @@ class SentryTracerTests: XCTestCase {
}

func testDeadlineTimer_MultipleSpansFinishedInParallel() {
let sut = fixture.getSut(idleTimeout: 0.01, dispatchQueueWrapper: SentryDispatchQueueWrapper())
SentryDependencyContainer.sharedInstance().dispatchQueueWrapper = SentryDispatchQueueWrapper()
let sut = fixture.getSut(idleTimeout: 0.01)

testConcurrentModifications(writeWork: { _ in
let child = sut.startChild(operation: self.fixture.transactionOperation)
child.finish()
})
SentryDependencyContainer.sharedInstance().dispatchQueueWrapper = fixture.dispatchQueue
}

func testFinish_CheckDefaultStatus() {
Expand All @@ -322,7 +311,7 @@ class SentryTracerTests: XCTestCase {

func testIdleTransactionWithStatus_KeepsStatusWhenAutoFinishing() {
let status = SentrySpanStatus.aborted
let sut = fixture.getSut(idleTimeout: fixture.idleTimeout, dispatchQueueWrapper: fixture.dispatchQueue)
let sut = fixture.getSut(idleTimeout: fixture.idleTimeout)
sut.status = status

let child = sut.startChild(operation: fixture.transactionOperation)
Expand All @@ -338,13 +327,13 @@ class SentryTracerTests: XCTestCase {
func testIdleTransaction_CreatingDispatchBlockFails_NoTransactionCaptured() {
fixture.dispatchQueue.createDispatchBlockReturnsNULL = true

let sut = fixture.getSut(idleTimeout: fixture.idleTimeout, dispatchQueueWrapper: fixture.dispatchQueue)
let sut = fixture.getSut(idleTimeout: fixture.idleTimeout)

assertTransactionNotCaptured(sut)
}

func testIdleTransaction_CreatingDispatchBlockFailsForFirstChild_FinishesTransaction() {
let sut = fixture.getSut(idleTimeout: fixture.idleTimeout, dispatchQueueWrapper: fixture.dispatchQueue)
let sut = fixture.getSut(idleTimeout: fixture.idleTimeout)

fixture.dispatchQueue.createDispatchBlockReturnsNULL = true

Expand Down Expand Up @@ -425,7 +414,7 @@ class SentryTracerTests: XCTestCase {
#endif // os(iOS) || os(tvOS) || targetEnvironment(macCatalyst)

func testFinish_IdleTimeout_ExceedsMaxDuration_NoTransactionCaptured() {
let sut = fixture.getSut(idleTimeout: fixture.idleTimeout, dispatchQueueWrapper: fixture.dispatchQueue)
let sut = fixture.getSut(idleTimeout: fixture.idleTimeout)

advanceTime(bySeconds: 500)

Expand All @@ -435,15 +424,15 @@ class SentryTracerTests: XCTestCase {
}

func testIdleTimeout_NoChildren_TransactionNotCaptured() {
_ = fixture.getSut(idleTimeout: fixture.idleTimeout, dispatchQueueWrapper: fixture.dispatchQueue)
_ = fixture.getSut(idleTimeout: fixture.idleTimeout)

fixture.dispatchQueue.invokeLastDispatchAfter()

XCTAssertEqual(0, fixture.hub.capturedEventsWithScopes.count)
}

func testIdleTimeout_NoChildren_SpanOnScopeUnset() {
let sut = fixture.getSut(idleTimeout: fixture.idleTimeout, dispatchQueueWrapper: fixture.dispatchQueue)
let sut = fixture.getSut(idleTimeout: fixture.idleTimeout)

fixture.hub.scope.span = sut

Expand All @@ -453,15 +442,15 @@ class SentryTracerTests: XCTestCase {
}

func testIdleTimeout_InvokesDispatchAfterWithCorrectWhen() {
_ = fixture.getSut(idleTimeout: fixture.idleTimeout, dispatchQueueWrapper: fixture.dispatchQueue)
_ = fixture.getSut(idleTimeout: fixture.idleTimeout)

fixture.dispatchQueue.invokeLastDispatchAfter()

XCTAssertEqual(fixture.idleTimeout, fixture.dispatchQueue.dispatchAfterInvocations.invocations.first?.interval)
}

func testIdleTimeout_SpanAdded_IdleTimeoutCancelled() {
let sut = fixture.getSut(idleTimeout: fixture.idleTimeout, dispatchQueueWrapper: fixture.dispatchQueue)
let sut = fixture.getSut(idleTimeout: fixture.idleTimeout)

sut.startChild(operation: fixture.transactionOperation)

Expand All @@ -470,7 +459,8 @@ class SentryTracerTests: XCTestCase {
}

func testIdleTimeoutWithRealDispatchQueue_SpanAdded_IdleTimeoutCancelled() {
let sut = fixture.getSut(idleTimeout: 0.1, dispatchQueueWrapper: SentryDispatchQueueWrapper())
SentryDependencyContainer.sharedInstance().dispatchQueueWrapper = SentryDispatchQueueWrapper()
let sut = fixture.getSut(idleTimeout: 0.1)

let child = sut.startChild(operation: fixture.transactionOperation)
let grandChild = child.startChild(operation: fixture.transactionOperation)
Expand All @@ -480,10 +470,11 @@ class SentryTracerTests: XCTestCase {
delayNonBlocking(timeout: 0.5)

assertOneTransactionCaptured(sut)
SentryDependencyContainer.sharedInstance().dispatchQueueWrapper = fixture.dispatchQueue
}

func testIdleTimeout_TwoChildren_FirstFinishes_WaitsForTheOther() {
let sut = fixture.getSut(idleTimeout: fixture.idleTimeout, dispatchQueueWrapper: fixture.dispatchQueue)
let sut = fixture.getSut(idleTimeout: fixture.idleTimeout)

let child1 = sut.startChild(operation: fixture.transactionOperation)
let child2 = sut.startChild(operation: fixture.transactionOperation)
Expand All @@ -501,7 +492,7 @@ class SentryTracerTests: XCTestCase {
}

func testIdleTimeout_ChildSpanFinished_IdleStarted() {
let sut = fixture.getSut(idleTimeout: fixture.idleTimeout, dispatchQueueWrapper: fixture.dispatchQueue)
let sut = fixture.getSut(idleTimeout: fixture.idleTimeout)
XCTAssertEqual(1, fixture.dispatchQueue.dispatchAfterInvocations.count)

let child = sut.startChild(operation: fixture.transactionOperation)
Expand All @@ -524,7 +515,7 @@ class SentryTracerTests: XCTestCase {
}

func testIdleTimeout_TimesOut_TrimsEndTimestamp() {
let sut = fixture.getSut(idleTimeout: fixture.idleTimeout, dispatchQueueWrapper: fixture.dispatchQueue)
let sut = fixture.getSut(idleTimeout: fixture.idleTimeout)

let child1 = sut.startChild(operation: fixture.transactionOperation)
advanceTime(bySeconds: 1.0)
Expand All @@ -543,7 +534,7 @@ class SentryTracerTests: XCTestCase {
}

func testIdleTimeout_CallFinish_TrimsEndTimestamp() {
let sut = fixture.getSut(idleTimeout: fixture.idleTimeout, dispatchQueueWrapper: fixture.dispatchQueue)
let sut = fixture.getSut(idleTimeout: fixture.idleTimeout)

let child = sut.startChild(operation: fixture.transactionOperation)
advanceTime(bySeconds: 1.0)
Expand All @@ -566,7 +557,7 @@ class SentryTracerTests: XCTestCase {

// Interact with sut in extra function so ARC deallocates it
func getSut() {
let sut = fixture.getSut(idleTimeout: fixture.idleTimeout, dispatchQueueWrapper: fixture.dispatchQueue)
let sut = fixture.getSut(idleTimeout: fixture.idleTimeout)

_ = sut.startChild(operation: fixture.transactionOperation)
}
Expand Down Expand Up @@ -597,7 +588,7 @@ class SentryTracerTests: XCTestCase {
}

func testIdleTimeoutWithUnfinishedChildren_TimesOut_TrimsEndTimestamp() {
let sut = fixture.getSut(idleTimeout: fixture.idleTimeout, dispatchQueueWrapper: fixture.dispatchQueue)
let sut = fixture.getSut(idleTimeout: fixture.idleTimeout)

let child1 = sut.startChild(operation: fixture.transactionOperation)
advanceTime(bySeconds: 1.0)
Expand All @@ -615,7 +606,7 @@ class SentryTracerTests: XCTestCase {
}

func testIdleTimeout_CallFinish_WaitsForChildren_DoesntStartTimeout() {
let sut = fixture.getSut(idleTimeout: fixture.idleTimeout, dispatchQueueWrapper: fixture.dispatchQueue)
let sut = fixture.getSut(idleTimeout: fixture.idleTimeout)

let child = sut.startChild(operation: fixture.transactionOperation)
XCTAssertEqual(1, fixture.dispatchQueue.dispatchAfterInvocations.count)
Expand Down Expand Up @@ -913,7 +904,7 @@ class SentryTracerTests: XCTestCase {
func testFinishCallback_CalledWhenTracerFinishes() {
let callbackExpectation = expectation(description: "FinishCallback called")

let sut = fixture.getSut(idleTimeout: fixture.idleTimeout, dispatchQueueWrapper: fixture.dispatchQueue)
let sut = fixture.getSut(idleTimeout: fixture.idleTimeout)

let block: (SentryTracer) -> Void = { tracer in
XCTAssertEqual(sut, tracer)
Expand Down

0 comments on commit d78ee13

Please sign in to comment.