Fix sequence iterator looping condition and improper state
This commit is contained in:
@@ -3,6 +3,7 @@ package automation
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
|
||||
. "github.com/cortezaproject/corteza-server/pkg/expr"
|
||||
)
|
||||
|
||||
@@ -18,7 +19,7 @@ func (i *sequenceIterator) More(context.Context, *Vars) (bool, error) {
|
||||
}
|
||||
|
||||
func (i *sequenceIterator) more() bool {
|
||||
return i.counter*(i.cStep/i.cStep) < i.cLast*(i.cStep/i.cStep)
|
||||
return i.counter < i.cLast
|
||||
}
|
||||
|
||||
func (i *sequenceIterator) Start(context.Context, *Vars) error { return nil }
|
||||
@@ -27,8 +28,8 @@ func (i *sequenceIterator) Next(context.Context, *Vars) (*Vars, error) {
|
||||
out := &Vars{}
|
||||
out.Set("counter", i.counter)
|
||||
out.Set("isFirst", i.counter == i.cFirst)
|
||||
out.Set("isLast", !i.more())
|
||||
i.counter = i.counter + i.cStep
|
||||
out.Set("isLast", !i.more())
|
||||
return out, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"bufio"
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/cortezaproject/corteza-server/pkg/expr"
|
||||
"github.com/cortezaproject/corteza-server/pkg/wfexec"
|
||||
)
|
||||
@@ -43,7 +44,7 @@ func (h loopHandler) sequence(_ context.Context, args *loopSequenceArgs) (wfexec
|
||||
}
|
||||
|
||||
i := &sequenceIterator{
|
||||
counter: 0,
|
||||
counter: args.First,
|
||||
cFirst: args.First,
|
||||
cLast: args.Last,
|
||||
cStep: args.Step,
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
package workflows
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
autTypes "github.com/cortezaproject/corteza-server/automation/types"
|
||||
"github.com/cortezaproject/corteza-server/pkg/wfexec"
|
||||
"github.com/spf13/cast"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func Test_iterator_sequence_complex(t *testing.T) {
|
||||
wfexec.MaxIteratorBufferSize = wfexec.DefaultMaxIteratorBufferSize
|
||||
defer func() {
|
||||
wfexec.MaxIteratorBufferSize = wfexec.DefaultMaxIteratorBufferSize
|
||||
}()
|
||||
|
||||
var (
|
||||
ctx = bypassRBAC(context.Background())
|
||||
req = require.New(t)
|
||||
)
|
||||
|
||||
req.NoError(defStore.TruncateComposeRecords(ctx, nil))
|
||||
req.NoError(defStore.TruncateComposeModules(ctx))
|
||||
req.NoError(defStore.TruncateComposeNamespaces(ctx))
|
||||
|
||||
loadNewScenario(ctx, t)
|
||||
|
||||
var (
|
||||
_, trace = mustExecWorkflow(ctx, t, "testing", autTypes.WorkflowExecParams{})
|
||||
)
|
||||
|
||||
req.Len(trace, 7)
|
||||
|
||||
frame := trace[1]
|
||||
req.True(frame.Scope.GetValue()["first"].Get().(bool))
|
||||
req.False(frame.Scope.GetValue()["last"].Get().(bool))
|
||||
aux := frame.Scope.GetValue()["i"].Get()
|
||||
i, err := cast.ToIntE(aux)
|
||||
req.NoError(err)
|
||||
req.Equal(2, i)
|
||||
|
||||
frame = trace[len(trace)-1]
|
||||
req.False(frame.Scope.GetValue()["first"].Get().(bool))
|
||||
req.True(frame.Scope.GetValue()["last"].Get().(bool))
|
||||
aux = frame.Scope.GetValue()["i"].Get()
|
||||
i, err = cast.ToIntE(aux)
|
||||
req.NoError(err)
|
||||
req.Equal(4, i)
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package workflows
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
autTypes "github.com/cortezaproject/corteza-server/automation/types"
|
||||
"github.com/cortezaproject/corteza-server/pkg/wfexec"
|
||||
"github.com/spf13/cast"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func Test_iterator_sequence(t *testing.T) {
|
||||
wfexec.MaxIteratorBufferSize = wfexec.DefaultMaxIteratorBufferSize
|
||||
defer func() {
|
||||
wfexec.MaxIteratorBufferSize = wfexec.DefaultMaxIteratorBufferSize
|
||||
}()
|
||||
|
||||
var (
|
||||
ctx = bypassRBAC(context.Background())
|
||||
req = require.New(t)
|
||||
)
|
||||
|
||||
req.NoError(defStore.TruncateComposeRecords(ctx, nil))
|
||||
req.NoError(defStore.TruncateComposeModules(ctx))
|
||||
req.NoError(defStore.TruncateComposeNamespaces(ctx))
|
||||
|
||||
loadNewScenario(ctx, t)
|
||||
|
||||
var (
|
||||
_, trace = mustExecWorkflow(ctx, t, "testing", autTypes.WorkflowExecParams{})
|
||||
)
|
||||
|
||||
req.Len(trace, 9)
|
||||
|
||||
frame := trace[1]
|
||||
req.True(frame.Scope.GetValue()["first"].Get().(bool))
|
||||
req.False(frame.Scope.GetValue()["last"].Get().(bool))
|
||||
aux := frame.Scope.GetValue()["i"].Get()
|
||||
i, err := cast.ToIntE(aux)
|
||||
req.NoError(err)
|
||||
req.Equal(0, i)
|
||||
|
||||
frame = trace[len(trace)-1]
|
||||
req.False(frame.Scope.GetValue()["first"].Get().(bool))
|
||||
req.True(frame.Scope.GetValue()["last"].Get().(bool))
|
||||
aux = frame.Scope.GetValue()["i"].Get()
|
||||
i, err = cast.ToIntE(aux)
|
||||
req.NoError(err)
|
||||
req.Equal(2, i)
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
workflows:
|
||||
testing:
|
||||
enabled: true
|
||||
trace: true
|
||||
triggers:
|
||||
- enabled: true
|
||||
stepID: 10
|
||||
|
||||
steps:
|
||||
- stepID: 10
|
||||
kind: iterator
|
||||
ref: loopSequence
|
||||
arguments:
|
||||
- { target: "last", expr: "3", type: "Integer"}
|
||||
- { target: "step", expr: "1", type: "Integer"}
|
||||
results:
|
||||
- { target: "i", expr: "counter" }
|
||||
- { target: "first", expr: "isFirst" }
|
||||
- { target: "last", expr: "isLast" }
|
||||
|
||||
- stepID: 11
|
||||
kind: continue
|
||||
|
||||
- stepID: 12
|
||||
kind: termination
|
||||
|
||||
paths:
|
||||
- { parentID: 10, childID: 11 }
|
||||
- { parentID: 10, childID: 12 }
|
||||
@@ -0,0 +1,30 @@
|
||||
workflows:
|
||||
testing:
|
||||
enabled: true
|
||||
trace: true
|
||||
triggers:
|
||||
- enabled: true
|
||||
stepID: 10
|
||||
|
||||
steps:
|
||||
- stepID: 10
|
||||
kind: iterator
|
||||
ref: loopSequence
|
||||
arguments:
|
||||
- { target: "first", expr: "2", type: "Integer"}
|
||||
- { target: "last", expr: "5", type: "Integer"}
|
||||
- { target: "step", expr: "2", type: "Integer"}
|
||||
results:
|
||||
- { target: "i", expr: "counter" }
|
||||
- { target: "first", expr: "isFirst" }
|
||||
- { target: "last", expr: "isLast" }
|
||||
|
||||
- stepID: 11
|
||||
kind: continue
|
||||
|
||||
- stepID: 12
|
||||
kind: termination
|
||||
|
||||
paths:
|
||||
- { parentID: 10, childID: 11 }
|
||||
- { parentID: 10, childID: 12 }
|
||||
Reference in New Issue
Block a user