Skip to content

Commit

Permalink
writes RedisCacheEngine test
Browse files Browse the repository at this point in the history
  • Loading branch information
Arthur1 committed Jul 13, 2024
1 parent e61a38a commit e5d942e
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 1 deletion.
101 changes: 101 additions & 0 deletions cache/engine/rediscache/redis_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,102 @@
package rediscache

import (
"bufio"
"bytes"
"context"
"io/ioutil"

Check failure on line 7 in cache/engine/rediscache/redis_test.go

View workflow job for this annotation

GitHub Actions / test-go (stable) / lint

SA1019: "io/ioutil" has been deprecated since Go 1.19: As of Go 1.16, the same functionality is now provided by package [io] or package [os], and those implementations should be preferred in new code. See the specific function documentation for details. (staticcheck)

Check failure on line 7 in cache/engine/rediscache/redis_test.go

View workflow job for this annotation

GitHub Actions / test-go (oldstable) / lint

SA1019: "io/ioutil" has been deprecated since Go 1.19: As of Go 1.16, the same functionality is now provided by package [io] or package [os], and those implementations should be preferred in new code. See the specific function documentation for details. (staticcheck)
"net/http"
"testing"
"time"

"github.com/Arthur1/http-client-cache/cache/key"
"github.com/alicebob/miniredis/v2"
"github.com/go-redis/cache/v9"
"github.com/redis/go-redis/v9"
"github.com/stretchr/testify/assert"
)

type testKeyGenerator struct{}

func (g *testKeyGenerator) Key(_ *http.Request) (string, error) {
return "test", nil
}

func TestNew(t *testing.T) {
t.Parallel()
t.Run("Default", func(t *testing.T) {
t.Parallel()
redisCli := redis.NewClient(&redis.Options{})
e := New(redisCli)
assert.IsType(t, &key.DefaultKeyGenerator{}, e.keyGenerator)
assert.NotEmpty(t, e.redisCache)
})

t.Run("WithKeyGenerator", func(t *testing.T) {
t.Parallel()
redisCli := redis.NewClient(&redis.Options{})
keyGenerator := &testKeyGenerator{}
e := New(redisCli, WithKeyGenerator(keyGenerator))
assert.Equal(t, keyGenerator, e.keyGenerator)
})

t.Run("WithLocalCache", func(t *testing.T) {
t.Parallel()
redisCli := redis.NewClient(&redis.Options{})
localCache := cache.NewTinyLFU(10, time.Minute)
New(redisCli, WithLocalCache(localCache))
})
}

func TestCacheEngineKey(t *testing.T) {
t.Parallel()
redisCli := redis.NewClient(&redis.Options{})
keyGenerator := &testKeyGenerator{}
e := New(redisCli, WithKeyGenerator(keyGenerator))
got, err := e.Key(nil)
assert.NoError(t, err)
assert.Equal(t, "test", got)
}

func TestCacheEngineGetAndSet(t *testing.T) {
t.Parallel()
t.Run("cache miss", func(t *testing.T) {
t.Parallel()
rs, err := miniredis.Run()
if err != nil {
t.Fatal(err)
}
redisCli := redis.NewClient(&redis.Options{Addr: rs.Addr(), DB: 0})
e := New(redisCli)
ctx := context.Background()
req, _ := http.NewRequest(http.MethodGet, "https://example.com", nil)

_, ok, err := e.Get(ctx, "key1", req)
assert.False(t, ok)
assert.NoError(t, err)
})

t.Run("set and cache hit", func(t *testing.T) {
t.Parallel()
rs, err := miniredis.Run()
if err != nil {
t.Fatal(err)
}
redisCli := redis.NewClient(&redis.Options{Addr: rs.Addr(), DB: 0})
e := New(redisCli)
ctx := context.Background()
req, _ := http.NewRequest(http.MethodGet, "https://example.com", nil)
serializedResMock := []byte("HTTP/1.1 200 OK\nContent-Length: 3\n\nOK\n")
resMock, _ := http.ReadResponse(bufio.NewReader(bytes.NewReader(serializedResMock)), req)

err = e.Set(ctx, "key1", resMock, time.Hour)
assert.NoError(t, err)
res, ok, err := e.Get(ctx, "key1", req)
assert.NoError(t, err)
assert.True(t, ok)
assert.Equal(t, http.StatusOK, res.StatusCode)
resb, err := ioutil.ReadAll(res.Body)
assert.NoError(t, err)
assert.Equal(t, "OK\n", string(resb))
})
}
5 changes: 4 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ github.com/alicebob/gopher-json v0.0.0-20230218143504-906a9b012302 h1:uvdUDbHQHO
github.com/alicebob/gopher-json v0.0.0-20230218143504-906a9b012302/go.mod h1:SGnFV6hVsYE877CKEZ6tDNTjaSXYUk6QqoIK6PrAtcc=
github.com/alicebob/miniredis/v2 v2.33.0 h1:uvTF0EDeu9RLnUEG27Db5I68ESoIxTiXbNUiji6lZrA=
github.com/alicebob/miniredis/v2 v2.33.0/go.mod h1:MhP4a3EU7aENRi9aO+tHfTBZicLqQevyi/DJpoj6mi0=
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c=
github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0=
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
Expand Down Expand Up @@ -78,7 +82,6 @@ github.com/onsi/gomega v1.25.0 h1:Vw7br2PCDYijJHSfBOWhov+8cAnUf8MfMaIOV323l6Y=
github.com/onsi/gomega v1.25.0/go.mod h1:r+zV744Re+DiYCIPRlYOTxn0YkOLcAnW8k1xXdMPGhM=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/redis/go-redis/v9 v9.0.0-rc.4 h1:JUhsiZMTZknz3vn50zSVlkwcSeTGPd51lMO3IKUrWpY=
github.com/redis/go-redis/v9 v9.0.0-rc.4/go.mod h1:Vo3EsyWnicKnSKCA7HhgnvnyA74wOA69Cd2Meli5mmA=
github.com/redis/go-redis/v9 v9.5.4 h1:vOFYDKKVgrI5u++QvnMT7DksSMYg7Aw/Np4vLJLKLwY=
github.com/redis/go-redis/v9 v9.5.4/go.mod h1:hdY0cQFCN4fnSYT6TkisLufl/4W5UIXyv0b/CLO2V2M=
Expand Down

0 comments on commit e5d942e

Please sign in to comment.