-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtracker.go
84 lines (66 loc) · 1.73 KB
/
tracker.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
package testcases
import (
"sort"
"sync"
"time"
"github.com/makasim/flowstate"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type Tracker struct {
IncludeTaskID bool
IncludeState bool
mux sync.Mutex
visited []string
}
func Track(stateCtx *flowstate.StateCtx, trkr *Tracker) {
trkr.mux.Lock()
defer trkr.mux.Unlock()
var postfix string
if trkr.IncludeState {
switch {
case flowstate.Resumed(stateCtx.Current):
postfix += `:resumed`
case flowstate.Paused(stateCtx.Current):
postfix += `:paused`
}
}
if trkr.IncludeTaskID {
postfix += `:` + string(stateCtx.Current.ID)
}
trkr.visited = append(trkr.visited, string(stateCtx.Current.Transition.ToID)+postfix)
}
func (trkr *Tracker) Visited() []string {
trkr.mux.Lock()
defer trkr.mux.Unlock()
return append([]string(nil), trkr.visited...)
}
func (trkr *Tracker) VisitedSorted() []string {
visited := trkr.Visited()
// sort to eliminate race conditions
sort.SliceStable(visited, func(i, j int) bool {
if visited[i] > visited[j] {
return false
}
return true
})
return visited
}
func (trkr *Tracker) WaitSortedVisitedEqual(t TestingT, expVisited []string, wait time.Duration) []string {
var visited []string
assert.Eventually(t, func() bool {
visited = trkr.VisitedSorted()
return len(visited) >= len(expVisited)
}, wait, time.Millisecond*50)
require.Equal(t, expVisited, visited)
return visited
}
func (trkr *Tracker) WaitVisitedEqual(t TestingT, expVisited []string, wait time.Duration) []string {
var visited []string
assert.Eventually(t, func() bool {
visited = trkr.Visited()
return len(visited) >= len(expVisited)
}, wait, time.Millisecond*50)
require.Equal(t, expVisited, visited)
return visited
}