From 08abc3a02cd372be09a2dc2da3b10ebf0c376d69 Mon Sep 17 00:00:00 2001 From: Fabian Fett Date: Wed, 18 Mar 2026 16:25:06 +0100 Subject: [PATCH] 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. --- .../tests_04_performance/Thresholds/6.0.json | 4 +- .../tests_04_performance/Thresholds/6.1.json | 4 +- .../tests_04_performance/Thresholds/6.2.json | 4 +- .../Thresholds/nightly-main.json | 6 +- .../Thresholds/nightly-next.json | 6 +- .../FileSystemTests.swift | 55 +++++++++++++++---- 6 files changed, 57 insertions(+), 22 deletions(-) diff --git a/IntegrationTests/tests_04_performance/Thresholds/6.0.json b/IntegrationTests/tests_04_performance/Thresholds/6.0.json index 3b1f86fd5..d772e9f5f 100644 --- a/IntegrationTests/tests_04_performance/Thresholds/6.0.json +++ b/IntegrationTests/tests_04_performance/Thresholds/6.0.json @@ -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, diff --git a/IntegrationTests/tests_04_performance/Thresholds/6.1.json b/IntegrationTests/tests_04_performance/Thresholds/6.1.json index 3b1f86fd5..d772e9f5f 100644 --- a/IntegrationTests/tests_04_performance/Thresholds/6.1.json +++ b/IntegrationTests/tests_04_performance/Thresholds/6.1.json @@ -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, diff --git a/IntegrationTests/tests_04_performance/Thresholds/6.2.json b/IntegrationTests/tests_04_performance/Thresholds/6.2.json index f0139d282..c1f1f84fd 100644 --- a/IntegrationTests/tests_04_performance/Thresholds/6.2.json +++ b/IntegrationTests/tests_04_performance/Thresholds/6.2.json @@ -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, diff --git a/IntegrationTests/tests_04_performance/Thresholds/nightly-main.json b/IntegrationTests/tests_04_performance/Thresholds/nightly-main.json index 8fab115da..e5f1af058 100644 --- a/IntegrationTests/tests_04_performance/Thresholds/nightly-main.json +++ b/IntegrationTests/tests_04_performance/Thresholds/nightly-main.json @@ -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, diff --git a/IntegrationTests/tests_04_performance/Thresholds/nightly-next.json b/IntegrationTests/tests_04_performance/Thresholds/nightly-next.json index f0139d282..b87aad146 100644 --- a/IntegrationTests/tests_04_performance/Thresholds/nightly-next.json +++ b/IntegrationTests/tests_04_performance/Thresholds/nightly-next.json @@ -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, diff --git a/Tests/NIOFSIntegrationTests/FileSystemTests.swift b/Tests/NIOFSIntegrationTests/FileSystemTests.swift index ed82cb5d5..00457d5e1 100644 --- a/Tests/NIOFSIntegrationTests/FileSystemTests.swift +++ b/Tests/NIOFSIntegrationTests/FileSystemTests.swift @@ -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-") {