Files
kopia/internal/blobtesting/map_test.go
Jarek Kowalski fe55dcb6a2 feat(repository): added hard size limit to the on-disk cache (#3238)
* test(providers): added capacity limits to blobtesting.mapStorage

* refactor(general): added mutex map which dynamically allocates and releases named mutexes

* refactor(repository): refactored cache cleanup and limit enforcement

* refactor(repository): plumb through cache size limits in the repository

* feat(cli): added CLI options to set cache size limits

* unified flag setting and field naming

* Update cli/command_cache_set.go

Co-authored-by: Shikhar Mall <mall.shikhar.in@gmail.com>

* pr feedback

---------

Co-authored-by: Shikhar Mall <mall.shikhar.in@gmail.com>
2023-08-24 09:38:56 -07:00

66 lines
2.2 KiB
Go

package blobtesting
import (
"testing"
"github.com/stretchr/testify/require"
"github.com/kopia/kopia/internal/gather"
"github.com/kopia/kopia/internal/testlogging"
"github.com/kopia/kopia/repo/blob"
)
func TestMapStorage(t *testing.T) {
data := DataMap{}
r := NewMapStorage(data, nil, nil)
if r == nil {
t.Errorf("unexpected result: %v", r)
}
VerifyStorage(testlogging.Context(t), t, r, blob.PutOptions{})
}
func TestMapStorageWithLimit(t *testing.T) {
ctx := testlogging.Context(t)
data := DataMap{}
r := NewMapStorageWithLimit(data, nil, nil, 10)
verifyCapacityAndFreeSpace(t, r, 10, 10)
require.NoError(t, r.PutBlob(ctx, "foo", gather.FromSlice([]byte("foo")), blob.PutOptions{}))
verifyCapacityAndFreeSpace(t, r, 10, 7)
require.NoError(t, r.PutBlob(ctx, "bar", gather.FromSlice([]byte("bar")), blob.PutOptions{}))
verifyCapacityAndFreeSpace(t, r, 10, 4)
require.NoError(t, r.PutBlob(ctx, "baz", gather.FromSlice([]byte("baz")), blob.PutOptions{}))
verifyCapacityAndFreeSpace(t, r, 10, 1)
// we're at 9/10 bytes, can't add 3 more
require.ErrorContains(t, r.PutBlob(ctx, "qux", gather.FromSlice([]byte("qux")), blob.PutOptions{}), "exceeded limit")
// remove 3 bytes
require.NoError(t, r.DeleteBlob(ctx, "baz"))
verifyCapacityAndFreeSpace(t, r, 10, 4)
// can add 4 bytes again
require.NoError(t, r.PutBlob(ctx, "qux", gather.FromSlice([]byte("qux1")), blob.PutOptions{}))
verifyCapacityAndFreeSpace(t, r, 10, 0)
// can't add any more bytes since we're at 10/10 bytes
require.ErrorContains(t, r.PutBlob(ctx, "aaa", gather.FromSlice([]byte("1")), blob.PutOptions{}), "exceeded limit")
// adding zero bytes won't fail in this situation.
require.NoError(t, r.PutBlob(ctx, "bbb", gather.FromSlice([]byte{}), blob.PutOptions{}), "exceeded limit")
verifyCapacityAndFreeSpace(t, r, 10, 0)
r = NewMapStorageWithLimit(DataMap{
"foo": []byte("foo"),
}, nil, nil, 20)
verifyCapacityAndFreeSpace(t, r, 20, 17)
}
func verifyCapacityAndFreeSpace(t *testing.T, r blob.Storage, wantSize, wantFree int64) {
t.Helper()
c, err := r.GetCapacity(testlogging.Context(t))
require.NoError(t, err)
require.Equal(t, uint64(wantSize), c.SizeB)
require.Equal(t, uint64(wantFree), c.FreeB)
}