Skip to content
This repository was archived by the owner on Sep 30, 2024. It is now read-only.

Commit 3edfaa8

Browse files
authored
migration: make stitch tests run in 7s (#64417)
This change caches what we download from google storage. Before this change on my desktop computer this test would timeout after 10 minutes. It now takes 4s. Test Plan: go test ./internal/database/migration/stitch
1 parent 5bc97d9 commit 3edfaa8

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

internal/database/migration/stitch/stitch_test.go

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"os"
99
"os/exec"
1010
"strings"
11+
"sync"
1112
"testing"
1213

1314
"github.com/google/go-cmp/cmp"
@@ -220,7 +221,7 @@ func TestStitchAndApplyCodeinsightsDefinitions(t *testing.T) {
220221
// asserts that the resulting graph has the expected root, leaf, and version boundary values.
221222
func testStitchGraphShape(t *testing.T, schemaName string, from, to, expectedRoot int, expectedLeaves []int, expectedBoundsByRev map[string]shared.MigrationBounds) {
222223
t.Run(fmt.Sprintf("stitch 3.%d -> 3.%d", from, to), func(t *testing.T) {
223-
stitched, err := StitchDefinitions(NewLazyMigrationsReader(), schemaName, makeRange(from, to))
224+
stitched, err := StitchDefinitions(testMigrationsReader, schemaName, makeRange(from, to))
224225
if err != nil {
225226
t.Fatalf("failed to stitch definitions: %s", err)
226227
}
@@ -247,7 +248,7 @@ func testStitchGraphShape(t *testing.T, schemaName string, from, to, expectedRoo
247248
// compared against the target version's description (in the git-tree).
248249
func testStitchApplication(t *testing.T, schemaName string, from, to int) {
249250
t.Run(fmt.Sprintf("upgrade 3.%d -> 3.%d", from, to), func(t *testing.T) {
250-
stitched, err := StitchDefinitions(NewLazyMigrationsReader(), schemaName, makeRange(from, to))
251+
stitched, err := StitchDefinitions(testMigrationsReader, schemaName, makeRange(from, to))
251252
if err != nil {
252253
t.Fatalf("failed to stitch definitions: %s", err)
253254
}
@@ -363,3 +364,31 @@ func canonicalize(schemaDescription schemas.SchemaDescription) schemas.SchemaDes
363364

364365
return schemaDescription
365366
}
367+
368+
var testMigrationsReader MigrationsReader = &cachedMigrationsReader{
369+
inner: NewLazyMigrationsReader(),
370+
m: make(map[string]func() (map[string]string, error)),
371+
}
372+
373+
type cachedMigrationsReader struct {
374+
inner MigrationsReader
375+
376+
mu sync.Mutex
377+
m map[string]func() (map[string]string, error)
378+
}
379+
380+
func (c *cachedMigrationsReader) Get(version string) (map[string]string, error) {
381+
c.mu.Lock()
382+
get, ok := c.m[version]
383+
if !ok {
384+
// we haven't calculated the version, store it as a sync.OnceValues to
385+
// singleflight requests.
386+
get = sync.OnceValues(func() (map[string]string, error) {
387+
return c.inner.Get(version)
388+
})
389+
c.m[version] = get
390+
}
391+
c.mu.Unlock()
392+
393+
return get()
394+
}

0 commit comments

Comments
 (0)