Skip to content

Commit

Permalink
Fix flaky FSWatch/FSScanner tests (#21625)
Browse files Browse the repository at this point in the history
## What does this PR do?

Unit tests in for `fswatch.go` do not depend on the order of the returned events anymore.

Closes #21489
  • Loading branch information
kvch authored Oct 8, 2020
1 parent 5389241 commit 4754602
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 26 deletions.
32 changes: 7 additions & 25 deletions filebeat/input/filestream/fswatch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ var (
)

func TestFileScanner(t *testing.T) {
t.Skip("Flaky test: https://github.com/elastic/beats/issues/21489")

testCases := map[string]struct {
paths []string
excludedFiles []match.Matcher
Expand Down Expand Up @@ -89,7 +87,7 @@ func TestFileScanner(t *testing.T) {
for p, _ := range files {
paths = append(paths, p)
}
assert.True(t, checkIfSameContents(test.expectedFiles, paths))
assert.ElementsMatch(t, paths, test.expectedFiles)
})
}
}
Expand All @@ -116,26 +114,7 @@ func removeFilesOfScannerTest(t *testing.T) {
}
}

// only handles sets
func checkIfSameContents(one, other []string) bool {
if len(one) != len(other) {
return false
}

mustFind := len(one)
for _, oneElem := range one {
for _, otherElem := range other {
if oneElem == otherElem {
mustFind--
}
}
}
return mustFind == 0
}

func TestFileWatchNewDeleteModified(t *testing.T) {
t.Skip("Flaky test: https://github.com/elastic/beats/issues/21489")

oldTs := time.Now()
newTs := oldTs.Add(5 * time.Second)
testCases := map[string]struct {
Expand Down Expand Up @@ -226,10 +205,13 @@ func TestFileWatchNewDeleteModified(t *testing.T) {

go w.watch(context.Background())

for _, expectedEvent := range test.expectedEvents {
evt := w.Event()
assert.Equal(t, expectedEvent, evt)
count := len(test.expectedEvents)
actual := make([]loginp.FSEvent, count)
for i := 0; i < count; i++ {
actual[i] = w.Event()
}

assert.ElementsMatch(t, actual, test.expectedEvents)
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion filebeat/input/filestream/fswatch_test_non_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func TestFileScannerSymlinks(t *testing.T) {
for p, _ := range files {
paths = append(paths, p)
}
assert.Equal(t, test.expectedFiles, paths)
assert.ElementsMatch(t, test.expectedFiles, paths)
})
}
}
Expand Down

0 comments on commit 4754602

Please sign in to comment.