-
Notifications
You must be signed in to change notification settings - Fork 0
/
shutdown_test.go
157 lines (137 loc) · 4.56 KB
/
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
package shutdown_test
import (
"context"
"errors"
"log"
"net/http"
"net/http/httptest"
"os"
"os/signal"
"syscall"
"testing"
"time"
shutdown "github.com/mheck136/ws-shutdown"
)
func ExampleShutdowner() {
ctx := context.Background()
// a single instance per application should be enough
var shutdowner shutdown.Shutdowner
// handler that hijacks connections, e.g. for websockets
var handler http.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// here, the hijacking of the connection would happen, e.g. upgrading to a websocket connection
w.WriteHeader(http.StatusOK)
})
// wrap the handler with the shutdowner middleware
handler = shutdowner.Middleware(handler)
server := http.Server{
Addr: ":8080",
Handler: handler,
}
// start the server
go func() {
err := server.ListenAndServe()
if err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Printf("server reported an error: %v", err)
}
}()
// wait for the interrupt signal
signalCtx, cancel := signal.NotifyContext(ctx, os.Interrupt)
defer cancel()
// simulate signal
go func() {
time.Sleep(1 * time.Second)
_ = syscall.Kill(syscall.Getpid(), syscall.SIGINT)
}()
<-signalCtx.Done()
// set a timeout for the shutdown
shutdownCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
// shutdown the server gracefully
err := shutdowner.ShutdownWithServer(shutdownCtx, &server)
if err != nil {
log.Printf("graceful server shutdown failed: %v", err)
return
}
log.Println("server shutdown gracefully")
//Output:
}
func TestShutdowner(t *testing.T) {
t.Parallel()
newSleepingHandler := func(d time.Duration) (http.Handler, chan struct{}) {
started := make(chan struct{})
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
close(started)
time.Sleep(d)
}), started
}
tt := []struct {
name string
durations []time.Duration
shutdownAfter time.Duration
shutdownTimeout time.Duration
expectDeadlineExceeded bool
}{
{
name: "shutdown before handler finishes - deadline exceeded",
durations: []time.Duration{10 * time.Millisecond},
shutdownAfter: 0,
shutdownTimeout: 5 * time.Millisecond,
expectDeadlineExceeded: true,
},
{
name: "shutdown before handler finishes - deadline not exceeded",
durations: []time.Duration{5 * time.Millisecond},
shutdownAfter: 0,
shutdownTimeout: 10 * time.Millisecond,
expectDeadlineExceeded: false,
},
{
name: "shutdown before multiple handlers finish - deadline exceeded",
durations: []time.Duration{10 * time.Millisecond, 5 * time.Millisecond, 5 * time.Millisecond},
shutdownAfter: 0,
shutdownTimeout: 5 * time.Millisecond,
expectDeadlineExceeded: true,
},
{
name: "shutdown before multiple handlers finish - deadline not exceeded",
durations: []time.Duration{5 * time.Millisecond, 5 * time.Millisecond, 5 * time.Millisecond},
shutdownAfter: 0,
shutdownTimeout: 10 * time.Millisecond,
expectDeadlineExceeded: false,
},
{
name: "shutdown after handler finishes",
durations: []time.Duration{5 * time.Millisecond},
shutdownAfter: 10 * time.Millisecond,
shutdownTimeout: 5 * time.Millisecond,
expectDeadlineExceeded: false,
},
{
name: "shutdown after multiple handlers finish",
durations: []time.Duration{5 * time.Millisecond, 5 * time.Millisecond, 5 * time.Millisecond},
shutdownAfter: 10 * time.Millisecond,
shutdownTimeout: 5 * time.Millisecond,
expectDeadlineExceeded: false,
},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
var shutdowner shutdown.Shutdowner
for _, d := range tc.durations {
handler, started := newSleepingHandler(d)
go shutdowner.Middleware(handler).ServeHTTP(httptest.NewRecorder(), httptest.NewRequest("GET", "/", nil))
<-started // wait for handler to start
}
time.Sleep(tc.shutdownAfter)
deadlineCtx, cancel := context.WithTimeout(context.Background(), tc.shutdownTimeout)
defer cancel()
err := shutdowner.Shutdown(deadlineCtx)
switch {
case tc.expectDeadlineExceeded && !errors.Is(err, context.DeadlineExceeded):
t.Errorf("expected %T, but got %T", context.DeadlineExceeded, err)
case !tc.expectDeadlineExceeded && err != nil:
t.Errorf("no error expected but got %v", err)
}
})
}
}