Skip to content

Commit

Permalink
Add immutable stateless default impl of step
Browse files Browse the repository at this point in the history
- CreateSimpleStep function to create an immutable stateless step
  • Loading branch information
saantiaguilera committed Feb 11, 2020
1 parent 916a41b commit c0e265d
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
8 changes: 7 additions & 1 deletion pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ A step is a single unit of work. It's an alias for Runnable
}
}
If your step is completely stateless, you can create an immutable instance through CreateSimpleStep
step := pipeline.CreateSimpleStep("step_name", func(ctx pipeline.Context) error {
// Do stuff.
})
Stage
A stage contains a collection of steps. The collection will be executed according to the stage implementation (eg. concurrently, sequentially, condition-based, etc).
Expand All @@ -56,7 +62,7 @@ To create one of the already defined stages, we can simply invoke its constructo
user.CreateUserStep(userService),
)
Since a stage is nothing more than an interface, you can create your own implementations abiding that contract.
Since a stage is nothing more than an interface, you can create your own custom implementations abiding that contract.
Stage group
Expand Down
25 changes: 25 additions & 0 deletions simple_step.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package pipeline

// Simple step structure. A simple step is a stateless unit of work (just a function to run).
type simpleStep struct {
name string
run func(ctx Context) error
}

func (s *simpleStep) Name() string {
return s.name
}

func (s *simpleStep) Run(ctx Context) error {
return s.run(ctx)
}

// CreateSimpleStep creates an immutable stateless unit of work based on a function that matches the Runnable contract.
// You can use this implementation when your use-cases will be completely stateless (they don't rely on a service
// or anything that can be injected at the start and stay immutable for the lifetime of the process)
func CreateSimpleStep(name string, run func(ctx Context) error) Step {
return &simpleStep{
name: name,
run: run,
}
}
43 changes: 43 additions & 0 deletions simple_step_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package pipeline_test

import (
"errors"
"testing"

"github.com/saantiaguilera/go-pipeline"
"github.com/stretchr/testify/assert"
)

func TestSimpleStep_GivenAName_WhenGettingItsName_ThenItsTheExpected(t *testing.T) {
expectedName := "test_name"
step := pipeline.CreateSimpleStep(expectedName, nil)

name := step.Name()

assert.Equal(t, expectedName, name)
}

func TestSimpleStep_GivenARunFunc_WhenRunning_ThenItsCalled(t *testing.T) {
called := false
run := func(ctx pipeline.Context) error {
called = true
return nil
}
step := pipeline.CreateSimpleStep("", run)

_ = step.Run(nil)

assert.True(t, called)
}

func TestSimpleStep_GivenARunFuncThatErrors_WhenRunning_ThenErrorIsReturned(t *testing.T) {
expectedErr := errors.New("some error")
run := func(ctx pipeline.Context) error {
return expectedErr
}
step := pipeline.CreateSimpleStep("", run)

err := step.Run(nil)

assert.Equal(t, expectedErr, err)
}

0 comments on commit c0e265d

Please sign in to comment.