-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_shutdown_test.go
169 lines (136 loc) · 4.12 KB
/
http_shutdown_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
package ctrl
import (
"bytes"
"context"
"errors"
"log/slog"
"net"
"net/http"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestShutdownHTTPServer(t *testing.T) {
// create a test server that we can control
server := &http.Server{
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}),
}
// find an available port
listener, err := net.Listen("tcp", "localhost:0")
require.NoError(t, err)
server.Addr = listener.Addr().String()
// start the server
go func() {
err := server.Serve(listener)
if err != nil && !errors.Is(err, http.ErrServerClosed) {
t.Errorf("unexpected server error: %v", err)
}
}()
// let the server start
time.Sleep(100 * time.Millisecond)
// verify server is running by making a request
resp, err := http.Get("http://" + server.Addr)
require.NoError(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)
// test shutdown with default timeout
err = ShutdownHTTPServer(context.Background(), server)
require.NoError(t, err)
// server should now be shut down, trying to connect should fail
_, err = http.Get("http://" + server.Addr)
assert.Error(t, err)
}
func TestRunHTTPServerWithContext(t *testing.T) {
t.Run("successful server", func(t *testing.T) {
// create a test server
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
server := &http.Server{
Handler: mux,
}
// find an available port
listener, err := net.Listen("tcp", "localhost:0")
require.NoError(t, err)
server.Addr = listener.Addr().String()
// create a buffer to capture logs
var logBuf bytes.Buffer
logger := slog.New(slog.NewTextHandler(&logBuf, nil))
// create a cancelable context
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// create a custom start function that uses our listener
startFn := func() error {
return server.Serve(listener)
}
// run server with context
errCh := RunHTTPServerWithContext(ctx, server, startFn,
WithHTTPLogger(logger),
WithHTTPShutdownTimeout(3*time.Second),
)
// let the server start
time.Sleep(100 * time.Millisecond)
// verify server is running by making a request
resp, err := http.Get("http://" + server.Addr)
require.NoError(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)
resp.Body.Close()
// trigger shutdown
cancel()
// wait for server to exit
err = <-errCh
require.NoError(t, err)
// server should now be shut down, trying to connect should fail
_, err = http.Get("http://" + server.Addr)
require.Error(t, err)
// verify shutdown log message was recorded
assert.Contains(t, logBuf.String(), "shutting down HTTP server")
})
t.Run("server error", func(t *testing.T) {
// create a server with a deliberately invalid address
server := &http.Server{
Addr: "invalid-address",
}
// create a context
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// create a start function that will fail
startFn := func() error {
return server.ListenAndServe()
}
// run server with context
errCh := RunHTTPServerWithContext(ctx, server, startFn)
// wait for error
err := <-errCh
assert.Error(t, err)
})
t.Run("cancel before server starts", func(t *testing.T) {
// create a test server
server := &http.Server{
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}),
}
// create a context that's already canceled
ctx, cancel := context.WithCancel(context.Background())
cancel()
// create a listener but don't actually use it in the start function
listener, err := net.Listen("tcp", "localhost:0")
require.NoError(t, err)
defer listener.Close()
server.Addr = listener.Addr().String()
// create a start function that blocks until context is canceled
startFn := func() error {
<-ctx.Done()
return http.ErrServerClosed
}
// run server with context
errCh := RunHTTPServerWithContext(ctx, server, startFn)
// wait for result
err = <-errCh
require.NoError(t, err)
})
}