1
0
Fork 0
mirror of https://github.com/restic/restic.git synced 2024-12-26 01:37:12 +00:00

Add option to create duplicate blobs in TestCreateSnapshot

This commit is contained in:
Alexander Neumann 2016-08-07 21:56:06 +02:00
parent 240b8f273a
commit 1058a91b39
4 changed files with 23 additions and 16 deletions

View file

@ -85,7 +85,7 @@ func TestFindUsedBlobs(t *testing.T) {
var snapshots []*Snapshot var snapshots []*Snapshot
for i := 0; i < findTestSnapshots; i++ { 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()) t.Logf("snapshot %v saved, tree %v", sn.ID().Str(), sn.Tree.Str())
snapshots = append(snapshots, sn) snapshots = append(snapshots, sn)
} }

View file

@ -15,11 +15,11 @@ var (
depth = 3 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) repo, cleanup := repository.TestRepository(t)
for i := 0; i < 3; i++ { 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 return repo, cleanup
@ -34,7 +34,7 @@ func validateIndex(t testing.TB, repo *repository.Repository, idx *Index) {
} }
func TestIndexNew(t *testing.T) { func TestIndexNew(t *testing.T) {
repo, cleanup := createFilledRepo(t, 3) repo, cleanup := createFilledRepo(t, 3, 0)
defer cleanup() defer cleanup()
idx, err := New(repo) idx, err := New(repo)
@ -50,7 +50,7 @@ func TestIndexNew(t *testing.T) {
} }
func TestIndexLoad(t *testing.T) { func TestIndexLoad(t *testing.T) {
repo, cleanup := createFilledRepo(t, 3) repo, cleanup := createFilledRepo(t, 3, 0)
defer cleanup() defer cleanup()
loadIdx, err := Load(repo) loadIdx, err := Load(repo)
@ -137,7 +137,7 @@ func openRepo(t testing.TB, dir, password string) *repository.Repository {
} }
func BenchmarkIndexNew(b *testing.B) { func BenchmarkIndexNew(b *testing.B) {
repo, cleanup := createFilledRepo(b, 3) repo, cleanup := createFilledRepo(b, 3, 0)
defer cleanup() defer cleanup()
b.ResetTimer() b.ResetTimer()

View file

@ -20,9 +20,10 @@ func fakeFile(t testing.TB, seed, size int64) io.Reader {
} }
type fakeFileSystem struct { type fakeFileSystem struct {
t testing.TB t testing.TB
repo *repository.Repository repo *repository.Repository
knownBlobs backend.IDSet knownBlobs backend.IDSet
duplication float32
} }
// saveFile reads from rd and saves the blobs in the repository. The list of // 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 { func (fs fakeFileSystem) blobIsKnown(id backend.ID, t pack.BlobType) bool {
if rand.Float32() < fs.duplication {
return false
}
if fs.knownBlobs.Has(id) { if fs.knownBlobs.Has(id) {
return true 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 // TestCreateSnapshot creates a snapshot filled with fake data. The
// fake data is generated deterministically from the timestamp `at`, which is // 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 // also used as the snapshot's timestamp. The tree's depth can be specified
// with the parameter depth. // with the parameter depth. The parameter duplication is a probability that
func TestCreateSnapshot(t testing.TB, repo *repository.Repository, at time.Time, depth int) *Snapshot { // 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() seed := at.Unix()
t.Logf("create fake snapshot at %s with seed %d", at, seed) 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 snapshot.Time = at
fs := fakeFileSystem{ fs := fakeFileSystem{
t: t, t: t,
repo: repo, repo: repo,
knownBlobs: backend.NewIDSet(), knownBlobs: backend.NewIDSet(),
duplication: duplication,
} }
treeID := fs.saveTree(seed, depth) treeID := fs.saveTree(seed, depth)

View file

@ -20,7 +20,7 @@ func TestCreateSnapshot(t *testing.T) {
defer cleanup() defer cleanup()
for i := 0; i < testCreateSnapshots; i++ { 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) snapshots, err := restic.LoadAllSnapshots(repo)
@ -55,7 +55,7 @@ func BenchmarkCreateSnapshot(b *testing.B) {
b.ResetTimer() b.ResetTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
restic.TestCreateSnapshot(b, repo, testSnapshotTime, testDepth) restic.TestCreateSnapshot(b, repo, testSnapshotTime, testDepth, 0)
restic.TestResetRepository(b, repo) restic.TestResetRepository(b, repo)
} }
} }