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)