-
Notifications
You must be signed in to change notification settings - Fork 0
/
control_test.go
43 lines (32 loc) · 800 Bytes
/
control_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
package mid
import (
"context"
"net/http"
"sync/atomic"
"testing"
"time"
)
// TODO actually test graceful shutdown
func TestListenWithContext(t *testing.T) {
var healthy int32
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if atomic.LoadInt32(&healthy) == 0 {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
w.Write([]byte("ok"))
})
server := &http.Server{
Addr: ":0",
Handler: handler,
ReadTimeout: 1 * time.Second,
WriteTimeout: 1 * time.Second,
IdleTimeout: 1 * time.Second,
}
ctx := InterruptContext()
ctx, _ = context.WithDeadline(ctx, time.Now().Add(time.Second))
err := ListenWithContext(ctx, server, &healthy)
if err != nil {
t.Fatal(err)
}
}