Skip to content

Commit

Permalink
Fix flaky fswatch test due to non-deterministic order (elastic#36304)
Browse files Browse the repository at this point in the history
The values are coming from a map, so the order is not deterministic.
This change sorts the events based on their filename and compares them afterwards.
  • Loading branch information
rdner authored Aug 14, 2023
1 parent 6b76cfe commit 5236157
Showing 1 changed file with 32 additions and 17 deletions.
49 changes: 32 additions & 17 deletions filebeat/input/filestream/fswatch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"fmt"
"os"
"path/filepath"
"sort"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -379,27 +380,41 @@ scanner:

go fw.Run(ctx)

e := fw.Event()
expEvent := loginp.FSEvent{
NewPath: firstFilename,
Op: loginp.OpCreate,
Descriptor: loginp.FileDescriptor{
Filename: firstFilename,
Info: testFileInfo{name: firstBasename, size: 5}, // "line\n"
expectedEvents := []loginp.FSEvent{
{
NewPath: firstFilename,
Op: loginp.OpCreate,
Descriptor: loginp.FileDescriptor{
Filename: firstFilename,
Info: testFileInfo{name: firstBasename, size: 5}, // "line\n"
},
},
{
NewPath: secondFilename,
Op: loginp.OpCreate,
Descriptor: loginp.FileDescriptor{
Filename: secondFilename,
Info: testFileInfo{name: secondBasename, size: 5}, // "line\n"
},
},
}
requireEqualEvents(t, expEvent, e)
var actualEvents []loginp.FSEvent
actualEvents = append(actualEvents, fw.Event())
actualEvents = append(actualEvents, fw.Event())

// since this is coming from a map, the order is not deterministic
// we need to sort events based on paths first
// we expect only creation events for two different files, so it's alright.
sort.Slice(actualEvents, func(i, j int) bool {
return actualEvents[i].NewPath < actualEvents[j].NewPath
})
sort.Slice(expectedEvents, func(i, j int) bool {
return expectedEvents[i].NewPath < expectedEvents[j].NewPath
})

e = fw.Event()
expEvent = loginp.FSEvent{
NewPath: secondFilename,
Op: loginp.OpCreate,
Descriptor: loginp.FileDescriptor{
Filename: secondFilename,
Info: testFileInfo{name: secondBasename, size: 5}, // "line\n"
},
for i, actualEvent := range actualEvents {
requireEqualEvents(t, expectedEvents[i], actualEvent)
}
requireEqualEvents(t, expEvent, e)

logs := logp.ObserverLogs().FilterLevelExact(logp.WarnLevel.ZapLevel()).TakeAll()
require.Lenf(t, logs, 0, "must be no warning messages, got: %v", logs)
Expand Down

0 comments on commit 5236157

Please sign in to comment.