From 41d77c76ae9d07adc48a989bc8ef75507844161e Mon Sep 17 00:00:00 2001 From: Paul Duffin Date: Sun, 7 Mar 2021 12:23:48 +0000 Subject: [PATCH] Ensure test/runtime order of singletons/pre-singletons is consistent Bug: 181953909 Test: m nothing Change-Id: I77e0106ceb04b44b6559630f4a8a510f1a66378b --- android/testing.go | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/android/testing.go b/android/testing.go index dab7e9206..042fa2b9a 100644 --- a/android/testing.go +++ b/android/testing.go @@ -101,8 +101,9 @@ type TestContext struct { // The list of pre-singletons and singletons registered for the test. preSingletons, singletons sortableComponents - // The order in which the mutators will be run in this test context; for debugging. - mutatorOrder []string + // The order in which the pre-singletons, mutators and singletons will be run in this test + // context; for debugging. + preSingletonOrder, mutatorOrder, singletonOrder []string } func (ctx *TestContext) PreArchMutators(f RegisterMutatorFunc) { @@ -315,8 +316,14 @@ type registrationSorter struct { // Used to ensure that this is only created once. once sync.Once + // The order of pre-singletons + preSingletonOrder registeredComponentOrder + // The order of mutators mutatorOrder registeredComponentOrder + + // The order of singletons + singletonOrder registeredComponentOrder } // populate initializes this structure from globally registered build components. @@ -324,9 +331,16 @@ type registrationSorter struct { // Only the first call has any effect. func (s *registrationSorter) populate() { s.once.Do(func() { + // Create an ordering from the globally registered pre-singletons. + s.preSingletonOrder = registeredComponentOrderFromExistingOrder("pre-singleton", preSingletons) + // Created an ordering from the globally registered mutators. globallyRegisteredMutators := collateGloballyRegisteredMutators() s.mutatorOrder = registeredComponentOrderFromExistingOrder("mutator", globallyRegisteredMutators) + + // Create an ordering from the globally registered singletons. + globallyRegisteredSingletons := collateGloballyRegisteredSingletons() + s.singletonOrder = registeredComponentOrderFromExistingOrder("singleton", globallyRegisteredSingletons) }) } @@ -346,6 +360,9 @@ func globallyRegisteredComponentsOrder() *registrationSorter { func (ctx *TestContext) Register() { globalOrder := globallyRegisteredComponentsOrder() + // Ensure that the pre-singletons used in the test are in the same order as they are used at + // runtime. + globalOrder.preSingletonOrder.enforceOrdering(ctx.preSingletons) ctx.preSingletons.registerAll(ctx.Context) mutators := collateRegisteredMutators(ctx.preArch, ctx.preDeps, ctx.postDeps, ctx.finalDeps) @@ -356,10 +373,14 @@ func (ctx *TestContext) Register() { // Register the env singleton with this context before sorting. ctx.RegisterSingletonType("env", EnvSingleton) + // Ensure that the singletons used in the test are in the same order as they are used at runtime. + globalOrder.singletonOrder.enforceOrdering(ctx.singletons) ctx.singletons.registerAll(ctx.Context) - // Save the mutator order away to make it easy to access while debugging. + // Save the sorted components order away to make them easy to access while debugging. + ctx.preSingletonOrder = globalOrder.preSingletonOrder.namesInOrder ctx.mutatorOrder = globalOrder.mutatorOrder.namesInOrder + ctx.singletonOrder = globalOrder.singletonOrder.namesInOrder } // RegisterForBazelConversion prepares a test context for bp2build conversion.