diff options
author | 2024-10-10 11:57:11 -0700 | |
---|---|---|
committer | 2024-10-10 11:59:32 -0700 | |
commit | 2d2e68ffaf29af085201f5200db5938d6040b79c (patch) | |
tree | 49f3559846b460918de182f439dcefad9ccc5d27 /android/mutator_test.go | |
parent | 597bad603265baaac8f2fa6209b5ccd22615bc7e (diff) |
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
Diffstat (limited to 'android/mutator_test.go')
-rw-r--r-- | android/mutator_test.go | 24 |
1 files changed, 17 insertions, 7 deletions
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) { |