aboutsummaryrefslogtreecommitdiff
path: root/internal/cache/clock/cache_test.go
blob: 3d734b395822c06ae3e333458c98afeadb6f076b (about) (plain) (blame)
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
95
96
97
98
99
100
package clock_test

import (
	"fmt"
	"strings"
	"testing"

	"lindenii.org/go/furgit/internal/cache/clock"
	"lindenii.org/go/lgo/intconv"
)

func byteWeight(_ string, value string) uint64 {
	weight, err := intconv.IntToUint64(len(value))
	if err != nil {
		return 0
	}

	return weight
}

func TestCacheAddGetPeek(t *testing.T) {
	t.Parallel()

	cache := clock.New(1<<20, byteWeight)

	if !cache.Add("a", "alpha") {
		t.Fatalf("Add(a) should succeed")
	}

	if got, ok := cache.Get("a"); !ok || got != "alpha" {
		t.Fatalf("Get(a) = (%q, %v), want (alpha, true)", got, ok)
	}

	if got, ok := cache.Peek("a"); !ok || got != "alpha" {
		t.Fatalf("Peek(a) = (%q, %v), want (alpha, true)", got, ok)
	}

	if _, ok := cache.Get("missing"); ok {
		t.Fatalf("Get(missing) should miss")
	}
}

func TestCacheWeightStaysBounded(t *testing.T) {
	t.Parallel()

	const maxWeight = 4096

	cache := clock.New(maxWeight, byteWeight)
	value := strings.Repeat("x", 64)

	for i := range 1000 {
		cache.Add(fmt.Sprintf("key-%d", i), value)
	}

	if got := cache.Weight(); got > maxWeight {
		t.Fatalf("weight = %d, exceeds max %d", got, maxWeight)
	}
}

func TestCacheLenAndClear(t *testing.T) {
	t.Parallel()

	cache := clock.New(1<<20, byteWeight)

	for i := range 10 {
		cache.Add(fmt.Sprintf("key-%d", i), "v")
	}

	if got := cache.Len(); got != 10 {
		t.Fatalf("Len = %d, want 10", got)
	}

	cache.Clear()

	if got := cache.Len(); got != 0 {
		t.Fatalf("Len after Clear = %d, want 0", got)
	}

	if got := cache.Weight(); got != 0 {
		t.Fatalf("Weight after Clear = %d, want 0", got)
	}
}

func TestCacheRejectsOversized(t *testing.T) {
	t.Parallel()

	cache := clock.New(4, byteWeight)

	if cache.Add("a", "xxxxx") {
		t.Fatalf("oversized Add should report false")
	}

	if _, ok := cache.Get("a"); ok {
		t.Fatalf("oversized entry must not be cached")
	}

	if got := cache.Weight(); got != 0 {
		t.Fatalf("weight = %d, want 0", got)
	}
}