2016-02-01 22:48:36 +00:00
|
|
|
package restic_test
|
2016-02-01 22:26:57 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/rand"
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
mrand "math/rand"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2016-02-01 22:48:36 +00:00
|
|
|
"github.com/restic/restic"
|
2016-02-01 22:26:57 +00:00
|
|
|
"github.com/restic/restic/backend"
|
|
|
|
"github.com/restic/restic/pack"
|
2016-02-01 22:48:36 +00:00
|
|
|
"github.com/restic/restic/repository"
|
2016-02-01 22:26:57 +00:00
|
|
|
)
|
|
|
|
|
2016-02-01 22:48:36 +00:00
|
|
|
const parallelSaves = 50
|
|
|
|
const testSaveIndexTime = 100 * time.Millisecond
|
|
|
|
const testTimeout = 2 * time.Second
|
2016-02-01 22:26:57 +00:00
|
|
|
|
|
|
|
var DupID backend.ID
|
|
|
|
|
|
|
|
func randomID() backend.ID {
|
|
|
|
if mrand.Float32() < 0.5 {
|
|
|
|
return DupID
|
|
|
|
}
|
|
|
|
|
|
|
|
id := backend.ID{}
|
|
|
|
_, err := io.ReadFull(rand.Reader, id[:])
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return id
|
|
|
|
}
|
|
|
|
|
|
|
|
// forgetfulBackend returns a backend that forgets everything.
|
|
|
|
func forgetfulBackend() backend.Backend {
|
|
|
|
be := &backend.MockBackend{}
|
|
|
|
|
|
|
|
be.TestFn = func(t backend.Type, name string) (bool, error) {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
be.LoadFn = func(h backend.Handle, p []byte, off int64) (int, error) {
|
|
|
|
return 0, errors.New("not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
be.SaveFn = func(h backend.Handle, p []byte) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
be.StatFn = func(h backend.Handle) (backend.BlobInfo, error) {
|
|
|
|
return backend.BlobInfo{}, errors.New("not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
be.RemoveFn = func(t backend.Type, name string) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
be.ListFn = func(t backend.Type, done <-chan struct{}) <-chan string {
|
|
|
|
ch := make(chan string)
|
|
|
|
close(ch)
|
|
|
|
return ch
|
|
|
|
}
|
|
|
|
|
|
|
|
be.DeleteFn = func() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return be
|
|
|
|
}
|
|
|
|
|
2016-02-01 22:48:36 +00:00
|
|
|
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-02-01 22:48:36 +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-02-01 22:48:36 +00:00
|
|
|
arch := restic.NewArchiver(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()
|
|
|
|
|
|
|
|
if repo.Index().Has(id) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := make([]byte, 50)
|
|
|
|
|
2016-02-01 22:48:36 +00:00
|
|
|
err := arch.Save(pack.Data, id, uint(len(buf)), bytes.NewReader(buf))
|
2016-02-01 22:26:57 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2016-02-01 22:48:36 +00:00
|
|
|
saveIndex := func() {
|
2016-02-01 22:26:57 +00:00
|
|
|
defer wg.Done()
|
|
|
|
|
2016-02-01 22:48:36 +00:00
|
|
|
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)
|
2016-02-01 22:48:36 +00:00
|
|
|
go saveIndex()
|
2016-02-01 22:26:57 +00:00
|
|
|
|
|
|
|
<-time.After(testTimeout)
|
|
|
|
close(done)
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
2016-02-01 22:48:36 +00:00
|
|
|
func TestArchiverDuplication(t *testing.T) {
|
2016-02-01 22:26:57 +00:00
|
|
|
for i := 0; i < 5; i++ {
|
2016-02-01 22:48:36 +00:00
|
|
|
testArchiverDuplication(t)
|
2016-02-01 22:26:57 +00:00
|
|
|
}
|
|
|
|
}
|