Skip to content

Commit b5a7583

Browse files
authored
ci: Fix flaky flush test (#5824)
* ci: Fix flaky flush test * ci: Fix flaky flush test * ci: Fix flaky flush test
1 parent d3b0d62 commit b5a7583

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

Tests/SentryTests/Networking/SentryHttpTransportFlushIntegrationTests.swift

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@ final class SentryHttpTransportFlushIntegrationTests: XCTestCase {
8888
func testFlush_CalledMultipleTimes_ImmediatelyReturnsFalse() throws {
8989
let (sut, requestManager, _, dispatchQueueWrapper) = try getSut()
9090

91+
// This must be long enough that all the threads we start below get to run
92+
// while the first call to flush is still blocking
93+
let flushTimeout = 10.0
94+
requestManager.waitForResponseDispatchGroup = true
95+
requestManager.responseDispatchGroup.enter()
96+
9197
requestManager.returnResponse(response: nil)
9298
for _ in 0..<30 {
9399
sut.send(envelope: SentryEnvelope(event: Event()))
@@ -96,24 +102,19 @@ final class SentryHttpTransportFlushIntegrationTests: XCTestCase {
96102
waitForEnvelopeToBeStored(dispatchQueueWrapper)
97103
requestManager.returnResponse(response: HTTPURLResponse())
98104

99-
let flushTimeout = 0.1
100-
requestManager.waitForResponseDispatchGroup = true
101-
requestManager.responseDispatchGroup.enter()
102-
103-
let allFlushCallsGroup = DispatchGroup()
105+
let initialFlushCallGroup = DispatchGroup()
104106
let ensureFlushingGroup = DispatchGroup()
105107
let ensureFlushingQueue = DispatchQueue(label: "First flushing")
106108

107109
sut.setStartFlushCallback {
108110
ensureFlushingGroup.leave()
109111
}
110112

111-
allFlushCallsGroup.enter()
113+
initialFlushCallGroup.enter()
112114
ensureFlushingGroup.enter()
113115
ensureFlushingQueue.async {
114-
XCTAssertEqual(.timedOut, sut.flush(flushTimeout))
115-
requestManager.responseDispatchGroup.leave()
116-
allFlushCallsGroup.leave()
116+
XCTAssertEqual(.success, sut.flush(flushTimeout), "Initial call to flush should succeed")
117+
initialFlushCallGroup.leave()
117118
}
118119

119120
// Ensure transport is flushing.
@@ -122,20 +123,23 @@ final class SentryHttpTransportFlushIntegrationTests: XCTestCase {
122123
// Now the transport should also have left the synchronized block, and the
123124
// flush should return immediately.
124125

126+
let parallelFlushCallsGroup = DispatchGroup()
125127
let initiallyInactiveQueue = DispatchQueue(label: "testFlush_CalledMultipleTimes_ImmediatelyReturnsFalse", qos: .userInitiated, attributes: [.concurrent, .initiallyInactive])
126128
for _ in 0..<2 {
127-
allFlushCallsGroup.enter()
129+
parallelFlushCallsGroup.enter()
128130
initiallyInactiveQueue.async {
129131
for _ in 0..<10 {
130132
XCTAssertEqual(.alreadyFlushing, sut.flush(flushTimeout), "Flush should have returned immediately")
131133
}
132134

133-
allFlushCallsGroup.leave()
135+
parallelFlushCallsGroup.leave()
134136
}
135137
}
136138

137139
initiallyInactiveQueue.activate()
138-
allFlushCallsGroup.waitWithTimeout()
140+
parallelFlushCallsGroup.waitWithTimeout()
141+
requestManager.responseDispatchGroup.leave()
142+
initialFlushCallGroup.waitWithTimeout()
139143
}
140144

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

0 commit comments

Comments
 (0)