-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataloader_test.go
More file actions
118 lines (103 loc) · 2.54 KB
/
dataloader_test.go
File metadata and controls
118 lines (103 loc) · 2.54 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
package dataloader
import (
"context"
"fmt"
"reflect"
"sync"
"testing"
)
type user struct {
ID string
Name string
}
type userBatchLoader struct {
users map[string]*user
calls int
}
func (c *userBatchLoader) BatchLoad(ctx context.Context, keys *[]string) (map[string]*user, error) {
users := make(map[string]*user)
for _, key := range *keys {
if user, ok := c.users[key]; ok {
users[key] = user
} else {
users[key] = nil
}
}
c.calls += 1
return users, nil
}
func generateTestUsers() map[string]*user {
users := make(map[string]*user)
// Generate test users
for i := 0; i < 20; i++ {
u := &user{
ID: fmt.Sprintf("%d", i),
Name: fmt.Sprintf("User %d", i),
}
users[u.ID] = u
}
return users
}
func TestNewDataLoader(t *testing.T) {
// Create a new data loader.
users := generateTestUsers()
ctx, cancel := context.WithCancel(context.Background())
c := &userBatchLoader{users: users, calls: 0}
dataLoader := NewDataLoader[string, user](ctx, c, 2, 20)
// Load a user by ID (sync)
loadedUser, err := dataLoader.Load("0")
if err != nil {
t.Errorf("Expected error to be nil, but got %v", err)
}
if loadedUser != users["0"] {
t.Errorf("Expected user to be %v, but got %v", users["0"], loadedUser)
}
if c.calls != 1 {
t.Errorf("Expected calls to be 1, but got %v", c.calls)
}
c.calls = 0
// Load a user by ID (async)
wg := sync.WaitGroup{}
for i := 1; i < 11; i++ {
go func(id string) {
loadedUser, err := dataLoader.Load(id)
if err != nil {
t.Errorf("Expected error to be nil, but got %v", err)
}
if loadedUser != users[id] {
t.Errorf("Expected user to be %v, but got %v", users[id], loadedUser)
}
wg.Done()
}(fmt.Sprintf("%d", i))
wg.Add(1)
}
wg.Wait()
if c.calls != 5 {
t.Errorf("Expected calls to be 5, but got %v", c.calls)
}
c.calls = 0
loadedUser, err = dataLoader.Load("55")
if err != nil {
t.Errorf("Expected error to be nil, but got %v", err)
}
if loadedUser != nil {
t.Errorf("Expected user to be nil, but got %v", loadedUser)
}
if c.calls != 1 {
t.Errorf("Expected calls to be 1, but got %v", c.calls)
}
c.calls = 0
loadedUsers, err := dataLoader.LoadMany(&[]string{"12", "13", "14"})
expectedUsers := []*user{users["12"], users["13"], users["14"]}
if err != nil {
t.Errorf("Expected error to be nil, but got %v", err)
}
if !reflect.DeepEqual(loadedUsers, expectedUsers) {
t.Errorf("Expected users to be %v, but got %v", expectedUsers, loadedUsers)
}
if c.calls != 2 {
t.Errorf("Expected calls to be 2, but got %v", c.calls)
}
c.calls = 0
cancel()
}