-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathdataloaden_test.go
351 lines (314 loc) · 7.23 KB
/
dataloaden_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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
package dataloadgen_test
import (
"context"
"fmt"
"strings"
"sync"
"testing"
"time"
"github.com/vikstrous/dataloadgen"
)
type benchmarkUser struct {
Name string
ID string
}
func TestUserLoader(t *testing.T) {
ctx := context.Background()
var fetches [][]string
var mu sync.Mutex
dl := dataloadgen.NewLoader(func(_ context.Context, keys []string) ([]*benchmarkUser, []error) {
mu.Lock()
fetches = append(fetches, keys)
mu.Unlock()
users := make([]*benchmarkUser, len(keys))
errors := make([]error, len(keys))
for i, key := range keys {
if strings.HasPrefix(key, "F") {
return nil, []error{fmt.Errorf("failed all fetches")}
}
if strings.HasPrefix(key, "E") {
errors[i] = fmt.Errorf("user not found")
} else {
users[i] = &benchmarkUser{ID: key, Name: "user " + key}
}
}
return users, errors
},
dataloadgen.WithBatchCapacity(5),
dataloadgen.WithWait(10*time.Millisecond),
)
t.Run("fetch concurrent data", func(t *testing.T) {
t.Run("load user successfully", func(t *testing.T) {
t.Parallel()
u, err := dl.Load(ctx, "U1")
if err != nil {
t.Fatal(err)
}
if u.ID != "U1" {
t.Fatal("not equal")
}
})
t.Run("load failed user", func(t *testing.T) {
t.Parallel()
u, err := dl.Load(ctx, "E1")
if err == nil {
t.Fatal("error expected")
}
if u != nil {
t.Fatal("not nil", u)
}
})
t.Run("load many users", func(t *testing.T) {
t.Parallel()
u, err := dl.LoadAll(ctx, []string{"U2", "E2", "E3", "U4"})
if u[0].Name != "user U2" {
t.Fatal("not equal")
}
if u[3].Name != "user U4" {
t.Fatal("not equal")
}
if err == nil {
t.Fatal("error expected")
}
if err.(dataloadgen.ErrorSlice)[1] == nil {
t.Fatal("error expected")
}
if err.(dataloadgen.ErrorSlice)[2] == nil {
t.Fatal("error expected")
}
})
t.Run("load thunk", func(t *testing.T) {
t.Parallel()
thunk1 := dl.LoadThunk(ctx, "U5")
thunk2 := dl.LoadThunk(ctx, "E5")
u1, err1 := thunk1()
if err1 != nil {
t.Fatal(err1)
}
if "user U5" != u1.Name {
t.Fatal("not equal")
}
u2, err2 := thunk2()
if err2 == nil {
t.Fatal("error expected")
}
if u2 != nil {
t.Fatal("not nil", u2)
}
})
})
t.Run("it sent two batches", func(t *testing.T) {
mu.Lock()
defer mu.Unlock()
if len(fetches) != 2 {
t.Fatal("wrong length", fetches)
}
if len(fetches[0]) != 5 {
t.Error("wrong number of keys in first fetch", fetches[0])
}
if len(fetches[1]) != 3 {
t.Error("wrong number of keys in second fetch", fetches[0])
}
})
t.Run("fetch more", func(t *testing.T) {
t.Run("previously cached", func(t *testing.T) {
t.Parallel()
u, err := dl.Load(ctx, "U1")
if err != nil {
t.Fatal("error expected")
}
if u.ID != "U1" {
t.Fatal("not equal")
}
})
t.Run("load many users", func(t *testing.T) {
t.Parallel()
u, err := dl.LoadAll(ctx, []string{"U2", "U4"})
if err != nil {
t.Fatal(err)
}
if u[0].Name != "user U2" {
t.Fatal("not equal")
}
if u[1].Name != "user U4" {
t.Fatal("not equal")
}
})
})
t.Run("no round trips", func(t *testing.T) {
mu.Lock()
defer mu.Unlock()
if len(fetches) != 2 {
t.Fatal("wrong length", fetches)
}
})
t.Run("fetch partial", func(t *testing.T) {
t.Run("errors not in cache cache value", func(t *testing.T) {
t.Parallel()
u, err := dl.Load(ctx, "E2")
if u != nil {
t.Fatal("not nil", u)
}
if err == nil {
t.Fatal("error expected")
}
})
t.Run("load all", func(t *testing.T) {
t.Parallel()
u, err := dl.LoadAll(ctx, []string{"U1", "U4", "E1", "U9", "U5"})
if u[0].ID != "U1" {
t.Fatal("not equal")
}
if u[1].ID != "U4" {
t.Fatal("not equal")
}
if err.(dataloadgen.ErrorSlice)[2] == nil {
t.Fatal("error expected")
}
if u[3].ID != "U9" {
t.Fatal("not equal")
}
if u[4].ID != "U5" {
t.Fatal("not equal")
}
})
})
t.Run("one partial trip", func(t *testing.T) {
mu.Lock()
defer mu.Unlock()
if len(fetches) != 3 {
t.Fatal("wrong length", fetches)
}
// U9 only because E1 and E2 are already cached as failed and only U9 is new
if len(fetches[2]) != 1 {
t.Fatal("wrong length", fetches[2])
}
})
t.Run("primed reads dont hit the fetcher", func(t *testing.T) {
dl.Prime("U99", &benchmarkUser{ID: "U99", Name: "Primed user"})
u, err := dl.Load(ctx, "U99")
if err != nil {
t.Fatal("error expected")
}
if "Primed user" != u.Name {
t.Fatal("not equal")
}
if len(fetches) != 3 {
t.Fatal("wrong length", fetches)
}
})
t.Run("priming in a loop is safe", func(t *testing.T) {
users := []benchmarkUser{
{ID: "Alpha", Name: "Alpha"},
{ID: "Omega", Name: "Omega"},
}
for _, user := range users {
user := user
dl.Prime(user.ID, &user)
}
u, err := dl.Load(ctx, "Alpha")
if err != nil {
t.Fatal("error expected")
}
if "Alpha" != u.Name {
t.Fatal("not equal")
}
u, err = dl.Load(ctx, "Omega")
if err != nil {
t.Fatal("error expected")
}
if "Omega" != u.Name {
t.Fatal("not equal")
}
if len(fetches) != 3 {
t.Fatal("wrong length", fetches)
}
})
t.Run("cleared results will go back to the fetcher", func(t *testing.T) {
dl.Clear("U99")
u, err := dl.Load(ctx, "U99")
if err != nil {
t.Fatal("error expected")
}
if "user U99" != u.Name {
t.Fatal("not equal")
}
if len(fetches) != 4 {
t.Fatal("wrong length", fetches)
}
})
t.Run("load all thunk", func(t *testing.T) {
thunk1 := dl.LoadAllThunk(ctx, []string{"U5", "U6"})
thunk2 := dl.LoadAllThunk(ctx, []string{"U6", "E6"})
users1, err1 := thunk1()
if len(fetches) != 5 {
t.Fatal("wrong length", fetches)
}
if err1 != nil {
t.Fatal(err1)
}
if "user U5" != users1[0].Name {
t.Fatal("not equal")
}
if "user U6" != users1[1].Name {
t.Fatal("not equal")
}
users2, err2 := thunk2()
// already cached
if len(fetches) != 5 {
t.Fatal("wrong length", fetches)
}
if err2.(dataloadgen.ErrorSlice)[0] != nil {
t.Fatal(err2.(dataloadgen.ErrorSlice)[0])
}
if err2.(dataloadgen.ErrorSlice)[1] == nil {
t.Fatal("error expected")
}
if "user U6" != users2[0].Name {
t.Fatal("not equal")
}
})
t.Run("single error return value works", func(t *testing.T) {
user, err := dl.Load(ctx, "F1")
if err == nil {
t.Fatal("error expected")
}
if "failed all fetches" != err.Error() {
t.Fatal("not equal")
}
if user != nil {
t.Fatal("not empty", user)
}
if len(fetches) != 6 {
t.Fatal("wrong length", fetches)
}
})
t.Run("LoadAll does a single fetch", func(t *testing.T) {
dl.Clear("U1")
dl.Clear("F1")
users, errs := dl.LoadAll(ctx, []string{"F1", "U1"})
if len(fetches) != 7 {
t.Fatal("wrong length", fetches)
}
for _, user := range users {
if user != nil {
t.Fatal("not empty", user)
}
}
if len(errs.(dataloadgen.ErrorSlice)) != 2 {
t.Fatal("wrong length", errs)
}
if errs.(dataloadgen.ErrorSlice)[0] == nil {
t.Fatal("error expected")
}
if "failed all fetches" != errs.(dataloadgen.ErrorSlice)[0].Error() {
t.Fatal("not equal")
}
if errs.(dataloadgen.ErrorSlice)[1] == nil {
t.Fatal("error expected")
}
if "failed all fetches" != errs.(dataloadgen.ErrorSlice)[1].Error() {
t.Fatal("not equal")
}
})
}