Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 062d9338 authored by Paul Duffin's avatar Paul Duffin Committed by Gerrit Code Review
Browse files

Merge "Ensure test/runtime order of singletons/pre-singletons is consistent"

parents 2034187a 41d77c76
Loading
Loading
Loading
Loading
+24 −3
Original line number Diff line number Diff line
@@ -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.