2014-12-02 22:13:03 +00:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
|
|
|
//
|
2015-03-07 20:36:35 +00:00
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
2017-02-09 06:52:18 +00:00
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2014-12-02 22:13:03 +00:00
|
|
|
|
|
|
|
package ignore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
2024-01-13 17:58:23 +00:00
|
|
|
|
|
|
|
"github.com/syncthing/syncthing/lib/ignore/ignoreresult"
|
2014-12-02 22:13:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCache(t *testing.T) {
|
2017-06-11 10:27:12 +00:00
|
|
|
fc := new(fakeClock)
|
|
|
|
oldClock := clock
|
|
|
|
clock = fc
|
|
|
|
defer func() {
|
|
|
|
clock = oldClock
|
|
|
|
}()
|
|
|
|
|
2014-12-02 22:13:03 +00:00
|
|
|
c := newCache(nil)
|
|
|
|
|
|
|
|
res, ok := c.get("nonexistent")
|
2016-12-18 18:57:41 +00:00
|
|
|
if res.IsIgnored() || res.IsDeletable() || ok {
|
2014-12-08 15:19:08 +00:00
|
|
|
t.Errorf("res %v, ok %v for nonexistent item", res, ok)
|
2014-12-02 22:13:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set and check some items
|
|
|
|
|
2024-01-13 17:58:23 +00:00
|
|
|
c.set("true", ignoreresult.IgnoredDeletable)
|
2016-05-01 15:58:23 +00:00
|
|
|
c.set("false", 0)
|
2014-12-02 22:13:03 +00:00
|
|
|
|
|
|
|
res, ok = c.get("true")
|
2016-12-18 18:57:41 +00:00
|
|
|
if !res.IsIgnored() || !res.IsDeletable() || !ok {
|
2014-12-02 22:13:03 +00:00
|
|
|
t.Errorf("res %v, ok %v for true item", res, ok)
|
|
|
|
}
|
|
|
|
|
|
|
|
res, ok = c.get("false")
|
2016-12-18 18:57:41 +00:00
|
|
|
if res.IsIgnored() || res.IsDeletable() || !ok {
|
2014-12-02 22:13:03 +00:00
|
|
|
t.Errorf("res %v, ok %v for false item", res, ok)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't clean anything
|
|
|
|
|
|
|
|
c.clean(time.Second)
|
|
|
|
|
|
|
|
// Same values should exist
|
|
|
|
|
|
|
|
res, ok = c.get("true")
|
2016-12-18 18:57:41 +00:00
|
|
|
if !res.IsIgnored() || !res.IsDeletable() || !ok {
|
2014-12-02 22:13:03 +00:00
|
|
|
t.Errorf("res %v, ok %v for true item", res, ok)
|
|
|
|
}
|
|
|
|
|
|
|
|
res, ok = c.get("false")
|
2016-12-18 18:57:41 +00:00
|
|
|
if res.IsIgnored() || res.IsDeletable() || !ok {
|
2014-12-02 22:13:03 +00:00
|
|
|
t.Errorf("res %v, ok %v for false item", res, ok)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sleep and access, to get some data for clean
|
|
|
|
|
2017-06-11 10:27:12 +00:00
|
|
|
*fc += 500 // milliseconds
|
2015-07-14 10:12:57 +00:00
|
|
|
|
2014-12-02 22:13:03 +00:00
|
|
|
c.get("true")
|
2015-07-14 10:12:57 +00:00
|
|
|
|
2017-06-11 10:27:12 +00:00
|
|
|
*fc += 100 // milliseconds
|
2014-12-02 22:13:03 +00:00
|
|
|
|
2015-07-14 10:12:57 +00:00
|
|
|
// "false" was accessed ~600 ms ago, "true" was accessed ~100 ms ago.
|
2014-12-02 22:13:03 +00:00
|
|
|
// This should clean out "false" but not "true"
|
|
|
|
|
2015-07-14 10:12:57 +00:00
|
|
|
c.clean(300 * time.Millisecond)
|
2014-12-02 22:13:03 +00:00
|
|
|
|
|
|
|
// Same values should exist
|
|
|
|
|
|
|
|
_, ok = c.get("true")
|
|
|
|
if !ok {
|
|
|
|
t.Error("item should still exist")
|
|
|
|
}
|
|
|
|
|
|
|
|
_, ok = c.get("false")
|
|
|
|
if ok {
|
|
|
|
t.Errorf("item should have been cleaned")
|
|
|
|
}
|
|
|
|
}
|
2017-06-11 10:27:12 +00:00
|
|
|
|
|
|
|
type fakeClock int64 // milliseconds
|
|
|
|
|
|
|
|
func (f *fakeClock) Now() time.Time {
|
|
|
|
t := time.Unix(int64(*f)/1000, (int64(*f)%1000)*int64(time.Millisecond))
|
|
|
|
*f++
|
|
|
|
return t
|
|
|
|
}
|