2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-25 22:27:35 +00:00

archiver: refactor TreeSaverTest

This commit is contained in:
Michael Eischer 2022-10-08 21:29:32 +02:00
parent 8e38c43c27
commit d7d7b4ab27

View File

@ -11,31 +11,38 @@ import (
"golang.org/x/sync/errgroup" "golang.org/x/sync/errgroup"
) )
func newFutureBlobWithResponse() FutureBlob { func treeSaveHelper(ctx context.Context, t restic.BlobType, buf *Buffer) FutureBlob {
ch := make(chan SaveBlobResponse, 1) ch := make(chan SaveBlobResponse, 1)
ch <- SaveBlobResponse{ ch <- SaveBlobResponse{
id: restic.NewRandomID(), id: restic.NewRandomID(),
known: false, known: false,
length: 123, length: len(buf.Data),
sizeInRepo: 123, sizeInRepo: len(buf.Data),
} }
return FutureBlob{ch: ch} return FutureBlob{ch: ch}
} }
func TestTreeSaver(t *testing.T) { func setupTreeSaver() (context.Context, context.CancelFunc, *TreeSaver, func() error) {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel()
wg, ctx := errgroup.WithContext(ctx) wg, ctx := errgroup.WithContext(ctx)
saveFn := func(ctx context.Context, t restic.BlobType, buf *Buffer) FutureBlob {
return newFutureBlobWithResponse()
}
errFn := func(snPath string, err error) error { errFn := func(snPath string, err error) error {
return err return err
} }
b := NewTreeSaver(ctx, wg, uint(runtime.NumCPU()), saveFn, errFn) b := NewTreeSaver(ctx, wg, uint(runtime.NumCPU()), treeSaveHelper, errFn)
shutdown := func() error {
b.TriggerShutdown()
return wg.Wait()
}
return ctx, cancel, b, shutdown
}
func TestTreeSaver(t *testing.T) {
ctx, cancel, b, shutdown := setupTreeSaver()
defer cancel()
var results []FutureNode var results []FutureNode
@ -52,9 +59,7 @@ func TestTreeSaver(t *testing.T) {
tree.take(ctx) tree.take(ctx)
} }
b.TriggerShutdown() err := shutdown()
err := wg.Wait()
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -76,20 +81,9 @@ func TestTreeSaverError(t *testing.T) {
for _, test := range tests { for _, test := range tests {
t.Run("", func(t *testing.T) { t.Run("", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel, b, shutdown := setupTreeSaver()
defer cancel() defer cancel()
wg, ctx := errgroup.WithContext(ctx)
saveFn := func(ctx context.Context, tpe restic.BlobType, buf *Buffer) FutureBlob {
return newFutureBlobWithResponse()
}
errFn := func(snPath string, err error) error {
return err
}
b := NewTreeSaver(ctx, wg, uint(runtime.NumCPU()), saveFn, errFn)
var results []FutureNode var results []FutureNode
for i := 0; i < test.trees; i++ { for i := 0; i < test.trees; i++ {
@ -115,13 +109,10 @@ func TestTreeSaverError(t *testing.T) {
tree.take(ctx) tree.take(ctx)
} }
b.TriggerShutdown() err := shutdown()
err := wg.Wait()
if err == nil { if err == nil {
t.Errorf("expected error not found") t.Errorf("expected error not found")
} }
if err != errTest { if err != errTest {
t.Fatalf("unexpected error found: %v", err) t.Fatalf("unexpected error found: %v", err)
} }