-
-
Notifications
You must be signed in to change notification settings - Fork 72
/
atreugo_unix_test.go
347 lines (315 loc) · 6.68 KB
/
atreugo_unix_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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
//go:build !windows
// +build !windows
package atreugo
import (
"bytes"
"errors"
"log"
"syscall"
"testing"
"time"
"github.com/atreugo/mock"
"github.com/valyala/fasthttp/fasthttputil"
)
func TestAtreugo_ServeGracefully(t *testing.T) { // nolint:funlen
type args struct {
lnAcceptError error
lnCloseError error
}
type want struct {
err bool
}
tests := []struct {
name string
args args
want want
}{
{
name: "Ok",
args: args{
lnAcceptError: nil,
lnCloseError: nil,
},
want: want{
err: false,
},
},
{
name: "ServeError",
args: args{
lnAcceptError: errors.New("listener accept error"),
lnCloseError: nil,
},
want: want{
err: true,
},
},
{
name: "ShutdownError",
args: args{
lnAcceptError: nil,
lnCloseError: errors.New("listener close error"),
},
want: want{
err: true,
},
},
}
for _, test := range tests {
tt := test
t.Run(tt.name, func(t *testing.T) {
t.Helper()
ln := &mock.Listener{
Listener: fasthttputil.NewInmemoryListener(),
AcceptError: tt.args.lnAcceptError,
CloseError: tt.args.lnCloseError,
}
defer ln.Listener.Close()
logOutput := &bytes.Buffer{}
log := log.New(logOutput, "", log.LstdFlags)
cfg := Config{Logger: log}
s := New(cfg)
errCh := make(chan error, 1)
go func() {
errCh <- s.ServeGracefully(ln)
}()
time.Sleep(100 * time.Millisecond)
if err := syscall.Kill(syscall.Getpid(), syscall.SIGINT); err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if err := <-errCh; (err == nil) == tt.want.err {
t.Fatalf("Unexpected error: %v", err)
}
if !s.cfg.GracefulShutdown {
t.Errorf("Config.GracefulShutdown = %v, want %v", cfg.GracefulShutdown, true)
}
if len(s.cfg.GracefulShutdownSignals) == 0 {
t.Errorf("Config.GracefulShutdownSignals is empty, want %v", defaultGracefulShutdownSignals)
}
lnAddr := ln.Addr().String()
if s.cfg.Addr != lnAddr {
t.Errorf("Atreugo.Config.Addr = %s, want %s", s.cfg.Addr, lnAddr)
}
lnNetwork := ln.Addr().Network()
if s.cfg.Network != lnNetwork {
t.Errorf("Atreugo.Config.Network = %s, want %s", s.cfg.Network, lnNetwork)
}
})
}
}
func TestAtreugo_ListenAndServe(t *testing.T) { //nolint:funlen
type want struct {
err error
}
tests := []struct {
name string
args Config
want want
}{
{
name: "NormalOk",
args: Config{
Addr: "localhost:8081",
GracefulShutdown: false,
TLSEnable: false,
Prefork: false,
Reuseport: false,
},
want: want{
err: nil,
},
},
{
name: "GracefulOk",
args: Config{
Addr: "localhost:8081",
GracefulShutdown: true,
TLSEnable: false,
Prefork: false,
Reuseport: false,
},
want: want{
err: nil,
},
},
{
name: "PreforkOk",
args: Config{
Addr: "localhost:8081",
GracefulShutdown: false,
TLSEnable: false,
Prefork: true,
Reuseport: false,
},
want: want{
err: nil,
},
},
{
name: "ReuseportOk",
args: Config{
Addr: "localhost:8081",
GracefulShutdown: false,
TLSEnable: false,
Prefork: false,
Reuseport: true,
},
want: want{
err: nil,
},
},
{
name: "GracefulPreforkOk",
args: Config{
Addr: "localhost:8081",
GracefulShutdown: true,
TLSEnable: false,
Prefork: true,
Reuseport: false,
},
want: want{
err: nil,
},
},
{
name: "GracefulPreforkReuseportOk",
args: Config{
Addr: "localhost:8081",
GracefulShutdown: true,
TLSEnable: false,
Prefork: true,
Reuseport: true,
},
want: want{
err: nil,
},
},
{
name: "NormalError",
args: Config{
Addr: "invalid",
GracefulShutdown: false,
TLSEnable: false,
Prefork: false,
Reuseport: false,
},
want: want{
err: errors.New(
"failed to announce on the local network address: listen tcp4: address invalid: missing port in address",
),
},
},
{
name: "GracefulError",
args: Config{
Addr: "invalid",
GracefulShutdown: true,
TLSEnable: false,
Prefork: false,
Reuseport: false,
},
want: want{
err: errors.New(
"failed to announce on the local network address: listen tcp4: address invalid: missing port in address",
),
},
},
{
name: "PreforkError",
args: Config{
Addr: "invalid",
GracefulShutdown: false,
TLSEnable: false,
Prefork: true,
Reuseport: false,
},
want: want{
err: errors.New("prefork error"),
},
},
{
name: "ReuseportError",
args: Config{
Addr: "invalid",
GracefulShutdown: false,
TLSEnable: false,
Prefork: false,
Reuseport: true,
},
want: want{
err: errors.New("address invalid: missing port in address"),
},
},
{
name: "GracefulPreforkError",
args: Config{
Addr: "invalid",
GracefulShutdown: true,
TLSEnable: false,
Prefork: true,
Reuseport: false,
},
want: want{
err: errors.New("graceful prefork error"),
},
},
{
name: "GracefulPreforkReuseportError",
args: Config{
Addr: "invalid",
GracefulShutdown: true,
TLSEnable: false,
Prefork: true,
Reuseport: true,
},
want: want{
err: errors.New("graceful prefork reuseport error"),
},
},
{
name: "TLSError",
args: Config{
Addr: "localhost:8081",
TLSEnable: true,
},
want: want{
err: errors.New("cert or key has not provided"),
},
},
}
for _, test := range tests {
tt := test
tt.args.Logger = testLog
waitTime := 200 * time.Millisecond
s := New(tt.args)
s.cfg.newPreforkServerFunc = func(s *Atreugo) preforkServer {
return newPreforkServerMock(s, tt.want.err)
}
t.Run(tt.name, func(t *testing.T) {
t.Helper()
errCh := make(chan error, 1)
go func() {
errCh <- s.ListenAndServe()
}()
var err error
select {
case err = <-errCh:
case <-time.After(waitTime):
if err := s.engine.Shutdown(); err != nil {
t.Errorf("Error shutting down the server: %+v", err)
}
}
errMessage := ""
if err != nil {
errMessage = err.Error()
}
wantErrMessage := ""
if tt.want.err != nil {
wantErrMessage = tt.want.err.Error()
}
if errMessage != wantErrMessage {
t.Errorf("Unexpected error: %s, want: %s", errMessage, wantErrMessage)
}
})
}
}