repository: replace StartPackUploader+Flush with WithBlobUploader

The new method combines both step into a single wrapper function. Thus
it ensures that both are always called in pairs. As an additional
benefit this slightly reduces the boilerplate to upload blobs.
This commit is contained in:
Michael Eischer
2025-10-08 22:49:45 +02:00
parent 481fcb9ca7
commit ac4642b479
19 changed files with 415 additions and 500 deletions
+6 -10
View File
@@ -10,7 +10,7 @@ import (
"github.com/restic/chunker"
"github.com/restic/restic/internal/restic"
"golang.org/x/sync/errgroup"
"github.com/restic/restic/internal/test"
)
// fakeFile returns a reader which yields deterministic pseudo-random data.
@@ -135,17 +135,13 @@ func TestCreateSnapshot(t testing.TB, repo restic.Repository, at time.Time, dept
rand: rand.New(rand.NewSource(seed)),
}
var wg errgroup.Group
repo.StartPackUploader(context.TODO(), &wg)
treeID := fs.saveTree(context.TODO(), seed, depth)
var treeID restic.ID
test.OK(t, repo.WithBlobUploader(context.TODO(), func(ctx context.Context) error {
treeID = fs.saveTree(ctx, seed, depth)
return nil
}))
snapshot.Tree = &treeID
err = repo.Flush(context.Background())
if err != nil {
t.Fatal(err)
}
id, err := SaveSnapshot(context.TODO(), repo, snapshot)
if err != nil {
t.Fatal(err)
+7 -11
View File
@@ -15,7 +15,6 @@ import (
"github.com/restic/restic/internal/repository"
"github.com/restic/restic/internal/restic"
rtest "github.com/restic/restic/internal/test"
"golang.org/x/sync/errgroup"
)
var testFiles = []struct {
@@ -106,15 +105,14 @@ func TestNodeComparison(t *testing.T) {
func TestEmptyLoadTree(t *testing.T) {
repo := repository.TestRepository(t)
var wg errgroup.Group
repo.StartPackUploader(context.TODO(), &wg)
// save tree
tree := data.NewTree(0)
id, err := data.SaveTree(context.TODO(), repo, tree)
rtest.OK(t, err)
// save packs
rtest.OK(t, repo.Flush(context.Background()))
var id restic.ID
rtest.OK(t, repo.WithBlobUploader(context.TODO(), func(ctx context.Context) error {
var err error
// save tree
id, err = data.SaveTree(ctx, repo, tree)
return err
}))
// load tree again
tree2, err := data.LoadTree(context.TODO(), repo, id)
@@ -187,7 +185,6 @@ func testLoadTree(t *testing.T, version uint) {
// archive a few files
repo, _, _ := repository.TestRepositoryWithVersion(t, version)
sn := archiver.TestSnapshot(t, repo, rtest.BenchArchiveDirectory, nil)
rtest.OK(t, repo.Flush(context.Background()))
_, err := data.LoadTree(context.TODO(), repo, *sn.Tree)
rtest.OK(t, err)
@@ -205,7 +202,6 @@ func benchmarkLoadTree(t *testing.B, version uint) {
// archive a few files
repo, _, _ := repository.TestRepositoryWithVersion(t, version)
sn := archiver.TestSnapshot(t, repo, rtest.BenchArchiveDirectory, nil)
rtest.OK(t, repo.Flush(context.Background()))
t.ResetTimer()