-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_test.go
230 lines (178 loc) · 5.07 KB
/
server_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
package lameduck
import (
"context"
"errors"
"fmt"
"os"
"strings"
"sync"
"testing"
"time"
"golang.org/x/sys/unix"
)
type testcase struct {
signal os.Signal
signalAfter time.Duration
shutdownAfter time.Duration
cancelAfter time.Duration
serveError error
shutdownError error
closeError error
runOptions []Option
want error
}
var (
errServeFailed = errors.New("server failed to start")
errShutdownFailed = errors.New("server failed to shutdown")
)
func TestRun(t *testing.T) {
// Test cases are generated from a series of ordered events that should
// result in a specific outcome (as declared by 'wantError'). If no error
// is declared, a nil error is expected.
cases := map[string]*testcase{
"normal": mkCase(sendSignal(unix.SIGTERM), shutdownReturn(nil), lameDuckExpires),
"expired": mkCase(sendSignal(unix.SIGTERM), lameDuckExpires, shutdownReturn(nil), wantError(&LameDuckError{Expired: true})),
"canceled": mkCase(cancelContext, wantError(&LameDuckError{Err: context.Canceled})),
"nostart": mkCase(serveReturn(errServeFailed), wantError(&LameDuckError{Err: errServeFailed})),
"badstop": mkCase(sendSignal(unix.SIGTERM), shutdownReturn(errShutdownFailed), lameDuckExpires, wantError(&LameDuckError{Err: errShutdownFailed})),
}
for label, tc := range cases {
t.Run(label, tc.test)
}
}
func (tc *testcase) test(t *testing.T) {
if tc.cancelAfter == 0 && tc.signalAfter == 0 && tc.serveError == nil {
t.Fatal("Invalid testcase: must set one of 'cancelAfter', 'signalAfter', or 'serveError'")
}
ts := injectSignaller()
defer ts.revert()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
tl := &testLogger{t.Logf}
tc.runOptions = append(tc.runOptions, WithLogger(tl))
svr := newTestServer(tl, tc.serveError, tc.shutdownError, tc.closeError)
errs := make(chan error)
var wg sync.WaitGroup
r, err := NewRunner(svr, tc.runOptions...)
if err != nil {
t.Fatalf("cannot create Runner: %v", err)
}
tc.chkState(t, r, NotStarted)
wg.Add(1)
go func() {
defer wg.Done()
defer close(errs)
if err := r.Run(ctx); err != nil {
tl.Infof("Run error: %v", err)
errs <- err
} else {
tl.Infof("Server Run Successful")
}
}()
if tc.cancelAfter != 0 {
wg.Add(1)
tl.Infof("Will cancel context after %v", tc.cancelAfter)
time.AfterFunc(tc.cancelAfter, func() {
defer wg.Done()
tl.Infof("Cancelling Context")
cancel()
})
}
if tc.signal != nil && tc.signalAfter != 0 {
wg.Add(1)
tl.Infof("Will emit signal %q after %v", tc.signal, tc.signalAfter)
time.AfterFunc(tc.signalAfter, func() {
defer wg.Done()
tl.Infof("Emitting signal: %v", tc.signal)
ts.emit(tc.signal)
})
}
if tc.shutdownAfter != 0 {
wg.Add(1)
tl.Infof("Will finish Shutdown after %v", tc.shutdownAfter)
time.AfterFunc(tc.shutdownAfter, func() {
defer wg.Done()
tl.Infof("Finishing server Shutdown method")
svr.shutdown.finish()
})
}
<-r.Ready()
tc.chkState(t, r, Running)
if got := <-errs; !tc.isWanted(got) {
t.Errorf("##### Run(ctx, svr) == %#v; wanted %#v", got, tc.want)
}
wg.Wait()
tc.chkState(t, r, Stopped)
}
func (tc *testcase) isWanted(got error) bool {
if lde, ok := tc.want.(*LameDuckError); ok {
return lde.isEqual(got)
}
return got == tc.want
}
func (tc *testcase) chkState(t *testing.T, r *Runner, want State) {
t.Helper()
t.Logf("want:%v tc:%#v", want, tc)
switch want {
case Stopped:
if errors.Is(tc.want, context.Canceled) {
want = Failed
}
if errors.Is(tc.want, errServeFailed) {
want = NotStarted
}
fallthrough
case Running:
time.Sleep(100 * time.Microsecond) // to compensate for test flakiness
if tc.serveError == errServeFailed {
want = Failed
}
}
if got := r.State(); got != want {
t.Errorf("##### r.State() == %v; wanted %v", got, want)
}
}
func (tc *testcase) GoString() string {
if tc == nil {
return "<nil>"
}
var parts []string
if tc.signal != nil {
parts = append(parts, fmt.Sprintf("signal: %v", tc.signal))
}
if tc.signalAfter != 0 {
parts = append(parts, fmt.Sprintf("signalAfter: %v", tc.signalAfter))
}
if tc.shutdownAfter != 0 {
parts = append(parts, fmt.Sprintf("shutdownAfter: %v", tc.shutdownAfter))
}
if tc.cancelAfter != 0 {
parts = append(parts, fmt.Sprintf("cancelAfter: %v", tc.cancelAfter))
}
if tc.serveError != nil {
parts = append(parts, fmt.Sprintf("serveError: %v", tc.serveError))
}
if tc.closeError != nil {
parts = append(parts, fmt.Sprintf("closeError: %v", tc.closeError))
}
if len(tc.runOptions) != 0 {
parts = append(parts, fmt.Sprintf("runtimeOptions: %#v", tc.runOptions))
}
if tc.want != nil {
parts = append(parts, fmt.Sprintf("want: %#v", tc.want))
}
return fmt.Sprintf("&testcase{%s}", strings.Join(parts, ", "))
}
// A convenience method added to type LameDuckError (only during testing).
func (lde *LameDuckError) isEqual(err error) bool {
switch {
case lde == nil && err == nil:
return true
case lde == nil || err == nil:
return false
}
if olde, ok := err.(*LameDuckError); ok {
return lde.Expired == olde.Expired && lde.Err == olde.Err
}
return false
}