From 18b9b88c6fa6cc2de52c0018970c208e2007b56e Mon Sep 17 00:00:00 2001 From: Paulo Janotti Date: Tue, 29 Oct 2024 16:40:22 -0700 Subject: [PATCH] [chore] Make TestReadRotatingFiles more robust on Windows (#36032) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Make the remove operation an "eventually required" since on Windows you can't delete if there are still handles opened to the file. This should prevent failures like https://github.com/open-telemetry/opentelemetry-collector-contrib/actions/runs/11559461472/job/32174166127#step:8:259 see below. On my box multiple runs were failing but that higher frequency of failures was probably to the usage of antivirus software, this change made it to pass consistently on my box. ```terminal D:/a/opentelemetry-collector-contrib/opentelemetry-collector-contrib/.tools/gotestsum --rerun-fails=1 --packages="./..." -- -race -timeout 600s -parallel 4 --tags="" ✖ . (1.777s) === Failed === FAIL: . TestReadRotatingFiles/CopyTruncate (2.73s) filelog_test.go:194: Error Trace: D:/a/opentelemetry-collector-contrib/opentelemetry-collector-contrib/receiver/filelogreceiver/filelog_test.go:194 Error: Received unexpected error: remove C:\Users\RUNNER~1\AppData\Local\Temp\TestReadRotatingFilesCopyTruncate3537600095\001\test-backup.log: The process cannot access the file because it is being used by another process. Test: TestReadRotatingFiles/CopyTruncate === FAIL: . TestReadRotatingFiles (0.00s) === FAIL: . TestReadRotatingFiles/CopyTruncate (re-run 1) (1.63s) filelog_test.go:194: Error Trace: D:/a/opentelemetry-collector-contrib/opentelemetry-collector-contrib/receiver/filelogreceiver/filelog_test.go:194 Error: Received unexpected error: remove C:\Users\RUNNER~1\AppData\Local\Temp\TestReadRotatingFilesCopyTruncate134417[272](https://github.com/open-telemetry/opentelemetry-collector-contrib/actions/runs/11559461472/job/32174166127#step:8:273)3\001\test-backup.log: The process cannot access the file because it is being used by another process. Test: TestReadRotatingFiles/CopyTruncate === FAIL: . TestReadRotatingFiles (re-run 1) (0.00s) ``` --- receiver/filelogreceiver/filelog_test.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/receiver/filelogreceiver/filelog_test.go b/receiver/filelogreceiver/filelog_test.go index ef1b153d39ec..80931bbe87ee 100644 --- a/receiver/filelogreceiver/filelog_test.go +++ b/receiver/filelogreceiver/filelog_test.go @@ -5,6 +5,7 @@ package filelogreceiver import ( "context" + "errors" "fmt" "io" "os" @@ -190,9 +191,13 @@ func (rt *rotationTest) Run(t *testing.T) { if rt.copyTruncate { // Recreate the backup file // if backupFileName exists - if _, err = os.Stat(backupFileName); err == nil { - require.NoError(t, os.Remove(backupFileName)) - } + require.Eventually(t, func() bool { + // On Windows you can't remove a file if it still has some handle opened to it. So remove the file + // in a loop until any async operation on it is done. + removeErr := os.Remove(backupFileName) + return errors.Is(removeErr, os.ErrNotExist) + }, 5*time.Second, 100*time.Millisecond) + backupFile, openErr := os.OpenFile(backupFileName, os.O_CREATE|os.O_RDWR, 0600) require.NoError(t, openErr)