1
0
mirror of https://github.com/golang/go synced 2024-11-18 11:04:42 -07:00

os/signal: derive TestAtomicStop timeout from overall test timeout

Previously, TestAtomicStop used a hard-coded 2-second timeout.

That empirically is not long enough on certain builders. Rather than
adjusting it to a different arbitrary value, use a slice of the
overall timeout for the test binary. If everything is working, we
won't block nearly that long anyway.

Updates #35085

Change-Id: I7b789388e3152413395088088fc497419976cf5c
Reviewed-on: https://go-review.googlesource.com/c/go/+/203499
Run-TryBot: Bryan C. Mills <bcmills@google.com>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
Bryan C. Mills 2019-10-25 13:55:10 -04:00
parent 3d457f1a36
commit 096126de6b

View File

@ -22,6 +22,22 @@ import (
"time"
)
var testDeadline time.Time
func TestMain(m *testing.M) {
flag.Parse()
// TODO(golang.org/issue/28135): Remove this setup and use t.Deadline instead.
timeoutFlag := flag.Lookup("test.timeout")
if timeoutFlag != nil {
if d := timeoutFlag.Value.(flag.Getter).Get().(time.Duration); d != 0 {
testDeadline = time.Now().Add(d)
}
}
os.Exit(m.Run())
}
func waitSig(t *testing.T, c <-chan os.Signal, sig os.Signal) {
// Sleep multiple times to give the kernel more tries to
// deliver the signal.
@ -392,7 +408,11 @@ func TestAtomicStop(t *testing.T) {
const execs = 10
for i := 0; i < execs; i++ {
cmd := exec.Command(os.Args[0], "-test.run=TestAtomicStop")
timeout := "0"
if !testDeadline.IsZero() {
timeout = testDeadline.Sub(time.Now()).String()
}
cmd := exec.Command(os.Args[0], "-test.run=TestAtomicStop", "-test.timeout="+timeout)
cmd.Env = append(os.Environ(), "GO_TEST_ATOMIC_STOP=1")
out, err := cmd.CombinedOutput()
if err == nil {
@ -431,6 +451,14 @@ func TestAtomicStop(t *testing.T) {
// either catch a signal or die from it.
func atomicStopTestProgram() {
const tries = 10
timeout := 2 * time.Second
if !testDeadline.IsZero() {
// Give each try an equal slice of the deadline, with one slice to spare for
// cleanup.
timeout = testDeadline.Sub(time.Now()) / (tries + 1)
}
pid := syscall.Getpid()
printed := false
for i := 0; i < tries; i++ {
@ -453,7 +481,7 @@ func atomicStopTestProgram() {
select {
case <-cs:
case <-time.After(2 * time.Second):
case <-time.After(timeout):
if !printed {
fmt.Print("lost signal on tries:")
printed = true