Files
gohoarder/pkg/storage/filesystem/quota_overwrite_test.go
T
lukaszraczylo 1f31256769 fix(storage): release replaced bytes when a key is overwritten
Put added the new size to the usage counter without subtracting the file it
replaced. Each overwrite inflated usage until restart and could trip the
quota on a cache that was not full.
2026-11-23 13:15:33 +00:00

39 lines
1.2 KiB
Go

package filesystem
import (
"context"
"strings"
"testing"
"github.com/stretchr/testify/require"
)
// Used bytes must equal the bytes on disk after a key is overwritten.
func TestPutOverwriteKeepsUsageExact(t *testing.T) {
ctx := context.Background()
fs, err := New(t.TempDir(), 1000)
require.NoError(t, err)
require.NoError(t, fs.Put(ctx, "npm/a/metadata", strings.NewReader(strings.Repeat("x", 400)), nil))
require.NoError(t, fs.Put(ctx, "npm/a/metadata", strings.NewReader(strings.Repeat("y", 300)), nil))
quota, err := fs.GetQuota(ctx)
require.NoError(t, err)
require.EqualValues(t, 300, quota.Used)
require.NoError(t, fs.Delete(ctx, "npm/a/metadata"))
quota, err = fs.GetQuota(ctx)
require.NoError(t, err)
require.EqualValues(t, 0, quota.Used)
}
// An overwrite that fits once the old bytes are released must not hit the quota.
func TestPutOverwriteWithinQuota(t *testing.T) {
ctx := context.Background()
fs, err := New(t.TempDir(), 500)
require.NoError(t, err)
require.NoError(t, fs.Put(ctx, "npm/a/metadata", strings.NewReader(strings.Repeat("x", 400)), nil))
require.NoError(t, fs.Put(ctx, "npm/a/metadata", strings.NewReader(strings.Repeat("y", 450)), nil))
}