reflector.go/store/gcache_test.go

111 lines
2.6 KiB
Go
Raw Permalink Normal View History

package store
import (
2021-07-23 23:44:21 +02:00
"fmt"
"os"
"reflect"
"testing"
2020-11-21 01:26:32 +01:00
"time"
"github.com/lbryio/lbry.go/v2/extras/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
2021-07-23 23:44:21 +02:00
const cacheMaxSize = 3
2021-07-23 23:44:21 +02:00
func getTestGcacheStore() (*GcacheStore, *MemStore) {
m := NewMemStore()
2021-07-23 23:44:21 +02:00
return NewGcacheStore("test", m, cacheMaxSize, LFU), m
}
2021-07-23 23:44:21 +02:00
func TestGcacheStore_Eviction(t *testing.T) {
lfu, mem := getTestGcacheStore()
b := []byte("x")
2021-07-23 23:44:21 +02:00
for i := 0; i < 3; i++ {
err := lfu.Put(fmt.Sprintf("%d", i), b)
require.NoError(t, err)
for j := 0; j < 3-i; j++ {
_, _, err = lfu.Get(fmt.Sprintf("%d", i))
require.NoError(t, err)
}
}
for k, v := range map[string]bool{
2021-07-23 23:44:21 +02:00
"0": true,
"1": true,
"2": true,
} {
2021-07-23 23:44:21 +02:00
has, err := lfu.Has(k)
assert.NoError(t, err)
assert.Equal(t, v, has)
}
2021-07-23 23:44:21 +02:00
err := lfu.Put("3", b)
require.NoError(t, err)
for k, v := range map[string]bool{
2021-07-23 23:44:21 +02:00
"0": true,
"1": true,
"2": false,
"3": true,
} {
2021-07-23 23:44:21 +02:00
has, err := lfu.Has(k)
assert.NoError(t, err)
assert.Equal(t, v, has)
}
2021-07-23 23:44:21 +02:00
assert.Equal(t, cacheMaxSize, len(mem.Debug()))
2021-07-23 23:44:21 +02:00
err = lfu.Delete("0")
assert.NoError(t, err)
2021-07-23 23:44:21 +02:00
err = lfu.Delete("1")
assert.NoError(t, err)
2021-07-23 23:44:21 +02:00
err = lfu.Delete("3")
assert.NoError(t, err)
assert.Equal(t, 0, len(mem.Debug()))
}
2021-07-23 23:44:21 +02:00
func TestGcacheStore_UnderlyingBlobMissing(t *testing.T) {
lfu, mem := getTestGcacheStore()
hash := "hash"
b := []byte("this is a blob of stuff")
2021-07-23 23:44:21 +02:00
err := lfu.Put(hash, b)
require.NoError(t, err)
err = mem.Delete(hash)
require.NoError(t, err)
2020-10-22 18:18:31 +02:00
// hash still exists in lru
2021-07-23 23:44:21 +02:00
assert.True(t, lfu.cache.Has(hash))
2020-10-22 18:18:31 +02:00
2021-07-23 23:44:21 +02:00
blob, _, err := lfu.Get(hash)
assert.Nil(t, blob)
assert.True(t, errors.Is(err, ErrBlobNotFound), "expected (%s) %s, got (%s) %s",
reflect.TypeOf(ErrBlobNotFound).String(), ErrBlobNotFound.Error(),
reflect.TypeOf(err).String(), err.Error())
2020-10-22 18:18:31 +02:00
// lru.Get() removes hash if underlying store doesn't have it
2021-07-23 23:44:21 +02:00
assert.False(t, lfu.cache.Has(hash))
}
2020-10-26 17:27:27 +01:00
2021-07-23 23:44:21 +02:00
func TestGcacheStore_loadExisting(t *testing.T) {
tmpDir, err := os.MkdirTemp("", "reflector_test_*")
require.NoError(t, err)
defer func() { _ = os.RemoveAll(tmpDir) }()
d := NewDiskStore(tmpDir, 2)
2020-10-26 17:27:27 +01:00
hash := "hash"
b := []byte("this is a blob of stuff")
err = d.Put(hash, b)
2020-10-26 17:27:27 +01:00
require.NoError(t, err)
existing, err := d.list()
require.NoError(t, err)
require.Equal(t, 1, len(existing), "blob should exist in cache")
assert.Equal(t, hash, existing[0])
2021-07-23 23:44:21 +02:00
lfu := NewGcacheStore("test", d, 3, LFU) // lru should load existing blobs when it's created
time.Sleep(100 * time.Millisecond) // async load so let's wait...
has, err := lfu.Has(hash)
2020-10-26 17:27:27 +01:00
require.NoError(t, err)
assert.True(t, has, "hash should be loaded from disk store but it's not")
}