forked from dropbox/godropbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_utils.go
76 lines (64 loc) · 1.57 KB
/
test_utils.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
// Utility functions for testing net2/http2
package test_utils
import (
"net"
"net/http"
"net/http/httptest"
"time"
. "gopkg.in/check.v1"
)
type TestServer struct {
*httptest.Server
CloseChan chan bool
}
// Create a dummy server for unittesting. DO NOT USE IN PRODUCTION.
func SetupTestServer(ssl bool) (*TestServer, string) {
closeChan := make(chan bool, 1)
serveMux := http.NewServeMux()
serveMux.HandleFunc(
"/slow_request",
func(writer http.ResponseWriter, req *http.Request) {
select {
case <-closeChan:
return
case <-time.After(500 * time.Millisecond):
return
}
})
serveMux.HandleFunc(
"/",
func(writer http.ResponseWriter, req *http.Request) {
writer.Write([]byte("ok"))
})
server := httptest.NewUnstartedServer(serveMux)
server.Config.ReadTimeout = 5 * time.Second
server.Config.WriteTimeout = 5 * time.Second
if ssl {
server.StartTLS()
} else {
server.Start()
}
addr := server.Listener.Addr().String()
return &TestServer{server, closeChan}, addr
}
// This returns a random port for unit testing. DO NOT USE IN PRODUCTION.
func RandomListenPort(c *C) int {
sock, err := net.Listen("tcp", "127.0.0.1:0")
c.Assert(err, IsNil)
port := sock.Addr().(*net.TCPAddr).Port
sock.Close()
return port
}
// This checks to ensure a server is running on the specified host port.
// DO NOT USE IN PRODUCTION.
func EnsureListen(c *C, hostport string) {
for i := 0; i < 10; i++ {
conn, err := net.Dial("tcp", hostport)
if err == nil {
conn.Close()
return
}
time.Sleep(time.Duration(50*(i+1)) * time.Millisecond)
}
c.FailNow()
}