aboutsummaryrefslogtreecommitdiff
path: root/lib/ignore/cache_test.go
blob: 6aae9946462feeafdcd95f74d2909f164ed7040a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright (C) 2014 The Syncthing Authors.
//
// 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 https://mozilla.org/MPL/2.0/.

package ignore

import (
	"testing"
	"time"

	"github.com/syncthing/syncthing/lib/ignore/ignoreresult"
)

func TestCache(t *testing.T) {
	fc := new(fakeClock)
	oldClock := clock
	clock = fc
	defer func() {
		clock = oldClock
	}()

	c := newCache(nil)

	res, ok := c.get("nonexistent")
	if res.IsIgnored() || res.IsDeletable() || ok {
		t.Errorf("res %v, ok %v for nonexistent item", res, ok)
	}

	// Set and check some items

	c.set("true", ignoreresult.IgnoredDeletable)
	c.set("false", 0)

	res, ok = c.get("true")
	if !res.IsIgnored() || !res.IsDeletable() || !ok {
		t.Errorf("res %v, ok %v for true item", res, ok)
	}

	res, ok = c.get("false")
	if res.IsIgnored() || res.IsDeletable() || !ok {
		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")
	if !res.IsIgnored() || !res.IsDeletable() || !ok {
		t.Errorf("res %v, ok %v for true item", res, ok)
	}

	res, ok = c.get("false")
	if res.IsIgnored() || res.IsDeletable() || !ok {
		t.Errorf("res %v, ok %v for false item", res, ok)
	}

	// Sleep and access, to get some data for clean

	*fc += 500 // milliseconds

	c.get("true")

	*fc += 100 // milliseconds

	// "false" was accessed ~600 ms ago, "true" was accessed ~100 ms ago.
	// This should clean out "false" but not "true"

	c.clean(300 * time.Millisecond)

	// 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")
	}
}

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
}