1
0
Fork 0
mirror of https://github.com/restic/restic.git synced 2025-03-30 00:00:14 +01:00

backend/test: parallelize slow tests

This commit is contained in:
Michael Eischer 2025-03-23 15:18:21 +01:00
parent c36970074d
commit 4640b3c41a

View file

@ -10,11 +10,13 @@ import (
"os" "os"
"reflect" "reflect"
"sort" "sort"
"sync"
"testing" "testing"
"time" "time"
"github.com/restic/restic/internal/errors" "github.com/restic/restic/internal/errors"
"github.com/restic/restic/internal/restic" "github.com/restic/restic/internal/restic"
"golang.org/x/sync/errgroup"
"github.com/restic/restic/internal/test" "github.com/restic/restic/internal/test"
@ -276,17 +278,27 @@ func (s *Suite[C]) TestList(t *testing.T) {
} }
list1 := make(map[restic.ID]int64) list1 := make(map[restic.ID]int64)
var m sync.Mutex
wg, ctx := errgroup.WithContext(context.TODO())
for i := 0; i < numTestFiles; i++ { for i := 0; i < numTestFiles; i++ {
data := test.Random(random.Int(), random.Intn(100)+55) data := test.Random(random.Int(), random.Intn(100)+55)
wg.Go(func() error {
id := restic.Hash(data) id := restic.Hash(data)
h := backend.Handle{Type: backend.PackFile, Name: id.String()} h := backend.Handle{Type: backend.PackFile, Name: id.String()}
err := b.Save(context.TODO(), h, backend.NewByteReader(data, b.Hasher())) err := b.Save(ctx, h, backend.NewByteReader(data, b.Hasher()))
m.Lock()
defer m.Unlock()
list1[id] = int64(len(data))
return err
})
}
err = wg.Wait()
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
list1[id] = int64(len(data))
}
t.Logf("wrote %v files", len(list1)) t.Logf("wrote %v files", len(list1))
@ -777,16 +789,19 @@ func delayedList(t testing.TB, b backend.Backend, tpe backend.FileType, max int,
// TestBackend tests all functions of the backend. // TestBackend tests all functions of the backend.
func (s *Suite[C]) TestBackend(t *testing.T) { func (s *Suite[C]) TestBackend(t *testing.T) {
for _, tpe := range []backend.FileType{
backend.PackFile, backend.KeyFile, backend.LockFile,
backend.SnapshotFile, backend.IndexFile,
} {
t.Run(tpe.String(), func(t *testing.T) {
t.Parallel()
b := s.open(t) b := s.open(t)
defer s.close(t, b) defer s.close(t, b)
test.Assert(t, !b.IsNotExist(nil), "IsNotExist() recognized nil error") test.Assert(t, !b.IsNotExist(nil), "IsNotExist() recognized nil error")
test.Assert(t, !b.IsPermanentError(nil), "IsPermanentError() recognized nil error") test.Assert(t, !b.IsPermanentError(nil), "IsPermanentError() recognized nil error")
for _, tpe := range []backend.FileType{
backend.PackFile, backend.KeyFile, backend.LockFile,
backend.SnapshotFile, backend.IndexFile,
} {
// detect non-existing files // detect non-existing files
for _, ts := range testStrings { for _, ts := range testStrings {
id, err := restic.ParseID(ts.id) id, err := restic.ParseID(ts.id)
@ -896,6 +911,7 @@ func (s *Suite[C]) TestBackend(t *testing.T) {
} }
test.OK(t, s.delayedRemove(t, b, handles...)) test.OK(t, s.delayedRemove(t, b, handles...))
})
} }
} }