-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmutex.go
168 lines (136 loc) · 3.97 KB
/
mutex.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package testcases
import (
"context"
"errors"
"fmt"
"time"
"github.com/makasim/flowstate"
"github.com/stretchr/testify/require"
"go.uber.org/goleak"
)
func Mutex(t TestingT, d flowstate.Doer, fr FlowRegistry) {
defer goleak.VerifyNone(t, goleak.IgnoreCurrent())
var raceDetector int
trkr := &Tracker{}
fr.SetFlow("mutex", flowstate.FlowFunc(func(stateCtx *flowstate.StateCtx, e flowstate.Engine) (flowstate.Command, error) {
return nil, fmt.Errorf("must not be called; mutex is always paused")
}))
fr.SetFlow("lock", flowstate.FlowFunc(func(stateCtx *flowstate.StateCtx, e flowstate.Engine) (flowstate.Command, error) {
Track(stateCtx, trkr)
wCmd := flowstate.Watch(map[string]string{"mutex": "theName"}).WithSinceLatest()
if err := e.Do(wCmd); err != nil {
return nil, err
}
w := wCmd.Listener
defer w.Close()
var mutexStateCtx *flowstate.StateCtx
for {
if mutexStateCtx != nil && mutexStateCtx.Current.Transition.ToID == "unlocked" {
copyStateCtx := &flowstate.StateCtx{}
stateCtx.CopyTo(copyStateCtx)
copyMutexStateCtx := &flowstate.StateCtx{}
mutexStateCtx.CopyTo(copyMutexStateCtx)
conflictErr := &flowstate.ErrCommitConflict{}
if err := e.Do(flowstate.Commit(
flowstate.Pause(copyMutexStateCtx).WithTransit(`locked`),
flowstate.Serialize(copyMutexStateCtx, copyStateCtx, `mutex_state`),
flowstate.Transit(copyStateCtx, `protected`),
)); errors.As(err, conflictErr) {
if conflictErr.Contains(mutexStateCtx.Current.ID) {
mutexStateCtx = nil
continue
}
return nil, err
} else if err != nil {
return nil, err
}
return flowstate.Execute(copyStateCtx), nil
}
select {
case mutexState := <-w.Listen():
if mutexStateCtx == nil {
mutexStateCtx = &flowstate.StateCtx{}
}
mutexStateCtx = mutexState.CopyToCtx(mutexStateCtx)
continue
case <-stateCtx.Done():
return flowstate.Noop(stateCtx), nil
}
}
}))
fr.SetFlow("protected", flowstate.FlowFunc(func(stateCtx *flowstate.StateCtx, e flowstate.Engine) (flowstate.Command, error) {
Track(stateCtx, trkr)
raceDetector += 1
return flowstate.Transit(stateCtx, `unlock`), nil
}))
fr.SetFlow("unlock", flowstate.FlowFunc(func(stateCtx *flowstate.StateCtx, e flowstate.Engine) (flowstate.Command, error) {
Track(stateCtx, trkr)
mutexStateCtx := &flowstate.StateCtx{}
if err := e.Do(flowstate.Commit(
flowstate.Deserialize(stateCtx, mutexStateCtx, `mutex_state`),
flowstate.Pause(mutexStateCtx).WithTransit(`unlocked`),
flowstate.End(stateCtx),
)); err != nil {
return nil, err
}
return flowstate.Noop(stateCtx), nil
}))
mutexStateCtx := &flowstate.StateCtx{
Current: flowstate.State{
ID: "aMutexTID",
Labels: map[string]string{
"mutex": "theName",
},
},
}
var states []*flowstate.StateCtx
for i := 0; i < 3; i++ {
statesCtx := &flowstate.StateCtx{
Current: flowstate.State{
ID: flowstate.StateID(fmt.Sprintf("aTID%d", i)),
},
}
states = append(states, statesCtx)
}
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))
}()
err = e.Do(flowstate.Commit(
flowstate.Pause(mutexStateCtx).WithTransit(`unlocked`),
))
require.NoError(t, err)
for _, stateCtx := range states {
err = e.Do(
flowstate.Commit(
flowstate.Transit(stateCtx, `lock`),
),
flowstate.Execute(stateCtx),
)
require.NoError(t, err)
}
time.Sleep(time.Millisecond * 500)
visited := trkr.Visited()
require.Len(t, visited, 9)
var lockCnt int
var protectedCnt int
var unlockCnt int
for i := range visited {
switch visited[i] {
case "lock":
lockCnt++
case "protected":
protectedCnt++
require.Equal(t, visited[i+1], "unlock")
case "unlock":
unlockCnt++
}
}
require.Equal(t, 3, lockCnt)
require.Equal(t, 3, protectedCnt)
require.Equal(t, 3, unlockCnt)
}