-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathcollapse_test.go
181 lines (129 loc) · 3.5 KB
/
collapse_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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package templar
import (
"net/http"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"github.com/vektra/neko"
)
type heldClient struct {
begin chan struct{}
wait chan struct{}
}
func (h *heldClient) Finish() {
h.wait <- struct{}{}
}
func (h *heldClient) Forward(res Responder, req *http.Request) error {
h.begin <- struct{}{}
<-h.wait
return nil
}
func TestCollapse(t *testing.T) {
n := neko.Start(t)
var (
collapse *Collapser
client MockClient
)
n.CheckMock(&client.Mock)
n.Setup(func() {
collapse = NewCollapser(&client, NewCategorizer())
})
n.It("sends a request on to the downstream client", func() {
req, err := http.NewRequest("GET", "http://google.com/foo/bar", nil)
require.NoError(t, err)
res := newRecordingSender()
client.On("Forward", mock.Anything, req).Return(nil)
collapse.Forward(res, req)
})
n.It("registers itself as an ongoing request", func() {
req, err := http.NewRequest("GET", "http://google.com/foo/bar", nil)
require.NoError(t, err)
res := newRecordingSender()
held := &heldClient{
make(chan struct{}),
make(chan struct{}),
}
defer held.Finish()
collapse := NewCollapser(held, NewCategorizer())
go func() {
collapse.Forward(res, req)
}()
<-held.begin
_, ok := collapse.running[req.URL.String()]
assert.True(t, ok)
})
n.It("reuses a ongoing request if possible", func() {
req, err := http.NewRequest("GET", "http://google.com/foo/bar", nil)
require.NoError(t, err)
parentRes := newRecordingSender()
childRes := newRecordingSender()
rr := &RunningRequest{
Request: req,
others: []Responder{parentRes},
done: make(chan struct{}),
}
collapse.running[req.URL.String()] = rr
c := make(chan struct{})
go func() {
collapse.Forward(childRes, req)
c <- struct{}{}
}()
// simulate waiting on the upstream
time.Sleep(10 * time.Millisecond)
upres := &http.Response{
Status: "200 OK",
StatusCode: 200,
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
Header: http.Header{"X-Templar-Check": []string{"ok"}},
Body: nil,
Request: req,
}
io := collapse.finish(req, upres, rr)
CopyBody(io, upres.Body)
select {
case <-time.NewTimer(1 * time.Second).C:
t.Fatal()
case <-c:
// all good
}
assert.Equal(t, childRes.w.Code, 200)
assert.Equal(t, "ok", childRes.w.Header().Get("X-Templar-Check"))
})
n.It("does not collapse stateful requests", func() {
req, err := http.NewRequest("POST", "http://google.com/foo/bar", nil)
require.NoError(t, err)
res := newRecordingSender()
client.On("Forward", mock.Anything, req).Return(nil)
collapse.Forward(res, req)
assert.Equal(t, len(collapse.running), 0)
})
n.It("can collapse multiple requests into one", func() {
upstream := NewUpstream(&slowTransport{1}, &MockStats{})
collapse := NewCollapser(upstream, NewCategorizer())
var wg sync.WaitGroup
var responses []*recordingSender
for i := 0; i < 10; i++ {
req, err := http.NewRequest("GET", "http://google.com/foo/bar", nil)
require.NoError(t, err)
res := newRecordingSender()
responses = append(responses, res)
wg.Add(1)
go func(res Responder, req *http.Request) {
defer wg.Done()
collapse.Forward(res, req)
}(res, req)
}
wg.Wait()
first := responses[0].w.Body.String()
assert.True(t, len(first) != 0)
for _, resp := range responses {
assert.Equal(t, first, resp.w.Body.String())
}
})
n.Meow()
}