Merge pull request #21902 from restic/speedup-test

Cut local test suite execution time in half
This commit is contained in:
Michael Eischer
2026-06-24 21:34:55 +02:00
committed by GitHub
33 changed files with 185 additions and 75 deletions
+1 -1
View File
@@ -22,7 +22,7 @@ import (
func generateRandomFiles(t testing.TB, random *rand.Rand, tpe backend.FileType, c *Cache) map[string]struct{} {
ids := make(map[string]struct{})
for i := 0; i < random.Intn(15)+10; i++ {
buf := rtest.Random(random.Int(), 1<<19)
buf := rtest.Random(random.Int(), 1<<15)
id := restic.Hash(buf)
h := backend.Handle{Type: tpe, Name: id.String()}
+10 -4
View File
@@ -6,6 +6,10 @@ import (
"github.com/restic/restic/internal/backend"
)
// disablePackSubdirs is used to disable the creation of pack subdirectories.
// Only used for testing.
var disablePackSubdirs = false
// DefaultLayout implements the default layout for local and sftp backends, as
// described in the Design document. The `data` directory has one level of
// subdirs, two characters each (taken from the first two characters of the
@@ -66,10 +70,12 @@ func (l *DefaultLayout) Paths() (dirs []string) {
dirs = append(dirs, l.join(l.path, p))
}
// also add subdirs
for i := 0; i < 256; i++ {
subdir := hex.EncodeToString([]byte{byte(i)})
dirs = append(dirs, l.join(l.path, defaultLayoutPaths[backend.PackFile], subdir))
if !disablePackSubdirs {
// also add subdirs
for i := 0; i < 256; i++ {
subdir := hex.EncodeToString([]byte{byte(i)})
dirs = append(dirs, l.join(l.path, defaultLayoutPaths[backend.PackFile], subdir))
}
}
return dirs
+9
View File
@@ -0,0 +1,9 @@
package layout
import (
"testing"
)
func TestDisablePackSubdirs(t testing.TB) {
disablePackSubdirs = true
}
+1
View File
@@ -34,6 +34,7 @@ func findRclone(t testing.TB) {
}
func TestBackendRclone(t *testing.T) {
t.Parallel()
defer func() {
if t.Skipped() {
rtest.SkipDisallowed(t, "restic/backend/rclone.TestBackendRclone")
+1
View File
@@ -12,6 +12,7 @@ import (
// restic should detect rclone exiting.
func TestRcloneExit(t *testing.T) {
t.Parallel()
dir := rtest.TempDir(t)
cfg := NewConfig()
cfg.Remote = dir
+21 -7
View File
@@ -143,7 +143,7 @@ func (s *Suite[C]) TestLoad(t *testing.T) {
test.Assert(t, b.IsNotExist(err), "IsNotExist() did not recognize non-existing blob: %v", err)
test.Assert(t, b.IsPermanentError(err), "IsPermanentError() did not recognize non-existing blob: %v", err)
length := random.Intn(1<<24) + 2000
length := random.Intn(1<<20) + 2000
data := test.Random(23, length)
id := restic.Hash(data)
@@ -426,10 +426,7 @@ func (s *Suite[C]) TestListCancel(t *testing.T) {
}
})
t.Run("Timeout", func(t *testing.T) {
// rather large timeout, let's try to get at least one item
timeout := time.Second
testTimeout := func(timeout time.Duration) error {
ctxTimeout, cancel := context.WithTimeout(context.TODO(), timeout)
defer cancel()
@@ -449,11 +446,28 @@ func (s *Suite[C]) TestListCancel(t *testing.T) {
})
if !errors.Is(err, context.DeadlineExceeded) {
t.Fatalf("expected error not found, want %#v, got %#v", context.DeadlineExceeded, err)
return errors.Errorf("expected error not found, want %#v, got %#v", context.DeadlineExceeded, err)
}
if i > 2 {
t.Fatalf("wrong number of files returned by List, want <= 2, got %v", i)
return errors.Errorf("wrong number of files returned by List, want <= 2, got %v", i)
}
return nil
}
t.Run("Timeout", func(t *testing.T) {
// try short timeouts first to speed up tests for fast backends
var err error
for _, timeout := range []time.Duration{10 * time.Millisecond, 100 * time.Millisecond, 1 * time.Second} {
err = testTimeout(timeout)
if err == nil {
break
}
}
// fails if last attempt also did not succeed
if err != nil {
t.Fatal(err)
}
})