@@ -211,6 +211,32 @@ func TestLoader(t *testing.T) {
211
211
// TODO: expect to get some kind of warning
212
212
})
213
213
214
+ t .Run ("first result is a nil" , func (t * testing.T ) {
215
+ t .Parallel ()
216
+ faultyLoader , _ := LoaderNilInsteadOfResult ()
217
+ ctx := context .Background ()
218
+
219
+ n := 10
220
+ reqs := []Thunk {}
221
+ keys := Keys {}
222
+ for i := 0 ; i < n ; i ++ {
223
+ key := StringKey (strconv .Itoa (i ))
224
+ reqs = append (reqs , faultyLoader .Load (ctx , key ))
225
+ keys = append (keys , key )
226
+ }
227
+
228
+ for i , future := range reqs {
229
+ _ , err := future ()
230
+ if i == 0 && err == nil {
231
+ t .Error ("expected first result to contain an error" )
232
+ }
233
+
234
+ if i != 0 && err != nil {
235
+ t .Error ("expected rest of results not to contain an error" )
236
+ }
237
+ }
238
+ })
239
+
214
240
t .Run ("responds to max batch size" , func (t * testing.T ) {
215
241
t .Parallel ()
216
242
identityLoader , loadCalls := IDLoader (2 )
@@ -586,6 +612,30 @@ func FaultyLoader() (*Loader, *[][]string) {
586
612
return loader , & loadCalls
587
613
}
588
614
615
+ // LoaderNilInsteadOfResult gives a nil result back for the first key.
616
+ func LoaderNilInsteadOfResult () (* Loader , * [][]string ) {
617
+ var mu sync.Mutex
618
+ var loadCalls [][]string
619
+
620
+ loader := NewBatchedLoader (func (_ context.Context , keys Keys ) []* Result {
621
+ var results []* Result
622
+ mu .Lock ()
623
+ loadCalls = append (loadCalls , keys .Keys ())
624
+ mu .Unlock ()
625
+
626
+ for i , key := range keys {
627
+ if i == 0 {
628
+ results = append (results , nil )
629
+ } else {
630
+ results = append (results , & Result {key , nil })
631
+ }
632
+ }
633
+ return results
634
+ })
635
+
636
+ return loader , & loadCalls
637
+ }
638
+
589
639
///////////////////////////////////////////////////
590
640
// Benchmarks
591
641
///////////////////////////////////////////////////
0 commit comments