2016-03-01 22:57:46 +00:00
|
|
|
// Copyright 2014 The Go Authors. All rights reserved.
|
2014-09-19 13:51:06 -04:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
|
|
package testing_test
|
|
|
|
|
|
|
|
|
|
import (
|
2016-10-22 07:25:21 -07:00
|
|
|
"fmt"
|
2014-09-19 13:51:06 -04:00
|
|
|
"os"
|
2016-10-22 07:25:21 -07:00
|
|
|
"runtime"
|
2014-09-19 13:51:06 -04:00
|
|
|
"testing"
|
2016-10-22 07:25:21 -07:00
|
|
|
"time"
|
2014-09-19 13:51:06 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
2016-10-22 07:25:21 -07:00
|
|
|
g0 := runtime.NumGoroutine()
|
|
|
|
|
|
|
|
|
|
code := m.Run()
|
|
|
|
|
if code != 0 {
|
|
|
|
|
os.Exit(code)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check that there are no goroutines left behind.
|
|
|
|
|
t0 := time.Now()
|
|
|
|
|
stacks := make([]byte, 1<<20)
|
|
|
|
|
for {
|
|
|
|
|
g1 := runtime.NumGoroutine()
|
|
|
|
|
if g1 == g0 {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
stacks = stacks[:runtime.Stack(stacks, true)]
|
|
|
|
|
time.Sleep(50 * time.Millisecond)
|
|
|
|
|
if time.Since(t0) > 2*time.Second {
|
|
|
|
|
fmt.Fprintf(os.Stderr, "Unexpected leftover goroutines detected: %v -> %v\n%s\n", g0, g1, stacks)
|
|
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestContextCancel(t *testing.T) {
|
|
|
|
|
ctx := t.Context()
|
|
|
|
|
// Tests we don't leak this goroutine:
|
|
|
|
|
go func() {
|
|
|
|
|
<-ctx.Done()
|
|
|
|
|
}()
|
2014-09-19 13:51:06 -04:00
|
|
|
}
|