Skip to content

Commit

Permalink
[chore] Make TestReadRotatingFiles more robust on Windows (open-telem…
Browse files Browse the repository at this point in the history
…etry#36032)

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)
```
  • Loading branch information
pjanotti authored Oct 29, 2024
1 parent 426e0a4 commit 18b9b88
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions receiver/filelogreceiver/filelog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package filelogreceiver

import (
"context"
"errors"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 18b9b88

Please sign in to comment.