Skip to content

Commit 568d9b3

Browse files
test: Use XCTestExpectation for FlushTests (#5903)
Replace DispatchGroup with XCTextExpectation in SentryHttpTransportFlushIntegrationTests.
1 parent 6faf565 commit 568d9b3

File tree

1 file changed

+24
-14
lines changed

1 file changed

+24
-14
lines changed

Tests/SentryTests/Networking/SentryHttpTransportFlushIntegrationTests.swift

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -98,48 +98,58 @@ final class SentryHttpTransportFlushIntegrationTests: XCTestCase {
9898
for _ in 0..<30 {
9999
sut.send(envelope: SentryEnvelope(event: Event()))
100100
}
101-
// Wait until the dispath queue drains to confirm the envelope is stored
101+
// Wait until the dispatch queue drains to confirm the envelope is stored
102102
waitForEnvelopeToBeStored(dispatchQueueWrapper)
103103
requestManager.returnResponse(response: HTTPURLResponse())
104104

105-
let initialFlushCallGroup = DispatchGroup()
106-
let ensureFlushingGroup = DispatchGroup()
105+
let initialFlushCallExpectation = XCTestExpectation(description: "Initial flush call should succeed")
106+
initialFlushCallExpectation.assertForOverFulfill = true
107+
108+
let ensureFlushingExpectation = XCTestExpectation(description: "Ensure flushing is called")
109+
ensureFlushingExpectation.assertForOverFulfill = true
110+
107111
let ensureFlushingQueue = DispatchQueue(label: "First flushing")
108112

109113
sut.setStartFlushCallback {
110-
ensureFlushingGroup.leave()
114+
ensureFlushingExpectation.fulfill()
111115
}
112116

113-
initialFlushCallGroup.enter()
114-
ensureFlushingGroup.enter()
115117
ensureFlushingQueue.async {
116118
XCTAssertEqual(.success, sut.flush(flushTimeout), "Initial call to flush should succeed")
117-
initialFlushCallGroup.leave()
119+
initialFlushCallExpectation.fulfill()
118120
}
119121

120122
// Ensure transport is flushing.
121-
ensureFlushingGroup.waitWithTimeout()
123+
wait(for: [ensureFlushingExpectation], timeout: 10.0)
122124

123125
// Now the transport should also have left the synchronized block, and the
124126
// flush should return immediately.
125127

126-
let parallelFlushCallsGroup = DispatchGroup()
128+
let loopCount = 2
129+
let parallelFlushCallsExpectation = XCTestExpectation(description: "Parallel flush calls should return immediately")
130+
parallelFlushCallsExpectation.expectedFulfillmentCount = loopCount
131+
parallelFlushCallsExpectation.assertForOverFulfill = true
132+
127133
let initiallyInactiveQueue = DispatchQueue(label: "testFlush_CalledMultipleTimes_ImmediatelyReturnsFalse", qos: .userInitiated, attributes: [.concurrent, .initiallyInactive])
128-
for _ in 0..<2 {
129-
parallelFlushCallsGroup.enter()
134+
for _ in 0..<loopCount {
135+
130136
initiallyInactiveQueue.async {
131137
for _ in 0..<10 {
132138
XCTAssertEqual(.alreadyFlushing, sut.flush(flushTimeout), "Flush should have returned immediately")
133139
}
134140

135-
parallelFlushCallsGroup.leave()
141+
parallelFlushCallsExpectation.fulfill()
136142
}
137143
}
138144

139145
initiallyInactiveQueue.activate()
140-
parallelFlushCallsGroup.waitWithTimeout()
146+
wait(for: [parallelFlushCallsExpectation], timeout: 10.0)
147+
141148
requestManager.responseDispatchGroup.leave()
142-
initialFlushCallGroup.waitWithTimeout()
149+
150+
// The initial call to flush is blocking and will take some time to finish.
151+
// Therefore, we wait at the end of the test.
152+
wait(for: [initialFlushCallExpectation], timeout: 10.0)
143153
}
144154

145155
// We use the test name as part of the DSN to ensure that each test runs in isolation.

0 commit comments

Comments
 (0)