-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgovnr_test.go
113 lines (92 loc) · 2.84 KB
/
govnr_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
// Copyright 2019 the orbs-network-go authors
// This file is part of the orbs-network-go library in the Orbs project.
//
// This source code is licensed under the MIT license found in the LICENSE file in the root directory of this source tree.
// The above notice should be included in all copies or substantial portions of the software.
package govnr
import (
"context"
"github.com/stretchr/testify/require"
"testing"
"time"
)
func localFunctionThatPanics() {
panic("foo")
}
func TestOnce_ReportsOnPanic(t *testing.T) {
logger := mockLogger()
require.NotPanicsf(t, func() {
Once(logger, localFunctionThatPanics)
}, "GoOnce panicked unexpectedly")
report := <-logger.errors
require.Error(t, report.err)
}
func TestForever_ReportsOnPanicAndRestarts(t *testing.T) {
numOfIterations := 10
logger := mockLogger()
ctx, cancel := context.WithCancel(context.Background())
count := 0
require.NotPanicsf(t, func() {
Forever(ctx, "some service", logger, func() {
if count > numOfIterations {
cancel()
} else {
count++
}
panic("foo")
})
}, "GoForever panicked unexpectedly")
for i := 0; i < numOfIterations; i++ {
select {
case report := <-logger.errors:
require.Error(t, report.err)
case <-time.After(1 * time.Second):
require.Fail(t, "long living goroutine didn't restart")
}
}
}
func TestForever_TerminatesWhenContextIsClosed(t *testing.T) {
logger := bufferedLogger()
ctx, cancel := context.WithCancel(context.Background())
bgStarted := make(chan struct{})
bgEnded := make(chan struct{})
handle := Forever(ctx, "another service", logger, func() {
bgStarted <- struct{}{}
select {
case <-ctx.Done():
bgEnded <- struct{}{}
return
}
})
handle.MarkSupervised()
<-bgStarted
cancel()
select {
case <-bgEnded:
// ok, invocation of cancel() caused goroutine to stop, we can now check if it restarts
case <-time.After(1 * time.Second):
require.Fail(t, "long living goroutine didn't stop")
}
shutdownCtx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
handle.WaitUntilShutdown(shutdownCtx)
require.Empty(t, logger.errors, "error was reported on shutdown")
}
func TestForeverHandle_ErrorsWhenTerminatedWithoutSupervision(t *testing.T) {
logger := bufferedLogger()
h := &ForeverHandle{closed: make(chan struct{}), errorHandler: logger, name: "foo"}
h.terminated()
select {
case report := <-logger.errors:
require.EqualError(t, report.err, "Forever governed goroutine foo terminated without being supervised")
default:
t.Errorf("handle didn't error on termination")
}
}
func TestForeverHandle_DoesNotErrorWhenTerminatedAfterSupervision(t *testing.T) {
logger := bufferedLogger()
h := &ForeverHandle{closed: make(chan struct{}), errorHandler: logger, name: "foo"}
h.MarkSupervised()
h.terminated()
require.Empty(t, logger.errors, "error was reported on shutdown")
}