Commit a3c1a3f4 authored by Russ Cox's avatar Russ Cox

runtime: deflake TestNumGoroutine

Fixes #14107.

Change-Id: Icd9463b1a77b139c7ebc2d8732482d704ea332d0
Reviewed-on: https://go-review.googlesource.com/19002Reviewed-by: default avatarBrad Fitzpatrick <bradfitz@golang.org>
parent 3a21f0a9
...@@ -345,13 +345,28 @@ func TestNumGoroutine(t *testing.T) { ...@@ -345,13 +345,28 @@ func TestNumGoroutine(t *testing.T) {
} }
buf := make([]byte, 1<<20) buf := make([]byte, 1<<20)
buf = buf[:runtime.Stack(buf, true)]
// Try up to 10 times for a match before giving up.
// This is a fundamentally racy check but it's important
// to notice if NumGoroutine and Stack are _always_ out of sync.
for i := 0; ; i++ {
// Give goroutines about to exit a chance to exit.
// The NumGoroutine and Stack below need to see
// the same state of the world, so anything we can do
// to keep it quiet is good.
runtime.Gosched()
n := runtime.NumGoroutine() n := runtime.NumGoroutine()
buf = buf[:runtime.Stack(buf, true)]
if nstk := strings.Count(string(buf), "goroutine "); n != nstk { nstk := strings.Count(string(buf), "goroutine ")
if n == nstk {
break
}
if i >= 10 {
t.Fatalf("NumGoroutine=%d, but found %d goroutines in stack dump: %s", n, nstk, buf) t.Fatalf("NumGoroutine=%d, but found %d goroutines in stack dump: %s", n, nstk, buf)
} }
}
} }
func TestPingPongHog(t *testing.T) { func TestPingPongHog(t *testing.T) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment