From 5236157cbb4de61db182ec31d1cd851536f27482 Mon Sep 17 00:00:00 2001 From: Denis Date: Mon, 14 Aug 2023 17:07:01 +0200 Subject: [PATCH] Fix flaky fswatch test due to non-deterministic order (#36304) 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. --- filebeat/input/filestream/fswatch_test.go | 49 +++++++++++++++-------- 1 file changed, 32 insertions(+), 17 deletions(-) diff --git a/filebeat/input/filestream/fswatch_test.go b/filebeat/input/filestream/fswatch_test.go index 3ca65673b78f..ded2459628be 100644 --- a/filebeat/input/filestream/fswatch_test.go +++ b/filebeat/input/filestream/fswatch_test.go @@ -22,6 +22,7 @@ import ( "fmt" "os" "path/filepath" + "sort" "strings" "testing" "time" @@ -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)