-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcondition.go
75 lines (61 loc) · 1.9 KB
/
condition.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package testcases
import (
"context"
"time"
"github.com/makasim/flowstate"
"github.com/stretchr/testify/require"
"go.uber.org/goleak"
)
func Condition(t TestingT, d flowstate.Doer, fr FlowRegistry) {
defer goleak.VerifyNone(t, goleak.IgnoreCurrent())
trkr := &Tracker{}
fr.SetFlow("first", flowstate.FlowFunc(func(stateCtx *flowstate.StateCtx, e flowstate.Engine) (flowstate.Command, error) {
Track(stateCtx, trkr)
bID := flowstate.FlowID(`third`)
if stateCtx.Current.Annotations["condition"] == "true" {
bID = `second`
}
return flowstate.Transit(stateCtx, bID), nil
}))
fr.SetFlow("second", flowstate.FlowFunc(func(stateCtx *flowstate.StateCtx, e flowstate.Engine) (flowstate.Command, error) {
Track(stateCtx, trkr)
return flowstate.End(stateCtx), nil
}))
fr.SetFlow("third", flowstate.FlowFunc(func(stateCtx *flowstate.StateCtx, e flowstate.Engine) (flowstate.Command, error) {
Track(stateCtx, trkr)
return flowstate.End(stateCtx), nil
}))
l, _ := NewTestLogger(t)
e, err := flowstate.NewEngine(d, l)
require.NoError(t, err)
defer func() {
sCtx, sCtxCancel := context.WithTimeout(context.Background(), time.Second*5)
defer sCtxCancel()
require.NoError(t, e.Shutdown(sCtx))
}()
// condition true
stateCtx := &flowstate.StateCtx{
Current: flowstate.State{
ID: "aTrueTID",
Annotations: map[string]string{
"condition": "true",
},
},
}
require.NoError(t, e.Do(flowstate.Transit(stateCtx, `first`)))
require.NoError(t, e.Execute(stateCtx))
require.Equal(t, []string{`first`, `second`}, trkr.Visited())
// condition false
trkr.visited = nil
stateCtx1 := &flowstate.StateCtx{
Current: flowstate.State{
ID: "aFalseTID",
Annotations: map[string]string{
"condition": "false",
},
},
}
require.NoError(t, e.Do(flowstate.Transit(stateCtx1, `first`)))
require.NoError(t, e.Execute(stateCtx1))
require.Equal(t, []string{`first`, `third`}, trkr.Visited())
}