-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactor.go
107 lines (92 loc) · 2.43 KB
/
actor.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
package testcases
import (
"context"
"fmt"
"time"
"github.com/makasim/flowstate"
"github.com/stretchr/testify/require"
"go.uber.org/goleak"
)
func Actor(t TestingT, d flowstate.Doer, fr FlowRegistry) {
defer goleak.VerifyNone(t, goleak.IgnoreCurrent())
trkr := &Tracker{IncludeTaskID: true}
fr.SetFlow("actor", flowstate.FlowFunc(func(stateCtx *flowstate.StateCtx, e flowstate.Engine) (flowstate.Command, error) {
Track(stateCtx, trkr)
w := flowstate.NewWatcher(e, flowstate.GetManyByLabels(map[string]string{
"actor.foo": "inbox",
}))
defer w.Close()
t := time.NewTimer(time.Millisecond * 100)
for {
select {
case msgState := <-w.Next():
Track(msgState.CopyToCtx(&flowstate.StateCtx{}), trkr)
// do stuff here
case <-t.C:
// make sure recovery is not triggered, t.C must be less than failoverDur
if err := e.Do(flowstate.Commit(
flowstate.Transit(stateCtx, `actor`),
)); err != nil {
return nil, err
}
case <-stateCtx.Done():
return flowstate.Commit(
flowstate.Pause(stateCtx),
), nil
}
}
}))
fr.SetFlow("inbox", flowstate.FlowFunc(func(stateCtx *flowstate.StateCtx, e flowstate.Engine) (flowstate.Command, error) {
return nil, fmt.Errorf("must never be executed")
}))
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))
}()
actorStateCtx := &flowstate.StateCtx{
Current: flowstate.State{
ID: "actorTID",
},
}
require.NoError(t, e.Do(
flowstate.Commit(
flowstate.Transit(actorStateCtx, `actor`),
),
flowstate.Execute(actorStateCtx),
))
msg0StateCtx := &flowstate.StateCtx{
Current: flowstate.State{
ID: "msg0TID",
Labels: map[string]string{
"actor.foo": "inbox",
},
},
}
require.NoError(t, e.Do(
flowstate.Commit(
flowstate.Pause(msg0StateCtx).WithTransit(`inbox`),
),
))
msg1StateCtx := &flowstate.StateCtx{
Current: flowstate.State{
ID: "msg1TID",
Labels: map[string]string{
"actor.foo": "inbox",
},
},
}
require.NoError(t, e.Do(
flowstate.Commit(
flowstate.Pause(msg1StateCtx).WithTransit(`inbox`),
),
))
require.Equal(t, []string{
"actor:actorTID",
"inbox:msg0TID",
"inbox:msg1TID",
}, trkr.WaitVisitedEqual(t, []string{`actor:actorTID`, `inbox:msg0TID`, `inbox:msg1TID`}, time.Millisecond*600))
}