55 "fmt"
66 "os"
77 "testing"
8+ "testing/synctest"
89 "time"
910
1011 "github.com/jordanschalm/lockctx"
@@ -436,9 +437,14 @@ func (p *PipelineFunctionalSuite) TestMainCtxCancellationDuringWaitingPersist()
436437
437438// TestPipelineShutdownOnParentAbandon verifies that the pipeline transitions correctly to a shutdown state when the parent is abandoned.
438439func (p * PipelineFunctionalSuite ) TestPipelineShutdownOnParentAbandon () {
440+ assertNoError := func (err error ) {
441+ p .Require ().NoError (err )
442+ }
443+
439444 tests := []struct {
440445 name string
441446 config PipelineConfig
447+ checkError func (err error )
442448 customSetup func (pipeline Pipeline , updateChan chan State , errChan chan error )
443449 }{
444450 {
@@ -449,43 +455,68 @@ func (p *PipelineFunctionalSuite) TestPipelineShutdownOnParentAbandon() {
449455 },
450456 parentState : StateAbandoned ,
451457 },
458+ checkError : assertNoError ,
459+ customSetup : func (pipeline Pipeline , updateChan chan State , errChan chan error ) {},
452460 },
453461 {
454462 name : "from StateProcessing" ,
463+ config : PipelineConfig {
464+ beforePipelineRun : func (pipeline * PipelineImpl ) {
465+ p .execDataRequester .On ("RequestExecutionData" , mock .Anything ).Return (func (ctx context.Context ) (* execution_data.BlockExecutionData , error ) {
466+ pipeline .OnParentStateUpdated (StateAbandoned ) // abandon during processing step
467+ return p .expectedExecutionData , nil
468+ }).Once ()
469+ // this method may not be called depending on how quickly the RequestExecutionData
470+ // mock returns.
471+ p .txResultErrMsgsRequester .On ("Request" , mock .Anything ).Return (p .expectedTxResultErrMsgs , nil ).Maybe ()
472+ },
473+ parentState : StateWaitingPersist ,
474+ },
475+ checkError : func (err error ) {
476+ // depending on the timing, the error may be during or after the indexing step.
477+ if err != nil {
478+ p .Require ().ErrorContains (err , "could not perform indexing" )
479+ } else {
480+ p .Require ().NoError (err )
481+ }
482+ },
455483 customSetup : func (pipeline Pipeline , updateChan chan State , errChan chan error ) {
456- waitForStateUpdates (p .T (), updateChan , errChan , StateProcessing )
457-
458- pipeline .OnParentStateUpdated (StateAbandoned )
484+ synctestWaitForStateUpdates (p .T (), updateChan , StateProcessing )
459485 },
460- config : p .config ,
461486 },
462487 {
463488 name : "from StateWaitingPersist" ,
489+ config : PipelineConfig {
490+ beforePipelineRun : func (pipeline * PipelineImpl ) {
491+ p .execDataRequester .On ("RequestExecutionData" , mock .Anything ).Return (p .expectedExecutionData , nil ).Once ()
492+ p .txResultErrMsgsRequester .On ("Request" , mock .Anything ).Return (p .expectedTxResultErrMsgs , nil ).Once ()
493+ },
494+ parentState : StateWaitingPersist ,
495+ },
496+ checkError : assertNoError ,
464497 customSetup : func (pipeline Pipeline , updateChan chan State , errChan chan error ) {
465- waitForStateUpdates (p .T (), updateChan , errChan , StateProcessing , StateWaitingPersist )
466-
498+ synctestWaitForStateUpdates (p .T (), updateChan , StateProcessing , StateWaitingPersist )
467499 pipeline .OnParentStateUpdated (StateAbandoned )
468500 },
469- config : p .config ,
470501 },
471502 }
472503
473504 for _ , test := range tests {
474505 p .T ().Run (test .name , func (t * testing.T ) {
475- p .WithRunningPipeline (func (pipeline Pipeline , updateChan chan State , errChan chan error , cancel context.CancelFunc ) {
476- p .execDataRequester .On ("RequestExecutionData" , mock .Anything ).Return (p .expectedExecutionData , nil ).Maybe ()
477- p .txResultErrMsgsRequester .On ("Request" , mock .Anything ).Return (p .expectedTxResultErrMsgs , nil ).Maybe ()
506+ p .execDataRequester .On ("RequestExecutionData" , mock .Anything ).Unset ()
507+ p .txResultErrMsgsRequester .On ("Request" , mock .Anything ).Unset ()
478508
479- if test .customSetup != nil {
509+ synctest .Test (p .T (), func (t * testing.T ) {
510+ p .WithRunningPipeline (func (pipeline Pipeline , updateChan chan State , errChan chan error , cancel context.CancelFunc ) {
480511 test .customSetup (pipeline , updateChan , errChan )
481- }
482512
483- waitForStateUpdates (p .T (), updateChan , errChan , StateAbandoned )
484- waitForError ( p . T (), errChan , nil )
513+ synctestWaitForStateUpdates (p .T (), updateChan , StateAbandoned )
514+ test . checkError ( <- errChan )
485515
486- p .Assert ().Equal (StateAbandoned , pipeline .GetState ())
487- p .Assert ().Nil (p .core .workingData )
488- }, test .config )
516+ p .Assert ().Equal (StateAbandoned , pipeline .GetState ())
517+ p .Assert ().Nil (p .core .workingData )
518+ }, test .config )
519+ })
489520 })
490521 }
491522}
@@ -534,13 +565,18 @@ func (p *PipelineFunctionalSuite) WithRunningPipeline(
534565 pipelineIsReady := make (chan struct {})
535566
536567 go func () {
568+ defer close (errChan )
569+
537570 if pipelineConfig .beforePipelineRun != nil {
538571 pipelineConfig .beforePipelineRun (pipeline )
539572 }
540573
541574 close (pipelineIsReady )
542575
543- errChan <- pipeline .Run (ctx , p .core , pipelineConfig .parentState )
576+ err := pipeline .Run (ctx , p .core , pipelineConfig .parentState )
577+ if err != nil {
578+ errChan <- err
579+ }
544580 }()
545581
546582 <- pipelineIsReady
0 commit comments