-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinvalidator_test.go
More file actions
159 lines (139 loc) · 3.69 KB
/
Copy pathinvalidator_test.go
File metadata and controls
159 lines (139 loc) · 3.69 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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package cascache_test
import (
"context"
"errors"
"testing"
"github.com/unkn0wn-root/cascache/v4"
"github.com/unkn0wn-root/cascache/v4/backend"
"github.com/unkn0wn-root/cascache/v4/codec"
"github.com/unkn0wn-root/cascache/v4/internal/memstore"
)
func TestNewInvalidatorValidatesOptions(t *testing.T) {
store := memstore.New(memstore.Options{})
b, err := backend.NewComposite(store, newFakeFences())
if err != nil {
t.Fatal(err)
}
if _, err := cascache.NewInvalidator(
cascache.InvalidatorOptions{Backend: b},
); !errors.Is(
err,
cascache.ErrNoNamespace,
) {
t.Fatalf("NewInvalidator without a namespace = %v, want ErrNoNamespace", err)
}
if _, err := cascache.NewInvalidator(
cascache.InvalidatorOptions{Namespace: "n"},
); !errors.Is(
err,
cascache.ErrNoBackend,
) {
t.Fatalf("NewInvalidator without a backend = %v, want ErrNoBackend", err)
}
}
func TestStandaloneInvalidatorRetiresACacheEntry(t *testing.T) {
const namespace = "test"
ctx := context.Background()
store := memstore.New(memstore.Options{})
fences := newFakeFences()
b, err := backend.NewComposite(store, fences)
if err != nil {
t.Fatal(err)
}
cache, err := cascache.New(cascache.Options[user]{
Namespace: namespace, Backend: b, Codec: codec.JSON[user]{},
})
if err != nil {
t.Fatal(err)
}
fence, err := cache.Snapshot(ctx, "42")
if err != nil {
t.Fatal(err)
}
if _, err := cache.Set(ctx, "42", ada, fence); err != nil {
t.Fatal(err)
}
if _, ok, _ := cache.Get(ctx, "42"); !ok {
t.Fatal("the value was not cached")
}
inv, err := cascache.NewInvalidator(cascache.InvalidatorOptions{
Namespace: namespace,
Backend: b,
})
if err != nil {
t.Fatal(err)
}
if err := inv.Invalidate(ctx, "42"); err != nil {
t.Fatalf("Invalidate: %v", err)
}
if _, ok, err := cache.Get(ctx, "42"); ok || err != nil {
t.Fatalf("Get after invalidation = %v, %v; want a miss", ok, err)
}
}
func TestInvalidatorNamespaceMustMatch(t *testing.T) {
ctx := context.Background()
store := memstore.New(memstore.Options{})
fences := newFakeFences()
b, err := backend.NewComposite(store, fences)
if err != nil {
t.Fatal(err)
}
cache, err := cascache.New(cascache.Options[user]{
Namespace: "right", Backend: b, Codec: codec.JSON[user]{},
})
if err != nil {
t.Fatal(err)
}
fence, err := cache.Snapshot(ctx, "42")
if err != nil {
t.Fatal(err)
}
if _, err := cache.Set(ctx, "42", ada, fence); err != nil {
t.Fatal(err)
}
wrong, err := cascache.NewInvalidator(cascache.InvalidatorOptions{
Namespace: "wrong", Backend: b,
})
if err != nil {
t.Fatal(err)
}
if err := wrong.Invalidate(ctx, "42"); err != nil {
t.Fatal(err)
}
if _, ok, _ := cache.Get(ctx, "42"); !ok {
t.Fatal("an invalidation in another namespace retired this entry")
}
}
func TestCacheInvalidatorSharesTheCacheNamespace(t *testing.T) {
h := newHarness(t, nil)
ctx := context.Background()
h.fill(t, "42", ada)
if err := h.cache.Invalidator().Invalidate(ctx, "42"); err != nil {
t.Fatalf("Invalidate: %v", err)
}
if _, ok := h.mustGet(t, "42"); ok {
t.Fatal("the handle from Cache.Invalidator did not retire the entry")
}
}
func TestDisabledInvalidatorIsANoOp(t *testing.T) {
store := memstore.New(memstore.Options{})
b, err := backend.NewComposite(store, newFakeFences())
if err != nil {
t.Fatal(err)
}
inv, err := cascache.NewInvalidator(cascache.InvalidatorOptions{
Namespace: "test", Backend: b, Disabled: true,
})
if err != nil {
t.Fatal(err)
}
if inv.Enabled() {
t.Fatal("Enabled reported true for a disabled invalidator")
}
if err := inv.Invalidate(context.Background(), "42"); err != nil {
t.Fatalf("Invalidate: %v", err)
}
if store.Len() != 0 {
t.Fatal("a disabled invalidator touched the store")
}
}