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,
|
|
|
|
// You can obtain one at http://mozilla.org/MPL/2.0/.
|
2014-12-02 22:13:03 +00:00
|
|
|
|
|
|
|
package ignore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestCache(t *testing.T) {
|
|
|
|
c := newCache(nil)
|
|
|
|
|
|
|
|
res, ok := c.get("nonexistent")
|
2016-04-07 09:34:07 +00:00
|
|
|
if res.IsIgnored() || res.IsDeletable() || ok != false {
|
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
|
|
|
|
|
2016-04-07 09:34:07 +00:00
|
|
|
c.set("true", Result{true, true})
|
|
|
|
c.set("false", Result{false, false})
|
2014-12-02 22:13:03 +00:00
|
|
|
|
|
|
|
res, ok = c.get("true")
|
2016-04-07 09:34:07 +00:00
|
|
|
if !res.IsIgnored() || !res.IsDeletable() || ok != true {
|
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-04-07 09:34:07 +00:00
|
|
|
if res.IsIgnored() || res.IsDeletable() || ok != true {
|
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-04-07 09:34:07 +00:00
|
|
|
if !res.IsIgnored() || !res.IsDeletable() || ok != true {
|
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-04-07 09:34:07 +00:00
|
|
|
if res.IsIgnored() || res.IsDeletable() || ok != true {
|
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
|
|
|
|
|
2015-07-14 10:12:57 +00:00
|
|
|
time.Sleep(500 * time.Millisecond)
|
|
|
|
|
2014-12-02 22:13:03 +00:00
|
|
|
c.get("true")
|
2015-07-14 10:12:57 +00:00
|
|
|
|
2014-12-02 22:13:03 +00:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|