-
Notifications
You must be signed in to change notification settings - Fork 0
/
replay_test.go
389 lines (334 loc) · 12.6 KB
/
replay_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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
package replay
import (
"bytes"
"fmt"
"io"
"log"
"net/http"
"net/http/httptest"
"os"
"strings"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
// Helper function to create an HTTP request with headers
func createRequest(url, method string, headers map[string]string) *http.Request {
req := httptest.NewRequest(method, url, nil)
for k, v := range headers {
req.Header.Add(k, v)
}
return req
}
// Helper function to create an HTTP response
func createResponse(statusCode int, body string, headers map[string]string) *http.Response {
resp := &http.Response{
StatusCode: statusCode,
Header: http.Header{},
Body: io.NopCloser(bytes.NewBufferString(body)),
ContentLength: int64(len(body)),
}
for k, v := range headers {
resp.Header.Add(k, v)
}
return resp
}
// Test initialization with default and custom configurations
func TestNewCache(t *testing.T) {
cache := NewCache()
assert.Equal(t, DefaultMaxSize, cache.maxSize)
assert.Equal(t, DefaultEvictionPolicy, cache.evictionPolicy)
assert.Equal(t, DefaultTTL, cache.ttl)
assert.Equal(t, []string{DefaultFilter}, cache.cacheFilters)
c := NewCache(
WithMaxSize(100), // Set the maximum number of entries in the cache
WithMaxMemory(100*1024*1024), // Set the maximum memory usage of the cache
WithCacheFilters([]string{"URL", "Method"}), // Set the filters used for generating cache keys
WithEvictionPolicy("LRU"), // Set the eviction policy for the cache [FIFO, LRU]
WithTTL(5*time.Minute), // Set the time a cache entry can live without being accessed
WithMaxTTL(30*time.Minute), // Set the maximum time a cache entry can live, including renewals
WithLogger(log.New(os.Stdout, "replay: ", log.LstdFlags)), // Set the logger, by default logs will be dropped
)
assert.Equal(t, 100, c.maxSize)
assert.Equal(t, EvictionPolicy("LRU"), c.evictionPolicy)
assert.Equal(t, 5*time.Minute, c.ttl)
assert.Equal(t, []string{"URL", "Method"}, c.cacheFilters)
}
// Test key generation
func TestGenerateKey(t *testing.T) {
cache := NewCache(WithCacheFilters([]string{"Method", "Header"}))
req := createRequest("http://example.com", "GET", map[string]string{"X-Test-Header": "test"})
expectedKey := "http://example.com|GET|X-Test-Header=test"
generatedKey := cache.generateKey(req)
assert.Equal(t, expectedKey, generatedKey)
}
// Test caching and retrieving responses
func TestCacheMiddleware(t *testing.T) {
cache := NewCache(
WithMaxSize(2),
WithEvictionPolicy("FIFO"),
WithTTL(1*time.Minute),
WithCacheFilters([]string{}),
WithLogger(log.New(os.Stdout, "replay-test: ", log.LstdFlags)),
)
handler := cache.Middleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("response"))
}))
// First request should generate a cache entry
req := httptest.NewRequest("GET", "http://example.com", nil)
rr := httptest.NewRecorder()
handler.ServeHTTP(rr, req)
assert.Equal(t, http.StatusOK, rr.Code)
assert.Equal(t, "response", rr.Body.String())
// Second request should be served from cache
rr2 := httptest.NewRecorder()
handler.ServeHTTP(rr2, req)
assert.Equal(t, http.StatusOK, rr2.Code)
assert.Equal(t, "response", rr2.Body.String())
}
// Test cache expiration
func TestCacheExpiration(t *testing.T) {
cache := NewCache(
WithMaxSize(1),
WithEvictionPolicy("FIFO"),
WithTTL(1*time.Second),
WithCacheFilters([]string{}),
WithLogger(log.New(os.Stdout, "replay-test: ", log.LstdFlags)),
)
handler := cache.Middleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("response"))
}))
req := httptest.NewRequest("GET", "http://example.com", nil)
rr := httptest.NewRecorder()
// First request should generate a cache entry
handler.ServeHTTP(rr, req)
assert.Equal(t, http.StatusOK, rr.Code)
// Sleep to let the cache expire
time.Sleep(2 * time.Second)
// Second request should miss the cache
rr2 := httptest.NewRecorder()
handler.ServeHTTP(rr2, req)
assert.Equal(t, http.StatusOK, rr2.Code)
}
// Test FIFO eviction policy
func TestFIFOCacheEviction(t *testing.T) {
cache := NewCache(
WithMaxSize(2),
WithEvictionPolicy("FIFO"),
WithTTL(1*time.Minute),
WithCacheFilters([]string{}),
WithLogger(log.New(os.Stdout, "replay-test: ", log.LstdFlags)),
)
handler := cache.Middleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("response-" + r.URL.Path))
}))
// Add two entries to the cache
req1 := httptest.NewRequest("GET", "http://example.com/1", nil)
rr1 := httptest.NewRecorder()
handler.ServeHTTP(rr1, req1)
assert.Equal(t, http.StatusOK, rr1.Code)
req2 := httptest.NewRequest("GET", "http://example.com/2", nil)
rr2 := httptest.NewRecorder()
handler.ServeHTTP(rr2, req2)
assert.Equal(t, http.StatusOK, rr2.Code)
// Add third entry, forcing eviction of the first one (FIFO)
req3 := httptest.NewRequest("GET", "http://example.com/3", nil)
rr3 := httptest.NewRecorder()
handler.ServeHTTP(rr3, req3)
assert.Equal(t, http.StatusOK, rr3.Code)
}
// Test LRU eviction policy
func TestLRUCacheEviction(t *testing.T) {
cache := NewCache(
WithMaxSize(2),
WithEvictionPolicy("LRU"),
WithTTL(1*time.Minute),
WithCacheFilters([]string{}),
WithLogger(log.New(os.Stdout, "replay-test: ", log.LstdFlags)),
)
handler := cache.Middleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("response-" + r.URL.Path))
}))
// Add two entries to the cache
req1 := httptest.NewRequest("GET", "http://example.com/1?test=oh-yeah", nil)
rr1 := httptest.NewRecorder()
handler.ServeHTTP(rr1, req1)
assert.Equal(t, http.StatusOK, rr1.Code)
req2 := httptest.NewRequest("GET", "http://example.com/2", nil)
rr2 := httptest.NewRecorder()
handler.ServeHTTP(rr2, req2)
assert.Equal(t, http.StatusOK, rr2.Code)
// Access the first entry to make it recently used
rr3 := httptest.NewRecorder()
handler.ServeHTTP(rr3, req1)
assert.Equal(t, http.StatusOK, rr3.Code)
assert.Equal(t, "response-/1", rr3.Body.String())
// Add third entry, forcing eviction of the least recently used (req2)
req3 := httptest.NewRequest("GET", "http://example.com/3", nil)
rr4 := httptest.NewRecorder()
handler.ServeHTTP(rr4, req3)
assert.Equal(t, http.StatusOK, rr4.Code)
// Second entry should be evicted
rr5 := httptest.NewRecorder()
handler.ServeHTTP(rr5, req2)
assert.Equal(t, http.StatusOK, rr5.Code)
assert.Equal(t, "response-/2", rr5.Body.String()) // Fresh response as it should be evicted
}
// Test concurrency
func TestCacheConcurrency(t *testing.T) {
cache := NewCache(
WithMaxSize(100),
WithEvictionPolicy("LRU"),
WithTTL(1*time.Minute),
WithCacheFilters([]string{}),
WithLogger(log.New(os.Stdout, "replay-test: ", log.LstdFlags)),
)
handler := cache.Middleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("response"))
}))
var wg sync.WaitGroup
for i := 0; i < 100; i++ {
wg.Add(1)
go func(n int) {
defer wg.Done()
req := httptest.NewRequest("GET", "http://example.com/"+fmt.Sprintf("%d", n), nil)
rr := httptest.NewRecorder()
handler.ServeHTTP(rr, req)
assert.Equal(t, http.StatusOK, rr.Code)
}(i)
}
wg.Wait()
}
// Test cache with different headers, methods, and URL params
func TestCacheWithDifferentRequests(t *testing.T) {
cache := NewCache(
WithMaxSize(100),
WithEvictionPolicy("LRU"),
WithTTL(1*time.Minute),
WithCacheFilters([]string{"Method", "Header"}),
WithLogger(log.New(os.Stdout, "replay-test: ", log.LstdFlags)),
)
handler := cache.Middleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("response"))
}))
req := createRequest("http://example.com", "GET", map[string]string{"Content-Type": "application/json"})
rr := httptest.NewRecorder()
handler.ServeHTTP(rr, req)
assert.Equal(t, http.StatusOK, rr.Code)
assert.Equal(t, "response", rr.Body.String())
req2 := createRequest("http://example.com", "POST", map[string]string{"Content-Type": "application/json"})
rr2 := httptest.NewRecorder()
handler.ServeHTTP(rr2, req2)
assert.Equal(t, http.StatusOK, rr2.Code)
assert.Equal(t, "response", rr2.Body.String())
req3 := createRequest("http://example.com", "GET", map[string]string{"Content-Type": "text/plain"})
rr3 := httptest.NewRecorder()
handler.ServeHTTP(rr3, req3)
assert.Equal(t, http.StatusOK, rr3.Code)
assert.Equal(t, "response", rr3.Body.String())
req4 := createRequest("http://example.com/different", "GET", map[string]string{"Content-Type": "application/json"})
rr4 := httptest.NewRecorder()
handler.ServeHTTP(rr4, req4)
assert.Equal(t, http.StatusOK, rr4.Code)
assert.Equal(t, "response", rr4.Body.String())
}
func TestMaxMemoryEviction(t *testing.T) {
cache := NewCache(
WithMaxMemory(5*1024*1024), // 5 MB
WithTTL(1*time.Minute),
WithCacheFilters([]string{"URL"}),
WithLogger(log.New(os.Stdout, "replay-test: ", log.LstdFlags)),
)
handler := cache.Middleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte(strings.Repeat("a", 2*1024*1024))) // ~2MB total response
}))
// First request should generate a cache entry
req1 := httptest.NewRequest("GET", "http://example.com/1", nil)
rr1 := httptest.NewRecorder()
handler.ServeHTTP(rr1, req1)
assert.Equal(t, http.StatusOK, rr1.Code)
assert.Equal(t, 2*1024*1024, rr1.Body.Len())
// Second request should generate another cache entry
req2 := httptest.NewRequest("GET", "http://example.com/2", nil)
rr2 := httptest.NewRecorder()
handler.ServeHTTP(rr2, req2)
assert.Equal(t, http.StatusOK, rr2.Code)
assert.Equal(t, 2*1024*1024, rr2.Body.Len())
// Third request should force eviction due to memory limit
req3 := httptest.NewRequest("GET", "http://example.com/3", nil)
rr3 := httptest.NewRecorder()
handler.ServeHTTP(rr3, req3)
assert.Equal(t, http.StatusOK, rr3.Code)
assert.Equal(t, 2*1024*1024, rr3.Body.Len())
time.Sleep(2 * time.Second)
// Ensure one of the previous entries is evicted
assert.True(t, cache.cacheList.Len() == 2, "One of the previous entries should be evicted from the cache")
}
// Test MaxSize eviction policy with limit of 5 entries
func TestMaxSizeEviction(t *testing.T) {
cache := NewCache(
WithMaxSize(5),
WithEvictionPolicy("FIFO"),
WithTTL(1*time.Minute),
WithCacheFilters([]string{"URL"}),
WithLogger(log.New(os.Stdout, "replay-test: ", log.LstdFlags)),
)
handler := cache.Middleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("response" + r.URL.Path))
}))
// Add up to the max size
for i := 0; i < 5; i++ {
req := httptest.NewRequest("GET", fmt.Sprintf("http://example.com/%d", i), nil)
rr := httptest.NewRecorder()
handler.ServeHTTP(rr, req)
assert.Equal(t, http.StatusOK, rr.Code)
assert.Equal(t, fmt.Sprintf("response/%d", i), rr.Body.String())
}
// Add one more to trigger eviction
req := httptest.NewRequest("GET", "http://example.com/5", nil)
rr := httptest.NewRecorder()
handler.ServeHTTP(rr, req)
assert.Equal(t, http.StatusOK, rr.Code)
assert.Equal(t, "response/5", rr.Body.String())
time.Sleep(2 * time.Second)
// The first entry should be evicted (FIFO)
reqFirst := httptest.NewRequest("GET", "http://example.com/0", nil)
rrFirst := httptest.NewRecorder()
handler.ServeHTTP(rrFirst, reqFirst)
assert.Equal(t, http.StatusOK, rrFirst.Code)
assert.Equal(t, "response/0", rrFirst.Body.String()) // Fresh response as it should be evicted
}
func TestCacheFailures(t *testing.T) {
cache := NewCache(
WithMaxSize(100),
WithEvictionPolicy("LRU"),
WithTTL(1*time.Minute),
WithCacheFilters([]string{}),
WithCacheFailures(true),
WithLogger(log.New(os.Stdout, "replay-test: ", log.LstdFlags)),
)
handler := cache.Middleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
}))
req := httptest.NewRequest("GET", "http://bad_url.com", nil)
rr := httptest.NewRecorder()
handler.ServeHTTP(rr, req)
assert.Equal(t, http.StatusInternalServerError, rr.Code)
assert.Equal(t, "Internal Server Error\n", rr.Body.String())
time.Sleep(2 * time.Second)
// Second request should be served from cache
rr2 := httptest.NewRecorder()
handler.ServeHTTP(rr2, req)
assert.Equal(t, http.StatusInternalServerError, rr2.Code)
assert.Equal(t, "Internal Server Error\n", rr2.Body.String())
assert.True(t, cache.cacheList.Len() == 1, "Cache should have one entry")
}