Commit 096126de authored by Bryan C. Mills's avatar Bryan C. Mills

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: default avatarBrad Fitzpatrick <bradfitz@golang.org>
parent 3d457f1a
...@@ -22,6 +22,22 @@ import ( ...@@ -22,6 +22,22 @@ import (
"time" "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) { func waitSig(t *testing.T, c <-chan os.Signal, sig os.Signal) {
// Sleep multiple times to give the kernel more tries to // Sleep multiple times to give the kernel more tries to
// deliver the signal. // deliver the signal.
...@@ -392,7 +408,11 @@ func TestAtomicStop(t *testing.T) { ...@@ -392,7 +408,11 @@ func TestAtomicStop(t *testing.T) {
const execs = 10 const execs = 10
for i := 0; i < execs; i++ { 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") cmd.Env = append(os.Environ(), "GO_TEST_ATOMIC_STOP=1")
out, err := cmd.CombinedOutput() out, err := cmd.CombinedOutput()
if err == nil { if err == nil {
...@@ -431,6 +451,14 @@ func TestAtomicStop(t *testing.T) { ...@@ -431,6 +451,14 @@ func TestAtomicStop(t *testing.T) {
// either catch a signal or die from it. // either catch a signal or die from it.
func atomicStopTestProgram() { func atomicStopTestProgram() {
const tries = 10 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() pid := syscall.Getpid()
printed := false printed := false
for i := 0; i < tries; i++ { for i := 0; i < tries; i++ {
...@@ -453,7 +481,7 @@ func atomicStopTestProgram() { ...@@ -453,7 +481,7 @@ func atomicStopTestProgram() {
select { select {
case <-cs: case <-cs:
case <-time.After(2 * time.Second): case <-time.After(timeout):
if !printed { if !printed {
fmt.Print("lost signal on tries:") fmt.Print("lost signal on tries:")
printed = true printed = true
......
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