Skip to content

Commit

Permalink
fix: flaky watch tests (#3027)
Browse files Browse the repository at this point in the history
When creating a new module, files can be added (scaffolded) and then
edited (eg: `go mod tidy`).
The watch tests are now lenient to update events being fired in this
case.
  • Loading branch information
matt2e authored Oct 8, 2024
1 parent 986a2d8 commit 05c5f2f
Showing 1 changed file with 27 additions and 3 deletions.
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

0 comments on commit 05c5f2f

Please sign in to comment.