From 98f2875b2239dda36a6a12921218ec0cf0c7e45e Mon Sep 17 00:00:00 2001 From: Jakob Borg Date: Sun, 4 Feb 2018 22:25:59 +0100 Subject: [PATCH] lib/fs: Further unflake watch tests (#4735) --- lib/fs/basicfs_watch_test.go | 38 ++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/lib/fs/basicfs_watch_test.go b/lib/fs/basicfs_watch_test.go index 5f351fdc5..7f8801dea 100644 --- a/lib/fs/basicfs_watch_test.go +++ b/lib/fs/basicfs_watch_test.go @@ -67,8 +67,11 @@ func TestWatchIgnore(t *testing.T) { expectedEvents := []Event{ {file, NonRemove}, } + allowedEvents := []Event{ + {name, NonRemove}, + } - testScenario(t, name, testCase, expectedEvents, false, ignored) + testScenario(t, name, testCase, expectedEvents, allowedEvents, ignored) } func TestWatchRename(t *testing.T) { @@ -91,10 +94,13 @@ func TestWatchRename(t *testing.T) { {old, Remove}, destEvent, } + allowedEvents := []Event{ + {name, NonRemove}, + } // set the "allow others" flag because we might get the create of // "oldfile" initially - testScenario(t, name, testCase, expectedEvents, true, "") + testScenario(t, name, testCase, expectedEvents, allowedEvents, "") } // TestWatchOutside checks that no changes from outside the folder make it in @@ -152,17 +158,23 @@ func TestWatchSubpath(t *testing.T) { func TestWatchOverflow(t *testing.T) { name := "overflow" - testCase := func() { - for i := 0; i < 5*backendBuffer; i++ { - createTestFile(name, "file"+strconv.Itoa(i)) - } - } - expectedEvents := []Event{ {".", NonRemove}, } - testScenario(t, name, testCase, expectedEvents, true, "") + allowedEvents := []Event{ + {name, NonRemove}, + } + + testCase := func() { + for i := 0; i < 5*backendBuffer; i++ { + file := "file" + strconv.Itoa(i) + createTestFile(name, file) + allowedEvents = append(allowedEvents, Event{file, NonRemove}) + } + } + + testScenario(t, name, testCase, expectedEvents, allowedEvents, "") } // path relative to folder root, also creates parent dirs if necessary @@ -191,7 +203,7 @@ func sleepMs(ms int) { time.Sleep(time.Duration(ms) * time.Millisecond) } -func testScenario(t *testing.T, name string, testCase func(), expectedEvents []Event, allowOthers bool, ignored string) { +func testScenario(t *testing.T, name string, testCase func(), expectedEvents, allowedEvents []Event, ignored string) { if err := testFs.MkdirAll(name, 0755); err != nil { panic(fmt.Sprintf("Failed to create directory %s: %s", name, err)) } @@ -209,7 +221,7 @@ func testScenario(t *testing.T, name string, testCase func(), expectedEvents []E panic(err) } - go testWatchOutput(t, name, eventChan, expectedEvents, allowOthers, ctx, cancel) + go testWatchOutput(t, name, eventChan, expectedEvents, allowedEvents, ctx, cancel) testCase() @@ -221,7 +233,7 @@ func testScenario(t *testing.T, name string, testCase func(), expectedEvents []E } } -func testWatchOutput(t *testing.T, name string, in <-chan Event, expectedEvents []Event, allowOthers bool, ctx context.Context, cancel context.CancelFunc) { +func testWatchOutput(t *testing.T, name string, in <-chan Event, expectedEvents, allowedEvents []Event, ctx context.Context, cancel context.CancelFunc) { var expected = make(map[Event]struct{}) for _, ev := range expectedEvents { ev.Name = filepath.Join(name, ev.Name) @@ -248,7 +260,7 @@ func testWatchOutput(t *testing.T, name string, in <-chan Event, expectedEvents } if _, ok := expected[received]; !ok { - if allowOthers { + if len(allowedEvents) > 0 { sleepMs(100) // To facilitate overflow continue }