forked from Shopify/toxiproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
toxiproxy_test.go
45 lines (36 loc) · 1.1 KB
/
toxiproxy_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
package toxiproxy_test
import (
"net"
"testing"
"github.com/Shopify/toxiproxy/v2"
"github.com/Shopify/toxiproxy/v2/collectors"
"github.com/Shopify/toxiproxy/v2/testhelper"
"github.com/prometheus/client_golang/prometheus"
)
func NewTestProxy(name, upstream string) *toxiproxy.Proxy {
srv := toxiproxy.NewServer(toxiproxy.NewMetricsContainer(prometheus.NewRegistry()))
srv.Metrics.ProxyMetrics = collectors.NewProxyMetricCollectors()
proxy := toxiproxy.NewProxy(srv, name, "localhost:0", upstream)
return proxy
}
func WithTCPProxy(
t *testing.T,
f func(proxy net.Conn, response chan []byte, proxyServer *toxiproxy.Proxy),
) {
testhelper.WithTCPServer(t, func(upstream string, response chan []byte) {
proxy := NewTestProxy("test", upstream)
proxy.Start()
conn := AssertProxyUp(t, proxy.Listen, true)
f(conn, response, proxy)
proxy.Stop()
})
}
func AssertProxyUp(t *testing.T, addr string, up bool) net.Conn {
conn, err := net.Dial("tcp", addr)
if err != nil && up {
t.Error("Expected proxy to be up:", err)
} else if err == nil && !up {
t.Error("Expected proxy to be down")
}
return conn
}