-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstate_test.go
158 lines (135 loc) · 3.25 KB
/
state_test.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
package routine
import (
"context"
"sync/atomic"
"testing"
"time"
protobuf_go_lite "github.com/aperturerobotics/protobuf-go-lite"
"github.com/sirupsen/logrus"
)
// TestStateRoutineContainer tests the routine container goroutine manager.
func TestStateRoutineContainer(t *testing.T) {
ctx := context.Background()
log := logrus.New()
log.SetLevel(logrus.DebugLevel)
le := logrus.NewEntry(log)
vals := make(chan int)
var exitWithErr atomic.Pointer[error]
var waitReturn chan struct{}
routineFn := func(ctx context.Context, st int) error {
if errPtr := exitWithErr.Load(); errPtr != nil {
return *errPtr
}
if waitReturn != nil {
select {
case <-ctx.Done():
return context.Canceled
case <-waitReturn:
}
}
select {
case <-ctx.Done():
return context.Canceled
case vals <- st:
return nil
}
}
k := NewStateRoutineContainerWithLogger[int](protobuf_go_lite.CompareComparable[int](), le)
if _, wasReset, running := k.SetStateRoutine(routineFn); wasReset || running {
// expected !wasReset and !running before context is set
t.FailNow()
}
// expect nothing to happen: context is unset.
<-time.After(time.Millisecond * 50)
select {
case val := <-vals:
t.Fatalf("unexpected value before set context: %v", val)
default:
}
// expect nothing to happen: state is unset
if k.SetContext(ctx, true) {
t.FailNow()
}
// expect to start now
if _, changed, reset, running := k.SetState(1); !changed || !running || reset {
t.FailNow()
}
checkVal := func(expected int) {
select {
case nval := <-vals:
if expected != 0 && nval != expected {
t.Fatalf("expected value %v but got %v", nval, expected)
}
default:
t.FailNow()
}
}
// expect value to be pushed to vals
<-time.After(time.Millisecond * 50)
checkVal(1)
// expect no extra value after
<-time.After(time.Millisecond * 50)
select {
case <-vals:
t.FailNow()
default:
}
// restart the routine
if !k.RestartRoutine() {
// expect it to be restarted
t.FailNow()
}
// expect value to be pushed to vals
<-time.After(time.Millisecond * 50)
checkVal(1)
// update state
if _, changed, _, running := k.SetState(2); !changed || !running {
t.FailNow()
}
// expect value to be pushed to vals
<-time.After(time.Millisecond * 50)
checkVal(2)
// expect nothing happened (no difference)
if _, changed, reset, running := k.SetState(2); changed || reset || running {
t.FailNow()
}
// unset context
if !k.SetContext(nil, false) {
// expect shutdown
t.FailNow()
}
// expect nothing happened (no difference)
if k.SetContext(nil, false) {
t.FailNow()
}
<-time.After(time.Millisecond * 50)
// test wait exited
var waitExitedReturned atomic.Pointer[error]
waitReturn = make(chan struct{})
startWaitExited := func() {
go func() {
err := k.WaitExited(ctx, false, nil)
waitExitedReturned.Store(&err)
}()
}
startWaitExited()
<-time.After(time.Millisecond * 50)
if waitExitedReturned.Load() != nil {
t.FailNow()
}
// set context
if !k.SetContext(ctx, true) {
t.FailNow()
}
<-time.After(time.Millisecond * 50)
if waitExitedReturned.Load() != nil {
t.FailNow()
}
close(waitReturn)
<-time.After(time.Millisecond * 50)
checkVal(2)
<-time.After(time.Millisecond * 50)
if waitExitedReturned.Load() == nil {
t.FailNow()
}
}