git.iko.soy/typeable/oauth2-proxy
on proxy-mapping help sign in
typeable/oauth2-proxy/http_test.go
39 lines 787 B history raw
1package main
3import (
4 "net/http"
5 "sync"
6 "testing"
7 "time"
9 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/options"
10 "github.com/stretchr/testify/assert"
13func TestGracefulShutdown(t *testing.T) {
14 opts := options.NewOptions()
15 stop := make(chan struct{}, 1)
16 srv := Server{Handler: http.DefaultServeMux, Opts: opts, stop: stop}
17 var wg sync.WaitGroup
18 wg.Add(1)
19 go func() {
20 defer wg.Done()
21 srv.ServeHTTP()
22 }()
24 stop <- struct{}{} // emulate catching signals
26 // An idiomatic for sync.WaitGroup with timeout
27 c := make(chan struct{})
28 go func() {
29 defer close(c)
30 wg.Wait()
31 }()
32 select {
33 case <-c:
34 case <-time.After(1 * time.Second):
35 t.Fatal("Server should return gracefully but timeout has occurred")
36 }
38 assert.Len(t, stop, 0) // check if stop chan is empty