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
diff --git a/android/mutator_test.go b/android/mutator_test.go
index 33fca9e..d8d4450 100644
--- a/android/mutator_test.go
+++ b/android/mutator_test.go
@@ -17,6 +17,8 @@
import (
"fmt"
"strings"
+ "sync"
+ "sync/atomic"
"testing"
"github.com/google/blueprint"
@@ -220,7 +222,7 @@
}
`
- finalGot := map[string]int{}
+ finalGot := sync.Map{}
GroupFixturePreparers(
FixtureRegisterWithContext(func(ctx RegistrationContext) {
@@ -236,7 +238,7 @@
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 @@
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 @@
"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) {