lib/fs: Further unflake watch tests (#4735)

This commit is contained in:
Jakob Borg 2018-02-04 22:25:59 +01:00 committed by GitHub
parent 2fdf9bc55d
commit 98f2875b22
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 25 additions and 13 deletions

View File

@ -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
}