Skip to content

Commit

Permalink
✅ Add unit test for inmemory cache (#270)
Browse files Browse the repository at this point in the history
  • Loading branch information
tosone authored Dec 29, 2023
1 parent 91018ba commit 51c0e7e
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 4 deletions.
1 change: 1 addition & 0 deletions pkg/auth/mocks/service.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pkg/auth/mocks/service_factory.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/configs/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ type ConfigurationCacheRedis struct {

// ConfigurationCacheInmemory ...
type ConfigurationCacheInmemory struct {
Size int64 `yaml:"size"`
Size int `yaml:"size"`
}

// ConfigurationCacheDatabase ...
Expand Down
6 changes: 6 additions & 0 deletions pkg/configs/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,10 @@ func defaultSettings() {
if configuration.Daemon.Gc.Retention == 0 {
configuration.Daemon.Gc.Retention = 72 * time.Hour
}
if configuration.Cache.Inmemory.Size == 0 {
configuration.Cache.Inmemory.Size = 10240
}
if configuration.Cache.Ttl == 0 {
configuration.Cache.Ttl = time.Second * 30
}
}
9 changes: 6 additions & 3 deletions pkg/modules/cacher/inmemory/inmemory.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type cacher[T any] struct {

// New returns a new Cacher.
func New[T any](config configs.Configuration, prefix string, fetcher definition.Fetcher[T]) (definition.Cacher[T], error) {
cache, err := lru.New2Q[string, ValueWithTtl[T]](10240)
cache, err := lru.New2Q[string, ValueWithTtl[T]](config.Cache.Inmemory.Size)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -86,14 +86,17 @@ func (c *cacher[T]) Get(ctx context.Context, key string) (T, error) {
}
return result, nil
}
if result.Ttl != nil && result.Ttl.After(time.Now()) {
if result.Ttl == nil {
return result.Value, nil
}
if result.Ttl.After(time.Now()) {
return result.Value, nil
}
err := c.Del(ctx, key)
if err != nil {
return result.Value, err
}
return result.Value, nil
return c.Get(ctx, key)
}

// Del deletes the value corresponding to the given key from the cache.
Expand Down
80 changes: 80 additions & 0 deletions pkg/modules/cacher/inmemory/inmemory_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright 2023 sigma
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package inmemory

import (
"context"
"fmt"
"testing"
"time"

"github.com/stretchr/testify/assert"

"github.com/go-sigma/sigma/pkg/configs"
)

func fetcher1(key string) (string, error) {
return "new-val", nil
}

func TestNew(t *testing.T) {
config := configs.Configuration{
Cache: configs.ConfigurationCache{
Ttl: time.Second * 3,
Inmemory: configs.ConfigurationCacheInmemory{
Size: 1000,
},
},
}
cacher, err := New(config, "test", fetcher1)
assert.NoError(t, err)
assert.NotNil(t, cacher)

ctx := context.Background()
err = cacher.Set(ctx, "test", "test")
assert.NoError(t, err)

res, err := cacher.Get(ctx, "test")
assert.NoError(t, err)
assert.Equal(t, "test", res)

err = cacher.Del(ctx, "test")
assert.NoError(t, err)

res, err = cacher.Get(ctx, "test")
assert.NoError(t, err)
assert.Equal(t, "new-val", res)

err = cacher.Set(ctx, "m-test", "m-val")
assert.NoError(t, err)

for i := 0; i < 1024; i++ {
err = cacher.Set(ctx, fmt.Sprintf("key-%d", i), "val")
assert.NoError(t, err)
}

res, err = cacher.Get(ctx, "m-test")
assert.NoError(t, err)
assert.Equal(t, "new-val", res)

err = cacher.Set(ctx, "expire-key", "expire-val", time.Second)
assert.NoError(t, err)

time.Sleep(time.Second * 3)

res, err = cacher.Get(ctx, "expire-key")
assert.NoError(t, err)
assert.Equal(t, "new-val", res)
}

0 comments on commit 51c0e7e

Please sign in to comment.