1- package parallel
1+ package pipeline
22
33import (
44 "context"
77 "testing"
88 "time"
99
10- pipeline "github.com/ccremer/go-command-pipeline"
1110 "github.com/stretchr/testify/assert"
1211 "go.uber.org/goleak"
1312)
@@ -16,7 +15,7 @@ func TestNewFanOutStep(t *testing.T) {
1615 var counts uint64
1716 tests := map [string ]struct {
1817 jobs int
19- givenResultHandler ResultHandler
18+ givenResultHandler ParallelResultHandler
2019 returnErr error
2120 expectedCounts int
2221 }{
@@ -31,9 +30,9 @@ func TestNewFanOutStep(t *testing.T) {
3130 "GivenPipelineWith_WhenRunningStep_ThenReturnSuccessButRunErrorHandler" : {
3231 jobs : 1 ,
3332 returnErr : fmt .Errorf ("should be called" ),
34- givenResultHandler : func (ctx context.Context , _ map [uint64 ]pipeline. Result ) pipeline. Result {
33+ givenResultHandler : func (ctx context.Context , _ map [uint64 ]Result ) error {
3534 atomic .AddUint64 (& counts , 1 )
36- return pipeline. Result {}
35+ return nil
3736 },
3837 expectedCounts : 2 ,
3938 },
@@ -44,15 +43,15 @@ func TestNewFanOutStep(t *testing.T) {
4443 goleak .VerifyNone (t )
4544 handler := tt .givenResultHandler
4645 if handler == nil {
47- handler = func (ctx context.Context , results map [uint64 ]pipeline. Result ) pipeline. Result {
46+ handler = func (ctx context.Context , results map [uint64 ]Result ) error {
4847 assert .NoError (t , results [0 ].Err ())
49- return pipeline. Result {}
48+ return nil
5049 }
5150 }
52- step := NewFanOutStep ("fanout" , func (_ context.Context , funcs chan * pipeline. Pipeline ) {
51+ step := NewFanOutStep ("fanout" , func (_ context.Context , funcs chan * Pipeline ) {
5352 defer close (funcs )
5453 for i := 0 ; i < tt .jobs ; i ++ {
55- funcs <- pipeline . NewPipeline ().WithSteps (pipeline . NewStepFromFunc ("step" , func (_ context.Context ) error {
54+ funcs <- NewPipeline ().WithSteps (NewStepFromFunc ("step" , func (_ context.Context ) error {
5655 atomic .AddUint64 (& counts , 1 )
5756 return tt .returnErr
5857 }))
@@ -68,36 +67,36 @@ func TestNewFanOutStep(t *testing.T) {
6867func TestNewFanOutStep_Cancel (t * testing.T ) {
6968 defer goleak .VerifyNone (t )
7069 var counts uint64
71- step := NewFanOutStep ("fanout" , func (ctx context.Context , pipelines chan * pipeline. Pipeline ) {
70+ step := NewFanOutStep ("fanout" , func (ctx context.Context , pipelines chan * Pipeline ) {
7271 defer close (pipelines )
7372 for i := 0 ; i < 10000 ; i ++ {
7473 select {
7574 case <- ctx .Done ():
7675 return
7776 default :
78- pipelines <- pipeline . NewPipeline ().WithSteps (pipeline . NewStepFromFunc ("increase" , func (_ context.Context ) error {
77+ pipelines <- NewPipeline ().WithSteps (NewStepFromFunc ("increase" , func (_ context.Context ) error {
7978 atomic .AddUint64 (& counts , 1 )
8079 return nil
8180 }))
8281 time .Sleep (10 * time .Millisecond )
8382 }
8483 }
8584 t .Fail () // should not reach this
86- }, func (ctx context.Context , results map [uint64 ]pipeline. Result ) pipeline. Result {
85+ }, func (ctx context.Context , results map [uint64 ]Result ) error {
8786 assert .Len (t , results , 3 )
88- return pipeline . NewResultWithError ( "fanout" , fmt .Errorf ("some error" ) )
87+ return fmt .Errorf ("some error" )
8988 })
9089 ctx , cancel := context .WithTimeout (context .Background (), 25 * time .Millisecond )
9190 defer cancel ()
92- result := pipeline . NewPipeline ().WithSteps (step ).RunWithContext (ctx )
91+ result := NewPipeline ().WithSteps (step ).RunWithContext (ctx )
9392 assert .Equal (t , 3 , int (counts ))
9493 assert .True (t , result .IsCanceled (), "canceled flag" )
9594 assert .EqualError (t , result .Err (), `step "fanout" failed: context deadline exceeded, collection error: some error` )
9695}
9796
9897func ExampleNewFanOutStep () {
99- p := pipeline . NewPipeline ()
100- fanout := NewFanOutStep ("fanout" , func (ctx context.Context , pipelines chan * pipeline. Pipeline ) {
98+ p := NewPipeline ()
99+ fanout := NewFanOutStep ("fanout" , func (ctx context.Context , pipelines chan * Pipeline ) {
101100 defer close (pipelines )
102101 // create some pipelines
103102 for i := 0 ; i < 3 ; i ++ {
@@ -106,20 +105,20 @@ func ExampleNewFanOutStep() {
106105 case <- ctx .Done ():
107106 return // parent pipeline has been canceled, let's not create more pipelines.
108107 default :
109- pipelines <- pipeline . NewPipeline ().AddStep (pipeline . NewStepFromFunc (fmt .Sprintf ("i = %d" , n ), func (_ context.Context ) error {
108+ pipelines <- NewPipeline ().AddStep (NewStepFromFunc (fmt .Sprintf ("i = %d" , n ), func (_ context.Context ) error {
110109 time .Sleep (time .Duration (n * 10000000 )) // fake some load
111110 fmt .Println (fmt .Sprintf ("I am worker %d" , n ))
112111 return nil
113112 }))
114113 }
115114 }
116- }, func (ctx context.Context , results map [uint64 ]pipeline. Result ) pipeline. Result {
115+ }, func (ctx context.Context , results map [uint64 ]Result ) error {
117116 for worker , result := range results {
118117 if result .IsFailed () {
119118 fmt .Println (fmt .Sprintf ("Worker %d failed: %v" , worker , result .Err ()))
120119 }
121120 }
122- return pipeline. Result {}
121+ return nil
123122 })
124123 p .AddStep (fanout )
125124 p .Run ()
0 commit comments