Fix flaky testCopyFileReplacingExistingFileSucceeds (#3554)

### Motivation
`testCopyFileReplacingExistingFileSucceeds` (and
`testCopySymlinkReplacingExistingSymlinkSucceeds`) verify that no .tmp-
prefixed files are left behind after a `copyItem(replaceExisting: true)`
call on Linux. However, both tests create their files in the shared
system temp directory (e.g. /tmp) and then scan that entire directory
for .tmp- files. When other tests run in parallel, their in-flight
`copyItem(replaceExisting: true)` operations leave transient .tmp- files
in the same directory, causing spurious failures.

### Changes

- Both tests now create a dedicated subdirectory for their files, so the
temp file scan only sees files belonging to that specific test.
- Cleanup is handled via `addTeardownBlock` to ensure the subdirectory
is removed even if the test fails early.

### Results

The temp file assertions are no longer affected by concurrent test
activity or other processes writing to the shared temp directory,
eliminating the flakiness.
This commit is contained in:
Fabian Fett authored and GitHub committed 2026-03-18 15:25:06 +00:00
1 parent 9939a5cf92
commit 08abc3a02c
6 files changed
+57 -22

No files matched your search

@@ -20,7 +20,7 @@
"1000_udpbootstraps": 2050,
"1000_udpconnections": 75050,
"1_reqs_1000_conn": 375050,
"assume_isolated_scheduling_10000_executions": 89,
"assume_isolated_scheduling_10000_executions": 88,
"bytebuffer_lots_of_rw": 2050,
"creating_10000_headers": 0,
"decode_1000_ws_frames": 2050,
@@ -51,7 +51,7 @@
"schedule_assume_isolated_10000_tasks": 40100,
"schedule_with_deadline_10000_tasks": 40100,
"schedule_with_deadline_assume_isolated_10000_tasks": 40100,
"scheduling_10000_executions": 89,
"scheduling_10000_executions": 88,
"submit_10000_tasks": 20100,
"submit_assume_isolated_10000_tasks": 20100,
"udp_1000_reqs_1_conn": 6200,
@@ -20,7 +20,7 @@
"1000_udpbootstraps": 2050,
"1000_udpconnections": 75050,
"1_reqs_1000_conn": 375050,
"assume_isolated_scheduling_10000_executions": 89,
"assume_isolated_scheduling_10000_executions": 88,
"bytebuffer_lots_of_rw": 2050,
"creating_10000_headers": 0,
"decode_1000_ws_frames": 2050,
@@ -51,7 +51,7 @@
"schedule_assume_isolated_10000_tasks": 40100,
"schedule_with_deadline_10000_tasks": 40100,
"schedule_with_deadline_assume_isolated_10000_tasks": 40100,
"scheduling_10000_executions": 89,
"scheduling_10000_executions": 88,
"submit_10000_tasks": 20100,
"submit_assume_isolated_10000_tasks": 20100,
"udp_1000_reqs_1_conn": 6200,
@@ -20,7 +20,7 @@
"1000_udpbootstraps": 2050,
"1000_udpconnections": 75050,
"1_reqs_1000_conn": 375050,
"assume_isolated_scheduling_10000_executions": 89,
"assume_isolated_scheduling_10000_executions": 88,
"bytebuffer_lots_of_rw": 2050,
"creating_10000_headers": 0,
"decode_1000_ws_frames": 2050,
@@ -51,7 +51,7 @@
"schedule_assume_isolated_10000_tasks": 40100,
"schedule_with_deadline_10000_tasks": 40100,
"schedule_with_deadline_assume_isolated_10000_tasks": 40100,
"scheduling_10000_executions": 89,
"scheduling_10000_executions": 88,
"submit_10000_tasks": 20100,
"submit_assume_isolated_10000_tasks": 20100,
"udp_1000_reqs_1_conn": 6200,
@@ -1,5 +1,5 @@
{
"10000000_asyncsequenceproducer": 18,
"10000000_asyncsequenceproducer": 21,
"1000000_asyncwriter": 1000050,
"1000_addHandlers": 46050,
"1000_addHandlers_sync": 39050,
@@ -20,7 +20,7 @@
"1000_udpbootstraps": 2050,
"1000_udpconnections": 74050,
"1_reqs_1000_conn": 372050,
"assume_isolated_scheduling_10000_executions": 89,
"assume_isolated_scheduling_10000_executions": 88,
"bytebuffer_lots_of_rw": 2050,
"creating_10000_headers": 0,
"decode_1000_ws_frames": 2050,
@@ -51,7 +51,7 @@
"schedule_assume_isolated_10000_tasks": 40100,
"schedule_with_deadline_10000_tasks": 40100,
"schedule_with_deadline_assume_isolated_10000_tasks": 40100,
"scheduling_10000_executions": 89,
"scheduling_10000_executions": 88,
"submit_10000_tasks": 20100,
"submit_assume_isolated_10000_tasks": 20100,
"udp_1000_reqs_1_conn": 6200,
@@ -1,5 +1,5 @@
{
"10000000_asyncsequenceproducer": 18,
"10000000_asyncsequenceproducer": 21,
"1000000_asyncwriter": 1000050,
"1000_addHandlers": 46050,
"1000_addHandlers_sync": 39050,
@@ -20,7 +20,7 @@
"1000_udpbootstraps": 2050,
"1000_udpconnections": 75050,
"1_reqs_1000_conn": 375050,
"assume_isolated_scheduling_10000_executions": 89,
"assume_isolated_scheduling_10000_executions": 88,
"bytebuffer_lots_of_rw": 2050,
"creating_10000_headers": 0,
"decode_1000_ws_frames": 2050,
@@ -51,7 +51,7 @@
"schedule_assume_isolated_10000_tasks": 40100,
"schedule_with_deadline_10000_tasks": 40100,
"schedule_with_deadline_assume_isolated_10000_tasks": 40100,
"scheduling_10000_executions": 89,
"scheduling_10000_executions": 88,
"submit_10000_tasks": 20100,
"submit_assume_isolated_10000_tasks": 20100,
"udp_1000_reqs_1_conn": 6200,
@@ -2007,8 +2007,27 @@ extension FileSystemTests {
}
func testCopyFileReplacingExistingFileSucceeds() async throws {
// Use a dedicated subdirectory so the temp file check isn't polluted by
// concurrent tests or other processes writing to the shared temp directory.
let testDirectory = try await self.fs.temporaryFilePath()
try await self.fs.createDirectory(
at: testDirectory,
withIntermediateDirectories: false,
permissions: .ownerReadWriteExecute
)
let fs = self.fs
self.addTeardownBlock {
// clean up the test directory
_ = try await fs.removeItem(
at: testDirectory,
strategy: .platformDefault,
recursively: true
)
}
let sourceFileContent: [UInt8] = [1, 2, 3]
let source = try await self.fs.temporaryFilePath()
let source = self.fs.temporaryFilePath(inDirectory: testDirectory.underlying)
_ = try await self.fs.withFileHandle(
forWritingAt: source,
options: .newFile(replaceExisting: false, permissions: .ownerReadWrite)
@@ -2020,7 +2039,7 @@ extension FileSystemTests {
}
let existingFileContent: [UInt8] = [4, 5, 6]
let destination = try await self.fs.temporaryFilePath()
let destination = self.fs.temporaryFilePath(inDirectory: testDirectory.underlying)
_ = try await self.fs.withFileHandle(
forWritingAt: destination,
options: .newFile(replaceExisting: false, permissions: .ownerReadWriteExecute)
@@ -2062,8 +2081,7 @@ extension FileSystemTests {
// verify no temp files are left (Linux only - Darwin uses COPYFILE_UNLINK)
#if canImport(Glibc) || canImport(Musl) || canImport(Bionic)
let destinationDirectory = NIOFilePath(destination.underlying.removingLastComponent())
let temporaryFiles = try await self.fs.withDirectoryHandle(atPath: destinationDirectory) { dir in
let temporaryFiles = try await self.fs.withDirectoryHandle(atPath: testDirectory) { dir in
var temporaryFiles: [String] = []
for try await batch in dir.listContents().batched() {
for entry in batch where entry.name.hasPrefix(".tmp-") {
@@ -2115,23 +2133,41 @@ extension FileSystemTests {
}
func testCopySymlinkReplacingExistingSymlinkSucceeds() async throws {
let sourceTarget = try await self.fs.temporaryFilePath()
// Use a dedicated subdirectory so the temp symlink check isn't polluted by
// concurrent tests or other processes writing to the shared temp directory.
let testDirectory = try await self.fs.temporaryFilePath()
try await self.fs.createDirectory(
at: testDirectory,
withIntermediateDirectories: false,
permissions: .ownerReadWriteExecute
)
let fs = self.fs
self.addTeardownBlock {
// clean up the test directory
_ = try await fs.removeItem(
at: testDirectory,
strategy: .platformDefault,
recursively: true
)
}
let sourceTarget = self.fs.temporaryFilePath(inDirectory: testDirectory.underlying)
try await self.fs.withFileHandle(
forWritingAt: sourceTarget,
options: .newFile(replaceExisting: false)
) { _ in }
let sourceSymlink = try await self.fs.temporaryFilePath()
let sourceSymlink = self.fs.temporaryFilePath(inDirectory: testDirectory.underlying)
try await self.fs.createSymbolicLink(
at: sourceSymlink,
withDestination: sourceTarget
)
let destinationTarget = try await self.fs.temporaryFilePath()
let destinationTarget = self.fs.temporaryFilePath(inDirectory: testDirectory.underlying)
try await self.fs.withFileHandle(
forWritingAt: destinationTarget,
options: .newFile(replaceExisting: false)
) { _ in }
let destinationSymlink = try await self.fs.temporaryFilePath()
let destinationSymlink = self.fs.temporaryFilePath(inDirectory: testDirectory.underlying)
try await self.fs.createSymbolicLink(
at: destinationSymlink,
withDestination: destinationTarget
@@ -2155,8 +2191,7 @@ extension FileSystemTests {
XCTAssertEqual(sourceTargetAfterCopy, sourceTarget)
// verify no temp symlinks are left
let destinationDirectory = NIOFilePath(destinationSymlink.underlying.removingLastComponent())
let temporarySymlinks = try await self.fs.withDirectoryHandle(atPath: destinationDirectory) { dir in
let temporarySymlinks = try await self.fs.withDirectoryHandle(atPath: testDirectory) { dir in
var temporarySymlinks: [String] = []
for try await batch in dir.listContents().batched() {
for entry in batch where entry.name.hasPrefix(".tmp-link-") {