Skip to content

Commit 7900fed

Browse files
ahoppenfabianfett
andauthored
Fix a race condition in testCopyFromHasWriteBackpressure (#614)
The test failed non-deterministically if `mockBackend` was executed before the `writeData` closure started executing in the background. Modify the test slightly to wait in `mockBackend` until the client started writing data. Also improve it slightly to reduce the likelihood of `isWriting` being `true` before `writer.write` is executed by adding a short `sleep` call. Co-authored-by: Fabian Fett <[email protected]>
1 parent 7383207 commit 7900fed

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

Tests/PostgresNIOTests/New/PostgresConnectionTests.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -873,9 +873,22 @@ import Synchronization
873873
} preCopyInResponse: { channel in
874874
channel.isWritable = false
875875
} mockBackend: { channel, _ in
876+
// The `writeData` closure is executed on a background task. Ensure it started executing before we proceed
877+
// with the backend mock. This typically doesn't enter the retry loop at all.
878+
var isWritingReloadCounter = 0
879+
while !isWriting.load(ordering: .sequentiallyConsistent), isWritingReloadCounter < 100 {
880+
try await Task.sleep(for: .milliseconds(10))
881+
isWritingReloadCounter += 1
882+
}
876883
let isWriting = isWriting.load(ordering: .sequentiallyConsistent)
877884
#expect(isWriting)
878885

886+
// Wait for another 10ms to ensure the `writer.write` call did indeed start and tried to write data, just
887+
// being blocked on the backpressure.
888+
try await Task.sleep(for: .milliseconds(10))
889+
890+
// Now that we know `writeData` is blocked, relieve the write backpressure and check that the copy operation
891+
// finishes.
879892
channel.isWritable = true
880893
channel.pipeline.fireChannelWritabilityChanged()
881894

0 commit comments

Comments
 (0)