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"
|
2022-02-25 12:47:16 -06:00
|
|
|
"time"
|
2019-01-12 22:45:25 -06:00
|
|
|
|
|
|
|
"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
|
2022-02-25 12:47:16 -06:00
|
|
|
activeCount := 0
|
|
|
|
maxCount := 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++
|
2022-02-25 12:47:16 -06:00
|
|
|
|
|
|
|
activeCount++
|
|
|
|
if activeCount > maxCount {
|
|
|
|
maxCount = activeCount
|
|
|
|
}
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
activeCount--
|
|
|
|
|
2019-01-12 22:45:25 -06:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2022-02-25 12:47:16 -06:00
|
|
|
err := NewParallelExecutor(2, emptyWorkflow, emptyWorkflow, emptyWorkflow)(ctx)
|
2019-01-12 22:45:25 -06:00
|
|
|
|
2022-02-25 12:47:16 -06:00
|
|
|
assert.Equal(3, count, "should run all 3 executors")
|
|
|
|
assert.Equal(2, maxCount, "should run at most 2 executors in parallel")
|
2019-01-12 22:45:25 -06:00
|
|
|
assert.Nil(err)
|
2023-06-05 22:00:54 -05:00
|
|
|
|
|
|
|
// Reset to test running the executor with 0 parallelism
|
|
|
|
count = 0
|
|
|
|
activeCount = 0
|
|
|
|
maxCount = 0
|
|
|
|
|
|
|
|
errSingle := NewParallelExecutor(0, emptyWorkflow, emptyWorkflow, emptyWorkflow)(ctx)
|
|
|
|
|
|
|
|
assert.Equal(3, count, "should run all 3 executors")
|
|
|
|
assert.Equal(1, maxCount, "should run at most 1 executors in parallel")
|
|
|
|
assert.Nil(errSingle)
|
2019-01-12 22:45:25 -06:00
|
|
|
}
|
2021-04-06 08:43:02 -05:00
|
|
|
|
|
|
|
func TestNewParallelExecutorFailed(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
cancel()
|
|
|
|
|
|
|
|
count := 0
|
|
|
|
errorWorkflow := NewPipelineExecutor(func(ctx context.Context) error {
|
|
|
|
count++
|
|
|
|
return fmt.Errorf("fake error")
|
|
|
|
})
|
2022-02-25 12:47:16 -06:00
|
|
|
err := NewParallelExecutor(1, errorWorkflow)(ctx)
|
2021-04-06 08:43:02 -05:00
|
|
|
assert.Equal(1, count)
|
|
|
|
assert.ErrorIs(context.Canceled, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewParallelExecutorCanceled(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
cancel()
|
|
|
|
|
|
|
|
errExpected := fmt.Errorf("fake error")
|
|
|
|
|
|
|
|
count := 0
|
|
|
|
successWorkflow := NewPipelineExecutor(func(ctx context.Context) error {
|
|
|
|
count++
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
errorWorkflow := NewPipelineExecutor(func(ctx context.Context) error {
|
|
|
|
count++
|
|
|
|
return errExpected
|
|
|
|
})
|
2022-02-25 12:47:16 -06:00
|
|
|
err := NewParallelExecutor(3, errorWorkflow, successWorkflow, successWorkflow)(ctx)
|
2021-04-06 08:43:02 -05:00
|
|
|
assert.Equal(3, count)
|
|
|
|
assert.Error(errExpected, err)
|
|
|
|
}
|