-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfork_with_commit.go
74 lines (60 loc) · 1.86 KB
/
fork_with_commit.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
package testcases
import (
"context"
"time"
"github.com/makasim/flowstate"
"github.com/stretchr/testify/require"
"go.uber.org/goleak"
)
func Fork_WithCommit(t TestingT, d flowstate.Doer, fr FlowRegistry) {
defer goleak.VerifyNone(t, goleak.IgnoreCurrent())
var forkedStateCtx *flowstate.StateCtx
stateCtx := &flowstate.StateCtx{
Current: flowstate.State{
ID: "aTID",
},
}
trkr := &Tracker{}
fr.SetFlow("fork", flowstate.FlowFunc(func(stateCtx *flowstate.StateCtx, e flowstate.Engine) (flowstate.Command, error) {
Track(stateCtx, trkr)
forkedStateCtx = &flowstate.StateCtx{}
stateCtx.CopyTo(forkedStateCtx)
forkedStateCtx.Current.ID = "forkedTID"
forkedStateCtx.Current.Rev = 0
forkedStateCtx.Current.CopyTo(&forkedStateCtx.Committed)
if err := e.Do(
flowstate.Commit(
flowstate.Transit(stateCtx, `origin`),
flowstate.Transit(forkedStateCtx, `forked`),
),
flowstate.Execute(stateCtx),
flowstate.Execute(forkedStateCtx),
); err != nil {
return nil, err
}
return flowstate.Noop(stateCtx), nil
}))
fr.SetFlow("forked", flowstate.FlowFunc(func(stateCtx *flowstate.StateCtx, e flowstate.Engine) (flowstate.Command, error) {
Track(stateCtx, trkr)
return flowstate.End(stateCtx), nil
}))
fr.SetFlow("origin", 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))
}()
require.NoError(t, e.Do(flowstate.Transit(stateCtx, `fork`)))
require.NoError(t, e.Execute(stateCtx))
trkr.WaitSortedVisitedEqual(t, []string{
`fork`,
`forked`,
`origin`,
}, time.Second)
}