mirror of
https://github.com/octoleo/restic.git
synced 2024-12-26 04:17:29 +00:00
cache: test forget behavior
This commit is contained in:
parent
3ff063e913
commit
5214af88e2
74
internal/cache/backend_test.go
vendored
74
internal/cache/backend_test.go
vendored
@ -5,6 +5,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"io"
|
"io"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
@ -91,7 +92,7 @@ func TestBackend(t *testing.T) {
|
|||||||
loadAndCompare(t, be, h, data)
|
loadAndCompare(t, be, h, data)
|
||||||
|
|
||||||
// load data via cache
|
// load data via cache
|
||||||
loadAndCompare(t, be, h, data)
|
loadAndCompare(t, wbe, h, data)
|
||||||
|
|
||||||
// remove directly
|
// remove directly
|
||||||
remove(t, be, h)
|
remove(t, be, h)
|
||||||
@ -114,6 +115,77 @@ func TestBackend(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type loadCountingBackend struct {
|
||||||
|
backend.Backend
|
||||||
|
ctr int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *loadCountingBackend) Load(ctx context.Context, h backend.Handle, length int, offset int64, fn func(rd io.Reader) error) error {
|
||||||
|
l.ctr++
|
||||||
|
return l.Backend.Load(ctx, h, length, offset, fn)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestOutOfBoundsAccess(t *testing.T) {
|
||||||
|
be := &loadCountingBackend{Backend: mem.New()}
|
||||||
|
c := TestNewCache(t)
|
||||||
|
wbe := c.Wrap(be)
|
||||||
|
|
||||||
|
h, data := randomData(50)
|
||||||
|
save(t, be, h, data)
|
||||||
|
|
||||||
|
// load out of bounds
|
||||||
|
err := wbe.Load(context.TODO(), h, 100, 100, func(rd io.Reader) error {
|
||||||
|
t.Error("cache returned non-existant file section")
|
||||||
|
return errors.New("broken")
|
||||||
|
})
|
||||||
|
test.Assert(t, strings.Contains(err.Error(), " is too short"), "expected too short error, got %v", err)
|
||||||
|
test.Equals(t, 1, be.ctr, "expected file to be loaded only once")
|
||||||
|
// file must nevertheless get cached
|
||||||
|
if !c.Has(h) {
|
||||||
|
t.Errorf("cache doesn't have file after load")
|
||||||
|
}
|
||||||
|
|
||||||
|
// start within bounds, but request too large chunk
|
||||||
|
err = wbe.Load(context.TODO(), h, 100, 0, func(rd io.Reader) error {
|
||||||
|
t.Error("cache returned non-existant file section")
|
||||||
|
return errors.New("broken")
|
||||||
|
})
|
||||||
|
test.Assert(t, strings.Contains(err.Error(), " is too short"), "expected too short error, got %v", err)
|
||||||
|
test.Equals(t, 1, be.ctr, "expected file to be loaded only once")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestForget(t *testing.T) {
|
||||||
|
be := &loadCountingBackend{Backend: mem.New()}
|
||||||
|
c := TestNewCache(t)
|
||||||
|
wbe := c.Wrap(be)
|
||||||
|
|
||||||
|
h, data := randomData(50)
|
||||||
|
save(t, be, h, data)
|
||||||
|
|
||||||
|
loadAndCompare(t, wbe, h, data)
|
||||||
|
test.Equals(t, 1, be.ctr, "expected file to be loaded once")
|
||||||
|
|
||||||
|
// must still exist even if load returns an error
|
||||||
|
exp := errors.New("error")
|
||||||
|
err := wbe.Load(context.TODO(), h, 0, 0, func(rd io.Reader) error {
|
||||||
|
return exp
|
||||||
|
})
|
||||||
|
test.Equals(t, exp, err, "wrong error")
|
||||||
|
test.Assert(t, c.Has(h), "missing cache entry")
|
||||||
|
|
||||||
|
test.OK(t, c.Forget(h))
|
||||||
|
test.Assert(t, !c.Has(h), "cache entry should have been removed")
|
||||||
|
|
||||||
|
// cache it again
|
||||||
|
loadAndCompare(t, wbe, h, data)
|
||||||
|
test.Assert(t, c.Has(h), "missing cache entry")
|
||||||
|
|
||||||
|
// forget must delete file only once
|
||||||
|
err = c.Forget(h)
|
||||||
|
test.Assert(t, strings.Contains(err.Error(), "circuit breaker prevents repeated deletion of cached file"), "wrong error message %q", err)
|
||||||
|
test.Assert(t, c.Has(h), "cache entry should still exist")
|
||||||
|
}
|
||||||
|
|
||||||
type loadErrorBackend struct {
|
type loadErrorBackend struct {
|
||||||
backend.Backend
|
backend.Backend
|
||||||
loadError error
|
loadError error
|
||||||
|
Loading…
Reference in New Issue
Block a user