@@ -14,6 +14,7 @@ import (
1414 "testing"
1515 "time"
1616
17+ "github.com/go-faker/faker/v4"
1718 "github.com/stretchr/testify/assert"
1819 "github.com/stretchr/testify/require"
1920 "go.uber.org/atomic"
@@ -418,6 +419,141 @@ func runActionWithParallelCheckFailAtRandom(t *testing.T, ctx context.Context) {
418419 assert .GreaterOrEqual (t , counter .Load (), int32 (1 ))
419420}
420421
422+ func TestRunActionWithParallelCheckAndResult (t * testing.T ) {
423+ type parallelisationCheckResult struct {
424+ checks int32
425+ status string
426+ }
427+
428+ t .Run ("Happy" , func (t * testing.T ) {
429+ defer goleak .VerifyNone (t )
430+
431+ counter := atomic .NewInt32 (0 )
432+
433+ res , ok , err := RunActionWithParallelCheckAndResult (
434+ context .Background (),
435+ func (ctx context.Context ) (err error ) {
436+ time .Sleep (120 * time .Millisecond )
437+ return
438+ },
439+ func (ctx context.Context ) (res parallelisationCheckResult , ok bool ) {
440+ return parallelisationCheckResult {
441+ checks : counter .Inc (),
442+ status : "healthy" ,
443+ }, true
444+ },
445+ 10 * time .Millisecond ,
446+ )
447+
448+ require .NoError (t , err )
449+ require .True (t , ok )
450+
451+ assert .GreaterOrEqual (t , res .checks , int32 (10 ))
452+ assert .Equal (t , res .checks , counter .Load ())
453+ assert .Equal (t , "healthy" , res .status )
454+ })
455+
456+ t .Run ("Check Fails With Reason" , func (t * testing.T ) {
457+ defer goleak .VerifyNone (t )
458+
459+ counter := atomic .NewInt32 (0 )
460+ actionStarted := atomic .NewBool (false )
461+
462+ status := "adrien"
463+
464+ res , ok , err := RunActionWithParallelCheckAndResult (
465+ context .Background (),
466+ func (ctx context.Context ) error {
467+ actionStarted .Store (true )
468+ <- ctx .Done ()
469+ return DetermineContextError (ctx )
470+ },
471+ func (ctx context.Context ) (res parallelisationCheckResult , ok bool ) {
472+ if n := counter .Inc (); n >= 5 {
473+ return parallelisationCheckResult {
474+ checks : n ,
475+ status : status ,
476+ }, false
477+ } else {
478+ return parallelisationCheckResult {
479+ checks : n ,
480+ status : "ok" ,
481+ }, true
482+ }
483+ },
484+ 5 * time .Millisecond ,
485+ )
486+
487+ require .True (t , actionStarted .Load ())
488+ require .Error (t , err )
489+ errortest .AssertError (t , err , commonerrors .ErrCancelled )
490+
491+ require .False (t , ok )
492+ assert .Equal (t , status , res .status )
493+ assert .Equal (t , int32 (5 ), res .checks )
494+ assert .Equal (t , int32 (5 ), counter .Load ())
495+ })
496+ t .Run ("Action Error (no context cancel)" , func (t * testing.T ) {
497+ defer goleak .VerifyNone (t )
498+
499+ counter := atomic .NewInt32 (0 )
500+ status := "abdel"
501+
502+ res , ok , err := RunActionWithParallelCheckAndResult (
503+ context .Background (),
504+ func (ctx context.Context ) error {
505+ time .Sleep (30 * time .Millisecond )
506+ return commonerrors .New (commonerrors .ErrForbidden , faker .Sentence ())
507+ },
508+ func (ctx context.Context ) (parallelisationCheckResult , bool ) {
509+ return parallelisationCheckResult {
510+ checks : counter .Inc (),
511+ status : status ,
512+ }, true
513+ },
514+ 5 * time .Millisecond ,
515+ )
516+
517+ require .Error (t , err )
518+ errortest .AssertError (t , err , commonerrors .ErrForbidden )
519+ require .True (t , ok )
520+
521+ assert .Equal (t , status , res .status )
522+ assert .GreaterOrEqual (t , res .checks , int32 (1 ))
523+ assert .Equal (t , res .checks , counter .Load ())
524+ })
525+
526+ t .Run ("Context cancel" , func (t * testing.T ) {
527+ defer goleak .VerifyNone (t )
528+
529+ ctx , cancel := context .WithTimeout (context .Background (), 100 * time .Millisecond )
530+ defer cancel ()
531+
532+ counter := atomic .NewInt32 (0 )
533+ status := "kem"
534+
535+ res , ok , err := RunActionWithParallelCheckAndResult (
536+ ctx ,
537+ func (ctx context.Context ) error {
538+ <- ctx .Done ()
539+ return DetermineContextError (ctx )
540+ },
541+ func (ctx context.Context ) (parallelisationCheckResult , bool ) {
542+ return parallelisationCheckResult {
543+ checks : counter .Inc (),
544+ status : status ,
545+ }, true
546+ },
547+ 5 * time .Millisecond ,
548+ )
549+
550+ require .Error (t , err )
551+ errortest .AssertError (t , err , commonerrors .ErrTimeout )
552+ assert .True (t , ok )
553+ assert .GreaterOrEqual (t , res .checks , int32 (1 ))
554+ })
555+ }
556+
421557func TestWaitUntil (t * testing.T ) {
422558 defer goleak .VerifyNone (t )
423559 verifiedCondition := func (ctx context.Context ) (bool , error ) {
0 commit comments