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.
221222func 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).
248249func 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