From 2d2e68ffaf29af085201f5200db5938d6040b79c Mon Sep 17 00:00:00 2001 From: Colin Cross Date: Thu, 10 Oct 2024 11:57:11 -0700 Subject: Fix TestFinalDepsPhase for parallel mutator All mutators will be parallel, fix TestFinalDepsPhase to use a sync.Map and atomic integers when modifying shared variables in a mutator. Bug: 372540665 Test: TestFinalDepsPhase passes with race detector enabled Flag: EXEMPT refactor Change-Id: I3b0844f7655a020a74b92568446afe5e9aedae64 --- android/mutator_test.go | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) (limited to 'android/mutator_test.go') diff --git a/android/mutator_test.go b/android/mutator_test.go index 33fca9e8b..d8d4450c3 100644 --- a/android/mutator_test.go +++ b/android/mutator_test.go @@ -17,6 +17,8 @@ package android import ( "fmt" "strings" + "sync" + "sync/atomic" "testing" "github.com/google/blueprint" @@ -220,7 +222,7 @@ func TestFinalDepsPhase(t *testing.T) { } ` - finalGot := map[string]int{} + finalGot := sync.Map{} GroupFixturePreparers( FixtureRegisterWithContext(func(ctx RegistrationContext) { @@ -236,7 +238,7 @@ func TestFinalDepsPhase(t *testing.T) { if !strings.HasPrefix(ctx.ModuleName(), "common_dep") { ctx.AddFarVariationDependencies([]blueprint.Variation{}, dep1Tag, "common_dep_1") } - }) + }).Parallel() ctx.Transition("variant", &testTransitionMutator{ split: func(ctx BaseModuleContext) []string { return []string{"a", "b"} @@ -249,13 +251,15 @@ func TestFinalDepsPhase(t *testing.T) { if !strings.HasPrefix(ctx.ModuleName(), "common_dep") { ctx.AddFarVariationDependencies([]blueprint.Variation{}, dep2Tag, "common_dep_2") } - }) + }).Parallel() ctx.BottomUp("final", func(ctx BottomUpMutatorContext) { - finalGot[ctx.Module().String()] += 1 + counter, _ := finalGot.LoadOrStore(ctx.Module().String(), &atomic.Int64{}) + counter.(*atomic.Int64).Add(1) ctx.VisitDirectDeps(func(mod Module) { - finalGot[fmt.Sprintf("%s -> %s", ctx.Module().String(), mod)] += 1 + counter, _ := finalGot.LoadOrStore(fmt.Sprintf("%s -> %s", ctx.Module().String(), mod), &atomic.Int64{}) + counter.(*atomic.Int64).Add(1) }) - }) + }).Parallel() }) ctx.RegisterModuleType("test", mutatorTestModuleFactory) @@ -276,7 +280,13 @@ func TestFinalDepsPhase(t *testing.T) { "foo{variant:b} -> common_dep_2{variant:a}": 1, } - AssertDeepEquals(t, "final", finalWant, finalGot) + finalGotMap := make(map[string]int) + finalGot.Range(func(k, v any) bool { + finalGotMap[k.(string)] = int(v.(*atomic.Int64).Load()) + return true + }) + + AssertDeepEquals(t, "final", finalWant, finalGotMap) } func TestTransitionMutatorInFinalDeps(t *testing.T) { -- cgit v1.2.3-59-g8ed1b