-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add immutable stateless default impl of step
- CreateSimpleStep function to create an immutable stateless step
- Loading branch information
1 parent
916a41b
commit c0e265d
Showing
3 changed files
with
75 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |