Repository navigation
Expand file tree
/
Copy pathiris_shutdown_closers_test.go
More file actions
287 lines (243 loc) · 7.46 KB
/
Copy pathiris_shutdown_closers_test.go
File metadata and controls
287 lines (243 loc) · 7.46 KB
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
package iris_test
import (
"context"
"io"
"net"
"net/http"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/kataras/iris/v14"
)
// Shutdown runs the closers in order and the server's own shutdown is only the
// first of them: Run and Listen return once the whole list has finished, so a
// main that ends with them does not cut a closer that is still running.
// freeAddr returns an address nothing listens on. Run takes an address, not a
// listener, so the port is picked by the system and given back.
func freeAddr(t *testing.T) string {
t.Helper()
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
addr := ln.Addr().String()
ln.Close()
return addr
}
// runInBackground starts the application on its own goroutine and returns once
// the server has answered a request. The channel carries what Run returned.
//
// OnServerCreated fires before the server keeps its http.Server and before it
// serves. A Shutdown called at that point reads the field Run is still writing,
// which go test -race reports, and it can find no server to stop. One answered
// request puts the caller after both.
func runInBackground(t *testing.T, app *iris.Application, addr string) <-chan error {
t.Helper()
listening, ran := make(chan struct{}), make(chan error, 1)
go func() {
ran <- app.Run(iris.ServerOptions{
Addr: addr,
DisableStartupLog: true,
DisableInterruptHandler: true,
LogWriter: io.Discard,
OnServerCreated: func(*http.Server) error {
close(listening)
return nil
},
})
}()
select {
case <-listening:
case err := <-ran:
t.Fatalf("Run returned before the server listened: %v", err)
case <-time.After(10 * time.Second):
t.Fatal("the server did not start listening")
}
client := &http.Client{Timeout: 10 * time.Second, Transport: &http.Transport{DisableKeepAlives: true}}
resp, err := client.Get("http://" + addr + "/")
if err != nil {
t.Fatalf("the server did not answer its first request: %v", err)
}
io.Copy(io.Discard, resp.Body) //nolint:errcheck // only the round trip matters.
resp.Body.Close()
return ran
}
// expectRun fails the test unless Run has returned nil by now.
func expectRun(t *testing.T, ran <-chan error, what string) {
t.Helper()
select {
case err := <-ran:
if err != nil {
t.Fatalf("expected Run to return nil %s, got %v", what, err)
}
case <-time.After(10 * time.Second):
t.Fatalf("Run did not return %s", what)
}
}
// newQuietApp returns an application that logs nothing and hands a failed Run
// back to the caller instead of exiting the process.
func newQuietApp() *iris.Application {
app := iris.New(iris.Options{LogLevel: "disable", DisableFatalRun: true})
app.Get("/", func(ctx iris.Context) { ctx.WriteString("ok") })
return app
}
// The closer that follows the server in the list is still running when the
// server's shutdown has returned. Run waits for it.
func TestRunWaitsForTheClosersOfShutdown(t *testing.T) {
app := newQuietApp()
var closerDone atomic.Bool
app.AddCloser(func() error {
time.Sleep(300 * time.Millisecond)
closerDone.Store(true)
return nil
})
ran := runInBackground(t, app, freeAddr(t))
shutdown := make(chan error, 1)
go func() { shutdown <- app.Shutdown(context.Background()) }()
expectRun(t, ran, "after the shutdown")
if !closerDone.Load() {
t.Error("Run returned while a closer was still running")
}
select {
case err := <-shutdown:
if err != nil {
t.Errorf("Shutdown failed: %v", err)
}
case <-time.After(10 * time.Second):
t.Fatal("Shutdown did not return")
}
}
// Two Shutdown calls at once: Run waits for the last one to finish.
func TestRunWaitsForEveryShutdownCall(t *testing.T) {
app := newQuietApp()
var running, peak atomic.Int32
var closerDone atomic.Int32
app.AddCloser(func() error {
if now := running.Add(1); now > peak.Load() {
peak.Store(now)
}
time.Sleep(200 * time.Millisecond)
running.Add(-1)
closerDone.Add(1)
return nil
})
ran := runInBackground(t, app, freeAddr(t))
var wg sync.WaitGroup
for range 2 {
wg.Go(func() {
if err := app.Shutdown(context.Background()); err != nil {
t.Error(err)
}
})
}
expectRun(t, ran, "after the shutdown")
if got := closerDone.Load(); got != 2 {
t.Errorf("Run returned after %d of the 2 closer calls", got)
}
wg.Wait()
if peak.Load() < 1 {
t.Error("the closer never ran")
}
}
// A closer that calls Shutdown itself does not deadlock. The second call runs
// the list again, so the closer guards itself with a sync.Once.
func TestRunWaitsForAShutdownStartedByACloser(t *testing.T) {
app := newQuietApp()
var (
started atomic.Bool
calls atomic.Int32
inner = make(chan error, 1)
)
app.AddCloser(func() error {
calls.Add(1)
// Only the first run starts another shutdown: that one runs the whole
// list again, this closer included.
if started.CompareAndSwap(false, true) {
inner <- app.Shutdown(context.Background())
}
return nil
})
ran := runInBackground(t, app, freeAddr(t))
if err := app.Shutdown(context.Background()); err != nil {
t.Fatal(err)
}
expectRun(t, ran, "after the nested shutdown")
select {
case err := <-inner:
if err != nil {
t.Errorf("the shutdown the closer started failed: %v", err)
}
case <-time.After(10 * time.Second):
t.Fatal("the shutdown the closer started did not return")
}
if got := calls.Load(); got != 2 {
t.Errorf("expected the closer to run twice (once per Shutdown), got %d", got)
}
}
// A handler that stops its own application on another goroutine: Run returns
// once the request is answered and the closers are done.
func TestRunWaitsForTheClosersOfAShutdownFromAHandler(t *testing.T) {
app := newQuietApp()
var closerDone atomic.Bool
app.AddCloser(func() error {
time.Sleep(200 * time.Millisecond)
closerDone.Store(true)
return nil
})
answered := make(chan string, 1)
app.Get("/stop", func(ctx iris.Context) {
go app.Shutdown(context.Background()) //nolint:errcheck // the test reads the result through Run.
ctx.WriteString("stopping")
})
addr := freeAddr(t)
ran := runInBackground(t, app, addr)
go func() {
client := &http.Client{Timeout: 10 * time.Second}
defer client.CloseIdleConnections()
resp, err := client.Get("http://" + addr + "/stop")
if err != nil {
answered <- "error: " + err.Error()
return
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
answered <- resp.Status + " " + string(body)
}()
expectRun(t, ran, "after the shutdown the handler started")
if !closerDone.Load() {
t.Error("Run returned while a closer was still running")
}
select {
case got := <-answered:
if want := "200 OK stopping"; got != want {
t.Errorf("expected the request to be answered with %q, got %q", want, got)
}
case <-time.After(10 * time.Second):
t.Fatal("the request was not answered")
}
}
// Shutdown before Run runs the closers registered by then, and the Run after
// it still serves and still returns.
func TestShutdownBeforeRun(t *testing.T) {
app := newQuietApp()
var calls atomic.Int32
app.AddCloser(func() error {
calls.Add(1)
return nil
})
if err := app.Shutdown(context.Background()); err != nil {
t.Fatal(err)
}
if got := calls.Load(); got != 1 {
t.Fatalf("expected the closer to run once before Run, got %d", got)
}
ran := runInBackground(t, app, freeAddr(t))
if err := app.Shutdown(context.Background()); err != nil {
t.Fatal(err)
}
expectRun(t, ran, "after the second shutdown")
if got := calls.Load(); got != 2 {
t.Errorf("expected the closer to run twice, got %d", got)
}
}