2019-01-12 22:45:25 -06:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2020-02-07 00:17:58 -06:00
|
|
|
"context"
|
2019-01-12 22:45:25 -06:00
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestNewWorkflow(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
2020-02-07 00:17:58 -06:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2019-01-12 22:45:25 -06:00
|
|
|
// empty
|
|
|
|
emptyWorkflow := NewPipelineExecutor()
|
2020-02-07 00:17:58 -06:00
|
|
|
assert.Nil(emptyWorkflow(ctx))
|
2019-01-12 22:45:25 -06:00
|
|
|
|
|
|
|
// error case
|
|
|
|
errorWorkflow := NewErrorExecutor(fmt.Errorf("test error"))
|
2020-02-07 00:17:58 -06:00
|
|
|
assert.NotNil(errorWorkflow(ctx))
|
2019-01-12 22:45:25 -06:00
|
|
|
|
|
|
|
// multiple success case
|
|
|
|
runcount := 0
|
|
|
|
successWorkflow := NewPipelineExecutor(
|
2020-02-07 00:17:58 -06:00
|
|
|
func(ctx context.Context) error {
|
2019-01-16 01:12:10 -06:00
|
|
|
runcount++
|
2019-01-12 22:45:25 -06:00
|
|
|
return nil
|
|
|
|
},
|
2020-02-07 00:17:58 -06:00
|
|
|
func(ctx context.Context) error {
|
2019-01-16 01:12:10 -06:00
|
|
|
runcount++
|
2019-01-12 22:45:25 -06:00
|
|
|
return nil
|
|
|
|
})
|
2020-02-07 00:17:58 -06:00
|
|
|
assert.Nil(successWorkflow(ctx))
|
2019-01-12 22:45:25 -06:00
|
|
|
assert.Equal(2, runcount)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewConditionalExecutor(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
2020-02-07 00:17:58 -06:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2019-01-12 22:45:25 -06:00
|
|
|
trueCount := 0
|
|
|
|
falseCount := 0
|
|
|
|
|
2020-02-07 00:17:58 -06:00
|
|
|
err := NewConditionalExecutor(func(ctx context.Context) bool {
|
2019-01-12 22:45:25 -06:00
|
|
|
return false
|
2020-02-07 00:17:58 -06:00
|
|
|
}, func(ctx context.Context) error {
|
2019-01-12 22:45:25 -06:00
|
|
|
trueCount++
|
|
|
|
return nil
|
2020-02-07 00:17:58 -06:00
|
|
|
}, func(ctx context.Context) error {
|
2019-01-12 22:45:25 -06:00
|
|
|
falseCount++
|
|
|
|
return nil
|
2020-02-07 00:17:58 -06:00
|
|
|
})(ctx)
|
2019-01-12 22:45:25 -06:00
|
|
|
|
|
|
|
assert.Nil(err)
|
|
|
|
assert.Equal(0, trueCount)
|
|
|
|
assert.Equal(1, falseCount)
|
|
|
|
|
2020-02-07 00:17:58 -06:00
|
|
|
err = NewConditionalExecutor(func(ctx context.Context) bool {
|
2019-01-12 22:45:25 -06:00
|
|
|
return true
|
2020-02-07 00:17:58 -06:00
|
|
|
}, func(ctx context.Context) error {
|
2019-01-12 22:45:25 -06:00
|
|
|
trueCount++
|
|
|
|
return nil
|
2020-02-07 00:17:58 -06:00
|
|
|
}, func(ctx context.Context) error {
|
2019-01-12 22:45:25 -06:00
|
|
|
falseCount++
|
|
|
|
return nil
|
2020-02-07 00:17:58 -06:00
|
|
|
})(ctx)
|
2019-01-12 22:45:25 -06:00
|
|
|
|
|
|
|
assert.Nil(err)
|
|
|
|
assert.Equal(1, trueCount)
|
|
|
|
assert.Equal(1, falseCount)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewParallelExecutor(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
2020-02-07 00:17:58 -06:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2019-01-12 22:45:25 -06:00
|
|
|
count := 0
|
2020-02-07 00:17:58 -06:00
|
|
|
emptyWorkflow := NewPipelineExecutor(func(ctx context.Context) error {
|
2019-01-12 22:45:25 -06:00
|
|
|
count++
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2020-02-07 00:17:58 -06:00
|
|
|
err := NewParallelExecutor(emptyWorkflow, emptyWorkflow)(ctx)
|
2019-01-12 22:45:25 -06:00
|
|
|
assert.Equal(2, count)
|
|
|
|
|
|
|
|
assert.Nil(err)
|
|
|
|
}
|