2018-05-12 19:40:31 +00:00
|
|
|
package archiver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"runtime"
|
2024-02-10 22:41:11 +00:00
|
|
|
"strings"
|
2022-10-07 18:23:38 +00:00
|
|
|
"sync"
|
2018-05-12 19:40:31 +00:00
|
|
|
"sync/atomic"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/restic/restic/internal/errors"
|
|
|
|
"github.com/restic/restic/internal/restic"
|
2024-02-10 22:41:11 +00:00
|
|
|
rtest "github.com/restic/restic/internal/test"
|
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 {
|
|
|
|
cnt int32
|
|
|
|
failAt int32
|
|
|
|
}
|
|
|
|
|
2023-05-18 17:27:38 +00:00
|
|
|
func (b *saveFail) SaveBlob(_ context.Context, _ restic.BlobType, _ []byte, id restic.ID, _ 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
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2024-05-19 11:03:14 +00:00
|
|
|
saver := &saveFail{}
|
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
|
|
|
|
2022-10-07 18:23:38 +00:00
|
|
|
var wait sync.WaitGroup
|
|
|
|
var results []SaveBlobResponse
|
2022-11-10 19:19:37 +00:00
|
|
|
var lock sync.Mutex
|
2018-05-12 19:40:31 +00:00
|
|
|
|
2022-10-07 18:23:38 +00:00
|
|
|
wait.Add(20)
|
2018-05-12 19:40:31 +00:00
|
|
|
for i := 0; i < 20; i++ {
|
|
|
|
buf := &Buffer{Data: []byte(fmt.Sprintf("foo%d", i))}
|
2022-10-07 18:23:38 +00:00
|
|
|
idx := i
|
2022-11-10 19:19:37 +00:00
|
|
|
lock.Lock()
|
2022-10-07 18:23:38 +00:00
|
|
|
results = append(results, SaveBlobResponse{})
|
2022-11-10 19:19:37 +00:00
|
|
|
lock.Unlock()
|
2024-02-10 22:41:11 +00:00
|
|
|
b.Save(ctx, restic.DataBlob, buf, "file", func(res SaveBlobResponse) {
|
2022-11-10 19:19:37 +00:00
|
|
|
lock.Lock()
|
2022-10-07 18:23:38 +00:00
|
|
|
results[idx] = res
|
2022-11-10 19:19:37 +00:00
|
|
|
lock.Unlock()
|
2022-10-07 18:23:38 +00:00
|
|
|
wait.Done()
|
|
|
|
})
|
2018-05-12 19:40:31 +00:00
|
|
|
}
|
|
|
|
|
2022-10-07 18:23:38 +00:00
|
|
|
wait.Wait()
|
|
|
|
for i, sbr := range results {
|
2022-05-22 13:14:25 +00:00
|
|
|
if sbr.known {
|
2018-05-12 19:40:31 +00:00
|
|
|
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{
|
|
|
|
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))}
|
2024-02-10 22:41:11 +00:00
|
|
|
b.Save(ctx, restic.DataBlob, buf, "errfile", func(res SaveBlobResponse) {})
|
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")
|
|
|
|
}
|
|
|
|
|
2024-02-10 22:41:11 +00:00
|
|
|
rtest.Assert(t, errors.Is(err, errTest), "unexpected error %v", err)
|
|
|
|
rtest.Assert(t, strings.Contains(err.Error(), "errfile"), "expected error to contain 'errfile' got: %v", err)
|
2018-05-12 19:40:31 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|