-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththc_test.go
107 lines (91 loc) · 2.31 KB
/
thc_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 thc
import (
"expvar"
"net/http"
"net/http/httptest"
"strconv"
"testing"
"time"
)
func TestMetrics(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(20 * time.Millisecond)
w.Write([]byte("OK"))
}))
defer server.Close()
// Default settings, no circuit breaker.
client := THC{}
client.PublishExpvar()
ok := func(resp *http.Response, err error) {
if err != nil {
t.Error(err)
} else {
resp.Body.Close()
}
}
// Do a few requests.
ok(client.Get(server.URL))
ok(client.Head(server.URL))
ok(client.Post(server.URL, "text/plain", nil))
ok(client.PostForm(server.URL, nil))
assert := func(key string, val float64, cond bool) {
if !cond {
t.Error(key, val)
}
}
var t1, t2, t3 float64
// Read expvars in random order and assert the values are coherent.
expvar.Do(func(elem expvar.KeyValue) {
k := elem.Key
v, _ := strconv.ParseFloat(elem.Value.String(), 64)
switch k {
case "thc-dns-lookup":
assert(k, v, v == 0) // We connect to an IP address.
case "thc-tcp-connection":
assert(k, v, v > 0)
t1 = v
case "thc-tls-handshake":
assert(k, v, v == 0) // No TLS in this test.
case "thc-get-connection":
assert(k, v, v > t1)
t2 = v
case "thc-write-request":
assert(k, v, v > t2)
t3 = v
case "thc-get-response":
assert(k, v, v > t3)
}
})
}
func TestCircuitBreaker(t *testing.T) {
const (
maxErrors = 2
healingTime = 100 * time.Millisecond
)
// A server which always fails.
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "FAIL", 500)
}))
defer server.Close()
// A client that becomes out of service after 2 failures, for 100ms.
client := THC{
Name: "test",
MaxErrors: maxErrors,
HealingTime: healingTime,
}
// These first requests should be fine.
for i := 0; i < maxErrors; i++ {
if _, err := client.Get(server.URL); err != nil {
t.Error("unexpected error:", err)
}
}
// The next request should fail.
if _, err := client.Get(server.URL); err != ErrOutOfService {
t.Error("expected OutOfService error")
}
time.Sleep(healingTime + 1*time.Millisecond)
// The client should be back in service now.
if _, err := client.Get(server.URL); err != nil {
t.Error("unexpected error:", err)
}
}