Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: flaky watch tests #3027

Merged
merged 1 commit into from
Oct 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions internal/watch/watch_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,14 @@ func TestWatch(t *testing.T) {
two = loadModule(t, ic.WorkingDir(), "two")
},
func(tb testing.TB, ic in.TestContext) {
waitForEvents(tb, events, []WatchEvent{
// Module creation may insert the files and then modify them (eg `go mod tidy`)
// so we allow change events to occur
waitForEventsWhileIgnoringEvents(tb, events, []WatchEvent{
WatchEventModuleAdded{Config: one},
WatchEventModuleAdded{Config: two},
}, []WatchEvent{
WatchEventModuleChanged{Config: one},
WatchEventModuleChanged{Config: two},
})
},

Expand Down Expand Up @@ -72,8 +77,10 @@ func TestWatchWithBuildModifyingFiles(t *testing.T) {

in.FtlNew("go", "one"),
func(tb testing.TB, ic in.TestContext) {
waitForEvents(tb, events, []WatchEvent{
waitForEventsWhileIgnoringEvents(tb, events, []WatchEvent{
WatchEventModuleAdded{Config: loadModule(t, ic.WorkingDir(), "one")},
}, []WatchEvent{
WatchEventModuleChanged{Config: loadModule(t, ic.WorkingDir(), "one")},
})
},
func(tb testing.TB, ic in.TestContext) {
Expand Down Expand Up @@ -106,8 +113,10 @@ func TestWatchWithBuildAndUserModifyingFiles(t *testing.T) {

in.FtlNew("go", "one"),
func(tb testing.TB, ic in.TestContext) {
waitForEvents(tb, events, []WatchEvent{
waitForEventsWhileIgnoringEvents(tb, events, []WatchEvent{
WatchEventModuleAdded{Config: loadModule(t, ic.WorkingDir(), "one")},
}, []WatchEvent{
WatchEventModuleChanged{Config: loadModule(t, ic.WorkingDir(), "one")},
})
},
// Change a file in a module, within a transaction
Expand Down Expand Up @@ -155,18 +164,33 @@ func startWatching(ctx context.Context, t testing.TB, w *Watcher, dir string) (c
// The expected events are matched by keyForEvent.
func waitForEvents(t testing.TB, events chan WatchEvent, expected []WatchEvent) {
t.Helper()

waitForEventsWhileIgnoringEvents(t, events, expected, []WatchEvent{})
}

func waitForEventsWhileIgnoringEvents(t testing.TB, events chan WatchEvent, expected []WatchEvent, ignoredEvents []WatchEvent) {
t.Helper()
visited := map[string]bool{}
expectedKeys := []string{}
for _, event := range expected {
key := keyForEvent(event)
visited[key] = false
expectedKeys = append(expectedKeys, key)
}
ignored := map[string]bool{}
for _, event := range ignoredEvents {
key := keyForEvent(event)
ignored[key] = true
}
eventCount := 0
for {
select {
case actual := <-events:
key := keyForEvent(actual)
_, isIgnored := ignored[key]
if isIgnored {
continue
}
hasVisited, isExpected := visited[key]
assert.True(t, isExpected, "unexpected event %v instead of %v", key, expectedKeys)
assert.False(t, hasVisited, "duplicate event %v", key)
Expand Down