Skip to content

Commit 8619e0f

Browse files
test: Fix ConcurrentTransactions_OnlyOneGetsMeasurement (#5747)
Fix the failing SentryTracer.testConcurrentTransactions_OnlyOneGetsMeasurement by removing the qos: .background for the DispatchQueue, which led to timeouts in CI, because the DisptachQueue simply didn't start the transactions.
1 parent c68d42f commit 8619e0f

File tree

1 file changed

+22
-14
lines changed

1 file changed

+22
-14
lines changed

Tests/SentryTests/Transaction/SentryTracerTests.swift

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class SentryTracerTests: XCTestCase {
5353
init() {
5454
SentryDependencyContainer.sharedInstance().dateProvider = currentDateProvider
5555
SentryDependencyContainer.sharedInstance().dispatchQueueWrapper = dispatchQueue
56-
56+
5757
debugImageProvider.debugImages = [TestData.debugImage]
5858
SentryDependencyContainer.sharedInstance().debugImageProvider = debugImageProvider
5959
appStart = currentDateProvider.date()
@@ -62,8 +62,11 @@ class SentryTracerTests: XCTestCase {
6262
transactionContext = TransactionContext(name: transactionName, operation: transactionOperation)
6363

6464
scope = Scope()
65-
client = TestClient(options: Options())
66-
client.options.tracesSampleRate = 1
65+
let options = Options()
66+
options.dsn = TestConstants.dsnAsString(username: "SentryTracerTests")
67+
options.tracesSampleRate = 1
68+
69+
client = TestClient(options: options)
6770
hub = TestHub(client: client, andScope: scope)
6871

6972
#if os(iOS) || os(tvOS) || targetEnvironment(macCatalyst)
@@ -1179,30 +1182,35 @@ class SentryTracerTests: XCTestCase {
11791182
func testConcurrentTransactions_OnlyOneGetsMeasurement() {
11801183
SentrySDKInternal.setAppStartMeasurement(fixture.getAppStartMeasurement(type: .warm))
11811184

1182-
let queue = DispatchQueue(label: "", qos: .background, attributes: [.concurrent, .initiallyInactive] )
1183-
let group = DispatchGroup()
1184-
1185+
let queue = DispatchQueue(label: "testConcurrentTransactions_OnlyOneGetsMeasurement", attributes: [.concurrent, .initiallyInactive] )
1186+
11851187
let transactions = 5
1188+
let finishTransactionExpectation = XCTestExpectation(description: "Finish transactions")
1189+
finishTransactionExpectation.expectedFulfillmentCount = transactions
1190+
11861191
for _ in 0..<transactions {
1187-
group.enter()
11881192
queue.async {
1189-
self.fixture.getSut().finish()
1190-
group.leave()
1193+
let tracer = self.fixture.getSut()
1194+
1195+
tracer.finish()
1196+
finishTransactionExpectation.fulfill()
11911197
}
11921198
}
11931199

11941200
queue.activate()
1195-
group.wait()
1196-
1197-
XCTAssertEqual(transactions, fixture.hub.capturedEventsWithScopes.count)
1198-
1201+
1202+
wait(for: [finishTransactionExpectation], timeout: 10.0)
1203+
1204+
XCTAssertEqual(fixture.hub.capturedEventsWithScopes.count, transactions, "Expected \(transactions) transactions to be captured, but got \(fixture.hub.capturedEventsWithScopes.count)")
1205+
11991206
let transactionsWithAppStartMeasurement = fixture.hub.capturedEventsWithScopes.invocations.filter { pair in
12001207
let serializedTransaction = pair.event.serialize()
12011208
let measurements = serializedTransaction["measurements"] as? [String: [String: Int]]
12021209
return measurements == ["app_start_warm": ["value": 500]]
12031210
}
12041211

1205-
XCTAssertEqual(1, transactionsWithAppStartMeasurement.count)
1212+
XCTAssertEqual(transactionsWithAppStartMeasurement.count, 1, "Only one transaction should have the app start measurement, but got \(transactionsWithAppStartMeasurement.count)")
1213+
12061214
}
12071215

12081216
#endif // os(iOS) || os(tvOS) || targetEnvironment(macCatalyst)

0 commit comments

Comments
 (0)