Skip to content

Commit

Permalink
chore: test cache
Browse files Browse the repository at this point in the history
  • Loading branch information
freak12techno committed Mar 1, 2024
1 parent 6ebe809 commit 72209b4
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
5 changes: 2 additions & 3 deletions pkg/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@ type Cache struct {
Entries map[string]CacheEntry
}

func NewCache(logger *zerolog.Logger) *Cache {
func NewCache() *Cache {
return &Cache{
StoreTime: 10 * time.Minute,
Entries: make(map[string]CacheEntry, 0),
Logger: logger.With().Str("component", "cache").Logger(),
Entries: make(map[string]CacheEntry),
}
}

Expand Down
38 changes: 38 additions & 0 deletions pkg/cache/cache_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package cache_test

import (
"github.com/stretchr/testify/require"

Check failure on line 4 in pkg/cache/cache_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `goimports`-ed (goimports)
cachePkg "main/pkg/cache"
"testing"
"time"
)

func TestCacheSet(t *testing.T) {
t.Parallel()

cache := cachePkg.NewCache()
cache.Set("key", "value")

entry, found := cache.Get("key")
require.Equal(t, "value", entry)
require.True(t, found)
}

func TestCacheGetNotExists(t *testing.T) {
t.Parallel()

cache := cachePkg.NewCache()
_, found := cache.Get("key")
require.False(t, found)
}

func TestCacheGetExpired(t *testing.T) {
t.Parallel()

cache := cachePkg.NewCache()
cache.Entries["key"] = cachePkg.CacheEntry{
Value: "test", StoredAt: time.Now().Add(-24 * time.Hour),
}
_, found := cache.Get("key")
require.False(t, found)
}
2 changes: 1 addition & 1 deletion pkg/data_fetcher/data_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func NewDataFetcher(
Logger: logger.With().
Str("component", "data_fetcher").
Logger(),
Cache: cache.NewCache(logger),
Cache: cache.NewCache(),
PriceFetchers: map[string]priceFetchers.PriceFetcher{},
Config: config,
TendermintApiClients: tendermintApiClients,
Expand Down

0 comments on commit 72209b4

Please sign in to comment.