-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransactional_route_test.go
129 lines (102 loc) · 4.22 KB
/
transactional_route_test.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package orchestrator
import (
"github.com/stretchr/testify/assert"
"testing"
)
func doActionTest(ctx *context) error {
if ctx.GetVariable("HK") == nil {
ctx.SetVariable("HK", 0)
}
ctx.SetVariable("HK", ctx.GetVariable("HK").(int)+1)
return nil
}
func undoActionTest(ctx context) error {
return nil
}
func execTestRoute(route *State) *routeRunner {
runner := newRouteRunner(route, nil)
ctx, _ := NewContext()
runner.run(ctx, nil)
return runner
}
func TestDefineUnconditionalTransactionalRoute(t *testing.T) {
r := NewTransactionalRoute("TEST_ROUTE").
AddNextStep("1", doActionTest, undoActionTest).
AddNextStep("2", doActionTest, undoActionTest).
AddNextStep("3", doActionTest, undoActionTest)
rr := execTestRoute(r.GetStartState())
assert.Equal(t, 3, rr.statemachine.context.GetVariable("HK"))
}
func TestDefineConditionalTransactionalRoute(t *testing.T) {
r := NewTransactionalRoute("TEST_ROUTE").
AddNextStep("1", doActionTest, undoActionTest).
AddNextStep("2", doActionTest, undoActionTest).
When(func(ctx context) bool { return true }).
AddNextStep("when_1", doActionTest, undoActionTest).
AddNextStep("when_2", doActionTest, undoActionTest).
AddNextStep("when_3", doActionTest, undoActionTest)
rh := execTestRoute(r.GetStartState())
assert.Equal(t, 5, rh.statemachine.context.GetVariable("HK"))
rf := NewTransactionalRoute("TEST_ROUTE").
AddNextStep("1", doActionTest, undoActionTest).
AddNextStep("2", doActionTest, undoActionTest).
When(func(ctx context) bool { return false }).
AddNextStep("when_1", doActionTest, undoActionTest).
AddNextStep("when_2", doActionTest, undoActionTest).
AddNextStep("when_3", doActionTest, undoActionTest)
rh = execTestRoute(rf.GetStartState())
assert.Equal(t, 2, rh.statemachine.context.GetVariable("HK"))
}
func TestDefineNestedConditionalTransactionalRoute(t *testing.T) {
r := NewTransactionalRoute("TEST_ROUTE").
AddNextStep("1", doActionTest, undoActionTest).
AddNextStep("2", doActionTest, undoActionTest).
When(func(ctx context) bool { return true }).
AddNextStep("when_1", doActionTest, undoActionTest).
When(func(ctx context) bool { return true }).
AddNextStep("when_when_1", doActionTest, undoActionTest).
AddNextStep("when_when_2", doActionTest, undoActionTest).
AddNextStep("when_when_3", doActionTest, undoActionTest)
rh := execTestRoute(r.GetStartState())
assert.Equal(t, 6, rh.statemachine.context.GetVariable("HK"))
}
func TestDefineConditionWithOtherwiseTransactionalRoute(t *testing.T) {
r := NewTransactionalRoute("TEST_ROUTE").
AddNextStep("1", doActionTest, undoActionTest).
AddNextStep("2", doActionTest, undoActionTest).
When(func(ctx context) bool { return true }).
AddNextStep("when_1", doActionTest, undoActionTest).
AddNextStep("when_2", doActionTest, undoActionTest).
Otherwise().
AddNextStep("otherwise_1", doActionTest, undoActionTest).
AddNextStep("otherwise_2", doActionTest, undoActionTest).
AddNextStep("otherwise_3", doActionTest, undoActionTest)
rh := execTestRoute(r.GetStartState())
assert.Equal(t, 4, rh.statemachine.context.GetVariable("HK"))
}
func TestDefineConditionWithOtherwiseAndEndTransactionalRoute(t *testing.T) {
r := NewTransactionalRoute("TEST_ROUTE").
AddNextStep("1", doActionTest, undoActionTest).
AddNextStep("2", doActionTest, undoActionTest).
When(func(ctx context) bool { return false }).
AddNextStep("condition_1", doActionTest, undoActionTest).
Otherwise().
AddNextStep("otherwise_1", doActionTest, undoActionTest).
AddNextStep("otherwise_2", doActionTest, undoActionTest).
AddNextStep("otherwise_3", doActionTest, undoActionTest)
rh := execTestRoute(r.GetStartState())
assert.Equal(t, 5, rh.statemachine.context.GetVariable("HK"))
}
func TestDefineTransactionalRoute(t *testing.T) {
r := NewTransactionalRoute("TEST_ROUTE").
AddNextStep("1", doActionTest, undoActionTest).
When(func(ctx context) bool { return true }).
AddNextStep("condition_1", doActionTest, undoActionTest).
AddNextStep("condition_2", doActionTest, undoActionTest).
Otherwise().
AddNextStep("otherwise_1", doActionTest, undoActionTest).
End().
AddNextStep("2", doActionTest, undoActionTest)
rh := execTestRoute(r.GetStartState())
assert.Equal(t, 4, rh.statemachine.context.GetVariable("HK"))
}