2024-02-24 15:26:29 +00:00
|
|
|
package repository
|
2021-11-14 16:38:56 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2023-04-07 17:51:31 +00:00
|
|
|
"runtime"
|
2023-02-16 15:58:36 +00:00
|
|
|
"strings"
|
2023-07-15 16:18:35 +00:00
|
|
|
"sync"
|
2021-11-14 16:38:56 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2023-10-01 09:40:12 +00:00
|
|
|
"github.com/restic/restic/internal/backend"
|
2023-07-09 13:18:16 +00:00
|
|
|
"github.com/restic/restic/internal/backend/mem"
|
2023-07-15 16:18:35 +00:00
|
|
|
"github.com/restic/restic/internal/debug"
|
2021-11-14 16:38:56 +00:00
|
|
|
"github.com/restic/restic/internal/restic"
|
2023-02-16 15:58:36 +00:00
|
|
|
"github.com/restic/restic/internal/test"
|
2024-02-24 15:26:29 +00:00
|
|
|
rtest "github.com/restic/restic/internal/test"
|
2021-11-14 16:38:56 +00:00
|
|
|
)
|
|
|
|
|
2024-02-24 15:26:29 +00:00
|
|
|
type backendWrapper func(r backend.Backend) (backend.Backend, error)
|
2023-07-09 13:18:16 +00:00
|
|
|
|
2024-02-24 15:26:29 +00:00
|
|
|
func openLockTestRepo(t *testing.T, wrapper backendWrapper) restic.Repository {
|
|
|
|
be := backend.Backend(mem.New())
|
|
|
|
// initialize repo
|
|
|
|
TestRepositoryWithBackend(t, be, 0, Options{})
|
2023-07-09 13:18:16 +00:00
|
|
|
|
2024-02-24 15:26:29 +00:00
|
|
|
// reopen repository to allow injecting a backend wrapper
|
2021-11-14 16:38:56 +00:00
|
|
|
if wrapper != nil {
|
2024-02-24 15:26:29 +00:00
|
|
|
var err error
|
|
|
|
be, err = wrapper(be)
|
|
|
|
rtest.OK(t, err)
|
2021-11-14 16:38:56 +00:00
|
|
|
}
|
|
|
|
|
2024-02-24 15:26:29 +00:00
|
|
|
repo, err := New(be, Options{})
|
|
|
|
rtest.OK(t, err)
|
|
|
|
rtest.OK(t, repo.SearchKey(context.TODO(), test.TestPassword, 1, ""))
|
|
|
|
return repo
|
2021-11-14 16:38:56 +00:00
|
|
|
}
|
|
|
|
|
2024-02-24 16:07:14 +00:00
|
|
|
func checkedLockRepo(ctx context.Context, t *testing.T, repo restic.Repository, lockerInst *locker, retryLock time.Duration) (*Unlocker, context.Context) {
|
|
|
|
lock, wrappedCtx, err := lockerInst.Lock(ctx, repo, false, retryLock, func(msg string) {}, func(format string, args ...interface{}) {})
|
2023-05-18 15:44:56 +00:00
|
|
|
test.OK(t, err)
|
|
|
|
test.OK(t, wrappedCtx.Err())
|
2024-02-24 15:45:57 +00:00
|
|
|
if lock.info.lock.Stale() {
|
2021-11-14 16:38:56 +00:00
|
|
|
t.Fatal("lock returned stale lock")
|
|
|
|
}
|
|
|
|
return lock, wrappedCtx
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLock(t *testing.T) {
|
2024-02-24 16:07:14 +00:00
|
|
|
t.Parallel()
|
2024-02-24 15:26:29 +00:00
|
|
|
repo := openLockTestRepo(t, nil)
|
2021-11-14 16:38:56 +00:00
|
|
|
|
2024-02-24 16:07:14 +00:00
|
|
|
lock, wrappedCtx := checkedLockRepo(context.Background(), t, repo, lockerInst, 0)
|
2024-02-24 15:45:57 +00:00
|
|
|
lock.Unlock()
|
2021-11-14 16:38:56 +00:00
|
|
|
if wrappedCtx.Err() == nil {
|
|
|
|
t.Fatal("unlock did not cancel context")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLockCancel(t *testing.T) {
|
2024-02-24 16:07:14 +00:00
|
|
|
t.Parallel()
|
2024-02-24 15:26:29 +00:00
|
|
|
repo := openLockTestRepo(t, nil)
|
2021-11-14 16:38:56 +00:00
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
2024-02-24 16:07:14 +00:00
|
|
|
lock, wrappedCtx := checkedLockRepo(ctx, t, repo, lockerInst, 0)
|
2021-11-14 16:38:56 +00:00
|
|
|
cancel()
|
|
|
|
if wrappedCtx.Err() == nil {
|
|
|
|
t.Fatal("canceled parent context did not cancel context")
|
|
|
|
}
|
|
|
|
|
2024-02-24 15:26:29 +00:00
|
|
|
// Unlock should not crash
|
2024-02-24 15:45:57 +00:00
|
|
|
lock.Unlock()
|
2021-11-14 16:38:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLockConflict(t *testing.T) {
|
2024-02-24 16:07:14 +00:00
|
|
|
t.Parallel()
|
2024-02-24 15:26:29 +00:00
|
|
|
repo := openLockTestRepo(t, nil)
|
|
|
|
repo2, err := New(repo.Backend(), Options{})
|
2023-05-18 15:44:56 +00:00
|
|
|
test.OK(t, err)
|
2024-02-24 15:26:29 +00:00
|
|
|
test.OK(t, repo2.SearchKey(context.TODO(), test.TestPassword, 1, ""))
|
2021-11-14 16:38:56 +00:00
|
|
|
|
2024-02-24 15:26:29 +00:00
|
|
|
lock, _, err := Lock(context.Background(), repo, true, 0, func(msg string) {}, func(format string, args ...interface{}) {})
|
2023-05-18 15:44:56 +00:00
|
|
|
test.OK(t, err)
|
2024-02-24 15:45:57 +00:00
|
|
|
defer lock.Unlock()
|
2024-02-24 15:26:29 +00:00
|
|
|
_, _, err = Lock(context.Background(), repo2, false, 0, func(msg string) {}, func(format string, args ...interface{}) {})
|
2021-11-14 16:38:56 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("second lock should have failed")
|
|
|
|
}
|
2023-06-02 21:16:49 +00:00
|
|
|
test.Assert(t, restic.IsAlreadyLocked(err), "unexpected error %v", err)
|
2021-11-14 16:38:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type writeOnceBackend struct {
|
2023-10-01 09:40:12 +00:00
|
|
|
backend.Backend
|
2021-11-14 16:38:56 +00:00
|
|
|
written bool
|
|
|
|
}
|
|
|
|
|
2023-10-01 09:40:12 +00:00
|
|
|
func (b *writeOnceBackend) Save(ctx context.Context, h backend.Handle, rd backend.RewindReader) error {
|
2021-11-14 16:38:56 +00:00
|
|
|
if b.written {
|
|
|
|
return fmt.Errorf("fail after first write")
|
|
|
|
}
|
|
|
|
b.written = true
|
|
|
|
return b.Backend.Save(ctx, h, rd)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLockFailedRefresh(t *testing.T) {
|
2024-02-24 16:07:14 +00:00
|
|
|
t.Parallel()
|
2024-02-24 15:26:29 +00:00
|
|
|
repo := openLockTestRepo(t, func(r backend.Backend) (backend.Backend, error) {
|
2021-11-14 16:38:56 +00:00
|
|
|
return &writeOnceBackend{Backend: r}, nil
|
|
|
|
})
|
|
|
|
|
|
|
|
// reduce locking intervals to be suitable for testing
|
2024-02-24 16:07:14 +00:00
|
|
|
li := &locker{
|
|
|
|
retrySleepStart: lockerInst.retrySleepStart,
|
|
|
|
retrySleepMax: lockerInst.retrySleepMax,
|
|
|
|
refreshInterval: 20 * time.Millisecond,
|
|
|
|
refreshabilityTimeout: 100 * time.Millisecond,
|
|
|
|
}
|
|
|
|
lock, wrappedCtx := checkedLockRepo(context.Background(), t, repo, li, 0)
|
2021-11-14 16:38:56 +00:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-wrappedCtx.Done():
|
|
|
|
// expected lock refresh failure
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
t.Fatal("failed lock refresh did not cause context cancellation")
|
|
|
|
}
|
2024-02-24 15:26:29 +00:00
|
|
|
// Unlock should not crash
|
2024-02-24 15:45:57 +00:00
|
|
|
lock.Unlock()
|
2021-11-14 16:38:56 +00:00
|
|
|
}
|
2022-10-29 09:26:00 +00:00
|
|
|
|
2022-12-03 11:05:38 +00:00
|
|
|
type loggingBackend struct {
|
2023-10-01 09:40:12 +00:00
|
|
|
backend.Backend
|
2022-12-03 11:05:38 +00:00
|
|
|
t *testing.T
|
|
|
|
}
|
|
|
|
|
2023-10-01 09:40:12 +00:00
|
|
|
func (b *loggingBackend) Save(ctx context.Context, h backend.Handle, rd backend.RewindReader) error {
|
2022-12-03 11:05:38 +00:00
|
|
|
b.t.Logf("save %v @ %v", h, time.Now())
|
2023-04-07 17:43:00 +00:00
|
|
|
err := b.Backend.Save(ctx, h, rd)
|
|
|
|
b.t.Logf("save finished %v @ %v", h, time.Now())
|
|
|
|
return err
|
2022-12-03 11:05:38 +00:00
|
|
|
}
|
|
|
|
|
2022-10-29 09:26:00 +00:00
|
|
|
func TestLockSuccessfulRefresh(t *testing.T) {
|
2024-02-24 16:07:14 +00:00
|
|
|
t.Parallel()
|
2024-02-24 15:26:29 +00:00
|
|
|
repo := openLockTestRepo(t, func(r backend.Backend) (backend.Backend, error) {
|
2022-12-03 11:05:38 +00:00
|
|
|
return &loggingBackend{
|
|
|
|
Backend: r,
|
|
|
|
t: t,
|
|
|
|
}, nil
|
|
|
|
})
|
2022-10-29 09:26:00 +00:00
|
|
|
|
2022-12-03 11:05:38 +00:00
|
|
|
t.Logf("test for successful lock refresh %v", time.Now())
|
2022-10-29 09:26:00 +00:00
|
|
|
// reduce locking intervals to be suitable for testing
|
2024-02-24 16:07:14 +00:00
|
|
|
li := &locker{
|
|
|
|
retrySleepStart: lockerInst.retrySleepStart,
|
|
|
|
retrySleepMax: lockerInst.retrySleepMax,
|
|
|
|
refreshInterval: 60 * time.Millisecond,
|
|
|
|
refreshabilityTimeout: 500 * time.Millisecond,
|
|
|
|
}
|
|
|
|
lock, wrappedCtx := checkedLockRepo(context.Background(), t, repo, li, 0)
|
2022-10-29 09:26:00 +00:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-wrappedCtx.Done():
|
2023-04-07 17:43:00 +00:00
|
|
|
// don't call t.Fatal to allow the lock to be properly cleaned up
|
|
|
|
t.Error("lock refresh failed", time.Now())
|
2023-04-07 17:51:31 +00:00
|
|
|
|
|
|
|
// Dump full stacktrace
|
|
|
|
buf := make([]byte, 1024*1024)
|
|
|
|
n := runtime.Stack(buf, true)
|
|
|
|
buf = buf[:n]
|
|
|
|
t.Log(string(buf))
|
|
|
|
|
2024-02-24 16:07:14 +00:00
|
|
|
case <-time.After(2 * li.refreshabilityTimeout):
|
2022-10-29 09:26:00 +00:00
|
|
|
// expected lock refresh to work
|
|
|
|
}
|
2024-02-24 15:26:29 +00:00
|
|
|
// Unlock should not crash
|
2024-02-24 15:45:57 +00:00
|
|
|
lock.Unlock()
|
2022-10-29 09:26:00 +00:00
|
|
|
}
|
2023-02-16 15:58:36 +00:00
|
|
|
|
2023-07-15 16:18:35 +00:00
|
|
|
type slowBackend struct {
|
2023-10-01 09:40:12 +00:00
|
|
|
backend.Backend
|
2023-07-15 16:18:35 +00:00
|
|
|
m sync.Mutex
|
|
|
|
sleep time.Duration
|
|
|
|
}
|
|
|
|
|
2023-10-01 09:40:12 +00:00
|
|
|
func (b *slowBackend) Save(ctx context.Context, h backend.Handle, rd backend.RewindReader) error {
|
2023-07-15 16:18:35 +00:00
|
|
|
b.m.Lock()
|
|
|
|
sleep := b.sleep
|
|
|
|
b.m.Unlock()
|
|
|
|
time.Sleep(sleep)
|
|
|
|
return b.Backend.Save(ctx, h, rd)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLockSuccessfulStaleRefresh(t *testing.T) {
|
2024-02-24 16:07:14 +00:00
|
|
|
t.Parallel()
|
2023-07-15 16:18:35 +00:00
|
|
|
var sb *slowBackend
|
2024-02-24 15:26:29 +00:00
|
|
|
repo := openLockTestRepo(t, func(r backend.Backend) (backend.Backend, error) {
|
2023-07-15 16:18:35 +00:00
|
|
|
sb = &slowBackend{Backend: r}
|
|
|
|
return sb, nil
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Logf("test for successful lock refresh %v", time.Now())
|
|
|
|
// reduce locking intervals to be suitable for testing
|
2024-02-24 16:07:14 +00:00
|
|
|
li := &locker{
|
|
|
|
retrySleepStart: lockerInst.retrySleepStart,
|
|
|
|
retrySleepMax: lockerInst.retrySleepMax,
|
|
|
|
refreshInterval: 10 * time.Millisecond,
|
|
|
|
refreshabilityTimeout: 50 * time.Millisecond,
|
|
|
|
}
|
|
|
|
|
|
|
|
lock, wrappedCtx := checkedLockRepo(context.Background(), t, repo, li, 0)
|
2023-07-15 16:18:35 +00:00
|
|
|
// delay lock refreshing long enough that the lock would expire
|
|
|
|
sb.m.Lock()
|
2024-02-24 16:07:14 +00:00
|
|
|
sb.sleep = li.refreshabilityTimeout + li.refreshInterval
|
2023-07-15 16:18:35 +00:00
|
|
|
sb.m.Unlock()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-wrappedCtx.Done():
|
|
|
|
// don't call t.Fatal to allow the lock to be properly cleaned up
|
|
|
|
t.Error("lock refresh failed", time.Now())
|
|
|
|
|
2024-02-24 16:07:14 +00:00
|
|
|
case <-time.After(li.refreshabilityTimeout):
|
2023-07-15 16:18:35 +00:00
|
|
|
}
|
|
|
|
// reset slow backend
|
|
|
|
sb.m.Lock()
|
|
|
|
sb.sleep = 0
|
|
|
|
sb.m.Unlock()
|
|
|
|
debug.Log("normal lock period has expired")
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-wrappedCtx.Done():
|
|
|
|
// don't call t.Fatal to allow the lock to be properly cleaned up
|
|
|
|
t.Error("lock refresh failed", time.Now())
|
|
|
|
|
2024-02-24 16:07:14 +00:00
|
|
|
case <-time.After(3 * li.refreshabilityTimeout):
|
2023-07-15 16:18:35 +00:00
|
|
|
// expected lock refresh to work
|
|
|
|
}
|
|
|
|
|
2024-02-24 15:26:29 +00:00
|
|
|
// Unlock should not crash
|
2024-02-24 15:45:57 +00:00
|
|
|
lock.Unlock()
|
2023-07-15 16:18:35 +00:00
|
|
|
}
|
|
|
|
|
2023-02-16 15:58:36 +00:00
|
|
|
func TestLockWaitTimeout(t *testing.T) {
|
2024-02-24 15:45:57 +00:00
|
|
|
t.Parallel()
|
2024-02-24 15:26:29 +00:00
|
|
|
repo := openLockTestRepo(t, nil)
|
2023-02-16 15:58:36 +00:00
|
|
|
|
2024-02-24 15:26:29 +00:00
|
|
|
elock, _, err := Lock(context.TODO(), repo, true, 0, func(msg string) {}, func(format string, args ...interface{}) {})
|
2023-02-16 15:58:36 +00:00
|
|
|
test.OK(t, err)
|
2024-02-24 15:45:57 +00:00
|
|
|
defer elock.Unlock()
|
2023-02-16 15:58:36 +00:00
|
|
|
|
2023-04-22 10:45:59 +00:00
|
|
|
retryLock := 200 * time.Millisecond
|
2023-02-16 15:58:36 +00:00
|
|
|
|
|
|
|
start := time.Now()
|
2024-02-24 15:45:57 +00:00
|
|
|
_, _, err = Lock(context.TODO(), repo, false, retryLock, func(msg string) {}, func(format string, args ...interface{}) {})
|
2023-02-16 15:58:36 +00:00
|
|
|
duration := time.Since(start)
|
|
|
|
|
|
|
|
test.Assert(t, err != nil,
|
|
|
|
"create normal lock with exclusively locked repo didn't return an error")
|
|
|
|
test.Assert(t, strings.Contains(err.Error(), "repository is already locked exclusively"),
|
|
|
|
"create normal lock with exclusively locked repo didn't return the correct error")
|
2023-04-22 10:45:59 +00:00
|
|
|
test.Assert(t, retryLock <= duration && duration < retryLock*3/2,
|
2023-02-16 15:58:36 +00:00
|
|
|
"create normal lock with exclusively locked repo didn't wait for the specified timeout")
|
|
|
|
}
|
2023-07-09 13:15:43 +00:00
|
|
|
|
2023-02-16 15:58:36 +00:00
|
|
|
func TestLockWaitCancel(t *testing.T) {
|
2024-02-24 15:45:57 +00:00
|
|
|
t.Parallel()
|
2024-02-24 15:26:29 +00:00
|
|
|
repo := openLockTestRepo(t, nil)
|
2023-02-16 15:58:36 +00:00
|
|
|
|
2024-02-24 15:26:29 +00:00
|
|
|
elock, _, err := Lock(context.TODO(), repo, true, 0, func(msg string) {}, func(format string, args ...interface{}) {})
|
2023-02-16 15:58:36 +00:00
|
|
|
test.OK(t, err)
|
2024-02-24 15:45:57 +00:00
|
|
|
defer elock.Unlock()
|
2023-02-16 15:58:36 +00:00
|
|
|
|
2023-04-22 10:45:59 +00:00
|
|
|
retryLock := 200 * time.Millisecond
|
2023-02-16 15:58:36 +00:00
|
|
|
cancelAfter := 40 * time.Millisecond
|
|
|
|
|
2023-07-16 13:00:48 +00:00
|
|
|
start := time.Now()
|
2023-02-16 15:58:36 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.TODO())
|
|
|
|
time.AfterFunc(cancelAfter, cancel)
|
|
|
|
|
2024-02-24 15:45:57 +00:00
|
|
|
_, _, err = Lock(ctx, repo, false, retryLock, func(msg string) {}, func(format string, args ...interface{}) {})
|
2023-02-16 15:58:36 +00:00
|
|
|
duration := time.Since(start)
|
|
|
|
|
|
|
|
test.Assert(t, err != nil,
|
|
|
|
"create normal lock with exclusively locked repo didn't return an error")
|
|
|
|
test.Assert(t, strings.Contains(err.Error(), "context canceled"),
|
|
|
|
"create normal lock with exclusively locked repo didn't return the correct error")
|
2023-04-22 10:45:59 +00:00
|
|
|
test.Assert(t, cancelAfter <= duration && duration < retryLock-10*time.Millisecond,
|
2023-07-15 16:31:52 +00:00
|
|
|
"create normal lock with exclusively locked repo didn't return in time, duration %v", duration)
|
2023-02-16 15:58:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLockWaitSuccess(t *testing.T) {
|
2024-02-24 15:45:57 +00:00
|
|
|
t.Parallel()
|
2024-02-24 15:26:29 +00:00
|
|
|
repo := openLockTestRepo(t, nil)
|
2023-02-16 15:58:36 +00:00
|
|
|
|
2024-02-24 15:26:29 +00:00
|
|
|
elock, _, err := Lock(context.TODO(), repo, true, 0, func(msg string) {}, func(format string, args ...interface{}) {})
|
2023-02-16 15:58:36 +00:00
|
|
|
test.OK(t, err)
|
|
|
|
|
2023-04-22 10:45:59 +00:00
|
|
|
retryLock := 200 * time.Millisecond
|
2023-02-16 15:58:36 +00:00
|
|
|
unlockAfter := 40 * time.Millisecond
|
|
|
|
|
|
|
|
time.AfterFunc(unlockAfter, func() {
|
2024-02-24 15:45:57 +00:00
|
|
|
elock.Unlock()
|
2023-02-16 15:58:36 +00:00
|
|
|
})
|
|
|
|
|
2024-02-24 15:26:29 +00:00
|
|
|
lock, _, err := Lock(context.TODO(), repo, false, retryLock, func(msg string) {}, func(format string, args ...interface{}) {})
|
2023-02-16 15:58:36 +00:00
|
|
|
test.OK(t, err)
|
2024-02-24 15:45:57 +00:00
|
|
|
lock.Unlock()
|
2023-02-16 15:58:36 +00:00
|
|
|
}
|