-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathstampede_test.go
292 lines (230 loc) · 6.2 KB
/
stampede_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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package stampede_test
import (
"context"
"io"
"log"
"net/http"
"net/http/httptest"
"runtime"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/go-chi/cors"
"github.com/go-chi/stampede"
"github.com/stretchr/testify/assert"
)
func TestGet(t *testing.T) {
var count uint64
cache := stampede.NewCache(512, time.Duration(2*time.Second), time.Duration(5*time.Second))
// repeat test multiple times
for x := 0; x < 5; x++ {
// time.Sleep(1 * time.Second)
var wg sync.WaitGroup
numGoroutines := runtime.NumGoroutine()
n := 10
ctx := context.Background()
for i := 0; i < n; i++ {
t.Logf("numGoroutines now %d", runtime.NumGoroutine())
wg.Add(1)
go func() {
defer wg.Done()
val, err := cache.Get(ctx, "t1", func() (any, error) {
t.Log("cache.Get(t1, ...)")
// some extensive op..
time.Sleep(2 * time.Second)
atomic.AddUint64(&count, 1)
return "result1", nil
})
assert.NoError(t, err)
assert.Equal(t, "result1", val)
}()
}
wg.Wait()
// ensure single call
assert.Equal(t, uint64(1), count)
// confirm same before/after num of goroutines
t.Logf("numGoroutines now %d", runtime.NumGoroutine())
assert.Equal(t, numGoroutines, runtime.NumGoroutine())
}
}
func TestHandler(t *testing.T) {
var numRequests = 30
var hits uint32
var expectedStatus int = 201
var expectedBody = []byte("hi")
app := func(w http.ResponseWriter, r *http.Request) {
// log.Println("app handler..")
atomic.AddUint32(&hits, 1)
hitsNow := atomic.LoadUint32(&hits)
if hitsNow > 1 {
// panic("uh oh")
}
// time.Sleep(100 * time.Millisecond) // slow handler
w.Header().Set("X-Httpjoin", "test")
w.WriteHeader(expectedStatus)
w.Write(expectedBody)
}
var count uint32
counter := func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
atomic.AddUint32(&count, 1)
next.ServeHTTP(w, r)
atomic.AddUint32(&count, ^uint32(0))
// log.Println("COUNT:", atomic.LoadUint32(&count))
})
}
recoverer := func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer func() {
if r := recover(); r != nil {
log.Println("recovered panicing request:", r)
}
}()
next.ServeHTTP(w, r)
})
}
h := stampede.Handler(512, 1*time.Second)
ts := httptest.NewServer(counter(recoverer(h(http.HandlerFunc(app)))))
defer ts.Close()
var wg sync.WaitGroup
for i := 0; i < numRequests; i++ {
wg.Add(1)
go func() {
defer wg.Done()
resp, err := http.Get(ts.URL)
if err != nil {
t.Fatal(err)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
// log.Println("got resp:", resp, "len:", len(body), "body:", string(body))
if string(body) != string(expectedBody) {
t.Error("expecting response body:", string(expectedBody))
}
if resp.StatusCode != expectedStatus {
t.Error("expecting response status:", expectedStatus)
}
assert.Equal(t, "test", resp.Header.Get("X-Httpjoin"), "expecting x-httpjoin test header")
}()
}
wg.Wait()
totalHits := atomic.LoadUint32(&hits)
// if totalHits > 1 {
// t.Error("handler was hit more than once. hits:", totalHits)
// }
log.Println("total hits:", totalHits)
finalCount := atomic.LoadUint32(&count)
if finalCount > 0 {
t.Error("queue count was expected to be empty, but count:", finalCount)
}
log.Println("final count:", finalCount)
}
func TestHash(t *testing.T) {
h1 := stampede.BytesToHash([]byte{1, 2, 3})
assert.Equal(t, uint64(8376154270085342629), h1)
h2 := stampede.StringToHash("123")
assert.Equal(t, uint64(4353148100880623749), h2)
}
func TestBypassCORSHeaders(t *testing.T) {
var expectedStatus int = 200
var expectedBody = []byte("hi")
var count uint64
domains := []string{
"google.com",
"sequence.build",
"horizon.io",
"github.com",
"ethereum.org",
}
app := func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("X-Another-Header", "wakka")
w.WriteHeader(expectedStatus)
w.Write(expectedBody)
atomic.AddUint64(&count, 1)
}
h := stampede.Handler(512, 1*time.Second)
c := cors.New(cors.Options{
AllowedOrigins: domains,
AllowedMethods: []string{"GET"},
AllowedHeaders: []string{"*"},
}).Handler
ts := httptest.NewServer(c(h(http.HandlerFunc(app))))
defer ts.Close()
var mu sync.Mutex
for i := 0; i < 10; i++ {
var wg sync.WaitGroup
var domainsHit = map[string]bool{}
for _, domain := range domains {
wg.Add(1)
go func(domain string) {
defer wg.Done()
req, err := http.NewRequest("GET", ts.URL, nil)
assert.NoError(t, err)
req.Header.Set("Origin", domain)
resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatal(err)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
if string(body) != string(expectedBody) {
t.Error("expecting response body:", string(expectedBody))
}
if resp.StatusCode != expectedStatus {
t.Error("expecting response status:", expectedStatus)
}
mu.Lock()
domainsHit[resp.Header.Get("Access-Control-Allow-Origin")] = true
mu.Unlock()
assert.Equal(t, "wakka", resp.Header.Get("X-Another-Header"))
}(domain)
}
wg.Wait()
// expect all domains to be returned and recorded in domainsHit
for _, domain := range domains {
assert.True(t, domainsHit[domain])
}
// expect to have only one actual hit
assert.Equal(t, uint64(1), count)
}
}
func TestPanic(t *testing.T) {
mux := http.NewServeMux()
middleware := stampede.Handler(100, 1*time.Hour)
mux.Handle("/", middleware(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
t.Log(r.Method, r.URL)
})))
ts := httptest.NewServer(mux)
defer ts.Close()
{
req, err := http.NewRequest(http.MethodGet, ts.URL, nil)
if err != nil {
t.Fatal(err)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
t.Log(resp.StatusCode)
}
{
req, err := http.NewRequest(http.MethodGet, ts.URL, nil)
if err != nil {
t.Fatal(err)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
t.Log(resp.StatusCode)
}
}