@@ -55,6 +55,30 @@ func TestLoader(t *testing.T) {
5555 }
5656 })
5757
58+ t .Run ("test Load Method cache error" , func (t * testing.T ) {
59+ t .Parallel ()
60+ errorCacheLoader , _ := ErrorCacheLoader [string ](0 )
61+ ctx := context .Background ()
62+ futures := []Thunk [string ]{}
63+ for i := 0 ; i < 2 ; i ++ {
64+ futures = append (futures , errorCacheLoader .Load (ctx , strconv .Itoa (i )))
65+ }
66+
67+ for _ , f := range futures {
68+ _ , err := f ()
69+ if err == nil {
70+ t .Error ("Error was not propagated" )
71+ }
72+ }
73+ nextFuture := errorCacheLoader .Load (ctx , "1" )
74+ _ , err := nextFuture ()
75+
76+ // Normal errors should be cached.
77+ if err == nil {
78+ t .Error ("Error from batch function was not cached" )
79+ }
80+ })
81+
5882 t .Run ("test Load Method Panic Safety in multiple keys" , func (t * testing.T ) {
5983 t .Parallel ()
6084 defer func () {
@@ -63,7 +87,7 @@ func TestLoader(t *testing.T) {
6387 t .Error ("Panic Loader's panic should have been handled'" )
6488 }
6589 }()
66- panicLoader , _ := PanicLoader [string ](0 )
90+ panicLoader , _ := PanicCacheLoader [string ](0 )
6791 futures := []Thunk [string ]{}
6892 ctx := context .Background ()
6993 for i := 0 ; i < 3 ; i ++ {
@@ -75,6 +99,18 @@ func TestLoader(t *testing.T) {
7599 t .Error ("Panic was not propagated as an error." )
76100 }
77101 }
102+
103+ futures = []Thunk [string ]{}
104+ for i := 0 ; i < 3 ; i ++ {
105+ futures = append (futures , panicLoader .Load (ctx , strconv .Itoa (1 )))
106+ }
107+
108+ for _ , f := range futures {
109+ _ , err := f ()
110+ if err != nil {
111+ t .Error ("Panic error from batch function was cached" )
112+ }
113+ }
78114 })
79115
80116 t .Run ("test LoadMany returns errors" , func (t * testing.T ) {
@@ -143,13 +179,21 @@ func TestLoader(t *testing.T) {
143179 t .Error ("Panic Loader's panic should have been handled'" )
144180 }
145181 }()
146- panicLoader , _ := PanicLoader [string ](0 )
182+ panicLoader , _ := PanicCacheLoader [string ](0 )
147183 ctx := context .Background ()
148- future := panicLoader .LoadMany (ctx , []string {"1" })
184+ future := panicLoader .LoadMany (ctx , []string {"1" , "2" })
149185 _ , errs := future ()
150- if len (errs ) < 1 || errs [0 ].Error () != "Panic received in batch function: Programming error" {
186+ if len (errs ) < 2 || errs [0 ].Error () != "Panic received in batch function: Programming error" {
151187 t .Error ("Panic was not propagated as an error." )
152188 }
189+
190+ future = panicLoader .LoadMany (ctx , []string {"1" })
191+ _ , errs = future ()
192+
193+ if len (errs ) > 0 {
194+ t .Error ("Panic error from batch function was cached" )
195+ }
196+
153197 })
154198
155199 t .Run ("test LoadMany method" , func (t * testing.T ) {
@@ -531,6 +575,53 @@ func PanicLoader[K comparable](max int) (*Loader[K, K], *[][]K) {
531575 }, WithBatchCapacity [K , K ](max ), withSilentLogger [K , K ]())
532576 return panicLoader , & loadCalls
533577}
578+
579+ func PanicCacheLoader [K comparable ](max int ) (* Loader [K , K ], * [][]K ) {
580+ var loadCalls [][]K
581+ panicCacheLoader := NewBatchedLoader (func (_ context.Context , keys []K ) []* Result [K ] {
582+ if len (keys ) > 1 {
583+ panic ("Programming error" )
584+ }
585+
586+ returnResult := make ([]* Result [K ], len (keys ))
587+ for idx := range returnResult {
588+ returnResult [idx ] = & Result [K ]{
589+ keys [0 ],
590+ nil ,
591+ }
592+ }
593+
594+ return returnResult
595+
596+ }, WithBatchCapacity [K , K ](max ), withSilentLogger [K , K ]())
597+ return panicCacheLoader , & loadCalls
598+ }
599+
600+ func ErrorCacheLoader [K comparable ](max int ) (* Loader [K , K ], * [][]K ) {
601+ var loadCalls [][]K
602+ errorCacheLoader := NewBatchedLoader (func (_ context.Context , keys []K ) []* Result [K ] {
603+ if len (keys ) > 1 {
604+ var results []* Result [K ]
605+ for _ , key := range keys {
606+ results = append (results , & Result [K ]{key , fmt .Errorf ("this is a test error" )})
607+ }
608+ return results
609+ }
610+
611+ returnResult := make ([]* Result [K ], len (keys ))
612+ for idx := range returnResult {
613+ returnResult [idx ] = & Result [K ]{
614+ keys [0 ],
615+ nil ,
616+ }
617+ }
618+
619+ return returnResult
620+
621+ }, WithBatchCapacity [K , K ](max ), withSilentLogger [K , K ]())
622+ return errorCacheLoader , & loadCalls
623+ }
624+
534625func BadLoader [K comparable ](max int ) (* Loader [K , K ], * [][]K ) {
535626 var mu sync.Mutex
536627 var loadCalls [][]K
0 commit comments