lib/ignore, lib/model: Use an interface to detect file changes, improving tests

This solves the erratic test failures on model.TestIgnores by ensuring
that the ignore patterns are reloaded even in the face of unchanged
timestamps.

GitHub-Pull-Request: https://github.com/syncthing/syncthing/pull/4208
This commit is contained in:
Jakob Borg
2017-06-11 10:27:12 +00:00
committed by Audrius Butkevicius
parent 2a38d2a3d2
commit 68c1a0b9b4
7 changed files with 195 additions and 87 deletions

View File

@@ -8,6 +8,12 @@ package ignore
import "time"
type nower interface {
Now() time.Time
}
var clock = nower(defaultClock{})
type cache struct {
patterns []Pattern
entries map[string]cacheEntry
@@ -27,7 +33,7 @@ func newCache(patterns []Pattern) *cache {
func (c *cache) clean(d time.Duration) {
for k, v := range c.entries {
if time.Since(v.access) > d {
if clock.Now().Sub(v.access) > d {
delete(c.entries, k)
}
}
@@ -36,7 +42,7 @@ func (c *cache) clean(d time.Duration) {
func (c *cache) get(key string) (Result, bool) {
entry, ok := c.entries[key]
if ok {
entry.access = time.Now()
entry.access = clock.Now()
c.entries[key] = entry
}
return entry.result, ok
@@ -50,3 +56,9 @@ func (c *cache) len() int {
l := len(c.entries)
return l
}
type defaultClock struct{}
func (defaultClock) Now() time.Time {
return time.Now()
}