1
0
mirror of https://github.com/golang/go synced 2024-11-26 09:08:07 -07:00

testing: convert common.hasSub to atomic type

Change-Id: I3d8a9b901efabe62f432c06361826f46c78d2605
Reviewed-on: https://go-review.googlesource.com/c/go/+/426080
Reviewed-by: Bryan Mills <bcmills@google.com>
Reviewed-by: Michael Pratt <mpratt@google.com>
Run-TryBot: Michael Pratt <mpratt@google.com>
Auto-Submit: Michael Pratt <mpratt@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
This commit is contained in:
cuiweixie 2022-08-27 10:30:46 +08:00 committed by Gopher Robot
parent b0144b3843
commit fd2ac5ef96
2 changed files with 6 additions and 6 deletions

View File

@ -242,7 +242,7 @@ func (b *B) run1() bool {
b.mu.RLock() b.mu.RLock()
finished := b.finished finished := b.finished
b.mu.RUnlock() b.mu.RUnlock()
if atomic.LoadInt32(&b.hasSub) != 0 || finished { if b.hasSub.Load() || finished {
tag := "BENCH" tag := "BENCH"
if b.skipped { if b.skipped {
tag = "SKIP" tag = "SKIP"
@ -639,7 +639,7 @@ var hideStdoutForTesting = false
func (b *B) Run(name string, f func(b *B)) bool { 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. // 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. // Release the lock and acquire it on exit to ensure locks stay paired.
atomic.StoreInt32(&b.hasSub, 1) b.hasSub.Store(true)
benchmarkLock.Unlock() benchmarkLock.Unlock()
defer benchmarkLock.Lock() defer benchmarkLock.Lock()
@ -671,7 +671,7 @@ func (b *B) Run(name string, f func(b *B)) bool {
if partial { if partial {
// Partial name match, like -bench=X/Y matching BenchmarkX. // Partial name match, like -bench=X/Y matching BenchmarkX.
// Only process sub-benchmarks, if any. // Only process sub-benchmarks, if any.
atomic.StoreInt32(&sub.hasSub, 1) sub.hasSub.Store(true)
} }
if b.chatty != nil { if b.chatty != nil {

View File

@ -539,7 +539,7 @@ type common struct {
chatty *chattyPrinter // A copy of chattyPrinter, if the chatty flag is set. chatty *chattyPrinter // A copy of chattyPrinter, if the chatty flag is set.
bench bool // Whether the current test is a benchmark. 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. raceErrors int // Number of races detected during test.
runner string // Function name of tRunner running the 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 // Do not lock t.done to allow race detector to detect race in case
// the user does not appropriately synchronize a goroutine. // the user does not appropriately synchronize a goroutine.
t.done = true t.done = true
if t.parent != nil && atomic.LoadInt32(&t.hasSub) == 0 { if t.parent != nil && !t.hasSub.Load() {
t.setRan() 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 // Run may be called simultaneously from multiple goroutines, but all such calls
// must return before the outer test function for t returns. // must return before the outer test function for t returns.
func (t *T) Run(name string, f func(t *T)) bool { 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) testName, ok, _ := t.context.match.fullName(&t.common, name)
if !ok || shouldFailFast() { if !ok || shouldFailFast() {
return true return true