Skip to content

Commit

Permalink
chore: fix storage cacher tests (#1916)
Browse files Browse the repository at this point in the history
* chore: why are you changing go.work.sum

* chore: fixup cacher tests into new eval-v2 branch

* chore: rm unknown file
  • Loading branch information
markphelps committed Jul 28, 2023
1 parent 3a036c5 commit 5104363
Show file tree
Hide file tree
Showing 4 changed files with 344 additions and 252 deletions.
1 change: 1 addition & 0 deletions go.work.sum
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,7 @@ github.com/prometheus/common v0.9.1/go.mod h1:yhUN8i9wzaXS3w1O07YhxHEBxD+W35wd8b
github.com/prometheus/common v0.42.0/go.mod h1:xBwqVerjNdUDjgODMpudtOMwlOwf2SaTr1yjz4b7Zbc=
github.com/prometheus/procfs v0.0.8/go.mod h1:7Qr8sr6344vo1JqZ6HhLceV9o3AJ1Ff+GxbHq6oeK9A=
github.com/rogpeppe/go-charset v0.0.0-20180617210344-2471d30d28b4/go.mod h1:qgYeAmZ5ZIpBWTGllZSQnw97Dj+woV0toclVaRGI8pc=
github.com/russross/blackfriday v1.6.0 h1:KqfZb0pUVN2lYqZUYRddxF4OR8ZMURnJIG5Y3VRLtww=
github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
github.com/sagikazarmark/crypt v0.10.0/go.mod h1:gwTNHQVoOS3xp9Xvz5LLR+1AauC5M6880z5NWzdhOyQ=
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
Expand Down
108 changes: 64 additions & 44 deletions internal/storage/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,78 +5,98 @@ import (
"errors"
"testing"

"github.com/stretchr/testify/require"
"github.com/stretchr/testify/assert"
"go.flipt.io/flipt/internal/storage"
"go.uber.org/zap/zaptest"
)

func TestSetCacheHandleMarshalError(t *testing.T) {
store := &mockStore{}
cacher := &mockCacher{}
logger := zaptest.NewLogger(t)
cachedStore, _ := New(store, cacher, logger).(*Store)
var (
store = &storeMock{}
cacher = &cacheSpy{}
logger = zaptest.NewLogger(t)
cachedStore, _ = New(store, cacher, logger).(*Store)
)

cachedStore.setCache(context.TODO(), "key", make(chan int))
require.Equal(t, "", cacher.CacheKey)
assert.Empty(t, cacher.cacheKey)
}

func TestGetCacheHandleGetError(t *testing.T) {
store := &mockStore{}
cacher := &mockCacher{GetErr: errors.New("get error")}
logger := zaptest.NewLogger(t)
cachedStore, _ := New(store, cacher, logger).(*Store)
var (
store = &storeMock{}
cacher = &cacheSpy{getErr: errors.New("get error")}
logger = zaptest.NewLogger(t)
cachedStore, _ = New(store, cacher, logger).(*Store)
)

value := make(map[string]string)
cacheHit := cachedStore.getCache(context.TODO(), "key", &value)
require.False(t, cacheHit)
assert.False(t, cacheHit)
}

func TestGetCacheHandleUnmarshalError(t *testing.T) {
store := &mockStore{}
cacher := &mockCacher{
Cached: true,
CachedValue: []byte(`{"invalid":"123"`),
}
logger := zaptest.NewLogger(t)
cachedStore, _ := New(store, cacher, logger).(*Store)
var (
store = &storeMock{}
cacher = &cacheSpy{
cached: true,
cachedValue: []byte(`{"invalid":"123"`),
}
logger = zaptest.NewLogger(t)
cachedStore, _ = New(store, cacher, logger).(*Store)
)

value := make(map[string]string)
cacheHit := cachedStore.getCache(context.TODO(), "key", &value)
require.False(t, cacheHit)
assert.False(t, cacheHit)
}

func TestGetEvaluationRules(t *testing.T) {
expectedRules := []*storage.EvaluationRule{{ID: "123"}}
store := &mockStore{
EvaluationRules: expectedRules,
}
cacher := &mockCacher{}
logger := zaptest.NewLogger(t)
cachedStore := New(store, cacher, logger)
var (
expectedRules = []*storage.EvaluationRule{{ID: "123"}}
store = &storeMock{}
)

store.On("GetEvaluationRules", context.TODO(), "ns", "flag-1").Return(
expectedRules, nil,
)

var (
cacher = &cacheSpy{}
logger = zaptest.NewLogger(t)
cachedStore = New(store, cacher, logger)
)

rules, err := cachedStore.GetEvaluationRules(context.TODO(), "ns", "flag-1")
require.Nil(t, err)
require.Equal(t, expectedRules, rules)
assert.Nil(t, err)
assert.Equal(t, expectedRules, rules)

require.Equal(t, "s:er:ns:flag-1", cacher.CacheKey)
require.Equal(t, []byte(`[{"id":"123"}]`), cacher.CachedValue)
assert.Equal(t, "s:er:ns:flag-1", cacher.cacheKey)
assert.Equal(t, []byte(`[{"id":"123"}]`), cacher.cachedValue)
}

func TestGetEvaluationRulesCached(t *testing.T) {
expectedRules := []*storage.EvaluationRule{{ID: "123"}}
store := &mockStore{
EvaluationRules: []*storage.EvaluationRule{{ID: "543"}},
}
cacher := &mockCacher{
Cached: true,
CachedValue: []byte(`[{"id":"123"}]`),
}
logger := zaptest.NewLogger(t)
cachedStore := New(store, cacher, logger)
var (
expectedRules = []*storage.EvaluationRule{{ID: "123"}}
store = &storeMock{}
)

rules, err := cachedStore.GetEvaluationRules(context.TODO(), "ns", "flag-1")
require.Nil(t, err)
require.Equal(t, expectedRules, rules)
store.On("GetEvaluationRules", context.TODO(), "ns", "flag-1").Return(
expectedRules, nil,
)

require.Equal(t, "s:er:ns:flag-1", cacher.CacheKey)
var (
cacher = &cacheSpy{
cached: true,
cachedValue: []byte(`[{"id":"123"}]`),
}

logger = zaptest.NewLogger(t)
cachedStore = New(store, cacher, logger)
)

rules, err := cachedStore.GetEvaluationRules(context.TODO(), "ns", "flag-1")
assert.Nil(t, err)
assert.Equal(t, expectedRules, rules)
assert.Equal(t, "s:er:ns:flag-1", cacher.cacheKey)
}
208 changes: 0 additions & 208 deletions internal/storage/cache/mocks_test.go

This file was deleted.

Loading

0 comments on commit 5104363

Please sign in to comment.