Repository navigation
Expand file tree
/
Copy pathcache_test.go
More file actions
68 lines (61 loc) · 1.81 KB
/
Copy pathcache_test.go
File metadata and controls
68 lines (61 loc) · 1.81 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
59
60
61
62
63
64
65
66
67
68
package whatsmeow
import (
"fmt"
"testing"
"time"
)
func TestPutBoundedCache(t *testing.T) {
cache := make(map[string]int, 2)
putBoundedCache(cache, "one", 1, 2)
putBoundedCache(cache, "two", 2, 2)
putBoundedCache(cache, "three", 3, 2)
if len(cache) != 2 {
t.Fatalf("cache grew to %d entries", len(cache))
}
if cache["three"] != 3 {
t.Fatal("new value was not cached")
}
putBoundedCache(cache, "three", 4, 2)
if len(cache) != 2 || cache["three"] != 4 {
t.Fatal("updating a value changed the cache size")
}
}
func TestPruneExpiredCache(t *testing.T) {
now := time.Now()
cache := map[string]time.Time{
"old": now.Add(-2 * time.Hour),
"new": now,
}
pruneExpiredCache(cache, now.Add(-time.Hour))
if _, ok := cache["old"]; ok {
t.Fatal("expired entry was retained")
}
if _, ok := cache["new"]; !ok {
t.Fatal("fresh entry was removed")
}
}
func TestClientCachesStayBounded(t *testing.T) {
messageRetries := make(map[string]int, maxMessageRetryEntries)
for i := 0; i <= maxMessageRetryEntries; i++ {
key := fmt.Sprintf("message-%d", i)
putBoundedCache(messageRetries, key, i, maxMessageRetryEntries)
}
if len(messageRetries) != maxMessageRetryEntries {
t.Fatalf("message retry cache grew to %d entries", len(messageRetries))
}
}
func TestIncrementBoundedCounterFailsClosedAndResets(t *testing.T) {
now := time.Now()
resetAt := now
cache := map[string]int{"one": 1}
if _, accepted := incrementBoundedCounter(cache, "two", 1, &resetAt, now); accepted {
t.Fatal("accepted a new counter after reaching capacity")
}
count, accepted := incrementBoundedCounter(cache, "two", 1, &resetAt, now.Add(retryCounterWindow))
if !accepted || count != 1 {
t.Fatalf("counter did not reset: accepted=%t count=%d", accepted, count)
}
if _, exists := cache["one"]; exists {
t.Fatal("old counter survived reset")
}
}