From fd2ac5ef968545e8283e32160fe69a9de1e98842 Mon Sep 17 00:00:00 2001 From: cuiweixie Date: Sat, 27 Aug 2022 10:30:46 +0800 Subject: [PATCH] testing: convert common.hasSub to atomic type Change-Id: I3d8a9b901efabe62f432c06361826f46c78d2605 Reviewed-on: https://go-review.googlesource.com/c/go/+/426080 Reviewed-by: Bryan Mills Reviewed-by: Michael Pratt Run-TryBot: Michael Pratt Auto-Submit: Michael Pratt TryBot-Result: Gopher Robot --- src/testing/benchmark.go | 6 +++--- src/testing/testing.go | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/testing/benchmark.go b/src/testing/benchmark.go index ce1ab6da379..2f7936611f7 100644 --- a/src/testing/benchmark.go +++ b/src/testing/benchmark.go @@ -242,7 +242,7 @@ func (b *B) run1() bool { b.mu.RLock() finished := b.finished b.mu.RUnlock() - if atomic.LoadInt32(&b.hasSub) != 0 || finished { + if b.hasSub.Load() || finished { tag := "BENCH" if b.skipped { tag = "SKIP" @@ -639,7 +639,7 @@ var hideStdoutForTesting = false func (b *B) Run(name string, f func(b *B)) bool { // Since b has subbenchmarks, we will no longer run it as a benchmark itself. // Release the lock and acquire it on exit to ensure locks stay paired. - atomic.StoreInt32(&b.hasSub, 1) + b.hasSub.Store(true) benchmarkLock.Unlock() defer benchmarkLock.Lock() @@ -671,7 +671,7 @@ func (b *B) Run(name string, f func(b *B)) bool { if partial { // Partial name match, like -bench=X/Y matching BenchmarkX. // Only process sub-benchmarks, if any. - atomic.StoreInt32(&sub.hasSub, 1) + sub.hasSub.Store(true) } if b.chatty != nil { diff --git a/src/testing/testing.go b/src/testing/testing.go index 5fd153954d1..7148537370f 100644 --- a/src/testing/testing.go +++ b/src/testing/testing.go @@ -539,7 +539,7 @@ type common struct { chatty *chattyPrinter // A copy of chattyPrinter, if the chatty flag is set. bench bool // Whether the current test is a benchmark. - hasSub int32 // Written atomically. + hasSub atomic.Bool // whether there are sub-benchmarks. raceErrors int // Number of races detected during test. runner string // Function name of tRunner running the test. @@ -1459,7 +1459,7 @@ func tRunner(t *T, fn func(t *T)) { // Do not lock t.done to allow race detector to detect race in case // the user does not appropriately synchronize a goroutine. t.done = true - if t.parent != nil && atomic.LoadInt32(&t.hasSub) == 0 { + if t.parent != nil && !t.hasSub.Load() { t.setRan() } }() @@ -1486,7 +1486,7 @@ func tRunner(t *T, fn func(t *T)) { // Run may be called simultaneously from multiple goroutines, but all such calls // must return before the outer test function for t returns. func (t *T) Run(name string, f func(t *T)) bool { - atomic.StoreInt32(&t.hasSub, 1) + t.hasSub.Store(true) testName, ok, _ := t.context.match.fullName(&t.common, name) if !ok || shouldFailFast() { return true