mirror of
https://github.com/zitadel/zitadel.git
synced 2025-12-01 18:42:16 +00:00
feat: project cache (#48)
* feat: eventstore repository * fix: remove gorm * version * feat: pkg * feat: add some files for project * feat: eventstore without eventstore-lib * rename files * gnueg * fix: key json * fix: add object * fix: change imports * fix: internal models * fix: some imports * fix: global model * fix: add some functions on repo * feat(eventstore): sdk * fix(eventstore): search query * fix(eventstore): rename app to eventstore * delete empty test * remove unused func * merge master * fix(eventstore): tests * fix(models): delete unused struct * fix: some funcitons * feat(eventstore): implemented push events * fix: move project eventstore to project package * fix: change project eventstore funcs * feat(eventstore): overwrite context data * fix: change project eventstore * fix: add project repo to mgmt server * feat(types): SQL-config * fix: commented code * feat(eventstore): options to overwrite editor * feat: auth interceptor and cockroach migrations * fix: migrations * fix: fix filter * fix: not found on getbyid * fix: add sequence * fix: add some tests * fix(eventstore): nullable sequence * fix: add some tests * merge * fix: add some tests * fix(migrations): correct statements for sequence * fix: add some tests * fix: add some tests * fix: changes from mr * Update internal/eventstore/models/field.go Co-Authored-By: livio-a <livio.a@gmail.com> * fix(eventstore): code quality * fix: add types to aggregate/Event-types * fix(eventstore): rename modifier* to editor* * fix(eventstore): delete editor_org * fix(migrations): remove editor_org field, rename modifier_* to editor_* * fix: generate files * fix(eventstore): tests * fix(eventstore): rename modifier to editor * fix(migrations): add cluster migration, fix(migrations): fix typo of host in clean clsuter * fix(eventstore): move health * fix(eventstore): AggregateTypeFilter aggregateType as param * code quality * feat: add member funcs * feat: add member model * feat: add member events * feat: add member repo model * fix: project member funcs * fix: add tests * fix: add tests * feat: implement member requests * fix: merge master * fix: read existing in project repo * fix: fix tests * feat: add internal cache * feat: add cache mock * fix: return values of cache mock * fix: add cache config * fix: use eventstore sdk * Update internal/project/model/project_member.go Co-Authored-By: Silvan <silvan.reusser@gmail.com> * fix: use get project func * fix: return err not nil * fix: change err types * Update internal/cache/bigcache/cache.go Co-Authored-By: livio-a <livio.a@gmail.com> * Update internal/cache/config/config.go Co-Authored-By: livio-a <livio.a@gmail.com> * Update internal/cache/config/config.go Co-Authored-By: livio-a <livio.a@gmail.com> * fix: config * fix: config * resolve conversations * fix: mr changes * fix: fix decode of bigcache * feat: test caches * fix: remove unnecessary code Co-authored-by: adlerhurst <silvan.reusser@gmail.com> Co-authored-by: livio-a <livio.a@gmail.com>
This commit is contained in:
221
internal/cache/bigcache/bigcache_test.go
vendored
Normal file
221
internal/cache/bigcache/bigcache_test.go
vendored
Normal file
@@ -0,0 +1,221 @@
|
||||
package bigcache
|
||||
|
||||
import (
|
||||
a_cache "github.com/allegro/bigcache"
|
||||
"github.com/caos/zitadel/internal/errors"
|
||||
es_models "github.com/caos/zitadel/internal/eventstore/models"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type TestStruct struct {
|
||||
Test string
|
||||
}
|
||||
|
||||
func getBigCacheMock() *Bigcache {
|
||||
cache, _ := a_cache.NewBigCache(a_cache.DefaultConfig(2000))
|
||||
return &Bigcache{cache: cache}
|
||||
}
|
||||
|
||||
func TestSet(t *testing.T) {
|
||||
type args struct {
|
||||
cache *Bigcache
|
||||
key string
|
||||
value *TestStruct
|
||||
}
|
||||
type res struct {
|
||||
result *TestStruct
|
||||
errFunc func(err error) bool
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
res res
|
||||
}{
|
||||
{
|
||||
name: "set cache no err",
|
||||
args: args{
|
||||
cache: getBigCacheMock(),
|
||||
key: "KEY",
|
||||
value: &TestStruct{Test: "Test"},
|
||||
},
|
||||
res: res{
|
||||
result: &TestStruct{},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "key empty",
|
||||
args: args{
|
||||
cache: getBigCacheMock(),
|
||||
key: "",
|
||||
value: &TestStruct{Test: "Test"},
|
||||
},
|
||||
res: res{
|
||||
errFunc: errors.IsErrorInvalidArgument,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "set cache nil value",
|
||||
args: args{
|
||||
cache: getBigCacheMock(),
|
||||
key: "KEY",
|
||||
},
|
||||
res: res{
|
||||
errFunc: errors.IsErrorInvalidArgument,
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
err := tt.args.cache.Set(tt.args.key, tt.args.value)
|
||||
|
||||
if tt.res.errFunc == nil && err != nil {
|
||||
t.Errorf("got wrong result should not get err: %v ", err)
|
||||
}
|
||||
|
||||
if tt.res.errFunc == nil {
|
||||
tt.args.cache.Get(tt.args.key, tt.res.result)
|
||||
if tt.res.result == nil {
|
||||
t.Errorf("got wrong result should get result: %v ", err)
|
||||
}
|
||||
}
|
||||
if tt.res.errFunc != nil && !tt.res.errFunc(err) {
|
||||
t.Errorf("got wrong err: %v ", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGet(t *testing.T) {
|
||||
type args struct {
|
||||
event []*es_models.Event
|
||||
cache *Bigcache
|
||||
key string
|
||||
setValue *TestStruct
|
||||
getValue *TestStruct
|
||||
}
|
||||
type res struct {
|
||||
result *TestStruct
|
||||
errFunc func(err error) bool
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
res res
|
||||
}{
|
||||
{
|
||||
name: "get cache no err",
|
||||
args: args{
|
||||
cache: getBigCacheMock(),
|
||||
key: "KEY",
|
||||
setValue: &TestStruct{Test: "Test"},
|
||||
getValue: &TestStruct{Test: "Test"},
|
||||
},
|
||||
res: res{
|
||||
result: &TestStruct{Test: "Test"},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "get cache no key",
|
||||
args: args{
|
||||
cache: getBigCacheMock(),
|
||||
setValue: &TestStruct{Test: "Test"},
|
||||
getValue: &TestStruct{Test: "Test"},
|
||||
},
|
||||
res: res{
|
||||
errFunc: errors.IsErrorInvalidArgument,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "get cache no value",
|
||||
args: args{
|
||||
cache: getBigCacheMock(),
|
||||
key: "KEY",
|
||||
setValue: &TestStruct{Test: "Test"},
|
||||
},
|
||||
res: res{
|
||||
errFunc: errors.IsErrorInvalidArgument,
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
err := tt.args.cache.Set("KEY", tt.args.setValue)
|
||||
if err != nil {
|
||||
t.Errorf("something went wrong")
|
||||
}
|
||||
|
||||
err = tt.args.cache.Get(tt.args.key, tt.args.getValue)
|
||||
|
||||
if tt.res.errFunc == nil && err != nil {
|
||||
t.Errorf("got wrong result should not get err: %v ", err)
|
||||
}
|
||||
|
||||
if tt.res.errFunc == nil && !reflect.DeepEqual(tt.args.getValue, tt.res.result) {
|
||||
t.Errorf("got wrong result expected: %v actual: %v", tt.res.result, tt.args.getValue)
|
||||
}
|
||||
|
||||
if tt.res.errFunc != nil && !tt.res.errFunc(err) {
|
||||
t.Errorf("got wrong err: %v ", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDelete(t *testing.T) {
|
||||
type args struct {
|
||||
event []*es_models.Event
|
||||
cache *Bigcache
|
||||
key string
|
||||
setValue *TestStruct
|
||||
getValue *TestStruct
|
||||
}
|
||||
type res struct {
|
||||
result *TestStruct
|
||||
errFunc func(err error) bool
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
res res
|
||||
}{
|
||||
{
|
||||
name: "delete cache no err",
|
||||
args: args{
|
||||
cache: getBigCacheMock(),
|
||||
key: "KEY",
|
||||
setValue: &TestStruct{Test: "Test"},
|
||||
},
|
||||
res: res{},
|
||||
},
|
||||
{
|
||||
name: "get cache no key",
|
||||
args: args{
|
||||
cache: getBigCacheMock(),
|
||||
setValue: &TestStruct{Test: "Test"},
|
||||
getValue: &TestStruct{Test: "Test"},
|
||||
},
|
||||
res: res{
|
||||
errFunc: errors.IsErrorInvalidArgument,
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
err := tt.args.cache.Set("KEY", tt.args.setValue)
|
||||
if err != nil {
|
||||
t.Errorf("something went wrong")
|
||||
}
|
||||
|
||||
err = tt.args.cache.Delete(tt.args.key)
|
||||
|
||||
if tt.res.errFunc == nil && err != nil {
|
||||
t.Errorf("got wrong result should not get err: %v ", err)
|
||||
}
|
||||
|
||||
if tt.res.errFunc != nil && !tt.res.errFunc(err) {
|
||||
t.Errorf("got wrong err: %v ", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
67
internal/cache/bigcache/cache.go
vendored
Normal file
67
internal/cache/bigcache/cache.go
vendored
Normal file
@@ -0,0 +1,67 @@
|
||||
package bigcache
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/gob"
|
||||
"github.com/caos/logging"
|
||||
"reflect"
|
||||
|
||||
"github.com/caos/zitadel/internal/errors"
|
||||
|
||||
a_cache "github.com/allegro/bigcache"
|
||||
)
|
||||
|
||||
type Bigcache struct {
|
||||
cache *a_cache.BigCache
|
||||
}
|
||||
|
||||
func NewBigcache(c *Config) (*Bigcache, error) {
|
||||
cacheConfig := a_cache.DefaultConfig(c.CacheLifetime)
|
||||
cacheConfig.HardMaxCacheSize = c.MaxCacheSizeInMB
|
||||
cache, err := a_cache.NewBigCache(cacheConfig)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &Bigcache{
|
||||
cache: cache,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (c *Bigcache) Set(key string, object interface{}) error {
|
||||
if key == "" || reflect.ValueOf(object).IsNil() {
|
||||
return errors.ThrowInvalidArgument(nil, "FASTC-du73s", "key or value should not be empty")
|
||||
}
|
||||
var b bytes.Buffer
|
||||
enc := gob.NewEncoder(&b)
|
||||
if err := enc.Encode(object); err != nil {
|
||||
return errors.ThrowInvalidArgument(err, "FASTC-RUyxI", "unable to encode object")
|
||||
}
|
||||
return c.cache.Set(key, b.Bytes())
|
||||
}
|
||||
|
||||
func (c *Bigcache) Get(key string, ptrToObject interface{}) error {
|
||||
if key == "" || reflect.ValueOf(ptrToObject).IsNil() {
|
||||
return errors.ThrowInvalidArgument(nil, "FASTC-dksoe", "key or value should not be empty")
|
||||
}
|
||||
value, err := c.cache.Get(key)
|
||||
if err == a_cache.ErrEntryNotFound {
|
||||
return errors.ThrowNotFound(err, "BIGCA-we32s", "not in cache")
|
||||
}
|
||||
if err != nil {
|
||||
logging.Log("BIGCA-ftofbc").WithError(err).Info("read from cache failed")
|
||||
return errors.ThrowInvalidArgument(err, "BIGCA-3idls", "error in reading from cache")
|
||||
}
|
||||
|
||||
b := bytes.NewBuffer(value)
|
||||
dec := gob.NewDecoder(b)
|
||||
|
||||
return dec.Decode(ptrToObject)
|
||||
}
|
||||
|
||||
func (c *Bigcache) Delete(key string) error {
|
||||
if key == "" {
|
||||
return errors.ThrowInvalidArgument(nil, "FASTC-clsi2", "key should not be empty")
|
||||
}
|
||||
return c.cache.Delete(key)
|
||||
}
|
||||
16
internal/cache/bigcache/config.go
vendored
Normal file
16
internal/cache/bigcache/config.go
vendored
Normal file
@@ -0,0 +1,16 @@
|
||||
package bigcache
|
||||
|
||||
import (
|
||||
"github.com/caos/zitadel/internal/cache"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
MaxCacheSizeInMB int
|
||||
//CacheLifetime if set, entries older than the lifetime will be deleted on cleanup (every minute)
|
||||
CacheLifetime time.Duration
|
||||
}
|
||||
|
||||
func (c *Config) NewCache() (cache.Cache, error) {
|
||||
return NewBigcache(c)
|
||||
}
|
||||
7
internal/cache/cache.go
vendored
Normal file
7
internal/cache/cache.go
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
package cache
|
||||
|
||||
type Cache interface {
|
||||
Set(key string, object interface{}) error
|
||||
Get(key string, ptrToObject interface{}) error
|
||||
Delete(key string) error
|
||||
}
|
||||
5
internal/cache/config.go
vendored
Normal file
5
internal/cache/config.go
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
package cache
|
||||
|
||||
type Config interface {
|
||||
NewCache() (Cache, error)
|
||||
}
|
||||
58
internal/cache/config/config.go
vendored
Normal file
58
internal/cache/config/config.go
vendored
Normal file
@@ -0,0 +1,58 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/caos/zitadel/internal/cache"
|
||||
"github.com/caos/zitadel/internal/cache/bigcache"
|
||||
"github.com/caos/zitadel/internal/cache/fastcache"
|
||||
"github.com/caos/zitadel/internal/errors"
|
||||
)
|
||||
|
||||
type CacheConfig struct {
|
||||
Type string
|
||||
Config cache.Config
|
||||
}
|
||||
|
||||
var caches = map[string]func() cache.Config{
|
||||
"bigcache": func() cache.Config { return &bigcache.Config{} },
|
||||
"fastcache": func() cache.Config { return &fastcache.Config{} },
|
||||
}
|
||||
|
||||
func (c *CacheConfig) UnmarshalJSON(data []byte) error {
|
||||
var rc struct {
|
||||
Type string
|
||||
Config json.RawMessage
|
||||
}
|
||||
|
||||
if err := json.Unmarshal(data, &rc); err != nil {
|
||||
return errors.ThrowInternal(err, "CONFI-98ejs", "unable to unmarshal config")
|
||||
}
|
||||
|
||||
c.Type = rc.Type
|
||||
|
||||
var err error
|
||||
c.Config, err = newCacheConfig(c.Type, rc.Config)
|
||||
if err != nil {
|
||||
return errors.ThrowInternal(err, "CONFI-do9es", "unable create config")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func newCacheConfig(cacheType string, configData []byte) (cache.Config, error) {
|
||||
t, ok := caches[cacheType]
|
||||
if !ok {
|
||||
return nil, errors.ThrowInternal(nil, "CONFI-di328s", "no config")
|
||||
}
|
||||
|
||||
cacheConfig := t()
|
||||
if len(configData) == 0 {
|
||||
return cacheConfig, nil
|
||||
}
|
||||
|
||||
if err := json.Unmarshal(configData, cacheConfig); err != nil {
|
||||
return nil, errors.ThrowInternal(nil, "CONFI-skei3", "could not read config")
|
||||
}
|
||||
|
||||
return cacheConfig, nil
|
||||
}
|
||||
11
internal/cache/fastcache/config.go
vendored
Normal file
11
internal/cache/fastcache/config.go
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
package fastcache
|
||||
|
||||
import "github.com/caos/zitadel/internal/cache"
|
||||
|
||||
type Config struct {
|
||||
MaxCacheSizeInByte int
|
||||
}
|
||||
|
||||
func (c *Config) NewCache() (cache.Cache, error) {
|
||||
return NewFastcache(c)
|
||||
}
|
||||
56
internal/cache/fastcache/fastcache.go
vendored
Normal file
56
internal/cache/fastcache/fastcache.go
vendored
Normal file
@@ -0,0 +1,56 @@
|
||||
package fastcache
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/gob"
|
||||
"github.com/caos/zitadel/internal/errors"
|
||||
"reflect"
|
||||
|
||||
"github.com/VictoriaMetrics/fastcache"
|
||||
)
|
||||
|
||||
type Fastcache struct {
|
||||
cache *fastcache.Cache
|
||||
}
|
||||
|
||||
func NewFastcache(config *Config) (*Fastcache, error) {
|
||||
return &Fastcache{
|
||||
cache: fastcache.New(config.MaxCacheSizeInByte),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (fc *Fastcache) Set(key string, object interface{}) error {
|
||||
if key == "" || reflect.ValueOf(object).IsNil() {
|
||||
return errors.ThrowInvalidArgument(nil, "FASTC-87dj3", "key or value should not be empty")
|
||||
}
|
||||
var b bytes.Buffer
|
||||
enc := gob.NewEncoder(&b)
|
||||
if err := enc.Encode(object); err != nil {
|
||||
return errors.ThrowInvalidArgument(err, "FASTC-RUyxI", "unable to encode object")
|
||||
}
|
||||
fc.cache.Set([]byte(key), b.Bytes())
|
||||
return nil
|
||||
}
|
||||
|
||||
func (fc *Fastcache) Get(key string, ptrToObject interface{}) error {
|
||||
if key == "" || reflect.ValueOf(ptrToObject).IsNil() {
|
||||
return errors.ThrowInvalidArgument(nil, "FASTC-di8es", "key or value should not be empty")
|
||||
}
|
||||
data := fc.cache.Get(nil, []byte(key))
|
||||
if len(data) == 0 {
|
||||
return errors.ThrowNotFound(nil, "FASTC-xYzSm", "key not found")
|
||||
}
|
||||
|
||||
b := bytes.NewBuffer(data)
|
||||
dec := gob.NewDecoder(b)
|
||||
|
||||
return dec.Decode(ptrToObject)
|
||||
}
|
||||
|
||||
func (fc *Fastcache) Delete(key string) error {
|
||||
if key == "" {
|
||||
return errors.ThrowInvalidArgument(nil, "FASTC-lod92", "key should not be empty")
|
||||
}
|
||||
fc.cache.Del([]byte(key))
|
||||
return nil
|
||||
}
|
||||
216
internal/cache/fastcache/fastcache_test.go
vendored
Normal file
216
internal/cache/fastcache/fastcache_test.go
vendored
Normal file
@@ -0,0 +1,216 @@
|
||||
package fastcache
|
||||
|
||||
import (
|
||||
"github.com/VictoriaMetrics/fastcache"
|
||||
"github.com/caos/zitadel/internal/errors"
|
||||
es_models "github.com/caos/zitadel/internal/eventstore/models"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type TestStruct struct {
|
||||
Test string
|
||||
}
|
||||
|
||||
func TestSet(t *testing.T) {
|
||||
type args struct {
|
||||
cache *Fastcache
|
||||
key string
|
||||
value *TestStruct
|
||||
}
|
||||
type res struct {
|
||||
result *TestStruct
|
||||
errFunc func(err error) bool
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
res res
|
||||
}{
|
||||
{
|
||||
name: "set cache no err",
|
||||
args: args{
|
||||
cache: &Fastcache{cache: fastcache.New(2000)},
|
||||
key: "KEY",
|
||||
value: &TestStruct{Test: "Test"},
|
||||
},
|
||||
res: res{
|
||||
result: &TestStruct{},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "key empty",
|
||||
args: args{
|
||||
cache: &Fastcache{cache: fastcache.New(2000)},
|
||||
key: "",
|
||||
value: &TestStruct{Test: "Test"},
|
||||
},
|
||||
res: res{
|
||||
errFunc: errors.IsErrorInvalidArgument,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "set cache nil value",
|
||||
args: args{
|
||||
cache: &Fastcache{cache: fastcache.New(2000)},
|
||||
key: "KEY",
|
||||
},
|
||||
res: res{
|
||||
errFunc: errors.IsErrorInvalidArgument,
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
err := tt.args.cache.Set(tt.args.key, tt.args.value)
|
||||
|
||||
if tt.res.errFunc == nil && err != nil {
|
||||
t.Errorf("got wrong result should not get err: %v ", err)
|
||||
}
|
||||
|
||||
if tt.res.errFunc == nil {
|
||||
tt.args.cache.Get(tt.args.key, tt.res.result)
|
||||
if tt.res.result == nil {
|
||||
t.Errorf("got wrong result should get result: %v ", err)
|
||||
}
|
||||
}
|
||||
if tt.res.errFunc != nil && !tt.res.errFunc(err) {
|
||||
t.Errorf("got wrong err: %v ", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGet(t *testing.T) {
|
||||
type args struct {
|
||||
event []*es_models.Event
|
||||
cache *Fastcache
|
||||
key string
|
||||
setValue *TestStruct
|
||||
getValue *TestStruct
|
||||
}
|
||||
type res struct {
|
||||
result *TestStruct
|
||||
errFunc func(err error) bool
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
res res
|
||||
}{
|
||||
{
|
||||
name: "get cache no err",
|
||||
args: args{
|
||||
cache: &Fastcache{cache: fastcache.New(2000)},
|
||||
key: "KEY",
|
||||
setValue: &TestStruct{Test: "Test"},
|
||||
getValue: &TestStruct{Test: "Test"},
|
||||
},
|
||||
res: res{
|
||||
result: &TestStruct{Test: "Test"},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "get cache no key",
|
||||
args: args{
|
||||
cache: &Fastcache{cache: fastcache.New(2000)},
|
||||
setValue: &TestStruct{Test: "Test"},
|
||||
getValue: &TestStruct{Test: "Test"},
|
||||
},
|
||||
res: res{
|
||||
errFunc: errors.IsErrorInvalidArgument,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "get cache no value",
|
||||
args: args{
|
||||
cache: &Fastcache{cache: fastcache.New(2000)},
|
||||
key: "KEY",
|
||||
setValue: &TestStruct{Test: "Test"},
|
||||
},
|
||||
res: res{
|
||||
errFunc: errors.IsErrorInvalidArgument,
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
err := tt.args.cache.Set("KEY", tt.args.setValue)
|
||||
if err != nil {
|
||||
t.Errorf("something went wrong")
|
||||
}
|
||||
|
||||
err = tt.args.cache.Get(tt.args.key, tt.args.getValue)
|
||||
|
||||
if tt.res.errFunc == nil && err != nil {
|
||||
t.Errorf("got wrong result should not get err: %v ", err)
|
||||
}
|
||||
|
||||
if tt.res.errFunc == nil && !reflect.DeepEqual(tt.args.getValue, tt.res.result) {
|
||||
t.Errorf("got wrong result expected: %v actual: %v", tt.res.result, tt.args.getValue)
|
||||
}
|
||||
|
||||
if tt.res.errFunc != nil && !tt.res.errFunc(err) {
|
||||
t.Errorf("got wrong err: %v ", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDelete(t *testing.T) {
|
||||
type args struct {
|
||||
event []*es_models.Event
|
||||
cache *Fastcache
|
||||
key string
|
||||
setValue *TestStruct
|
||||
getValue *TestStruct
|
||||
}
|
||||
type res struct {
|
||||
result *TestStruct
|
||||
errFunc func(err error) bool
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
res res
|
||||
}{
|
||||
{
|
||||
name: "delete cache no err",
|
||||
args: args{
|
||||
cache: &Fastcache{cache: fastcache.New(2000)},
|
||||
key: "KEY",
|
||||
setValue: &TestStruct{Test: "Test"},
|
||||
},
|
||||
res: res{},
|
||||
},
|
||||
{
|
||||
name: "get cache no key",
|
||||
args: args{
|
||||
cache: &Fastcache{cache: fastcache.New(2000)},
|
||||
setValue: &TestStruct{Test: "Test"},
|
||||
getValue: &TestStruct{Test: "Test"},
|
||||
},
|
||||
res: res{
|
||||
errFunc: errors.IsErrorInvalidArgument,
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
err := tt.args.cache.Set("KEY", tt.args.setValue)
|
||||
if err != nil {
|
||||
t.Errorf("something went wrong")
|
||||
}
|
||||
|
||||
err = tt.args.cache.Delete(tt.args.key)
|
||||
|
||||
if tt.res.errFunc == nil && err != nil {
|
||||
t.Errorf("got wrong result should not get err: %v ", err)
|
||||
}
|
||||
|
||||
if tt.res.errFunc != nil && !tt.res.errFunc(err) {
|
||||
t.Errorf("got wrong err: %v ", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
3
internal/cache/generate.go
vendored
Normal file
3
internal/cache/generate.go
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
package cache
|
||||
|
||||
//go:generate mockgen -package mock -destination ./mock/cache.mock.go github.com/caos/zitadel/internal/cache Cache
|
||||
75
internal/cache/mock/cache.mock.go
vendored
Normal file
75
internal/cache/mock/cache.mock.go
vendored
Normal file
@@ -0,0 +1,75 @@
|
||||
// Code generated by MockGen. DO NOT EDIT.
|
||||
// Source: github.com/caos/zitadel/internal/cache (interfaces: Cache)
|
||||
|
||||
// Package mock is a generated GoMock package.
|
||||
package mock
|
||||
|
||||
import (
|
||||
gomock "github.com/golang/mock/gomock"
|
||||
reflect "reflect"
|
||||
)
|
||||
|
||||
// MockCache is a mock of Cache interface
|
||||
type MockCache struct {
|
||||
ctrl *gomock.Controller
|
||||
recorder *MockCacheMockRecorder
|
||||
}
|
||||
|
||||
// MockCacheMockRecorder is the mock recorder for MockCache
|
||||
type MockCacheMockRecorder struct {
|
||||
mock *MockCache
|
||||
}
|
||||
|
||||
// NewMockCache creates a new mock instance
|
||||
func NewMockCache(ctrl *gomock.Controller) *MockCache {
|
||||
mock := &MockCache{ctrl: ctrl}
|
||||
mock.recorder = &MockCacheMockRecorder{mock}
|
||||
return mock
|
||||
}
|
||||
|
||||
// EXPECT returns an object that allows the caller to indicate expected use
|
||||
func (m *MockCache) EXPECT() *MockCacheMockRecorder {
|
||||
return m.recorder
|
||||
}
|
||||
|
||||
// Delete mocks base method
|
||||
func (m *MockCache) Delete(arg0 string) error {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "Delete", arg0)
|
||||
ret0, _ := ret[0].(error)
|
||||
return ret0
|
||||
}
|
||||
|
||||
// Delete indicates an expected call of Delete
|
||||
func (mr *MockCacheMockRecorder) Delete(arg0 interface{}) *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Delete", reflect.TypeOf((*MockCache)(nil).Delete), arg0)
|
||||
}
|
||||
|
||||
// Get mocks base method
|
||||
func (m *MockCache) Get(arg0 string, arg1 interface{}) error {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "Get", arg0, arg1)
|
||||
ret0, _ := ret[0].(error)
|
||||
return ret0
|
||||
}
|
||||
|
||||
// Get indicates an expected call of Get
|
||||
func (mr *MockCacheMockRecorder) Get(arg0, arg1 interface{}) *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Get", reflect.TypeOf((*MockCache)(nil).Get), arg0, arg1)
|
||||
}
|
||||
|
||||
// Set mocks base method
|
||||
func (m *MockCache) Set(arg0 string, arg1 interface{}) error {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "Set", arg0, arg1)
|
||||
ret0, _ := ret[0].(error)
|
||||
return ret0
|
||||
}
|
||||
|
||||
// Set indicates an expected call of Set
|
||||
func (mr *MockCacheMockRecorder) Set(arg0, arg1 interface{}) *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Set", reflect.TypeOf((*MockCache)(nil).Set), arg0, arg1)
|
||||
}
|
||||
Reference in New Issue
Block a user