From 6227821b4ed27a7ec995edb9a0ad3c49ea5e024f Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Mon, 1 Aug 2016 20:24:15 +0200 Subject: [PATCH] Move functions to correct file --- src/restic/repository/index_rebuild_test.go | 112 -------------------- src/restic/repository/repack_test.go | 104 ++++++++++++++++++ 2 files changed, 104 insertions(+), 112 deletions(-) delete mode 100644 src/restic/repository/index_rebuild_test.go diff --git a/src/restic/repository/index_rebuild_test.go b/src/restic/repository/index_rebuild_test.go deleted file mode 100644 index 7b1683203..000000000 --- a/src/restic/repository/index_rebuild_test.go +++ /dev/null @@ -1,112 +0,0 @@ -package repository_test - -import ( - "io" - "math/rand" - "restic/backend" - "restic/pack" - "restic/repository" - "testing" -) - -func randomSize(min, max int) int { - return rand.Intn(max-min) + min -} - -func random(t *testing.T, length int) []byte { - rd := repository.NewRandReader(rand.New(rand.NewSource(int64(length)))) - buf := make([]byte, length) - _, err := io.ReadFull(rd, buf) - if err != nil { - t.Fatalf("unable to read %d random bytes: %v", length, err) - } - - return buf -} - -func createRandomBlobs(t *testing.T, repo *repository.Repository, blobs int, pData float32) { - for i := 0; i < blobs; i++ { - var ( - tpe pack.BlobType - length int - ) - - if rand.Float32() < pData { - tpe = pack.Data - length = randomSize(50*1024, 2*1024*1024) // 50KiB to 2MiB of data - } else { - tpe = pack.Tree - length = randomSize(5*1024, 50*1024) // 5KiB to 50KiB - } - - _, err := repo.SaveAndEncrypt(tpe, random(t, length), nil) - if err != nil { - t.Fatalf("SaveFrom() error %v", err) - } - - if rand.Float32() < 0.2 { - if err = repo.Flush(); err != nil { - t.Fatalf("repo.Flush() returned error %v", err) - } - } - } - - if err := repo.Flush(); err != nil { - t.Fatalf("repo.Flush() returned error %v", err) - } -} - -// selectBlobs splits the list of all blobs randomly into two lists. A blob -// will be contained in the firstone ith probability p. -func selectBlobs(t *testing.T, repo *repository.Repository, p float32) (list1, list2 backend.IDSet) { - done := make(chan struct{}) - defer close(done) - - list1 = backend.NewIDSet() - list2 = backend.NewIDSet() - - for id := range repo.List(backend.Data, done) { - entries, err := repo.ListPack(id) - if err != nil { - t.Fatalf("error listing pack %v: %v", id, err) - } - - for _, entry := range entries { - if rand.Float32() <= p { - list1.Insert(entry.ID) - } else { - list2.Insert(entry.ID) - } - } - } - - return list1, list2 -} - -func listPacks(t *testing.T, repo *repository.Repository) backend.IDSet { - done := make(chan struct{}) - defer close(done) - - list := backend.NewIDSet() - for id := range repo.List(backend.Data, done) { - list.Insert(id) - } - - return list -} - -func findPacksForBlobs(t *testing.T, repo *repository.Repository, blobs backend.IDSet) backend.IDSet { - packs := backend.NewIDSet() - - idx := repo.Index() - for id := range blobs { - pb, err := idx.Lookup(id) - if err != nil { - t.Fatal(err) - } - - packs.Insert(pb.PackID) - } - - return packs -} diff --git a/src/restic/repository/repack_test.go b/src/restic/repository/repack_test.go index 145736fab..9b40e92bd 100644 --- a/src/restic/repository/repack_test.go +++ b/src/restic/repository/repack_test.go @@ -1,12 +1,116 @@ package repository_test import ( + "io" "math/rand" "restic/backend" + "restic/pack" "restic/repository" "testing" ) +func randomSize(min, max int) int { + return rand.Intn(max-min) + min +} + +func random(t *testing.T, length int) []byte { + rd := repository.NewRandReader(rand.New(rand.NewSource(int64(length)))) + buf := make([]byte, length) + _, err := io.ReadFull(rd, buf) + if err != nil { + t.Fatalf("unable to read %d random bytes: %v", length, err) + } + + return buf +} + +func createRandomBlobs(t *testing.T, repo *repository.Repository, blobs int, pData float32) { + for i := 0; i < blobs; i++ { + var ( + tpe pack.BlobType + length int + ) + + if rand.Float32() < pData { + tpe = pack.Data + length = randomSize(50*1024, 2*1024*1024) // 50KiB to 2MiB of data + } else { + tpe = pack.Tree + length = randomSize(5*1024, 50*1024) // 5KiB to 50KiB + } + + _, err := repo.SaveAndEncrypt(tpe, random(t, length), nil) + if err != nil { + t.Fatalf("SaveFrom() error %v", err) + } + + if rand.Float32() < 0.2 { + if err = repo.Flush(); err != nil { + t.Fatalf("repo.Flush() returned error %v", err) + } + } + } + + if err := repo.Flush(); err != nil { + t.Fatalf("repo.Flush() returned error %v", err) + } +} + +// selectBlobs splits the list of all blobs randomly into two lists. A blob +// will be contained in the firstone ith probability p. +func selectBlobs(t *testing.T, repo *repository.Repository, p float32) (list1, list2 backend.IDSet) { + done := make(chan struct{}) + defer close(done) + + list1 = backend.NewIDSet() + list2 = backend.NewIDSet() + + for id := range repo.List(backend.Data, done) { + entries, err := repo.ListPack(id) + if err != nil { + t.Fatalf("error listing pack %v: %v", id, err) + } + + for _, entry := range entries { + if rand.Float32() <= p { + list1.Insert(entry.ID) + } else { + list2.Insert(entry.ID) + } + } + } + + return list1, list2 +} + +func listPacks(t *testing.T, repo *repository.Repository) backend.IDSet { + done := make(chan struct{}) + defer close(done) + + list := backend.NewIDSet() + for id := range repo.List(backend.Data, done) { + list.Insert(id) + } + + return list +} + +func findPacksForBlobs(t *testing.T, repo *repository.Repository, blobs backend.IDSet) backend.IDSet { + packs := backend.NewIDSet() + + idx := repo.Index() + for id := range blobs { + pb, err := idx.Lookup(id) + if err != nil { + t.Fatal(err) + } + + packs.Insert(pb.PackID) + } + + return packs +} + func repack(t *testing.T, repo *repository.Repository, packs, blobs backend.IDSet) { err := repository.Repack(repo, packs, blobs) if err != nil {