From 1058a91b39fdbf901fd0187b209d63c2c0c63754 Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Sun, 7 Aug 2016 21:56:06 +0200 Subject: [PATCH] Add option to create duplicate blobs in TestCreateSnapshot --- src/restic/find_test.go | 2 +- src/restic/index/index_test.go | 10 +++++----- src/restic/testing.go | 23 +++++++++++++++-------- src/restic/testing_test.go | 4 ++-- 4 files changed, 23 insertions(+), 16 deletions(-) diff --git a/src/restic/find_test.go b/src/restic/find_test.go index b3050e269..17fbc83b0 100644 --- a/src/restic/find_test.go +++ b/src/restic/find_test.go @@ -85,7 +85,7 @@ func TestFindUsedBlobs(t *testing.T) { var snapshots []*Snapshot for i := 0; i < findTestSnapshots; i++ { - sn := TestCreateSnapshot(t, repo, findTestTime.Add(time.Duration(i)*time.Second), findTestDepth) + sn := TestCreateSnapshot(t, repo, findTestTime.Add(time.Duration(i)*time.Second), findTestDepth, 0) t.Logf("snapshot %v saved, tree %v", sn.ID().Str(), sn.Tree.Str()) snapshots = append(snapshots, sn) } diff --git a/src/restic/index/index_test.go b/src/restic/index/index_test.go index 335b93051..307a1c32e 100644 --- a/src/restic/index/index_test.go +++ b/src/restic/index/index_test.go @@ -15,11 +15,11 @@ var ( depth = 3 ) -func createFilledRepo(t testing.TB, snapshots int) (*repository.Repository, func()) { +func createFilledRepo(t testing.TB, snapshots int, dup float32) (*repository.Repository, func()) { repo, cleanup := repository.TestRepository(t) for i := 0; i < 3; i++ { - restic.TestCreateSnapshot(t, repo, snapshotTime.Add(time.Duration(i)*time.Second), depth) + restic.TestCreateSnapshot(t, repo, snapshotTime.Add(time.Duration(i)*time.Second), depth, dup) } return repo, cleanup @@ -34,7 +34,7 @@ func validateIndex(t testing.TB, repo *repository.Repository, idx *Index) { } func TestIndexNew(t *testing.T) { - repo, cleanup := createFilledRepo(t, 3) + repo, cleanup := createFilledRepo(t, 3, 0) defer cleanup() idx, err := New(repo) @@ -50,7 +50,7 @@ func TestIndexNew(t *testing.T) { } func TestIndexLoad(t *testing.T) { - repo, cleanup := createFilledRepo(t, 3) + repo, cleanup := createFilledRepo(t, 3, 0) defer cleanup() loadIdx, err := Load(repo) @@ -137,7 +137,7 @@ func openRepo(t testing.TB, dir, password string) *repository.Repository { } func BenchmarkIndexNew(b *testing.B) { - repo, cleanup := createFilledRepo(b, 3) + repo, cleanup := createFilledRepo(b, 3, 0) defer cleanup() b.ResetTimer() diff --git a/src/restic/testing.go b/src/restic/testing.go index ce4e98cfa..873a9ed0b 100644 --- a/src/restic/testing.go +++ b/src/restic/testing.go @@ -20,9 +20,10 @@ func fakeFile(t testing.TB, seed, size int64) io.Reader { } type fakeFileSystem struct { - t testing.TB - repo *repository.Repository - knownBlobs backend.IDSet + t testing.TB + repo *repository.Repository + knownBlobs backend.IDSet + duplication float32 } // saveFile reads from rd and saves the blobs in the repository. The list of @@ -77,6 +78,10 @@ func (fs fakeFileSystem) treeIsKnown(tree *Tree) (bool, backend.ID) { } func (fs fakeFileSystem) blobIsKnown(id backend.ID, t pack.BlobType) bool { + if rand.Float32() < fs.duplication { + return false + } + if fs.knownBlobs.Has(id) { return true } @@ -142,8 +147,9 @@ func (fs fakeFileSystem) saveTree(seed int64, depth int) backend.ID { // TestCreateSnapshot creates a snapshot filled with fake data. The // fake data is generated deterministically from the timestamp `at`, which is // also used as the snapshot's timestamp. The tree's depth can be specified -// with the parameter depth. -func TestCreateSnapshot(t testing.TB, repo *repository.Repository, at time.Time, depth int) *Snapshot { +// with the parameter depth. The parameter duplication is a probability that +// the same blob will saved again. +func TestCreateSnapshot(t testing.TB, repo *repository.Repository, at time.Time, depth int, duplication float32) *Snapshot { seed := at.Unix() t.Logf("create fake snapshot at %s with seed %d", at, seed) @@ -155,9 +161,10 @@ func TestCreateSnapshot(t testing.TB, repo *repository.Repository, at time.Time, snapshot.Time = at fs := fakeFileSystem{ - t: t, - repo: repo, - knownBlobs: backend.NewIDSet(), + t: t, + repo: repo, + knownBlobs: backend.NewIDSet(), + duplication: duplication, } treeID := fs.saveTree(seed, depth) diff --git a/src/restic/testing_test.go b/src/restic/testing_test.go index 1427d4a62..3c5ea5a6f 100644 --- a/src/restic/testing_test.go +++ b/src/restic/testing_test.go @@ -20,7 +20,7 @@ func TestCreateSnapshot(t *testing.T) { defer cleanup() for i := 0; i < testCreateSnapshots; i++ { - restic.TestCreateSnapshot(t, repo, testSnapshotTime.Add(time.Duration(i)*time.Second), testDepth) + restic.TestCreateSnapshot(t, repo, testSnapshotTime.Add(time.Duration(i)*time.Second), testDepth, 0) } snapshots, err := restic.LoadAllSnapshots(repo) @@ -55,7 +55,7 @@ func BenchmarkCreateSnapshot(b *testing.B) { b.ResetTimer() for i := 0; i < b.N; i++ { - restic.TestCreateSnapshot(b, repo, testSnapshotTime, testDepth) + restic.TestCreateSnapshot(b, repo, testSnapshotTime, testDepth, 0) restic.TestResetRepository(b, repo) } }