Repository navigation
Expand file tree
/
Copy pathfile_cache_missing_test.go
More file actions
58 lines (55 loc) · 1.74 KB
/
Copy pathfile_cache_missing_test.go
File metadata and controls
58 lines (55 loc) · 1.74 KB
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
//go:build linux || darwin || dragonfly || freebsd || netbsd || openbsd || windows
package http
import (
"fmt"
"testing"
)
// What the cache remembers of names with no file is bounded: requests for
// ever more names that are not there forget the older ones rather than
// growing the cache without end, as they would where a watch keeps every
// entry until its directory changes.
func TestFileCacheMissingEntriesBounded(t *testing.T) {
fc, _, _ := newTestFileCache(t, true)
for i := range maxMissingEntries + 500 {
if f := fc.lookup(fileKey{fmt.Sprintf("nope-%d.txt", i), noTwin}); f == nil || !f.missing {
t.Fatalf("lookup %d: %+v", i, f)
}
}
if f := fc.lookup(fileKey{"plain.txt", noTwin}); f == nil || f.missing {
t.Fatal("plain.txt is missing")
}
count := func() (missing, all int) {
fc.mu.RLock()
defer fc.mu.RUnlock()
for _, e := range fc.entries {
if e.missing {
missing++
}
}
if missing != fc.missing {
t.Fatalf("%d missing entries, counted %d", missing, fc.missing)
}
return missing, len(fc.entries)
}
if missing, all := count(); missing != maxMissingEntries || all != maxMissingEntries+1 {
t.Fatalf("%d missing entries of %d, want %d of %d", missing, all, maxMissingEntries, maxMissingEntries+1)
}
// A change forgets what it names, and its count with it.
fc.mu.RLock()
var name string
for key, e := range fc.entries {
if e.missing {
name = key.name
break
}
}
fc.mu.RUnlock()
fc.changed(name)
if missing, _ := count(); missing != maxMissingEntries-1 {
t.Fatalf("%d missing entries after a change, want %d", missing, maxMissingEntries-1)
}
fc.changed("")
if missing, all := count(); missing != 0 || all != 0 {
t.Fatalf("%d missing entries of %d after everything changed", missing, all)
}
}