Skip to content

Commit

Permalink
test: Use Invocation in TestHub (#2871)
Browse files Browse the repository at this point in the history
Use thread safe Invocation in TestHub as running the tests locally
sometimes lead to crashes in capturedTransactionsWithScope.
Furthermore, remove DispatchGroup so we can remove plenty
of calls to fixture.hub.group.wait().
  • Loading branch information
philipphofmann committed Apr 7, 2023
1 parent e681215 commit 2cf1b84
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 42 deletions.
2 changes: 1 addition & 1 deletion SentryTestUtils/TestSentryNSTimerWrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class TestSentryNSTimerWrapper: SentryNSTimerWrapper {
var block: ((Timer) -> Void)?
}

public lazy var overrides = Overrides()
public var overrides = Overrides()

public override func scheduledTimer(withTimeInterval interval: TimeInterval, repeats: Bool, block: @escaping (Timer) -> Void) -> Timer {
let timer = TestTimer()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -625,8 +625,6 @@ class SentryNetworkTrackerTests: XCTestCase {

sut.urlSessionTask(task, setState: .completed)

fixture.hub.group.wait()

guard let envelope = self.fixture.hub.capturedEventsWithScopes.first else {
XCTFail("Expected to capture 1 event")
return
Expand Down Expand Up @@ -656,8 +654,6 @@ class SentryNetworkTrackerTests: XCTestCase {

sut.urlSessionTask(task, setState: .completed)

fixture.hub.group.wait()

guard let envelope = self.fixture.hub.capturedEventsWithScopes.first else {
XCTFail("Expected to capture 1 event")
return
Expand All @@ -677,8 +673,6 @@ class SentryNetworkTrackerTests: XCTestCase {

sut.urlSessionTask(task, setState: .completed)

fixture.hub.group.wait()

guard let envelope = self.fixture.hub.capturedEventsWithScopes.first else {
XCTFail("Expected to capture 1 event")
return
Expand Down
25 changes: 10 additions & 15 deletions Tests/SentryTests/State/TestHub.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import Foundation
import SentryTestUtils

class TestHub: SentryHub {

let group = DispatchGroup()
let queue = DispatchQueue(label: "TestHub", attributes: .concurrent)

var startSessionInvocations: Int = 0
var closeCachedSessionInvocations: Int = 0
Expand All @@ -23,30 +21,27 @@ class TestHub: SentryHub {
endSessionTimestamp = timestamp
}

var sentCrashEvents: [Event] = []
var sentCrashEvents = Invocations<Event>()
override func captureCrash(_ event: Event) {
sentCrashEvents.append(event)
sentCrashEvents.record(event)
}

var sentCrashEventsWithScope: [(event: Event, scope: Scope)] = []
var sentCrashEventsWithScope = Invocations<(event: Event, scope: Scope)>()
override func captureCrash(_ event: Event, with scope: Scope) {
sentCrashEventsWithScope.append((event, scope))
sentCrashEventsWithScope.record((event, scope))
}

var capturedEventsWithScopes: [(event: Event, scope: Scope, additionalEnvelopeItems: [SentryEnvelopeItem])] = []
var capturedEventsWithScopes = Invocations<(event: Event, scope: Scope, additionalEnvelopeItems: [SentryEnvelopeItem])>()
override func capture(event: Event, scope: Scope, additionalEnvelopeItems: [SentryEnvelopeItem]) -> SentryId {
group.enter()
queue.async(flags: .barrier) {
self.capturedEventsWithScopes.append((event, scope, additionalEnvelopeItems))
self.group.leave()
}

self.capturedEventsWithScopes.record((event, scope, additionalEnvelopeItems))

return event.eventId
}

var capturedTransactionsWithScope: [(transaction: [String: Any], scope: Scope)] = []
var capturedTransactionsWithScope = Invocations<(transaction: [String: Any], scope: Scope)>()
override func capture(_ transaction: Transaction, with scope: Scope) -> SentryId {
capturedTransactionsWithScope.append((transaction.serialize(), scope))
capturedTransactionsWithScope.record((transaction.serialize(), scope))
return super.capture(transaction, with: scope)
}
}
22 changes: 2 additions & 20 deletions Tests/SentryTests/Transaction/SentryTracerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,6 @@ class SentryTracerTests: XCTestCase {
let sut = fixture.getSut()
advanceTime(bySeconds: 1)
sut.finish()
fixture.hub.group.wait()

XCTAssertEqual(1, fixture.hub.capturedEventsWithScopes.count)

Expand All @@ -623,16 +622,14 @@ class SentryTracerTests: XCTestCase {
advanceTime(bySeconds: 0.5)
secondTransaction.finish()

fixture.hub.group.wait()
XCTAssertEqual(1, fixture.hub.capturedEventsWithScopes.count)
let serializedSecondTransaction = fixture.hub.capturedEventsWithScopes.first!.event.serialize()
XCTAssertNil(serializedSecondTransaction["measurements"])

firstTransaction.finish()
fixture.hub.group.wait()

XCTAssertEqual(2, fixture.hub.capturedEventsWithScopes.count)
assertAppStartMeasurementOn(transaction: fixture.hub.capturedEventsWithScopes[1].event as! Transaction, appStartMeasurement: appStartMeasurement)
assertAppStartMeasurementOn(transaction: fixture.hub.capturedEventsWithScopes.invocations[1].event as! Transaction, appStartMeasurement: appStartMeasurement)
}

func testAddUnknownAppStartMeasurement_NotPutOnNextTransaction() {
Expand All @@ -647,7 +644,6 @@ class SentryTracerTests: XCTestCase {
))

fixture.getSut().finish()
fixture.hub.group.wait()

assertAppStartMeasurementNotPutOnTransaction()
}
Expand Down Expand Up @@ -694,7 +690,6 @@ class SentryTracerTests: XCTestCase {

let sut = fixture.hub.startTransaction(transactionContext: TransactionContext(name: "custom", operation: "custom")) as! SentryTracer
sut.finish()
fixture.hub.group.wait()

XCTAssertNotNil(SentrySDK.getAppStartMeasurement())

Expand All @@ -718,7 +713,6 @@ class SentryTracerTests: XCTestCase {
let sut = fixture.getSut()
advanceTime(bySeconds: 1.0)
sut.finish()
fixture.hub.group.wait()

assertAppStartMeasurementNotPutOnTransaction()
}
Expand All @@ -732,7 +726,6 @@ class SentryTracerTests: XCTestCase {
let sut = fixture.getSut()
advanceTime(bySeconds: 1.0)
sut.finish()
fixture.hub.group.wait()

assertAppStartMeasurementNotPutOnTransaction()
}
Expand All @@ -744,7 +737,6 @@ class SentryTracerTests: XCTestCase {

let sut = fixture.getSut()
sut.finish()
fixture.hub.group.wait()

assertAppStartMeasurementNotPutOnTransaction()
}
Expand All @@ -760,7 +752,6 @@ class SentryTracerTests: XCTestCase {
childSpan.setMeasurement(name: name, value: value, unit: unit)
childSpan.finish()
sut.finish()
fixture.hub.group.wait()

XCTAssertEqual(1, fixture.hub.capturedEventsWithScopes.count)
let serializedTransaction = fixture.hub.capturedEventsWithScopes.first?.event.serialize()
Expand Down Expand Up @@ -893,11 +884,9 @@ class SentryTracerTests: XCTestCase {
queue.activate()
group.wait()

fixture.hub.group.wait()

XCTAssertEqual(transactions, fixture.hub.capturedEventsWithScopes.count)

let transactionsWithAppStartMeasurement = fixture.hub.capturedEventsWithScopes.filter { pair in
let transactionsWithAppStartMeasurement = fixture.hub.capturedEventsWithScopes.invocations.filter { pair in
let serializedTransaction = pair.event.serialize()
let measurements = serializedTransaction["measurements"] as? [String: [String: Int]]
return measurements == ["app_start_warm": ["value": 500]]
Expand Down Expand Up @@ -970,8 +959,6 @@ class SentryTracerTests: XCTestCase {

sut.finish()

fixture.hub.group.wait()

XCTAssertEqual(1, fixture.hub.capturedEventsWithScopes.count)
let serializedTransaction = fixture.hub.capturedEventsWithScopes.first!.event.serialize()
let measurements = serializedTransaction["measurements"] as? [String: [String: Int]]
Expand Down Expand Up @@ -1023,16 +1010,13 @@ class SentryTracerTests: XCTestCase {
let sut = fixture.getSut()
sut.updateStartTime(fixture.appStartEnd.addingTimeInterval(startTimestamp))
sut.finish()
fixture.hub.group.wait()
}

private func assertTransactionNotCaptured(_ tracer: SentryTracer) {
fixture.hub.group.wait()
XCTAssertEqual(0, fixture.hub.capturedEventsWithScopes.count)
}

private func assertOneTransactionCaptured(_ tracer: SentryTracer) {
fixture.hub.group.wait()
XCTAssertTrue(tracer.isFinished)
XCTAssertEqual(1, fixture.hub.capturedEventsWithScopes.count)
}
Expand Down Expand Up @@ -1115,8 +1099,6 @@ class SentryTracerTests: XCTestCase {
}

private func assertNoMeasurementsAdded() {
fixture.hub.group.wait()

XCTAssertEqual(1, fixture.hub.capturedEventsWithScopes.count)
let serializedTransaction = fixture.hub.capturedEventsWithScopes.first?.event.serialize()
XCTAssertNil(serializedTransaction?["measurements"])
Expand Down

0 comments on commit 2cf1b84

Please sign in to comment.