-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhooks_test.go
More file actions
109 lines (97 loc) · 2.58 KB
/
Copy pathhooks_test.go
File metadata and controls
109 lines (97 loc) · 2.58 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
package c3e
import (
"context"
"testing"
"time"
)
func TestHooks_OnGet_calledOnFallback(t *testing.T) {
defer func() {
if r := recover(); r != nil {
t.Skip("Skipping: Valkey not available to build a real command builder")
}
}()
cm, err := NewCacheManager(newMockCacheRepository(), false)
if err != nil {
t.Fatalf("NewCacheManager: %v", err)
}
var (
calls int
gotResult Result
gotID CacheIdentifier
gotElapsed bool
)
cfg := SafeCacheManagerConfig{
HardTTL: 2 * time.Hour,
SoftTTL: 1 * time.Hour,
JitterPercent: 0.1,
Hooks: Hooks{
OnGet: func(_ context.Context, id CacheIdentifier, r Result, took time.Duration) {
calls++
gotResult = r
gotID = id
gotElapsed = took >= 0
},
},
}
sm, err := NewSafeCacheManager(cm, cfg)
if err != nil {
t.Fatalf("NewSafeCacheManager: %v", err)
}
fetcher := func(_ context.Context) (any, []CacheIdentifier, error) {
return testData{ID: 1, Name: "from source"}, nil, nil
}
id := CacheIdentifier{Type: "thing", ID: "1"}
var dst testData
if err := sm.Get(context.Background(), id, &dst, fetcher); err != nil {
t.Fatalf("Get: %v", err)
}
if calls != 1 {
t.Fatalf("OnGet called %d times, want 1", calls)
}
if gotID != id {
t.Errorf("OnGet id = %v, want %v", gotID, id)
}
if gotResult == "" {
t.Error("OnGet result is empty")
}
if !gotElapsed {
t.Error("OnGet elapsed not reported")
}
}
func TestHooks_OnInvalidate_called(t *testing.T) {
defer func() {
if r := recover(); r != nil {
t.Skip("Skipping: Valkey not available to build a real command builder")
}
}()
cm, err := NewCacheManager(newMockCacheRepository(), false)
if err != nil {
t.Fatalf("NewCacheManager: %v", err)
}
called := false
cfg := SafeCacheManagerConfig{
HardTTL: 2 * time.Hour,
SoftTTL: 1 * time.Hour,
JitterPercent: 0.1,
Hooks: Hooks{
OnInvalidate: func(_ context.Context, _ CacheIdentifier, _ time.Duration, _ error) {
called = true
},
},
}
sm, err := NewSafeCacheManager(cm, cfg)
if err != nil {
t.Fatalf("NewSafeCacheManager: %v", err)
}
_ = sm.Invalidate(context.Background(), CacheIdentifier{Type: "thing", ID: "1"})
if !called {
t.Error("OnInvalidate was not called")
}
}
func TestHooks_zeroValueIsNoop(t *testing.T) {
// A zero Hooks must not panic when invoked.
var h Hooks
h.onGet(context.Background(), CacheIdentifier{Type: "t", ID: "1"}, ResultHit, time.Millisecond)
h.onRefresh(context.Background(), CacheIdentifier{Type: "t", ID: "1"}, nil)
h.onInvalidate(context.Background(), CacheIdentifier{Type: "t", ID: "1"}, time.Millisecond, nil)
}