-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers_test.go
More file actions
138 lines (103 loc) · 2.94 KB
/
helpers_test.go
File metadata and controls
138 lines (103 loc) · 2.94 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
package scache_test
import (
"context"
"sync"
"time"
"github.com/nussjustin/scache"
)
func blockingFunc(ctx context.Context) scache.Func[string, string] {
return func(funcCtx context.Context, _ string) (string, error) {
select {
case <-ctx.Done():
return "", context.Cause(ctx)
case <-funcCtx.Done():
return "", context.Cause(funcCtx)
}
}
}
func panicFunc(v any) scache.Func[string, string] {
return func(context.Context, string) (string, error) {
panic(v)
}
}
func valueFunc(v string) scache.Func[string, string] {
return func(context.Context, string) (string, error) {
return v, nil
}
}
type errorAdapter struct {
err error
}
var _ scache.Adapter[string, string] = (*panicAdapter)(nil)
func (e errorAdapter) Get(_ context.Context, _ string) (value string, age time.Duration, err error) {
return "", 0, e.err
}
func (e errorAdapter) Set(context.Context, string, string) error {
panic("Set called on errorAdapter")
}
type memoryValue struct {
age time.Duration
v string
}
type memoryAdapter struct {
mu sync.Mutex
m map[string]memoryValue
chans []chan<- string
}
var _ scache.Adapter[string, string] = (*memoryAdapter)(nil)
func newMemoryAdapter() *memoryAdapter {
return &memoryAdapter{m: make(map[string]memoryValue)}
}
func (m *memoryAdapter) Get(_ context.Context, key string) (value string, age time.Duration, err error) {
m.mu.Lock()
v, ok := m.m[key]
m.mu.Unlock()
if !ok {
return value, age, scache.ErrNotFound
}
return v.v, v.age, nil
}
func (m *memoryAdapter) Set(_ context.Context, key string, value string) error {
m.setWithAge(key, value, 0)
return nil
}
func (m *memoryAdapter) setWithAge(key string, value string, age time.Duration) {
m.mu.Lock()
defer m.mu.Unlock()
m.m[key] = memoryValue{v: value, age: age}
for _, c := range m.chans {
c <- key
}
m.chans = m.chans[:0]
}
func (m *memoryAdapter) changeCh() <-chan string {
ch := make(chan string, 1)
m.mu.Lock()
m.chans = append(m.chans, ch)
m.mu.Unlock()
return ch
}
type noopAdapter struct{}
var _ scache.Adapter[string, string] = (*noopAdapter)(nil)
func (n noopAdapter) Get(_ context.Context, _ string) (value string, age time.Duration, err error) {
return value, age, scache.ErrNotFound
}
func (n noopAdapter) Set(context.Context, string, string) error {
return nil
}
type panicAdapter struct{}
var _ scache.Adapter[string, string] = (*panicAdapter)(nil)
func (p panicAdapter) Get(_ context.Context, _ string) (value string, age time.Duration, err error) {
panic("Get called on panicAdapter")
}
func (p panicAdapter) Set(context.Context, string, string) error {
panic("Set called on panicAdapter")
}
type staticValueAdapter struct{ age time.Duration }
var _ scache.Adapter[string, string] = staticValueAdapter{}
func (s staticValueAdapter) Get(_ context.Context, key string) (value string, age time.Duration, err error) {
return key, s.age, nil
}
func (s staticValueAdapter) Set(context.Context, string, string) error {
return nil
}