-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver_test.go
107 lines (86 loc) · 2.42 KB
/
server_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
package graceful
import (
"context"
"errors"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/pressly/chi"
"github.com/stretchr/testify/assert"
)
func TestShutdown(t *testing.T) {
shutdownMsg := "Service Status: unavailable!"
simpleErr := errors.New("Test Error!")
period := time.Millisecond
r := chi.NewRouter()
r.Get("/func", func(w http.ResponseWriter, req *http.Request) {
Error(w, simpleErr, http.StatusInternalServerError)
})
srv := NewServer()
srv.UseShutdownMiddleware(http.StatusServiceUnavailable, []byte(shutdownMsg))
srv.Mount("/test", r)
fl := NewFakeListener()
serveErrCh := make(chan error, 2)
go func() {
serveErrCh <- srv.Serve(fl)
}()
cl, err := NewClient("/test", &JSONCodec{}, nil)
assert.NoError(t, err)
http.DefaultTransport = FakeRoudTripper{func(req *http.Request) (*http.Response, error) {
w := httptest.NewRecorder()
srv.ServeHTTP(w, req)
return w.Result(), nil
}}
err = cl.Call(context.TODO(), "func", nil, nil)
assert.Equal(t, simpleErr, err)
go func() {
err1 := srv.Shutdown(context.TODO(), period)
assert.Equal(t, http.ErrServerClosed, err1)
serveErrCh <- err1
}()
begin := time.Now()
for time.Since(begin) < period {
time.Sleep(period / 4)
err = cl.Call(context.TODO(), "func", nil, nil)
assert.EqualError(t, err, shutdownMsg)
}
err1 := <-serveErrCh
err2 := <-serveErrCh
assert.Equal(t, err1, err2)
}
func TestTimeout(t *testing.T) {
var (
testErr = errors.New("All right")
timeout time.Duration = time.Second / 10
)
l := NewFakeListener()
http.DefaultTransport = l.DefaultTransport
s := NewServer()
go s.Serve(l)
defer s.Stop()
s.Handle("/slow", http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
time.Sleep(timeout * 2)
Error(w, testErr, http.StatusInternalServerError)
}))
s.Handle("/quick", http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
time.Sleep(timeout / 2)
Error(w, testErr, http.StatusInternalServerError)
}))
c, err := NewClient("http://localhost", &JSONCodec{}, nil)
if err != nil {
t.Error(err)
}
// 1
ctx, cancel := context.WithTimeout(context.Background(), timeout)
err = c.Call(ctx, "slow", nil, nil)
err = ErrorFromURL(err)
cancel()
assert.Equal(t, context.DeadlineExceeded, err)
// 2
ctx, cancel = context.WithTimeout(context.Background(), timeout)
err = c.Call(ctx, "quick", nil, nil)
err = ErrorFromURL(err)
cancel()
assert.Equal(t, testErr, err)
}