-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwith_loader_test.go
More file actions
137 lines (117 loc) · 2.8 KB
/
Copy pathwith_loader_test.go
File metadata and controls
137 lines (117 loc) · 2.8 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
package golazy
import (
"context"
"errors"
"sync"
"testing"
"time"
)
// Internal tests for newWithLoader and withLoader implementation
func TestNewWithLoader(t *testing.T) {
t.Parallel()
loader := func(ctx context.Context, args ...any) (string, error) {
return "value", nil
}
wl := newWithLoader(loader, false, 0)
if wl.loader == nil {
t.Error("loader should not be nil")
}
if wl.withTTL != false {
t.Error("withTTL should be false")
}
}
func TestNewWithLoaderPreloaded(t *testing.T) {
t.Parallel()
loader := func(ctx context.Context, args ...any) (string, error) {
return "new_value", nil
}
wl := newWithLoaderPreloaded("preloaded", loader, false, 0)
if !wl.loaded && wl.value != "preloaded" {
t.Error("preloaded value not set correctly")
}
}
func TestValue_CachedValue(t *testing.T) {
t.Parallel()
callCount := 0
loader := func(ctx context.Context, args ...any) (string, error) {
callCount++
return "value", nil
}
wl := newWithLoader(loader, false, 0)
v1, err := wl.Value()
if err != nil || v1 != "value" || callCount != 1 {
t.Error("first call should invoke loader")
}
v2, err := wl.Value()
if err != nil || v2 != "value" || callCount != 1 {
t.Error("second call should use cache")
}
}
func TestValue_LoaderError(t *testing.T) {
t.Parallel()
loader := func(ctx context.Context, args ...any) (string, error) {
return "", errors.New("loader error")
}
wl := newWithLoader(loader, false, 0)
_, err := wl.Value()
if err == nil || err.Error() != "loader error" {
t.Error("error should be propagated")
}
}
func TestValue_TTLExpiration(t *testing.T) {
callCount := 0
loader := func(ctx context.Context, args ...any) (string, error) {
callCount++
return "value", nil
}
wl := newWithLoader(loader, true, 100*time.Millisecond)
wl.Value()
if callCount != 1 {
t.Error("loader should be called once")
}
wl.Value()
if callCount != 1 {
t.Error("cached value should be used")
}
time.Sleep(150 * time.Millisecond)
wl.Value()
if callCount != 2 {
t.Error("expired cache should trigger reload")
}
}
func TestClear(t *testing.T) {
t.Parallel()
loader := func(ctx context.Context, args ...any) (string, error) {
return "value", nil
}
wl := newWithLoader(loader, false, 0)
wl.Value()
if !wl.loaded {
t.Error("value should be loaded")
}
wl.Clear()
if wl.loaded {
t.Error("value should be cleared")
}
}
func TestConcurrency(t *testing.T) {
callCount := 0
loader := func(ctx context.Context, args ...any) (string, error) {
callCount++
time.Sleep(10 * time.Millisecond)
return "value", nil
}
wl := newWithLoader(loader, false, 0)
var wg sync.WaitGroup
for i := 0; i < 10; i++ {
wg.Add(1)
go func() {
defer wg.Done()
wl.Value()
}()
}
wg.Wait()
if callCount != 1 {
t.Errorf("loader should be called once, but was called %d times", callCount)
}
}