-
Notifications
You must be signed in to change notification settings - Fork 1
/
transport_test.go
147 lines (134 loc) · 4.15 KB
/
transport_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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package main
import (
"context"
"fmt"
"net"
"net/http"
"net/http/httptest"
"sync/atomic"
"testing"
"time"
"github.com/matryer/is"
"github.com/valyala/fasthttp"
"github.com/valyala/fasthttp/fasthttputil"
)
func TestTransport_CallAgentCH(t *testing.T) {
is := is.New(t)
target := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
is.Equal(r.Header.Get(gciHeader), checkHeapHeader)
w.Write([]byte("10"))
}))
defer target.Close()
gci := newTransport(target.URL[7:], 1000, true, "", "") // Need to remove the http:// from the beginning of the URL.
is.Equal(int64(10), gci.callAgentCH())
}
func TestTransport_CallAgentGC(t *testing.T) {
is := is.New(t)
target := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
is.Equal(r.Header.Get(gciHeader), gcHeader)
w.WriteHeader(fasthttp.StatusOK)
}))
defer target.Close()
gci := newTransport(target.URL[7:], 1000, true, "", "") // Need to remove the http:// from the beginning of the URL.
gci.callAgentGC()
}
func TestTransport_RoundTrip(t *testing.T) {
t.Run("ServiceUnavailable", func(t *testing.T) {
is := is.New(t)
gci := newTransport("", 1000, true, "", "") // Need to remove the http:// from the beginning of the URL.
gci.isAvailable = false
ctx := fasthttp.RequestCtx{}
gci.RoundTrip(&ctx)
is.Equal(fasthttp.StatusServiceUnavailable, ctx.Response.StatusCode())
})
t.Run("Proxy", func(t *testing.T) {
is := is.New(t)
target := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/hello" {
fmt.Fprint(w, "Hello")
}
}))
defer target.Close()
ctx := fasthttp.RequestCtx{
Request: fasthttp.Request{},
Response: fasthttp.Response{},
}
ctx.Request.SetRequestURI("http://test/hello")
gci := newTransport(target.URL[7:], 1000, true, "", "") // Need to remove the http:// from the beginning of the URL.
gci.RoundTrip(&ctx)
is.Equal([]byte("Hello"), ctx.Response.Body())
})
}
func TestTransport_RoundTrip_GCI(t *testing.T) {
is := is.New(t)
gciHandler := "gci"
chCalled := int32(0)
gcCalled := int32(0)
helloCalled := int64(0)
target := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/hello":
fmt.Fprint(w, "Hello")
atomic.AddInt64(&helloCalled, 1)
case "/" + gciHandler:
switch r.Header.Get(gciHeader) {
case checkHeapHeader:
w.Write([]byte("10"))
atomic.AddInt32(&chCalled, 1)
case gcHeader:
atomic.AddInt32(&gcCalled, 1)
}
}
}))
defer target.Close()
gci := newTransport(target.URL[7:], 5, true, "", gciHandler) // Need to remove the http:// from the beginning of the URL.
for i := int64(0); i < defaultSampleSize; i++ { // Need one more call after defaultSampleSize to trigger gci checks.
ctx := fasthttp.RequestCtx{
Request: fasthttp.Request{},
Response: fasthttp.Response{},
}
ctx.Request.SetRequestURI("http://test/hello")
gci.RoundTrip(&ctx)
}
for atomic.LoadInt64(&helloCalled) != defaultSampleSize {
time.Sleep(10 * time.Millisecond)
}
for atomic.LoadInt32(&chCalled) != 1 {
time.Sleep(10 * time.Millisecond)
}
for atomic.LoadInt32(&gcCalled) != 1 {
time.Sleep(10 * time.Millisecond)
}
// Double checking if nothing has been corruped and the request reaches the target.
helloCalled = 0
ctx := fasthttp.RequestCtx{
Request: fasthttp.Request{},
Response: fasthttp.Response{},
}
ctx.Request.SetRequestURI("http://test/hello")
gci.RoundTrip(&ctx)
for atomic.LoadInt64(&helloCalled) != 1 {
time.Sleep(10 * time.Millisecond)
}
is.Equal([]byte("Hello"), ctx.Response.Body())
}
// serve serves http request using provided fasthttp handler
func newFasthttpServer(handler fasthttp.RequestHandler) (*fasthttp.Server, http.Client) {
ln := fasthttputil.NewInmemoryListener()
s := &fasthttp.Server{
Handler: handler,
DisableKeepalive: true,
}
go func() {
if err := s.Serve(ln); err != nil {
panic(fmt.Errorf("failed to serve: %v", err))
}
}()
return s, http.Client{
Transport: &http.Transport{
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
return ln.Dial()
},
},
}
}