This repository has been archived by the owner on Nov 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdoi_info_cache_test.go
188 lines (156 loc) · 4.28 KB
/
doi_info_cache_test.go
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package main_test
import (
"errors"
"fmt"
"strings"
"testing"
"time"
"github.com/go-test/deep"
pass "github.com/oa-pass/pass-download-service"
)
func TestSimpleCase(t *testing.T) {
cache := pass.NewDoiCache(pass.DoiCacheConfig{})
expected := &pass.DoiInfo{
Manuscripts: []pass.Manuscript{
{
RepositoryInstitution: "Foo",
Location: "Bar",
},
},
}
manuscripts, _ := cache.GetOrAdd("foo", func() (*pass.DoiInfo, error) {
return &pass.DoiInfo{
Manuscripts: []pass.Manuscript{
{
RepositoryInstitution: "Foo",
Location: "Bar",
},
},
}, nil
})
diffs := deep.Equal(manuscripts, expected)
if len(diffs) > 0 {
t.Fatalf("Found differences between cached results and expected:\n%s", strings.Join(diffs, "\n"))
}
}
func TestEvictSize(t *testing.T) {
cache := pass.NewDoiCache(pass.DoiCacheConfig{
MaxSize: 1,
MaxAge: 1 * time.Second,
})
assertComputed(t, cache, "foo")
assertNotComputed(t, cache, "foo") // foo should be cached here
assertComputed(t, cache, "bar")
assertComputed(t, cache, "foo") // no longer cached if it'd been evicted
}
func TestEvictTimeout(t *testing.T) {
cache := pass.NewDoiCache(pass.DoiCacheConfig{
MaxSize: 1,
MaxAge: 1 * time.Microsecond,
})
assertComputed(t, cache, "foo")
for i := 1; i < 10; i++ {
if didCompute(cache, "foo") {
return // good, it was evicted and had to be re-computed
}
time.Sleep(10 * time.Millisecond)
}
t.Fatalf("Cache entry should have been evicted by now")
}
// Make sure only one simultaneous/contested add wins
func TestContested(t *testing.T) {
cache := pass.NewDoiCache(pass.DoiCacheConfig{})
errChan := make(chan error)
exec1 := make(chan bool)
ready1 := make(chan bool)
ready2 := make(chan bool)
result1 := make(chan *pass.DoiInfo)
result2 := make(chan *pass.DoiInfo)
expected := &pass.DoiInfo{
Manuscripts: []pass.Manuscript{
{
RepositoryInstitution: "Foo",
Location: "Bar",
},
},
}
// 1: This will execute and calculate the result once we signal it to do so
// on the exec channel
go func() {
result, _ := cache.GetOrAdd("foo", func() (*pass.DoiInfo, error) {
ready1 <- true
<-exec1
return &pass.DoiInfo{
Manuscripts: []pass.Manuscript{
{
RepositoryInstitution: "Foo",
Location: "Bar",
},
},
}, nil
})
result1 <- result
}()
<-ready1 // Wait until our generator function is running, but paused until we signal
// 2: This will block, and return the result from 1
go func() {
ready2 <- true
result, _ := cache.GetOrAdd("foo", func() (*pass.DoiInfo, error) {
// This shouldn't execute
errChan <- errors.New("cache function executed when not expected to")
return &pass.DoiInfo{}, nil
})
result2 <- result
errChan <- nil
}()
<-ready2 // Wait until 2 blocks on 1 finishing
exec1 <- true // Let 1 execute
if diffs := deep.Equal(expected, <-result2); len(diffs) > 0 {
t.Fatalf("Did not get expected cached result:\n%s", strings.Join(diffs, "\n"))
}
if diffs := deep.Equal(expected, <-result1); len(diffs) > 0 {
t.Fatalf("Did not get expected cached result:\n%s", strings.Join(diffs, "\n"))
}
if err := <-errChan; err != nil {
t.Fatal(err)
}
}
func TestError(t *testing.T) {
cache := pass.NewDoiCache(pass.DoiCacheConfig{})
_, err := cache.GetOrAdd("foo", func() (*pass.DoiInfo, error) {
return nil, fmt.Errorf("error")
})
if err == nil {
t.Fatalf("Should have gotten an error!")
}
// After our error, we should be able to add just fine
assertComputed(t, cache, "foo")
}
// expects the cache to execute the generator function for a given key
func assertComputed(t *testing.T, cache *pass.DoiCache, doi string) {
t.Helper()
if !didCompute(cache, doi) {
t.Fatalf("Cache did compute and cache when expected")
}
}
func assertNotComputed(t *testing.T, cache *pass.DoiCache, doi string) {
t.Helper()
if didCompute(cache, doi) {
t.Fatalf("Cache computed and cached result when not expected to")
}
}
func didCompute(cache *pass.DoiCache, doi string) bool {
var computed bool
_, _ = cache.GetOrAdd(doi, func() (*pass.DoiInfo, error) {
computed = true
return &pass.DoiInfo{
Manuscripts: []pass.Manuscript{
{
RepositoryInstitution: "Foo",
Location: "Bar",
},
},
}, nil
})
return computed
}