Skip to content

Commit ef46b05

Browse files
test: Use XCTestExpectation for SentryTracer (#5902)
Replace DispatchGroup with XCTextExpectation in SentryTracerTests.
1 parent 568d9b3 commit ef46b05

File tree

1 file changed

+35
-22
lines changed

1 file changed

+35
-22
lines changed

Tests/SentryTests/Transaction/SentryTracerTests.swift

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,13 +1146,16 @@ class SentryTracerTests: XCTestCase {
11461146
let child = sut.startChild(operation: fixture.transactionOperation)
11471147
sut.finish()
11481148

1149-
let queue = DispatchQueue(label: "SentryTracerTests", attributes: [.concurrent, .initiallyInactive])
1150-
let group = DispatchGroup()
1151-
11521149
let children = 5
11531150
let grandchildren = 10
1151+
1152+
let queue = DispatchQueue(label: "SentryTracerTests", attributes: [.concurrent, .initiallyInactive])
1153+
let expectation = XCTestExpectation(description: "Finish Children Async")
1154+
expectation.expectedFulfillmentCount = children
1155+
expectation.assertForOverFulfill = true
1156+
11541157
for _ in 0 ..< children {
1155-
group.enter()
1158+
11561159
queue.async {
11571160
let grandChild = child.startChild(operation: self.fixture.transactionOperation)
11581161
for _ in 0 ..< grandchildren {
@@ -1162,12 +1165,14 @@ class SentryTracerTests: XCTestCase {
11621165

11631166
grandChild.finish()
11641167
self.assertTransactionNotCaptured(sut)
1165-
group.leave()
1168+
1169+
expectation.fulfill()
11661170
}
11671171
}
11681172

11691173
queue.activate()
1170-
group.wait()
1174+
1175+
wait(for: [expectation], timeout: 10.0)
11711176

11721177
child.finish()
11731178

@@ -1225,32 +1230,40 @@ class SentryTracerTests: XCTestCase {
12251230
}
12261231

12271232
let queue = DispatchQueue(label: "SentryTracerTests", attributes: [.concurrent, .initiallyInactive])
1228-
let group = DispatchGroup()
12291233

1230-
func addChildrenAsync() {
1231-
for _ in 0 ..< 100 {
1232-
group.enter()
1233-
queue.async {
1234-
let child = sut.startChild(operation: self.fixture.transactionOperation)
1235-
Dynamic(child).frames = [] as [Frame]
1236-
child.finish()
1237-
group.leave()
1238-
}
1234+
let addChildrenCount = 100
1235+
let expectation = XCTestExpectation(description: "Add spans while finishing")
1236+
expectation.expectedFulfillmentCount = addChildrenCount * 2 + 1
1237+
expectation.assertForOverFulfill = true
1238+
1239+
for _ in 0 ..< 100 {
1240+
queue.async {
1241+
let child = sut.startChild(operation: self.fixture.transactionOperation)
1242+
Dynamic(child).frames = [] as [Frame]
1243+
child.finish()
1244+
1245+
expectation.fulfill()
12391246
}
12401247
}
12411248

1242-
addChildrenAsync()
1243-
1244-
group.enter()
12451249
queue.async {
12461250
sut.finish()
1247-
group.leave()
1251+
expectation.fulfill()
12481252
}
12491253

1250-
addChildrenAsync()
1254+
for _ in 0 ..< 100 {
1255+
queue.async {
1256+
let child = sut.startChild(operation: self.fixture.transactionOperation)
1257+
Dynamic(child).frames = [] as [Frame]
1258+
child.finish()
1259+
1260+
expectation.fulfill()
1261+
}
1262+
}
12511263

12521264
queue.activate()
1253-
group.wait()
1265+
1266+
wait(for: [expectation], timeout: 10.0)
12541267

12551268
let spans = try XCTUnwrap(try getSerializedTransaction()["spans"]! as? [[String: Any]])
12561269
XCTAssertGreaterThanOrEqual(spans.count, children)

0 commit comments

Comments
 (0)