Skip to content

Commit

Permalink
Merge pull request #40 from ccremer/util
Browse files Browse the repository at this point in the history
Add AddStepFromFunc convenience method
  • Loading branch information
ccremer committed Apr 12, 2022
2 parents becd0df + 2c05cbf commit 1ff2a54
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
5 changes: 5 additions & 0 deletions pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ func (p *Pipeline) AddStep(step Step) *Pipeline {
return p
}

// AddStepFromFunc appends the given function to the Pipeline at the end and returns itself.
func (p *Pipeline) AddStepFromFunc(name string, fn func(ctx context.Context) error) *Pipeline {
return p.AddStep(NewStepFromFunc(name, fn))
}

// WithSteps appends the given array of steps to the Pipeline at the end and returns itself.
func (p *Pipeline) WithSteps(steps ...Step) *Pipeline {
p.steps = steps
Expand Down
21 changes: 10 additions & 11 deletions pipeline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,18 +171,17 @@ func TestPipeline_Run(t *testing.T) {
}

func TestPipeline_RunWithContext_CancelLongRunningStep(t *testing.T) {
p := NewPipeline().WithSteps(
NewStepFromFunc("long running", func(ctx context.Context) error {
for {
select {
case <-ctx.Done():
return ctx.Err()
default:
// doing nothing
}
p := NewPipeline().AddStepFromFunc("long running", func(ctx context.Context) error {
for {
select {
case <-ctx.Done():
return ctx.Err()
default:
// doing nothing
}
}),
)
}
})

ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
go func() {
time.Sleep(5 * time.Millisecond)
Expand Down

0 comments on commit 1ff2a54

Please sign in to comment.