Skip to content

Commit e8a1cfc

Browse files
committed
fix vikstrous#14 fix panic when wrong number of errors returned
1 parent 7777976 commit e8a1cfc

File tree

2 files changed

+57
-31
lines changed

2 files changed

+57
-31
lines changed

dataloadgen.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,16 +132,21 @@ func (l *Loader[KeyT, ValueT]) LoadThunk(ctx context.Context, key KeyT) func() (
132132

133133
// If the batch function returned the wrong number of responses, return an error to all callers
134134
if len(batch.results) != len(batch.keys) {
135-
return data, fmt.Errorf("bug in loader: %d values returned for %d keys", len(batch.results), len(batch.keys))
135+
return data, fmt.Errorf("bug in fetch function: %d values returned for %d keys", len(batch.results), len(batch.keys))
136136
}
137137

138138
if pos < len(batch.results) {
139139
data = batch.results[pos]
140140
}
141141

142142
var err error
143-
if batch.errors != nil {
144-
err = batch.errors[pos]
143+
if len(batch.errors) != 0 {
144+
if pos < len(batch.errors) {
145+
err = batch.errors[pos]
146+
} else {
147+
err = fmt.Errorf("bug in fetch function: %d errors returned for %d keys; last error: %w", len(batch.errors), len(batch.keys), batch.errors[len(batch.errors)-1])
148+
}
149+
145150
}
146151

147152
return data, err

dataloadgen_test.go

Lines changed: 49 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func ExampleLoader() {
3232
// Output: 1
3333
}
3434

35-
func TestEdgeCases(t *testing.T) {
35+
func TestCache(t *testing.T) {
3636
ctx := context.Background()
3737
var fetches [][]int
3838
var mu sync.Mutex
@@ -57,33 +57,54 @@ func TestEdgeCases(t *testing.T) {
5757
dataloadgen.WithWait(1*time.Millisecond),
5858
)
5959

60-
t.Run("load function called only once when cached", func(t *testing.T) {
61-
for i := 0; i < 2; i++ {
62-
_, err := dl.Load(ctx, 0)
63-
if err == nil {
64-
t.Fatal("expected error")
65-
}
66-
if len(fetches) != 1 {
67-
t.Fatal("wrong number of fetches", fetches)
68-
}
69-
if len(fetches[0]) != 1 {
70-
t.Fatal("wrong number of keys in fetch request")
71-
}
60+
for i := 0; i < 2; i++ {
61+
_, err := dl.Load(ctx, 0)
62+
if err == nil {
63+
t.Fatal("expected error")
7264
}
73-
for i := 0; i < 2; i++ {
74-
r, err := dl.Load(ctx, 1)
75-
if err != nil {
76-
t.Fatal(err)
77-
}
78-
if len(fetches) != 2 {
79-
t.Fatal("wrong number of fetches", fetches)
80-
}
81-
if len(fetches[1]) != 1 {
82-
t.Fatal("wrong number of keys in fetch request")
83-
}
84-
if r != "1" {
85-
t.Fatal("wrong data fetched", r)
86-
}
65+
if len(fetches) != 1 {
66+
t.Fatal("wrong number of fetches", fetches)
67+
}
68+
if len(fetches[0]) != 1 {
69+
t.Fatal("wrong number of keys in fetch request")
70+
}
71+
}
72+
for i := 0; i < 2; i++ {
73+
r, err := dl.Load(ctx, 1)
74+
if err != nil {
75+
t.Fatal(err)
76+
}
77+
if len(fetches) != 2 {
78+
t.Fatal("wrong number of fetches", fetches)
79+
}
80+
if len(fetches[1]) != 1 {
81+
t.Fatal("wrong number of keys in fetch request")
8782
}
88-
})
83+
if r != "1" {
84+
t.Fatal("wrong data fetched", r)
85+
}
86+
}
87+
}
88+
89+
func TestErrors(t *testing.T) {
90+
ctx := context.Background()
91+
dl := dataloadgen.NewLoader(func(_ context.Context, keys []int) ([]string, []error) {
92+
return []string{"1", "2", "3"}, []error{fmt.Errorf("error 1"), fmt.Errorf("error 2")}
93+
},
94+
dataloadgen.WithBatchCapacity(3),
95+
)
96+
_, err := dl.LoadAll(ctx, []int{1, 2, 3})
97+
errs := err.(dataloadgen.ErrorSlice)
98+
if len(errs) != 3 {
99+
t.Fatalf("wrong number of errors: %d", len(errs))
100+
}
101+
if errs[0].Error() != "error 1" {
102+
t.Fatalf("wrong error: %s", errs[0].Error())
103+
}
104+
if errs[1].Error() != "error 2" {
105+
t.Fatalf("wrong error: %s", errs[1].Error())
106+
}
107+
if errs[2].Error() != "bug in fetch function: 2 errors returned for 3 keys; last error: error 2" {
108+
t.Fatalf("wrong error: %s", errs[2].Error())
109+
}
89110
}

0 commit comments

Comments
 (0)