@@ -3,6 +3,7 @@ package wpool
33import (
44 "bytes"
55 "context"
6+ "errors"
67 "fmt"
78 "iter"
89 "log/slog"
@@ -17,15 +18,44 @@ import (
1718 "go.uber.org/goleak"
1819)
1920
20- func ifExistsIs (targetErr ... error ) tst.ErrorAssertionFunc {
21+ func optionalErrorIs (targetErr ... error ) tst.ErrorAssertionFunc {
2122 return func (t tst.TestingT , err error ) bool {
23+ if h , ok := t .(interface { Helper () }); ok {
24+ h .Helper ()
25+ }
26+
2227 if err == nil {
2328 return true
2429 }
2530 return tst .ErrorIs (targetErr ... )(t , err )
2631 }
2732}
2833
34+ func optionalErrorOneOf (targetErr ... error ) tst.ErrorAssertionFunc {
35+ return func (t tst.TestingT , err error ) bool {
36+ if h , ok := t .(interface { Helper () }); ok {
37+ h .Helper ()
38+ }
39+
40+ if err == nil {
41+ return true
42+ }
43+
44+ is := false
45+ for _ , e := range targetErr {
46+ if errors .Is (err , e ) {
47+ is = true
48+ }
49+ }
50+
51+ if ! is {
52+ tst .ErrorIs (targetErr ... )(t , err )
53+ }
54+
55+ return is
56+ }
57+ }
58+
2959func TestMain (m * testing.M ) {
3060 goleak .VerifyTestMain (m )
3161}
@@ -388,7 +418,7 @@ func (tc WorkerPoolTestCase) Test(t *testing.T) {
388418 defer sendersWG .Done ()
389419 <- startSignal
390420 for i := range intIter (tc .sendsPerSender ) {
391- err := subject .Submit (ctx , i )
421+ err := subject .Submit (context . Background () , i ) //nolint:contextcheck // we want to emulate that submit uses a different context than the workers
392422 if tc .assertErrorOnSubmit != nil {
393423 tc .assertErrorOnSubmit (t , err )
394424 }
@@ -491,7 +521,7 @@ func TestFlow(t *testing.T) {
491521 subject .Stop (ctx )
492522 },
493523 },
494- assertErrorOnSubmit : ifExistsIs (ErrWorkerPoolStopped ),
524+ assertErrorOnSubmit : optionalErrorIs (ErrWorkerPoolStopped ),
495525 asserts : func (t * testing.T , itemsSent , itemsProcessed uint64 ) {
496526 assert .Equal (t , itemsSent , itemsProcessed )
497527 },
@@ -510,7 +540,7 @@ func TestFlow(t *testing.T) {
510540 subject .Stop (ctx )
511541 },
512542 },
513- assertErrorOnSubmit : ifExistsIs (ErrWorkerPoolStopped ),
543+ assertErrorOnSubmit : optionalErrorIs (ErrWorkerPoolStopped ),
514544 asserts : func (t * testing.T , itemsSent , itemsProcessed uint64 ) {
515545 assert .Less (t , itemsProcessed , itemsSent )
516546 },
@@ -529,7 +559,7 @@ func TestFlow(t *testing.T) {
529559 ctxCancelFn ()
530560 },
531561 },
532- assertErrorOnSubmit : ifExistsIs ( context .Canceled ),
562+ assertErrorOnSubmit : optionalErrorOneOf ( ErrWorkerPoolStopped , context .Canceled ),
533563 asserts : func (t * testing.T , itemsSent , itemsProcessed uint64 ) {
534564 assert .Less (t , itemsProcessed , itemsSent )
535565 },
0 commit comments