-
Notifications
You must be signed in to change notification settings - Fork 79
/
server_test.go
128 lines (123 loc) · 3 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package tigertonic
import (
"fmt"
"net"
"net/http"
"testing"
"time"
)
func TestServerCATLS(t *testing.T) {
s, err := NewTLSServer("", "test.crt", "test.key", NotFoundHandler{})
if nil != err {
t.Fatal(err)
}
s.CA("test.crt")
if nil == s.TLSConfig.Certificates || 1 != len(s.TLSConfig.Certificates) {
t.Fatal("no Certificates")
}
if nil == s.TLSConfig.RootCAs || 1 != len(s.TLSConfig.RootCAs.Subjects()) {
t.Fatal("no RootCAs")
}
}
func TestServerGracefulStop(t *testing.T) {
chT := make(chan time.Time, 1)
s := NewServer(
"127.0.0.1:0",
http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
time.Sleep(2 * time.Millisecond)
w.WriteHeader(http.StatusNoContent)
chT <- time.Now()
}),
)
l, err := net.Listen("tcp", s.Addr)
if nil != err {
t.Fatal(err)
}
go s.Serve(l)
chR := make(chan *http.Response, 1)
go func() {
rs, err := http.Get(fmt.Sprintf("http://%s", l.Addr()))
if nil != err {
t.Fatal(err)
}
chR <- rs
}()
time.Sleep(time.Millisecond)
s.Close()
now := time.Now()
then := <-chT
if now.Before(then) {
t.Fatal(now, "before", then)
}
if then.Add(10 * time.Millisecond).Before(now) {
t.Fatal("connection not closed quickly")
}
rs := <-chR
if http.StatusNoContent != rs.StatusCode {
t.Fatal(rs)
}
if _, err := http.Get(fmt.Sprintf("http://%s", l.Addr())); nil == err {
t.Fatal("GET / should have failed after server stopped")
}
}
func TestServerGracefulStopKeepAlive(t *testing.T) {
s := NewServer(
"127.0.0.1:0",
http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusNoContent)
}),
)
l, err := net.Listen("tcp", s.Addr)
if nil != err {
t.Fatal(err)
}
go s.Serve(l)
if _, err := http.Get(fmt.Sprintf("http://%s", l.Addr())); nil != err {
t.Fatal(err)
}
then := time.Now()
s.Close()
now := time.Now()
if then.Add(510 * time.Millisecond).Before(now) {
t.Fatal("connection not closed quickly")
}
if _, err := http.Get(fmt.Sprintf("http://%s", l.Addr())); nil == err {
t.Fatal("GET / should have failed after server stopped")
}
}
func TestServerGracefulStopMulti(t *testing.T) {
s := NewServer(
"127.0.0.1:0",
http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusNoContent)
}),
)
l1, err := net.Listen("tcp", s.Addr)
if nil != err {
t.Fatal(err)
}
l2, err := net.Listen("tcp", s.Addr)
if nil != err {
t.Fatal(err)
}
go s.Serve(l1)
go s.Serve(l2)
if _, err := http.Get(fmt.Sprintf("http://%s", l1.Addr())); nil != err {
t.Fatal(err)
}
if _, err := http.Get(fmt.Sprintf("http://%s", l2.Addr())); nil != err {
t.Fatal(err)
}
then := time.Now()
s.Close()
now := time.Now()
if then.Add(510 * time.Millisecond).Before(now) {
t.Fatal("connection not closed quickly")
}
if _, err := http.Get(fmt.Sprintf("http://%s", l1.Addr())); nil == err {
t.Fatal("GET / should have failed after server stopped")
}
if _, err := http.Get(fmt.Sprintf("http://%s", l2.Addr())); nil == err {
t.Fatal("GET / should have failed after server stopped")
}
}