2020-10-21 23:31:15 +02:00
|
|
|
package store
|
|
|
|
|
|
|
|
import (
|
2021-07-23 23:44:21 +02:00
|
|
|
"fmt"
|
2020-10-21 23:31:15 +02:00
|
|
|
"os"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
2020-11-21 01:26:32 +01:00
|
|
|
"time"
|
2020-10-21 23:31:15 +02:00
|
|
|
|
|
|
|
"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
|
2020-10-21 23:31:15 +02:00
|
|
|
|
2021-07-23 23:44:21 +02:00
|
|
|
func getTestGcacheStore() (*GcacheStore, *MemStore) {
|
2020-11-02 20:35:04 +01:00
|
|
|
m := NewMemStore()
|
2021-07-23 23:44:21 +02:00
|
|
|
return NewGcacheStore("test", m, cacheMaxSize, LFU), m
|
2020-10-21 23:31:15 +02:00
|
|
|
}
|
|
|
|
|
2021-07-23 23:44:21 +02:00
|
|
|
func TestGcacheStore_Eviction(t *testing.T) {
|
|
|
|
lfu, mem := getTestGcacheStore()
|
2020-10-21 23:31:15 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2020-10-21 23:31:15 +02:00
|
|
|
for k, v := range map[string]bool{
|
2021-07-23 23:44:21 +02:00
|
|
|
"0": true,
|
|
|
|
"1": true,
|
|
|
|
"2": true,
|
2020-10-21 23:31:15 +02:00
|
|
|
} {
|
2021-07-23 23:44:21 +02:00
|
|
|
has, err := lfu.Has(k)
|
2020-10-21 23:31:15 +02:00
|
|
|
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)
|
2020-10-21 23:31:15 +02:00
|
|
|
for k, v := range map[string]bool{
|
2021-07-23 23:44:21 +02:00
|
|
|
"0": true,
|
|
|
|
"1": true,
|
|
|
|
"2": false,
|
|
|
|
"3": true,
|
2020-10-21 23:31:15 +02:00
|
|
|
} {
|
2021-07-23 23:44:21 +02:00
|
|
|
has, err := lfu.Has(k)
|
2020-10-21 23:31:15 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, v, has)
|
|
|
|
}
|
2021-07-23 23:44:21 +02:00
|
|
|
assert.Equal(t, cacheMaxSize, len(mem.Debug()))
|
2020-10-21 23:31:15 +02:00
|
|
|
|
2021-07-23 23:44:21 +02:00
|
|
|
err = lfu.Delete("0")
|
2020-10-21 23:31:15 +02:00
|
|
|
assert.NoError(t, err)
|
2021-07-23 23:44:21 +02:00
|
|
|
err = lfu.Delete("1")
|
2020-10-21 23:31:15 +02:00
|
|
|
assert.NoError(t, err)
|
2021-07-23 23:44:21 +02:00
|
|
|
err = lfu.Delete("3")
|
2020-10-21 23:31:15 +02:00
|
|
|
assert.NoError(t, err)
|
2020-11-02 20:35:04 +01:00
|
|
|
assert.Equal(t, 0, len(mem.Debug()))
|
2020-10-21 23:31:15 +02:00
|
|
|
}
|
|
|
|
|
2021-07-23 23:44:21 +02:00
|
|
|
func TestGcacheStore_UnderlyingBlobMissing(t *testing.T) {
|
|
|
|
lfu, mem := getTestGcacheStore()
|
2020-10-21 23:31:15 +02:00
|
|
|
hash := "hash"
|
|
|
|
b := []byte("this is a blob of stuff")
|
2021-07-23 23:44:21 +02:00
|
|
|
err := lfu.Put(hash, b)
|
2020-10-21 23:31:15 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-11-02 20:35:04 +01:00
|
|
|
err = mem.Delete(hash)
|
2020-10-21 23:31:15 +02:00
|
|
|
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)
|
2020-10-21 23:31:15 +02:00
|
|
|
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-21 23:31:15 +02:00
|
|
|
}
|
2020-10-26 17:27:27 +01:00
|
|
|
|
2021-07-23 23:44:21 +02:00
|
|
|
func TestGcacheStore_loadExisting(t *testing.T) {
|
2023-03-09 18:41:41 +01:00
|
|
|
tmpDir, err := os.MkdirTemp("", "reflector_test_*")
|
2020-11-02 20:35:04 +01:00
|
|
|
require.NoError(t, err)
|
2023-03-09 18:41:41 +01:00
|
|
|
defer func() { _ = os.RemoveAll(tmpDir) }()
|
2020-11-02 20:35:04 +01:00
|
|
|
d := NewDiskStore(tmpDir, 2)
|
2020-10-26 17:27:27 +01:00
|
|
|
|
|
|
|
hash := "hash"
|
|
|
|
b := []byte("this is a blob of stuff")
|
2020-11-02 20:35:04 +01:00
|
|
|
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")
|
|
|
|
}
|