2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-02 19:49:44 +00:00
restic/src/restic/archiver/archiver_duplication_test.go

152 lines
2.4 KiB
Go
Raw Normal View History

2016-08-31 20:39:36 +00:00
package archiver_test
2016-02-01 22:26:57 +00:00
import (
"crypto/rand"
"io"
mrand "math/rand"
"sync"
"testing"
"time"
"github.com/pkg/errors"
"restic"
2016-08-31 21:07:50 +00:00
"restic/archiver"
"restic/mock"
"restic/repository"
2016-02-01 22:26:57 +00:00
)
const parallelSaves = 50
const testSaveIndexTime = 100 * time.Millisecond
const testTimeout = 2 * time.Second
2016-02-01 22:26:57 +00:00
2016-08-31 21:07:50 +00:00
var DupID restic.ID
2016-02-01 22:26:57 +00:00
2016-08-31 21:07:50 +00:00
func randomID() restic.ID {
2016-02-01 22:26:57 +00:00
if mrand.Float32() < 0.5 {
return DupID
}
2016-08-31 21:07:50 +00:00
id := restic.ID{}
2016-02-01 22:26:57 +00:00
_, err := io.ReadFull(rand.Reader, id[:])
if err != nil {
panic(err)
}
return id
}
// forgetfulBackend returns a backend that forgets everything.
func forgetfulBackend() restic.Backend {
be := &mock.Backend{}
2016-02-01 22:26:57 +00:00
be.TestFn = func(t restic.FileType, name string) (bool, error) {
2016-02-01 22:26:57 +00:00
return false, nil
}
be.LoadFn = func(h restic.Handle, p []byte, off int64) (int, error) {
2016-02-01 22:26:57 +00:00
return 0, errors.New("not found")
}
be.SaveFn = func(h restic.Handle, p []byte) error {
2016-02-01 22:26:57 +00:00
return nil
}
2016-08-31 21:07:50 +00:00
be.StatFn = func(h restic.Handle) (restic.FileInfo, error) {
return restic.FileInfo{}, errors.New("not found")
2016-02-01 22:26:57 +00:00
}
be.RemoveFn = func(t restic.FileType, name string) error {
2016-02-01 22:26:57 +00:00
return nil
}
be.ListFn = func(t restic.FileType, done <-chan struct{}) <-chan string {
2016-02-01 22:26:57 +00:00
ch := make(chan string)
close(ch)
return ch
}
be.DeleteFn = func() error {
return nil
}
return be
}
func testArchiverDuplication(t *testing.T) {
2016-02-01 22:26:57 +00:00
_, err := io.ReadFull(rand.Reader, DupID[:])
if err != nil {
t.Fatal(err)
}
2016-03-06 12:14:06 +00:00
repo := repository.New(forgetfulBackend())
2016-02-01 22:26:57 +00:00
err = repo.Init("foo")
if err != nil {
t.Fatal(err)
}
2016-08-31 21:07:50 +00:00
arch := archiver.New(repo)
2016-02-01 22:26:57 +00:00
wg := &sync.WaitGroup{}
done := make(chan struct{})
for i := 0; i < parallelSaves; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for {
select {
case <-done:
return
default:
}
id := randomID()
2016-08-31 20:39:36 +00:00
if repo.Index().Has(id, restic.DataBlob) {
2016-02-01 22:26:57 +00:00
continue
}
buf := make([]byte, 50)
2016-08-31 20:39:36 +00:00
err := arch.Save(restic.DataBlob, buf, id)
2016-02-01 22:26:57 +00:00
if err != nil {
t.Fatal(err)
}
}
}()
}
saveIndex := func() {
2016-02-01 22:26:57 +00:00
defer wg.Done()
ticker := time.NewTicker(testSaveIndexTime)
2016-02-01 22:26:57 +00:00
defer ticker.Stop()
for {
select {
case <-done:
return
case <-ticker.C:
err := repo.SaveFullIndex()
if err != nil {
t.Fatal(err)
}
}
}
}
wg.Add(1)
go saveIndex()
2016-02-01 22:26:57 +00:00
<-time.After(testTimeout)
close(done)
wg.Wait()
}
func TestArchiverDuplication(t *testing.T) {
2016-02-01 22:26:57 +00:00
for i := 0; i < 5; i++ {
testArchiverDuplication(t)
2016-02-01 22:26:57 +00:00
}
}