-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathgracehttp_test.go
116 lines (102 loc) · 2.39 KB
/
gracehttp_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
package gracehttp
import (
"flag"
"fmt"
"io/ioutil"
"net/http"
"syscall"
"testing"
"time"
)
var (
runChan chan struct{}
httpPort1 string
httpPort2 string
)
func init() {
flag.StringVar(&httpPort1, "http_port_1", "9090", "the port of http server 1")
flag.StringVar(&httpPort2, "http_port_2", "9091", "the port of http server 2")
runChan = make(chan struct{}, 1)
}
type Controller struct {
}
func (this *Controller) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
if req.URL.Path == "/ping" {
resp.Write([]byte(fmt.Sprintf("pong by pid:%d", syscall.Getpid())))
} else {
resp.Write([]byte("unknown"))
}
}
func runServer(t *testing.T) {
hd := &Controller{}
grace := NewGraceHTTP()
{
srv1 := &http.Server{
Addr: ":" + httpPort1,
Handler: hd,
ReadTimeout: time.Duration(time.Second),
WriteTimeout: time.Duration(time.Second),
}
option := &ServerOption{
HTTPServer: srv1,
}
grace.AddServer(option)
}
{
srv2 := &http.Server{
Addr: ":" + httpPort2,
Handler: hd,
ReadTimeout: time.Duration(time.Second),
WriteTimeout: time.Duration(time.Second),
}
option := &ServerOption{
HTTPServer: srv2,
}
grace.AddServer(option)
}
runChan <- struct{}{}
if err := grace.Run(); err != nil {
t.Fatal(err)
}
}
func TestHTTPServer(t *testing.T) {
go runServer(t)
<-runChan
testServer1 := func() {
t.Log("[test http server 1]")
resp, err := http.Get("http://localhost:" + httpPort1 + "/ping")
if err != nil {
t.Fatal("http server 1 error:", err)
} else {
defer resp.Body.Close()
data, respErr := ioutil.ReadAll(resp.Body)
if respErr != nil {
t.Fatal("http server 1 error:", respErr)
}
t.Log("http server 1 success, response:", string(data))
}
}
testServer2 := func() {
t.Log("[test http server 2]")
resp, err := http.Get("http://localhost:" + httpPort2 + "/ping")
if err != nil {
t.Fatal("http server 2 error:", err)
} else {
defer resp.Body.Close()
data, respErr := ioutil.ReadAll(resp.Body)
if respErr != nil {
t.Fatal("http server 2 error:", respErr)
}
t.Log("http server 2 success, response:", string(data))
}
}
t.Log("******* test multi server *******")
testServer1()
testServer2()
// t.Log("******* test grace restart *******")
// pid := syscall.Getpid()
// syscall.Kill(pid, syscall.SIGUSR1)
// time.Sleep(time.Second)
// testServer1()
// testServer2()
}