From 5fab87834604024ce9990805209cd176f9187e10 Mon Sep 17 00:00:00 2001 From: Philipp Hofmann Date: Thu, 6 Apr 2023 15:29:43 +0200 Subject: [PATCH] test: Use Invocation in TestHub 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(). --- .../TestSentryNSTimerWrapper.swift | 2 +- .../Network/SentryNetworkTrackerTests.swift | 6 ----- Tests/SentryTests/State/TestHub.swift | 25 ++++++++----------- .../Transaction/SentryTracerTests.swift | 22 ++-------------- 4 files changed, 13 insertions(+), 42 deletions(-) diff --git a/SentryTestUtils/TestSentryNSTimerWrapper.swift b/SentryTestUtils/TestSentryNSTimerWrapper.swift index 34b10dfb8cb..5def3be65f8 100644 --- a/SentryTestUtils/TestSentryNSTimerWrapper.swift +++ b/SentryTestUtils/TestSentryNSTimerWrapper.swift @@ -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() diff --git a/Tests/SentryTests/Integrations/Performance/Network/SentryNetworkTrackerTests.swift b/Tests/SentryTests/Integrations/Performance/Network/SentryNetworkTrackerTests.swift index 7641face2c3..d567a7a42e1 100644 --- a/Tests/SentryTests/Integrations/Performance/Network/SentryNetworkTrackerTests.swift +++ b/Tests/SentryTests/Integrations/Performance/Network/SentryNetworkTrackerTests.swift @@ -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 @@ -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 @@ -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 diff --git a/Tests/SentryTests/State/TestHub.swift b/Tests/SentryTests/State/TestHub.swift index a4bc4ea5695..c66c452063f 100644 --- a/Tests/SentryTests/State/TestHub.swift +++ b/Tests/SentryTests/State/TestHub.swift @@ -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 @@ -23,30 +21,27 @@ class TestHub: SentryHub { endSessionTimestamp = timestamp } - var sentCrashEvents: [Event] = [] + var sentCrashEvents = Invocations() 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) } } diff --git a/Tests/SentryTests/Transaction/SentryTracerTests.swift b/Tests/SentryTests/Transaction/SentryTracerTests.swift index 6fd41429f49..f27f3da33b3 100644 --- a/Tests/SentryTests/Transaction/SentryTracerTests.swift +++ b/Tests/SentryTests/Transaction/SentryTracerTests.swift @@ -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) @@ -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() { @@ -647,7 +644,6 @@ class SentryTracerTests: XCTestCase { )) fixture.getSut().finish() - fixture.hub.group.wait() assertAppStartMeasurementNotPutOnTransaction() } @@ -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()) @@ -718,7 +713,6 @@ class SentryTracerTests: XCTestCase { let sut = fixture.getSut() advanceTime(bySeconds: 1.0) sut.finish() - fixture.hub.group.wait() assertAppStartMeasurementNotPutOnTransaction() } @@ -732,7 +726,6 @@ class SentryTracerTests: XCTestCase { let sut = fixture.getSut() advanceTime(bySeconds: 1.0) sut.finish() - fixture.hub.group.wait() assertAppStartMeasurementNotPutOnTransaction() } @@ -744,7 +737,6 @@ class SentryTracerTests: XCTestCase { let sut = fixture.getSut() sut.finish() - fixture.hub.group.wait() assertAppStartMeasurementNotPutOnTransaction() } @@ -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() @@ -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]] @@ -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]] @@ -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) } @@ -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"])