Skip to content

Commit

Permalink
chore: generalize watcher test (aws#5457)
Browse files Browse the repository at this point in the history
The number of events received by the watcher appears to be system dependent, so this converts the expected and actual events received from an array to a set. This means that we don't expect an order of events, and we just need one copy of each event in order to succeed.

Was causing `make local-test` to fail, should be fixed.



By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the Apache 2.0 License.
  • Loading branch information
CaptainCarpensir authored Nov 8, 2023
1 parent 90d3af3 commit 752e751
Showing 1 changed file with 20 additions and 30 deletions.
50 changes: 20 additions & 30 deletions internal/pkg/cli/file/watch_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,53 +21,45 @@ func TestRecursiveWatcher(t *testing.T) {
var (
watcher *file.RecursiveWatcher
tmp string
eventsExpected []fsnotify.Event
eventsActual []fsnotify.Event
eventsExpected map[fsnotify.Event]struct{}
eventsActual map[fsnotify.Event]struct{}
)

tmp = os.TempDir()
eventsActual = make([]fsnotify.Event, 0)
eventsExpected = []fsnotify.Event{
eventsActual = make(map[fsnotify.Event]struct{})
eventsExpected = map[fsnotify.Event]struct{}{
{
Name: fmt.Sprintf("%s/watch/subdir/testfile", tmp),
Op: fsnotify.Create,
},
}: {},
{
Name: fmt.Sprintf("%s/watch/subdir/testfile", tmp),
Op: fsnotify.Chmod,
},
}: {},
{
Name: fmt.Sprintf("%s/watch/subdir/testfile", tmp),
Op: fsnotify.Write,
},
{
Name: fmt.Sprintf("%s/watch/subdir/testfile", tmp),
Op: fsnotify.Write,
},
}: {},
{
Name: fmt.Sprintf("%s/watch/subdir", tmp),
Op: fsnotify.Rename,
},
}: {},
{
Name: fmt.Sprintf("%s/watch/subdir2", tmp),
Op: fsnotify.Create,
},
{
Name: fmt.Sprintf("%s/watch/subdir", tmp),
Op: fsnotify.Rename,
},
}: {},
{
Name: fmt.Sprintf("%s/watch/subdir2/testfile", tmp),
Op: fsnotify.Rename,
},
}: {},
{
Name: fmt.Sprintf("%s/watch/subdir2/testfile2", tmp),
Op: fsnotify.Create,
},
}: {},
{
Name: fmt.Sprintf("%s/watch/subdir2/testfile2", tmp),
Op: fsnotify.Remove,
},
}: {},
}

t.Run("Setup Watcher", func(t *testing.T) {
Expand All @@ -86,45 +78,43 @@ func TestRecursiveWatcher(t *testing.T) {
eventsCh := watcher.Events()
errorsCh := watcher.Errors()

expectEvents := func(t *testing.T, n int) []fsnotify.Event {
receivedEvents := []fsnotify.Event{}
expectAndPopulateEvents := func(t *testing.T, n int, events map[fsnotify.Event]struct{}) {
for i := 0; i < n; i++ {
select {
case e := <-eventsCh:
receivedEvents = append(receivedEvents, e)
events[e] = struct{}{}
case <-time.After(time.Second):
}
}
return receivedEvents
}

// WATCH
file, err := os.Create(fmt.Sprintf("%s/watch/subdir/testfile", tmp))
require.NoError(t, err)
eventsActual = append(eventsActual, expectEvents(t, 1)...)
expectAndPopulateEvents(t, 1, eventsActual)

err = os.Chmod(fmt.Sprintf("%s/watch/subdir/testfile", tmp), 0755)
require.NoError(t, err)
eventsActual = append(eventsActual, expectEvents(t, 1)...)
expectAndPopulateEvents(t, 1, eventsActual)

err = os.WriteFile(fmt.Sprintf("%s/watch/subdir/testfile", tmp), []byte("write to file"), fs.ModeAppend)
require.NoError(t, err)
eventsActual = append(eventsActual, expectEvents(t, 2)...)
expectAndPopulateEvents(t, 2, eventsActual)

err = file.Close()
require.NoError(t, err)

err = os.Rename(fmt.Sprintf("%s/watch/subdir", tmp), fmt.Sprintf("%s/watch/subdir2", tmp))
require.NoError(t, err)
eventsActual = append(eventsActual, expectEvents(t, 3)...)
expectAndPopulateEvents(t, 3, eventsActual)

err = os.Rename(fmt.Sprintf("%s/watch/subdir2/testfile", tmp), fmt.Sprintf("%s/watch/subdir2/testfile2", tmp))
require.NoError(t, err)
eventsActual = append(eventsActual, expectEvents(t, 2)...)
expectAndPopulateEvents(t, 2, eventsActual)

err = os.Remove(fmt.Sprintf("%s/watch/subdir2/testfile2", tmp))
require.NoError(t, err)
eventsActual = append(eventsActual, expectEvents(t, 1)...)
expectAndPopulateEvents(t, 1, eventsActual)

// CLOSE
err = watcher.Close()
Expand Down

0 comments on commit 752e751

Please sign in to comment.