Skip to content

Commit

Permalink
cache test
Browse files Browse the repository at this point in the history
  • Loading branch information
sayem314 committed Sep 26, 2024
1 parent 69a8f43 commit 0ef4d32
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 0 deletions.
12 changes: 12 additions & 0 deletions pkg/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@ func init() {
}
}

// SetCache sets a value in the cache with the specified TTL.
func SetCache(key string, value []byte) error {
return cache.Update(func(txn *badger.Txn) error {
e := badger.NewEntry([]byte(key), value).WithTTL(ttl)
return txn.SetEntry(e)
})
}

// GetCache retrieves a value from the cache.
func GetCache(key string) ([]byte, error) {
var valCopy []byte
err := cache.View(func(txn *badger.Txn) error {
Expand All @@ -49,3 +51,13 @@ func GetCache(key string) ([]byte, error) {
})
return valCopy, err
}

// FlushCache flushes the pending writes to the disk.
func FlushCache() error {
return cache.Flatten(0)
}

// CloseCache closes the Badger DB instance gracefully.
func CloseCache() error {
return cache.Close()
}
83 changes: 83 additions & 0 deletions pkg/cache/cache_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package cache

import (
"os"
"testing"
"time"

"github.com/dgraph-io/badger/v4"
"github.com/stretchr/testify/assert"
)

func TestSetAndGetCache(t *testing.T) {
key := "test-key"
value := []byte("test-value")

// Test setting a cache value
err := SetCache(key, value)
assert.NoError(t, err, "expected no error when setting cache value")

// Test getting a cache value
cachedValue, err := GetCache(key)
assert.NoError(t, err, "expected no error when getting cache value")
assert.Equal(t, value, cachedValue, "expected cached value to match the set value")
}

func TestFlushCache(t *testing.T) {
// Test flushing the cache
err := FlushCache()
assert.NoError(t, err, "expected no error when flushing cache")
}

func TestCloseCache(t *testing.T) {
// Test closing the cache
err := CloseCache()
assert.NoError(t, err, "expected no error when closing cache")

// Reinitialize cache after closing for other tests
initCache()
}

func initCache() {
cacheDir := "./test_cache"
opts := badger.DefaultOptions(cacheDir).
WithLogger(nil).
WithBlockCacheSize(64 * 1024 * 1024).
WithIndexCacheSize(32 * 1024 * 1024).
WithMemTableSize(32 * 1024 * 1024)

var err error
cache, err = badger.Open(opts)
if err != nil {
panic("Failed to reinitialize cache: " + err.Error())
}

// Cleanup after tests
go func() {
time.Sleep(1 * time.Second) // Wait a moment before cleanup
_ = CloseCache()
_ = os.RemoveAll(cacheDir)
}()
}

func TestCacheExpiry(t *testing.T) {
key := "expire-key"
value := []byte("expire-value")

// Set cache with a short TTL for testing expiration
originalTTL := ttl
ttl = 1 * time.Second
err := SetCache(key, value)
assert.NoError(t, err, "expected no error when setting cache value with TTL")

// Sleep to let the cache entry expire
time.Sleep(2 * time.Second)

// Attempt to get the expired cache value
cachedValue, err := GetCache(key)
assert.Error(t, err, "expected error when getting expired cache value")
assert.Nil(t, cachedValue, "expected no value for expired cache key")

// Reset TTL to original value after test
ttl = originalTTL
}

0 comments on commit 0ef4d32

Please sign in to comment.