From d4bf7a3cb1cb170f7dd913a08699b9aacd73ad16 Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Sun, 9 Jul 2023 15:35:37 +0200 Subject: [PATCH] restic: cleanup lock test --- internal/restic/lock_test.go | 42 ++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/internal/restic/lock_test.go b/internal/restic/lock_test.go index 9e6bd3083..839ebefc7 100644 --- a/internal/restic/lock_test.go +++ b/internal/restic/lock_test.go @@ -253,15 +253,10 @@ func TestRemoveAllLocks(t *testing.T) { 3, processed) } -func TestLockRefresh(t *testing.T) { - repo := repository.TestRepository(t) - - lock, err := restic.NewLock(context.TODO(), repo) - rtest.OK(t, err) - time0 := lock.Time - +func checkSingleLock(t *testing.T, repo restic.Repository) restic.ID { + t.Helper() var lockID *restic.ID - err = repo.List(context.TODO(), restic.LockFile, func(id restic.ID, size int64) error { + err := repo.List(context.TODO(), restic.LockFile, func(id restic.ID, size int64) error { if lockID != nil { t.Error("more than one lock found") } @@ -271,25 +266,30 @@ func TestLockRefresh(t *testing.T) { if err != nil { t.Fatal(err) } + if lockID == nil { + t.Fatal("no lock found") + } + return *lockID +} + +func TestLockRefresh(t *testing.T) { + repo := repository.TestRepository(t) + restic.TestSetLockTimeout(t, 5*time.Millisecond) + + lock, err := restic.NewLock(context.TODO(), repo) + rtest.OK(t, err) + time0 := lock.Time + + lockID := checkSingleLock(t, repo) time.Sleep(time.Millisecond) rtest.OK(t, lock.Refresh(context.TODO())) - var lockID2 *restic.ID - err = repo.List(context.TODO(), restic.LockFile, func(id restic.ID, size int64) error { - if lockID2 != nil { - t.Error("more than one lock found") - } - lockID2 = &id - return nil - }) - if err != nil { - t.Fatal(err) - } + lockID2 := checkSingleLock(t, repo) - rtest.Assert(t, !lockID.Equal(*lockID2), + rtest.Assert(t, !lockID.Equal(lockID2), "expected a new ID after lock refresh, got the same") - lock2, err := restic.LoadLock(context.TODO(), repo, *lockID2) + lock2, err := restic.LoadLock(context.TODO(), repo, lockID2) rtest.OK(t, err) rtest.Assert(t, lock2.Time.After(time0), "expected a later timestamp after lock refresh")