-
Notifications
You must be signed in to change notification settings - Fork 13
/
main_test.go
116 lines (100 loc) · 2.62 KB
/
main_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 main
import (
"io/ioutil"
"log"
"net/http"
"net/http/httputil"
"net/url"
"syscall"
"testing"
"time"
"github.com/Clever/sphinx/config"
"github.com/Clever/sphinx/daemon"
"github.com/Clever/sphinx/handlers"
"github.com/Clever/sphinx/ratelimiter"
)
var host = "http://localhost:8081"
type Handler struct{}
func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Write([]byte{})
}
func setUpLocalServer() {
go http.ListenAndServe(":8081", Handler{})
}
func setUpHTTPLimiter(b *testing.B) {
config, err := config.New("example.yaml")
if err != nil {
b.Fatalf("LOAD_CONFIG_FAILED: %s", err.Error())
}
rateLimiter, err := ratelimiter.New(config)
if err != nil {
b.Fatalf("SPHINX_INIT_FAILED: %s", err.Error())
}
// if configuration says that use http
if config.Proxy.Handler != "http" {
b.Fatalf("sphinx only supports the http handler")
}
// ignore the url in the config and use localhost
target, _ := url.Parse(host)
proxy := httputil.NewSingleHostReverseProxy(target)
httpLimiter := handlers.NewHTTPLimiter(rateLimiter, proxy, false)
go http.ListenAndServe(":8082", httpLimiter)
}
func makeRequestTo(port string) error {
// Add basic auth so that we match some buckets.
if resp, err := http.Get("http://user:pass@localhost" + port); err != nil {
log.Printf("got resp %#v", resp)
return err
}
return nil
}
func BenchmarkNoLimiter(b *testing.B) {
setUpLocalServer()
b.ResetTimer()
for i := 0; i < b.N; i++ {
if err := makeRequestTo(":8081"); err != nil {
b.Fatal(err)
}
}
}
func BenchmarkReasonableConfig(b *testing.B) {
setUpLocalServer()
setUpHTTPLimiter(b)
// So we don't spam with logs
log.SetOutput(ioutil.Discard)
b.ResetTimer()
for i := 0; i < b.N; i++ {
makeRequestTo(":8082")
}
}
func TestSighupHandler(t *testing.T) {
ranHandler := make(chan bool, 1)
handler := func(d daemon.Daemon) {
ranHandler <- true
}
conf, _ := config.New("example.yaml")
d, _ := daemon.New(conf)
setupSighupHandler(d, handler)
// Need to sleep here so that the goroutine has time to set up the signal listener, otherwise
// the signal gets missed
time.Sleep(1 * time.Second)
syscall.Kill(syscall.Getpid(), syscall.SIGHUP)
// Give the syscall 1 second to be handled, should be more than enough time
timeout := make(chan bool, 1)
go func() {
time.Sleep(time.Duration(1 * time.Second))
timeout <- true
}()
select {
case <-ranHandler:
case <-timeout:
t.Fatal("Didn't run handler")
}
// Try calling again and make sure it still happens
syscall.Kill(syscall.Getpid(), syscall.SIGHUP)
select {
case <-ranHandler:
case <-timeout:
t.Fatal("Didn't run handler second time")
}
}