forked from viamrobotics/goutils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runtime_test.go
249 lines (228 loc) · 6.67 KB
/
runtime_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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package utils
import (
"context"
"os"
"testing"
"time"
"github.com/edaniels/golog"
"github.com/pkg/errors"
"go.uber.org/multierr"
"go.viam.com/test"
)
//nolint:dupl
func TestContextualMain(t *testing.T) {
var captured []interface{}
fatal = func(logger ILogger, args ...interface{}) {
captured = args
}
err1 := errors.New("whoops")
mainWithArgs := func(ctx context.Context, args []string, logger golog.Logger) error {
return err1
}
logger := golog.NewTestLogger(t)
ContextualMain(mainWithArgs, logger)
test.That(t, captured, test.ShouldResemble, []interface{}{err1})
captured = nil
mainWithArgs = func(ctx context.Context, args []string, logger golog.Logger) error {
return context.Canceled
}
ContextualMain(mainWithArgs, logger)
test.That(t, captured, test.ShouldBeNil)
mainWithArgs = func(ctx context.Context, args []string, logger golog.Logger) error {
return multierr.Combine(context.Canceled, err1)
}
ContextualMain(mainWithArgs, logger)
test.That(t, captured, test.ShouldResemble, []interface{}{err1})
}
//nolint:dupl
func TestContextualMainQuit(t *testing.T) {
var captured []interface{}
fatal = func(logger ILogger, args ...interface{}) {
captured = args
}
err1 := errors.New("whoops")
mainWithArgs := func(ctx context.Context, args []string, logger golog.Logger) error {
return err1
}
logger := golog.NewTestLogger(t)
ContextualMainQuit(mainWithArgs, logger)
test.That(t, captured, test.ShouldResemble, []interface{}{err1})
captured = nil
mainWithArgs = func(ctx context.Context, args []string, logger golog.Logger) error {
return context.Canceled
}
ContextualMainQuit(mainWithArgs, logger)
test.That(t, captured, test.ShouldBeNil)
mainWithArgs = func(ctx context.Context, args []string, logger golog.Logger) error {
return multierr.Combine(context.Canceled, err1)
}
ContextualMainQuit(mainWithArgs, logger)
test.That(t, captured, test.ShouldResemble, []interface{}{err1})
}
func TestContextWithQuitSignal(t *testing.T) {
ctx := context.Background()
sig := make(chan os.Signal, 1)
ctx = ContextWithQuitSignal(ctx, sig)
sig2 := ContextMainQuitSignal(context.Background())
test.That(t, sig2, test.ShouldBeNil)
sig2 = ContextMainQuitSignal(ctx)
test.That(t, sig2, test.ShouldEqual, (<-chan os.Signal)(sig))
}
func TestContextWithReadyFunc(t *testing.T) {
ctx := context.Background()
sig := make(chan struct{}, 1)
ctx = ContextWithReadyFunc(ctx, sig)
func1 := ContextMainReadyFunc(context.Background())
func1()
var ok bool
select {
case <-sig:
ok = true
default:
}
test.That(t, ok, test.ShouldBeFalse)
func1 = ContextMainReadyFunc(ctx)
func1()
select {
case <-sig:
ok = true
default:
}
test.That(t, ok, test.ShouldBeTrue)
func1()
func1()
select {
case <-sig:
ok = true
default:
}
test.That(t, ok, test.ShouldBeTrue)
}
func TestContextWithIterFunc(t *testing.T) {
ctx := context.Background()
sig := make(chan struct{}, 1)
ctx = ContextWithIterFunc(ctx, func() {
sig <- struct{}{}
})
func1 := ContextMainIterFunc(context.Background())
func1()
var ok bool
select {
case <-sig:
ok = true
default:
}
test.That(t, ok, test.ShouldBeFalse)
func1 = ContextMainIterFunc(ctx)
go func1()
<-sig
go func1()
go func1()
<-sig
}
func TestPanicCapturingGo(t *testing.T) {
running := make(chan struct{})
PanicCapturingGo(func() {
close(running)
panic("dead")
})
<-running
time.Sleep(time.Second)
}
func TestPanicCapturingGoWithCallback(t *testing.T) {
running := make(chan struct{})
errCh := make(chan interface{})
PanicCapturingGoWithCallback(func() {
close(running)
panic("dead")
}, func(err interface{}) {
errCh <- err
})
<-running
test.That(t, <-errCh, test.ShouldEqual, "dead")
}
func TestSelectContextOrWait(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()
ok := SelectContextOrWait(ctx, time.Hour)
test.That(t, ok, test.ShouldBeFalse)
ctx, cancel = context.WithCancel(context.Background())
go func() {
time.Sleep(time.Second)
cancel()
}()
ok = SelectContextOrWait(ctx, time.Hour)
test.That(t, ok, test.ShouldBeFalse)
ok = SelectContextOrWait(context.Background(), time.Second)
test.That(t, ok, test.ShouldBeTrue)
}
func TestSelectContextOrWaitChan(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()
timer := time.NewTimer(time.Second)
timer.Stop()
ok := SelectContextOrWaitChan(ctx, timer.C)
test.That(t, ok, test.ShouldBeFalse)
ctx, cancel = context.WithCancel(context.Background())
go func() {
time.Sleep(time.Second)
cancel()
}()
ok = SelectContextOrWaitChan(ctx, timer.C)
test.That(t, ok, test.ShouldBeFalse)
timer = time.NewTimer(time.Second)
defer timer.Stop()
ok = SelectContextOrWaitChan(context.Background(), timer.C)
test.That(t, ok, test.ShouldBeTrue)
}
func TestManagedGo(t *testing.T) {
dieCount := 3
done := make(chan struct{})
ManagedGo(func() {
time.Sleep(50 * time.Millisecond)
if dieCount == 0 {
return
}
dieCount--
panic(dieCount)
}, func() {
close(done)
})
<-done
}
func TestSlowGoroutineWatcher(t *testing.T) {
logger, observedLogs := golog.NewObservedTestLogger(t)
ch, cancel := SlowGoroutineWatcher(2*time.Second, "hello", logger)
cancel()
<-ch
test.That(t, observedLogs.All(), test.ShouldHaveLength, 0)
ch, cancel = SlowGoroutineWatcher(2*time.Second, "hello", logger)
<-ch
cancel()
test.That(t, len(observedLogs.All()), test.ShouldBeGreaterThan, 0)
test.That(t, observedLogs.All()[0].Message, test.ShouldContainSubstring, "hello")
test.That(t, observedLogs.All()[0].Message, test.ShouldContainSubstring, "[chan receive]")
test.That(t, observedLogs.All()[0].Message, test.ShouldContainSubstring, "go.viam.com/utils.TestSlowGoroutineWatcher(")
}
func TestSlowGoroutineWatcherAfterContext(t *testing.T) {
logger, observedLogs := golog.NewObservedTestLogger(t)
ch, cancel := SlowGoroutineWatcherAfterContext(context.Background(), 2*time.Second, "hello", logger)
cancel()
<-ch
test.That(t, observedLogs.All(), test.ShouldHaveLength, 0)
ctx, ctxCancel := context.WithCancel(context.Background())
ch, cancel = SlowGoroutineWatcherAfterContext(ctx, 2*time.Second, "hello", logger)
ctxCancel()
cancel()
<-ch
test.That(t, observedLogs.All(), test.ShouldHaveLength, 0)
ctx, ctxCancel = context.WithCancel(context.Background())
ch, cancel = SlowGoroutineWatcherAfterContext(ctx, 2*time.Second, "hello", logger)
ctxCancel()
<-ch
cancel()
test.That(t, len(observedLogs.All()), test.ShouldBeGreaterThan, 0)
test.That(t, observedLogs.All()[0].Message, test.ShouldContainSubstring, "hello")
test.That(t, observedLogs.All()[0].Message, test.ShouldContainSubstring, "[chan receive]")
test.That(t, observedLogs.All()[0].Message, test.ShouldContainSubstring, "go.viam.com/utils.TestSlowGoroutineWatcherAfterContext(")
}