reflector.go/store/lru_test.go

124 lines
2.8 KiB
Go
Raw Normal View History

package store
import (
"io/ioutil"
"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"
)
const cacheMaxBlobs = 3
func getTestLRUStore() (*LRUStore, *MemStore) {
m := NewMemStore()
return NewLRUStore("test", m, 3), m
}
2020-10-22 18:18:31 +02:00
func TestLRUStore_Eviction(t *testing.T) {
lru, mem := getTestLRUStore()
b := []byte("x")
2020-10-22 18:18:31 +02:00
err := lru.Put("one", b)
require.NoError(t, err)
2020-10-22 18:18:31 +02:00
err = lru.Put("two", b)
require.NoError(t, err)
2020-10-22 18:18:31 +02:00
err = lru.Put("three", b)
require.NoError(t, err)
2020-10-22 18:18:31 +02:00
err = lru.Put("four", b)
require.NoError(t, err)
2020-10-22 18:18:31 +02:00
err = lru.Put("five", b)
require.NoError(t, err)
assert.Equal(t, cacheMaxBlobs, len(mem.Debug()))
for k, v := range map[string]bool{
"one": false,
"two": false,
"three": true,
"four": true,
"five": true,
"six": false,
} {
2020-10-22 18:18:31 +02:00
has, err := lru.Has(k)
assert.NoError(t, err)
assert.Equal(t, v, has)
}
2020-10-22 18:18:31 +02:00
lru.Get("three") // touch so it stays in cache
lru.Put("six", b)
assert.Equal(t, cacheMaxBlobs, len(mem.Debug()))
for k, v := range map[string]bool{
"one": false,
"two": false,
"three": true,
"four": false,
"five": true,
"six": true,
} {
2020-10-22 18:18:31 +02:00
has, err := lru.Has(k)
assert.NoError(t, err)
assert.Equal(t, v, has)
}
2020-10-22 18:18:31 +02:00
err = lru.Delete("three")
assert.NoError(t, err)
2020-10-22 18:18:31 +02:00
err = lru.Delete("five")
assert.NoError(t, err)
2020-10-22 18:18:31 +02:00
err = lru.Delete("six")
assert.NoError(t, err)
assert.Equal(t, 0, len(mem.Debug()))
}
2020-10-22 18:18:31 +02:00
func TestLRUStore_UnderlyingBlobMissing(t *testing.T) {
lru, mem := getTestLRUStore()
hash := "hash"
b := []byte("this is a blob of stuff")
2020-10-22 18:18:31 +02:00
err := lru.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
assert.True(t, lru.lru.Contains(hash))
2021-01-09 05:08:20 +01:00
blob, _, err := lru.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
assert.False(t, lru.lru.Contains(hash))
}
2020-10-26 17:27:27 +01:00
func TestLRUStore_loadExisting(t *testing.T) {
tmpDir, err := ioutil.TempDir("", "reflector_test_*")
require.NoError(t, err)
defer 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])
2020-11-21 01:26:32 +01:00
lru := NewLRUStore("test", d, 3) // lru should load existing blobs when it's created
time.Sleep(100 * time.Millisecond) // async load so let's wait...
2020-10-26 17:27:27 +01:00
has, err := lru.Has(hash)
require.NoError(t, err)
assert.True(t, has, "hash should be loaded from disk store but it's not")
}