lib/model: Fix test flakyness regression (ref #5592) (#5718)

This commit is contained in:
Simon Frei 2019-05-18 08:52:50 +02:00 committed by Jakob Borg
parent 1df8701c46
commit 5ffbb7668d
3 changed files with 48 additions and 39 deletions

View File

@ -1501,6 +1501,8 @@ func TestIgnores(t *testing.T) {
m.db.Close() m.db.Close()
}() }()
m.RemoveFolder(defaultFolderConfig)
m.AddFolder(defaultFolderConfig)
// Reach in and update the ignore matcher to one that always does // Reach in and update the ignore matcher to one that always does
// reloads when asked to, instead of checking file mtimes. This is // reloads when asked to, instead of checking file mtimes. This is
// because we will be changing the files on disk often enough that the // because we will be changing the files on disk often enough that the
@ -1508,6 +1510,7 @@ func TestIgnores(t *testing.T) {
m.fmut.Lock() m.fmut.Lock()
m.folderIgnores["default"] = ignore.New(defaultFs, ignore.WithCache(true), ignore.WithChangeDetector(newAlwaysChanged())) m.folderIgnores["default"] = ignore.New(defaultFs, ignore.WithCache(true), ignore.WithChangeDetector(newAlwaysChanged()))
m.fmut.Unlock() m.fmut.Unlock()
m.StartFolder("default")
// Make sure the initial scan has finished (ScanFolders is blocking) // Make sure the initial scan has finished (ScanFolders is blocking)
m.ScanFolders() m.ScanFolders()
@ -3318,39 +3321,6 @@ func TestFolderRestartZombies(t *testing.T) {
} }
} }
type alwaysChangedKey struct {
fs fs.Filesystem
name string
}
// alwaysChanges is an ignore.ChangeDetector that always returns true on Changed()
type alwaysChanged struct {
seen map[alwaysChangedKey]struct{}
}
func newAlwaysChanged() *alwaysChanged {
return &alwaysChanged{
seen: make(map[alwaysChangedKey]struct{}),
}
}
func (c *alwaysChanged) Remember(fs fs.Filesystem, name string, _ time.Time) {
c.seen[alwaysChangedKey{fs, name}] = struct{}{}
}
func (c *alwaysChanged) Reset() {
c.seen = make(map[alwaysChangedKey]struct{})
}
func (c *alwaysChanged) Seen(fs fs.Filesystem, name string) bool {
_, ok := c.seen[alwaysChangedKey{fs, name}]
return ok
}
func (c *alwaysChanged) Changed() bool {
return true
}
func TestRequestLimit(t *testing.T) { func TestRequestLimit(t *testing.T) {
wrapper := createTmpWrapper(defaultCfg.Copy()) wrapper := createTmpWrapper(defaultCfg.Copy())
defer os.Remove(wrapper.ConfigPath()) defer os.Remove(wrapper.ConfigPath())

View File

@ -306,14 +306,12 @@ func TestPullInvalidIgnoredSR(t *testing.T) {
// This test checks that (un-)ignored/invalid/deleted files are treated as expected. // This test checks that (un-)ignored/invalid/deleted files are treated as expected.
func pullInvalidIgnored(t *testing.T, ft config.FolderType) { func pullInvalidIgnored(t *testing.T, ft config.FolderType) {
t.Helper()
w := createTmpWrapper(defaultCfgWrapper.RawCopy()) w := createTmpWrapper(defaultCfgWrapper.RawCopy())
fcfg := testFolderConfigTmp() fcfg := testFolderConfigTmp()
fss := fcfg.Filesystem() fss := fcfg.Filesystem()
fcfg.Type = ft fcfg.Type = ft
w.SetFolder(fcfg) w.SetFolder(fcfg)
m, fc := setupModelWithConnectionFromWrapper(w) m := setupModel(w)
defer func() { defer func() {
m.Stop() m.Stop()
m.db.Close() m.db.Close()
@ -321,6 +319,8 @@ func pullInvalidIgnored(t *testing.T, ft config.FolderType) {
os.Remove(w.ConfigPath()) os.Remove(w.ConfigPath())
}() }()
m.RemoveFolder(fcfg)
m.AddFolder(fcfg)
// Reach in and update the ignore matcher to one that always does // Reach in and update the ignore matcher to one that always does
// reloads when asked to, instead of checking file mtimes. This is // reloads when asked to, instead of checking file mtimes. This is
// because we might be changing the files on disk often enough that the // because we might be changing the files on disk often enough that the
@ -328,6 +328,10 @@ func pullInvalidIgnored(t *testing.T, ft config.FolderType) {
m.fmut.Lock() m.fmut.Lock()
m.folderIgnores["default"] = ignore.New(fss, ignore.WithChangeDetector(newAlwaysChanged())) m.folderIgnores["default"] = ignore.New(fss, ignore.WithChangeDetector(newAlwaysChanged()))
m.fmut.Unlock() m.fmut.Unlock()
m.StartFolder(fcfg.ID)
fc := addFakeConn(m, device1)
fc.folder = "default"
if err := m.SetIgnores("default", []string{"*ignored*"}); err != nil { if err := m.SetIgnores("default", []string{"*ignored*"}); err != nil {
panic(err) panic(err)
@ -366,7 +370,7 @@ func pullInvalidIgnored(t *testing.T, ft config.FolderType) {
for name := range expected { for name := range expected {
t.Errorf("File %v wasn't added to index", name) t.Errorf("File %v wasn't added to index", name)
} }
done <- struct{}{} close(done)
} }
fc.mut.Unlock() fc.mut.Unlock()
@ -377,12 +381,13 @@ func pullInvalidIgnored(t *testing.T, ft config.FolderType) {
select { select {
case ev := <-sub.C(): case ev := <-sub.C():
t.Fatalf("Errors while pulling: %v", ev) t.Fatalf("Errors while scanning/pulling: %v", ev)
case <-time.After(5 * time.Second): case <-time.After(5 * time.Second):
t.Fatalf("timed out before index was received") t.Fatalf("timed out before index was received")
case <-done: case <-done:
} }
done = make(chan struct{})
fc.mut.Lock() fc.mut.Lock()
fc.indexFn = func(folder string, fs []protocol.FileInfo) { fc.indexFn = func(folder string, fs []protocol.FileInfo) {
expected := map[string]struct{}{ign: {}, ignExisting: {}} expected := map[string]struct{}{ign: {}, ignExisting: {}}
@ -411,7 +416,7 @@ func pullInvalidIgnored(t *testing.T, ft config.FolderType) {
for name := range expected { for name := range expected {
t.Errorf("File %v wasn't updated in index", name) t.Errorf("File %v wasn't updated in index", name)
} }
done <- struct{}{} close(done)
} }
// Make sure pulling doesn't interfere, as index updates are racy and // Make sure pulling doesn't interfere, as index updates are racy and
// thus we cannot distinguish between scan and pull results. // thus we cannot distinguish between scan and pull results.

View File

@ -8,6 +8,7 @@ package model
import ( import (
"io/ioutil" "io/ioutil"
"time"
"github.com/syncthing/syncthing/lib/config" "github.com/syncthing/syncthing/lib/config"
"github.com/syncthing/syncthing/lib/db" "github.com/syncthing/syncthing/lib/db"
@ -121,3 +122,36 @@ func createTmpDir() string {
} }
return tmpDir return tmpDir
} }
type alwaysChangedKey struct {
fs fs.Filesystem
name string
}
// alwaysChanges is an ignore.ChangeDetector that always returns true on Changed()
type alwaysChanged struct {
seen map[alwaysChangedKey]struct{}
}
func newAlwaysChanged() *alwaysChanged {
return &alwaysChanged{
seen: make(map[alwaysChangedKey]struct{}),
}
}
func (c *alwaysChanged) Remember(fs fs.Filesystem, name string, _ time.Time) {
c.seen[alwaysChangedKey{fs, name}] = struct{}{}
}
func (c *alwaysChanged) Reset() {
c.seen = make(map[alwaysChangedKey]struct{})
}
func (c *alwaysChanged) Seen(fs fs.Filesystem, name string) bool {
_, ok := c.seen[alwaysChangedKey{fs, name}]
return ok
}
func (c *alwaysChanged) Changed() bool {
return true
}