mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
testing: make Cleanup work for benchmarks too.
Fixes #37073. Change-Id: I6fb24a3f9d7b7adf3213ac6a8bcbf5fb43975b7e Reviewed-on: https://go-review.googlesource.com/c/go/+/218117 Reviewed-by: Bryan C. Mills <bcmills@google.com> Reviewed-by: Ian Lance Taylor <iant@golang.org> Run-TryBot: Bryan C. Mills <bcmills@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
parent
ee3a3717aa
commit
ab7c174183
2 changed files with 41 additions and 0 deletions
|
|
@ -179,6 +179,7 @@ func (b *B) ReportAllocs() {
|
||||||
func (b *B) runN(n int) {
|
func (b *B) runN(n int) {
|
||||||
benchmarkLock.Lock()
|
benchmarkLock.Lock()
|
||||||
defer benchmarkLock.Unlock()
|
defer benchmarkLock.Unlock()
|
||||||
|
defer b.runCleanup(normalPanic)
|
||||||
// Try to get a comparable environment for each run
|
// Try to get a comparable environment for each run
|
||||||
// by clearing garbage from previous runs.
|
// by clearing garbage from previous runs.
|
||||||
runtime.GC()
|
runtime.GC()
|
||||||
|
|
|
||||||
|
|
@ -613,6 +613,46 @@ func TestBRun(t *T) {
|
||||||
t.Errorf("MemBytes was %v; want %v", got, 2*bufSize)
|
t.Errorf("MemBytes was %v; want %v", got, 2*bufSize)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
}, {
|
||||||
|
desc: "cleanup is called",
|
||||||
|
f: func(b *B) {
|
||||||
|
var calls, cleanups, innerCalls, innerCleanups int
|
||||||
|
b.Run("", func(b *B) {
|
||||||
|
calls++
|
||||||
|
b.Cleanup(func() {
|
||||||
|
cleanups++
|
||||||
|
})
|
||||||
|
b.Run("", func(b *B) {
|
||||||
|
b.Cleanup(func() {
|
||||||
|
innerCleanups++
|
||||||
|
})
|
||||||
|
innerCalls++
|
||||||
|
})
|
||||||
|
work(b)
|
||||||
|
})
|
||||||
|
if calls == 0 || calls != cleanups {
|
||||||
|
t.Errorf("mismatched cleanups; got %d want %d", cleanups, calls)
|
||||||
|
}
|
||||||
|
if innerCalls == 0 || innerCalls != innerCleanups {
|
||||||
|
t.Errorf("mismatched cleanups; got %d want %d", cleanups, calls)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}, {
|
||||||
|
desc: "cleanup is called on failure",
|
||||||
|
failed: true,
|
||||||
|
f: func(b *B) {
|
||||||
|
var calls, cleanups int
|
||||||
|
b.Run("", func(b *B) {
|
||||||
|
calls++
|
||||||
|
b.Cleanup(func() {
|
||||||
|
cleanups++
|
||||||
|
})
|
||||||
|
b.Fatalf("failure")
|
||||||
|
})
|
||||||
|
if calls == 0 || calls != cleanups {
|
||||||
|
t.Errorf("mismatched cleanups; got %d want %d", cleanups, calls)
|
||||||
|
}
|
||||||
|
},
|
||||||
}}
|
}}
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
var ok bool
|
var ok bool
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue