2018-05-12 19:40:31 +00:00
|
|
|
package archiver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"runtime"
|
|
|
|
"sync/atomic"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/restic/restic/internal/errors"
|
|
|
|
"github.com/restic/restic/internal/repository"
|
|
|
|
"github.com/restic/restic/internal/restic"
|
2022-05-27 17:08:50 +00:00
|
|
|
"golang.org/x/sync/errgroup"
|
2018-05-12 19:40:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var errTest = errors.New("test error")
|
|
|
|
|
|
|
|
type saveFail struct {
|
2020-07-25 19:19:46 +00:00
|
|
|
idx restic.MasterIndex
|
2018-05-12 19:40:31 +00:00
|
|
|
cnt int32
|
|
|
|
failAt int32
|
|
|
|
}
|
|
|
|
|
2022-05-01 12:26:57 +00:00
|
|
|
func (b *saveFail) SaveBlob(ctx context.Context, t restic.BlobType, buf []byte, id restic.ID, storeDuplicates bool) (restic.ID, bool, int, error) {
|
2018-05-12 19:40:31 +00:00
|
|
|
val := atomic.AddInt32(&b.cnt, 1)
|
|
|
|
if val == b.failAt {
|
2022-05-01 12:26:57 +00:00
|
|
|
return restic.ID{}, false, 0, errTest
|
2018-05-12 19:40:31 +00:00
|
|
|
}
|
|
|
|
|
2022-05-01 12:26:57 +00:00
|
|
|
return id, false, 0, nil
|
2018-05-12 19:40:31 +00:00
|
|
|
}
|
|
|
|
|
2020-07-25 19:19:46 +00:00
|
|
|
func (b *saveFail) Index() restic.MasterIndex {
|
2018-05-12 19:40:31 +00:00
|
|
|
return b.idx
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBlobSaver(t *testing.T) {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
2022-05-27 17:08:50 +00:00
|
|
|
wg, ctx := errgroup.WithContext(ctx)
|
2018-05-12 19:40:31 +00:00
|
|
|
saver := &saveFail{
|
2020-06-14 11:26:10 +00:00
|
|
|
idx: repository.NewMasterIndex(),
|
2018-05-12 19:40:31 +00:00
|
|
|
}
|
|
|
|
|
2022-05-27 17:08:50 +00:00
|
|
|
b := NewBlobSaver(ctx, wg, saver, uint(runtime.NumCPU()))
|
2018-05-12 19:40:31 +00:00
|
|
|
|
|
|
|
var results []FutureBlob
|
|
|
|
|
|
|
|
for i := 0; i < 20; i++ {
|
|
|
|
buf := &Buffer{Data: []byte(fmt.Sprintf("foo%d", i))}
|
|
|
|
fb := b.Save(ctx, restic.DataBlob, buf)
|
|
|
|
results = append(results, fb)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, blob := range results {
|
|
|
|
blob.Wait(ctx)
|
|
|
|
if blob.Known() {
|
|
|
|
t.Errorf("blob %v is known, that should not be the case", i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-27 17:08:50 +00:00
|
|
|
b.TriggerShutdown()
|
2018-05-12 19:40:31 +00:00
|
|
|
|
2022-05-27 17:08:50 +00:00
|
|
|
err := wg.Wait()
|
2018-05-12 19:40:31 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBlobSaverError(t *testing.T) {
|
|
|
|
var tests = []struct {
|
|
|
|
blobs int
|
|
|
|
failAt int
|
|
|
|
}{
|
|
|
|
{20, 2},
|
|
|
|
{20, 5},
|
|
|
|
{20, 15},
|
|
|
|
{200, 150},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run("", func(t *testing.T) {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
2022-05-27 17:08:50 +00:00
|
|
|
wg, ctx := errgroup.WithContext(ctx)
|
2018-05-12 19:40:31 +00:00
|
|
|
saver := &saveFail{
|
2020-06-14 11:26:10 +00:00
|
|
|
idx: repository.NewMasterIndex(),
|
2018-05-12 19:40:31 +00:00
|
|
|
failAt: int32(test.failAt),
|
|
|
|
}
|
|
|
|
|
2022-05-27 17:08:50 +00:00
|
|
|
b := NewBlobSaver(ctx, wg, saver, uint(runtime.NumCPU()))
|
2018-05-12 19:40:31 +00:00
|
|
|
|
|
|
|
for i := 0; i < test.blobs; i++ {
|
|
|
|
buf := &Buffer{Data: []byte(fmt.Sprintf("foo%d", i))}
|
2020-03-06 22:38:30 +00:00
|
|
|
b.Save(ctx, restic.DataBlob, buf)
|
2018-05-12 19:40:31 +00:00
|
|
|
}
|
|
|
|
|
2022-05-27 17:08:50 +00:00
|
|
|
b.TriggerShutdown()
|
2018-05-12 19:40:31 +00:00
|
|
|
|
2022-05-27 17:08:50 +00:00
|
|
|
err := wg.Wait()
|
2018-05-12 19:40:31 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("expected error not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != errTest {
|
|
|
|
t.Fatalf("unexpected error found: %v", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|