-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch_test.go
More file actions
145 lines (129 loc) · 3.27 KB
/
fetch_test.go
File metadata and controls
145 lines (129 loc) · 3.27 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
package ttlmap_test
import (
"log"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/packaged/ttlmap"
)
func TestFetch_MissSingleflight(t *testing.T) {
cache := ttlmap.New(ttlmap.WithDefaultTTL(100 * time.Millisecond))
var calls int32
source := func(key string) (int, error) {
time.Sleep(10 * time.Millisecond)
atomic.AddInt32(&calls, 1)
return 123, nil
}
// Launch many concurrent callers for a missing key
const n = 32
var wg sync.WaitGroup
wg.Add(n)
results := make([]int, n)
for i := 0; i < n; i++ {
go func(ix int) {
defer wg.Done()
v, err := ttlmap.Fetch[int](cache, "k1", source)
if err != nil {
t.Errorf("unexpected error: %v", err)
return
}
results[ix] = v
}(i)
}
wg.Wait()
if atomic.LoadInt32(&calls) != 1 {
t.Fatalf("expected single source call, got %d", calls)
}
for i := 0; i < n; i++ {
if results[i] != 123 {
t.Fatalf("unexpected result at %d: %d", i, results[i])
}
}
}
func TestFetch_TypeMismatch(t *testing.T) {
cache := ttlmap.New(ttlmap.WithDefaultTTL(100 * time.Millisecond))
cache.Set("k", "hello", nil)
var called int32
src := func(key string) (int, error) {
atomic.AddInt32(&called, 1)
return 42, nil
}
_, err := ttlmap.Fetch[int](cache, "k", src)
if err == nil {
t.Fatalf("expected ErrTypeMismatch, got nil")
}
if err != ttlmap.ErrTypeMismatch {
t.Fatalf("expected ErrTypeMismatch, got %v", err)
}
if atomic.LoadInt32(&called) != 0 {
t.Fatalf("source should not be called on type mismatch hit")
}
}
func TestFetch_StaleWhileRevalidate(t *testing.T) {
// Short TTL to trigger expiry
cache := ttlmap.New(ttlmap.WithDefaultTTL(time.Second), ttlmap.WithMaxLifetime(2*time.Second))
ttl := 20 * time.Millisecond
cache.Set("sk", 1, &ttl)
src := func(key string) (int, error) {
time.Sleep(15 * time.Millisecond)
return 2, nil
}
// First call should return stale value 1 and trigger background refresh
v1, err := ttlmap.Fetch[int](cache, "sk", src)
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
if v1 != 1 {
t.Fatalf("expected stale value 1, got %d", v1)
}
wg := sync.WaitGroup{}
for i := 0; i < 100000; i++ {
wg.Add(1)
go func() {
defer wg.Done()
_, fetchErr := ttlmap.Fetch[int](cache, "sk", src)
if fetchErr != nil {
log.Print(fetchErr.Error())
}
}()
}
wg.Wait()
time.Sleep(10 * time.Millisecond)
// Next call should observe refreshed value 2
v2, err := ttlmap.Fetch[int](cache, "sk", src)
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
if v2 != 2 {
t.Fatalf("expected refreshed value 2, got %d", v2)
}
}
func TestFetch_ZeroDelay(t *testing.T) {
cache := ttlmap.New(ttlmap.WithDefaultTTL(15*time.Millisecond), ttlmap.WithMaxLifetime(2*time.Second))
funcTime := 15 * time.Millisecond
funcTime = time.Millisecond * 100
ttl := 20 * time.Millisecond
cache.Set("sk", 1, &ttl)
src := func(key string) (int, error) {
time.Sleep(funcTime)
return time.Now().Second(), nil
}
wg := sync.WaitGroup{}
for i := 0; i < 1000; i++ {
wg.Add(1)
go func() {
defer wg.Done()
start := time.Now()
_, fetchErr := ttlmap.Fetch[int](cache, "sk", src)
if fetchErr != nil {
log.Print(fetchErr.Error())
}
if time.Since(start) >= funcTime-time.Millisecond {
log.Print("fetch took too long")
}
}()
time.Sleep(time.Millisecond)
}
wg.Wait()
}