-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecovery_first_attempt_fail.go
88 lines (71 loc) · 2.01 KB
/
recovery_first_attempt_fail.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
package testcases
import (
"context"
"time"
"github.com/makasim/flowstate"
"github.com/stretchr/testify/require"
"go.uber.org/goleak"
)
func RecoveryFirstAttemptFail(t TestingT, d flowstate.Doer, fr FlowRegistry) {
defer goleak.VerifyNone(t, goleak.IgnoreCurrent())
trkr := &Tracker{IncludeState: true}
fr.SetFlow("first_attempt_fail", flowstate.FlowFunc(func(stateCtx *flowstate.StateCtx, e flowstate.Engine) (flowstate.Command, error) {
Track(stateCtx, trkr)
if flowstate.RecoveryAttempt(stateCtx.Current) < 1 {
// simulate a fail on first attempt
return flowstate.Noop(stateCtx), nil
}
return flowstate.Commit(
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))
}()
r := flowstate.Recoverer(time.Millisecond * 500)
require.NoError(t, r.Init(e))
defer func() {
sCtx, sCtxCancel := context.WithTimeout(context.Background(), time.Second*5)
defer sCtxCancel()
require.NoError(t, r.Shutdown(sCtx))
}()
stateCtx := &flowstate.StateCtx{
Current: flowstate.State{
ID: "aTID",
Rev: 0,
Labels: map[string]string{
`theRecovery`: `aTID`,
},
},
}
require.NoError(t, e.Do(flowstate.Commit(
flowstate.Transit(stateCtx, `first_attempt_fail`))),
)
require.NoError(t, e.Execute(stateCtx))
wCmd := flowstate.Watch(map[string]string{
`theRecovery`: `aTID`,
})
require.NoError(t, e.Do(wCmd))
w := wCmd.Listener
defer w.Close()
var visited []string
loop:
for {
select {
case latestState := <-w.Listen():
if flowstate.Ended(latestState) {
visited = append(visited, "ended")
break loop
}
visited = append(visited, string(latestState.Transition.ToID))
case <-time.NewTimer(time.Second * 2).C:
t.Fatalf("expected to receive a state")
}
}
require.Equal(t, []string{"first_attempt_fail", "first_attempt_fail", "ended"}, visited)
}