Skip to content

Commit bb26e6c

Browse files
authored
Feature/integrate influxdata (#29)
* add more tests for cache * fix cache's exipration mechanism broken and add purge key test
1 parent f37a969 commit bb26e6c

File tree

16 files changed

+539
-83
lines changed

16 files changed

+539
-83
lines changed

backends/redis_test.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,22 @@ func (suite *RedisBackendTestSuite) SetupSuite() {
2424
log.Fatal(err)
2525
}
2626

27-
resource, err := pool.Run("redis", "5.0.9", []string{})
27+
redisVersion := "5.0.9"
28+
resource, err := pool.Run("redis", redisVersion, []string{})
2829
if err != nil {
2930
log.Fatal(err)
3031
}
3132

3233
suite.pool = pool
3334
suite.resource = resource
34-
3535
port := resource.GetPort("6379/tcp")
36-
InitRedisClient(fmt.Sprintf("localhost:%s", port), "", 0)
36+
37+
err = suite.pool.Retry(func() error {
38+
return InitRedisClient(fmt.Sprintf("localhost:%s", port), "", 0)
39+
})
40+
if err != nil {
41+
log.Fatal(err)
42+
}
3743
}
3844

3945
func (suite *RedisBackendTestSuite) TestParseRedisConfig() {

cache.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ var (
3131
)
3232

3333
// intend to mock for test
34-
var now = time.Now().UTC
34+
var now = func() time.Time { return time.Now().UTC() }
3535

3636
// RuleMatcherType specifies the type of matching rule to cache.
3737
type RuleMatcherType string
@@ -431,7 +431,7 @@ func (h *HTTPCache) Get(key string, request *http.Request) (*Entry, bool) {
431431
return nil, false
432432
}
433433

434-
// Keys list the keys holded by this cache
434+
// Keys list the keys holden by this cache
435435
func (h *HTTPCache) Keys() []string {
436436
keys := []string{}
437437
for index, l := range h.entriesLock {

cache_test.go

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,15 +202,54 @@ func (suite *EntryTestSuite) TearDownSuite() {
202202
type HTTPCacheTestSuite struct {
203203
suite.Suite
204204
config *Config
205+
cache *HTTPCache
205206
}
206207

207208
func (suite *HTTPCacheTestSuite) SetupSuite() {
208209
err := backends.InitGroupCacheRes(50 * 1024 * 1024)
209210
suite.Nil(err)
210211
suite.config = getDefaultConfig()
212+
suite.cache = NewHTTPCache(suite.config)
211213
}
212214

213-
// TODO: add http cache test
215+
func (suite *HTTPCacheTestSuite) TestGetNonExistEntry() {
216+
req := makeRequest("/", http.Header{})
217+
entry, exists := suite.cache.Get("abc", req)
218+
suite.Nil(entry)
219+
suite.False(exists)
220+
}
221+
222+
func (suite *HTTPCacheTestSuite) TestGetExistEntry() {
223+
req := makeRequest("/", http.Header{})
224+
res := makeResponse(200, http.Header{})
225+
entry := NewEntry("hello", req, res, suite.config)
226+
suite.cache.Put(req, entry)
227+
228+
prevEntry, exists := suite.cache.Get("hello", req)
229+
suite.Equal(prevEntry, entry)
230+
suite.True(exists)
231+
}
232+
233+
func (suite *HTTPCacheTestSuite) TestCleanEntry() {
234+
req := makeRequest("/", http.Header{})
235+
res := makeResponse(200, http.Header{})
236+
key := "friday"
237+
238+
entry := NewEntry(key, req, res, suite.config)
239+
suite.cache.Put(req, entry)
240+
241+
keyInKeys := false
242+
keys := suite.cache.Keys()
243+
for _, k := range keys {
244+
if k == key {
245+
keyInKeys = true
246+
}
247+
}
248+
suite.True(keyInKeys)
249+
250+
err := suite.cache.Del(key)
251+
suite.Nil(err)
252+
}
214253

215254
func (suite *HTTPCacheTestSuite) TearDownSuite() {
216255
err := backends.ReleaseGroupCacheRes()
@@ -219,6 +258,7 @@ func (suite *HTTPCacheTestSuite) TearDownSuite() {
219258

220259
func TestCacheStatusTestSuite(t *testing.T) {
221260
suite.Run(t, new(CacheStatusTestSuite))
261+
suite.Run(t, new(HTTPCacheTestSuite))
222262
suite.Run(t, new(RuleMatcherTestSuite))
223263
suite.Run(t, new(EntryTestSuite))
224264
}

caddyfile.go

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -250,13 +250,6 @@ func (h *Handler) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
250250

251251
h.DistributedRaw = caddyconfig.JSONModuleObject(unm, "distributed", "consul", nil)
252252

253-
// case keyInfluxLog:
254-
// raw, err := setupInfluxLog(keyInfluxLog, d, args)
255-
// if err != nil {
256-
// return err
257-
// }
258-
// h.InfluxLogRaw = raw
259-
260253
default:
261254
return d.Err("Unknown cache parameter: " + parameter)
262255
}
@@ -268,28 +261,6 @@ func (h *Handler) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
268261
return nil
269262
}
270263

271-
func setupInfluxLog(key string, d *caddyfile.Dispenser, args []string) (json.RawMessage, error) {
272-
273-
mod, err := caddy.GetModule("caddy.logging.writers." + key)
274-
if err != nil {
275-
return nil, d.Errf("getting influxlog module '%s': '%v'", mod, err)
276-
}
277-
278-
unm, ok := mod.New().(caddyfile.Unmarshaler)
279-
if !ok {
280-
return nil, d.Errf("influxlog module '%s' is not a Caddyfile unmarshaler", mod)
281-
}
282-
283-
err = unm.UnmarshalCaddyfile(d.NewFromNextSegment())
284-
if err != nil {
285-
return nil, err
286-
}
287-
288-
raw := caddyconfig.JSONModuleObject(unm, "mylog", "influxlog", nil)
289-
290-
return raw, nil
291-
}
292-
293264
// Interface guards
294265
var (
295266
_ caddyfile.Unmarshaler = (*Handler)(nil)

purge.go renamed to endpoint.go

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"sync"
1212

1313
"github.com/caddyserver/caddy/v2"
14+
"github.com/sillygod/cdp-cache/pkg/helper"
1415
)
1516

1617
var (
@@ -65,8 +66,8 @@ func (p *PurgePayload) parseURI() {
6566

6667
func (p *PurgePayload) pruneHost() {
6768

68-
if strings.HasPrefix(p.Host, "http") {
69-
p.Host = strings.Split(p.Host, ":")[1]
69+
if strings.HasPrefix(p.Host, "http") || strings.HasPrefix(p.Host, "https") {
70+
p.Host = strings.Split(p.Host, "//")[1]
7071
}
7172

7273
if !strings.HasSuffix(p.Host, "/") {
@@ -120,8 +121,8 @@ func (c cachePurge) Routes() []caddy.AdminRoute {
120121
Handler: caddy.AdminHandlerFunc(c.handlePurge),
121122
},
122123
{
123-
Pattern: "/caches",
124-
Handler: caddy.AdminHandlerFunc(c.handleListCacheKeys),
124+
Pattern: "/caches/",
125+
Handler: caddy.AdminHandlerFunc(c.handleCacheEndpoints),
125126
},
126127
}
127128
}
@@ -132,7 +133,43 @@ func health(w http.ResponseWriter, r *http.Request) error {
132133
return nil
133134
}
134135

136+
func (c cachePurge) handleShowCache(w http.ResponseWriter, r *http.Request) error {
137+
var err error
138+
139+
if r.Method != http.MethodGet {
140+
return caddy.APIError{
141+
Code: http.StatusMethodNotAllowed,
142+
Err: fmt.Errorf("method not allowed"),
143+
}
144+
}
145+
146+
key := helper.TrimBy(r.URL.Path, "/", 2)
147+
cache := getHandlerCache()
148+
149+
entry, exists := cache.Get(key, r)
150+
if exists {
151+
err = entry.WriteBodyTo(w)
152+
}
153+
154+
return err
155+
}
156+
157+
func (c cachePurge) handleCacheEndpoints(w http.ResponseWriter, r *http.Request) error {
158+
// a workaround for handling the wildcard endpoint. Caddy uses the standard library's mux
159+
// so it doesn't support this natively.
160+
161+
path := r.URL.Path
162+
163+
switch path {
164+
case "/caches/":
165+
return c.handleListCacheKeys(w, r)
166+
default:
167+
return c.handleShowCache(w, r)
168+
}
169+
}
170+
135171
func (c cachePurge) handleListCacheKeys(w http.ResponseWriter, r *http.Request) error {
172+
136173
if r.Method != http.MethodGet {
137174
return caddy.APIError{
138175
Code: http.StatusMethodNotAllowed,

0 commit comments

Comments
 (0)