-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_store_get_with_commit.go
81 lines (65 loc) · 1.97 KB
/
data_store_get_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
75
76
77
78
79
80
81
package testcases
import (
"context"
"time"
"github.com/makasim/flowstate"
"github.com/stretchr/testify/require"
"go.uber.org/goleak"
)
func DataStoreGetWithCommit(t TestingT, d flowstate.Doer, fr FlowRegistry) {
defer goleak.VerifyNone(t, goleak.IgnoreCurrent())
stateCtx := &flowstate.StateCtx{
Current: flowstate.State{
ID: "aTID",
},
}
trkr := &Tracker{}
expData := &flowstate.Data{
ID: `aDataID`,
Rev: 1,
B: []byte(`foo`),
}
actData := &flowstate.Data{}
fr.SetFlow("store", flowstate.FlowFunc(func(stateCtx *flowstate.StateCtx, e flowstate.Engine) (flowstate.Command, error) {
Track(stateCtx, trkr)
d := &flowstate.Data{
ID: `aDataID`,
B: []byte(`foo`),
}
if err := e.Do(flowstate.Commit(
flowstate.StoreData(d),
flowstate.ReferenceData(stateCtx, d, `aDataKey`),
flowstate.Transit(stateCtx, `get`),
)); err != nil {
return nil, err
}
return flowstate.Execute(stateCtx), nil
}))
fr.SetFlow("get", flowstate.FlowFunc(func(stateCtx *flowstate.StateCtx, e flowstate.Engine) (flowstate.Command, error) {
Track(stateCtx, trkr)
if err := e.Do(flowstate.Commit(
flowstate.DereferenceData(stateCtx, actData, `aDataKey`),
flowstate.GetData(actData),
flowstate.Transit(stateCtx, `end`),
)); err != nil {
return nil, err
}
return flowstate.Execute(stateCtx), nil
}))
fr.SetFlow("end", 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, `store`)))
require.NoError(t, e.Execute(stateCtx))
trkr.WaitVisitedEqual(t, []string{`store`, `get`, `end`}, time.Second*2)
require.Equal(t, expData, actData)
}