-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
loader_test.go
56 lines (46 loc) · 1.27 KB
/
loader_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
package hot
import (
"sync/atomic"
"testing"
"github.com/stretchr/testify/assert"
)
func TestLoaders_run(t *testing.T) {
is := assert.New(t)
counter := int32(0)
// without error
loaders := LoaderChain[int, int]{
func(keys []int) (map[int]int, error) {
atomic.AddInt32(&counter, 1)
return map[int]int{1: 1, 2: 2}, nil
},
func(keys []int) (map[int]int, error) {
atomic.AddInt32(&counter, 1)
return map[int]int{2: 42, 3: 3}, nil
},
}
results, missing, err := loaders.run([]int{1, 2, 3, 4})
is.EqualValues(map[int]int{1: 1, 2: 42, 3: 3}, results)
is.EqualValues([]int{4}, missing)
is.Nil(err)
is.EqualValues(2, atomic.LoadInt32(&counter))
// with error
loaders = LoaderChain[int, int]{
func(keys []int) (map[int]int, error) {
atomic.AddInt32(&counter, 1)
return map[int]int{1: 1, 2: 2}, nil
},
func(keys []int) (map[int]int, error) {
atomic.AddInt32(&counter, 1)
return map[int]int{2: 42, 3: 3}, nil
},
func(keys []int) (map[int]int, error) {
atomic.AddInt32(&counter, 1)
return map[int]int{4: 4}, assert.AnError
},
}
results, missing, err = loaders.run([]int{1, 2, 3, 4})
is.EqualValues(map[int]int{}, results)
is.EqualValues([]int{}, missing)
is.ErrorIs(assert.AnError, err)
is.EqualValues(5, atomic.LoadInt32(&counter))
}