Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✅ Add unit test for inmemory cache #270

Merged
merged 1 commit into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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
}

Check warning on line 61 in pkg/configs/default.go

View check run for this annotation

Codecov / codecov/patch

pkg/configs/default.go#L60-L61

Added lines #L60 - L61 were not covered by tests
}
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 @@

// 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 @@
}
return result, nil
}
if result.Ttl != nil && result.Ttl.After(time.Now()) {
if result.Ttl == nil {
return result.Value, nil
}

Check warning on line 91 in pkg/modules/cacher/inmemory/inmemory.go

View check run for this annotation

Codecov / codecov/patch

pkg/modules/cacher/inmemory/inmemory.go#L90-L91

Added lines #L90 - L91 were not covered by tests
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)
}