Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -98,48 +98,58 @@ final class SentryHttpTransportFlushIntegrationTests: XCTestCase {
for _ in 0..<30 {
sut.send(envelope: SentryEnvelope(event: Event()))
}
// Wait until the dispath queue drains to confirm the envelope is stored
// Wait until the dispatch queue drains to confirm the envelope is stored
waitForEnvelopeToBeStored(dispatchQueueWrapper)
requestManager.returnResponse(response: HTTPURLResponse())

let initialFlushCallGroup = DispatchGroup()
let ensureFlushingGroup = DispatchGroup()
let initialFlushCallExpectation = XCTestExpectation(description: "Initial flush call should succeed")
initialFlushCallExpectation.assertForOverFulfill = true

let ensureFlushingExpectation = XCTestExpectation(description: "Ensure flushing is called")
ensureFlushingExpectation.assertForOverFulfill = true

let ensureFlushingQueue = DispatchQueue(label: "First flushing")

sut.setStartFlushCallback {
ensureFlushingGroup.leave()
ensureFlushingExpectation.fulfill()
}

initialFlushCallGroup.enter()
ensureFlushingGroup.enter()
ensureFlushingQueue.async {
XCTAssertEqual(.success, sut.flush(flushTimeout), "Initial call to flush should succeed")
initialFlushCallGroup.leave()
initialFlushCallExpectation.fulfill()
}

// Ensure transport is flushing.
ensureFlushingGroup.waitWithTimeout()
wait(for: [ensureFlushingExpectation], timeout: 10.0)

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

let parallelFlushCallsGroup = DispatchGroup()
let loopCount = 2
let parallelFlushCallsExpectation = XCTestExpectation(description: "Parallel flush calls should return immediately")
parallelFlushCallsExpectation.expectedFulfillmentCount = loopCount
parallelFlushCallsExpectation.assertForOverFulfill = true

let initiallyInactiveQueue = DispatchQueue(label: "testFlush_CalledMultipleTimes_ImmediatelyReturnsFalse", qos: .userInitiated, attributes: [.concurrent, .initiallyInactive])
for _ in 0..<2 {
parallelFlushCallsGroup.enter()
for _ in 0..<loopCount {

initiallyInactiveQueue.async {
for _ in 0..<10 {
XCTAssertEqual(.alreadyFlushing, sut.flush(flushTimeout), "Flush should have returned immediately")
}

parallelFlushCallsGroup.leave()
parallelFlushCallsExpectation.fulfill()
}
}

initiallyInactiveQueue.activate()
parallelFlushCallsGroup.waitWithTimeout()
wait(for: [parallelFlushCallsExpectation], timeout: 10.0)

requestManager.responseDispatchGroup.leave()
initialFlushCallGroup.waitWithTimeout()

// The initial call to flush is blocking and will take some time to finish.
// Therefore, we wait at the end of the test.
wait(for: [initialFlushCallExpectation], timeout: 10.0)
}

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