From 096126de6b33a0c7831aebcdde00081876991438 Mon Sep 17 00:00:00 2001 From: "Bryan C. Mills" Date: Fri, 25 Oct 2019 13:55:10 -0400 Subject: [PATCH] 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 Reviewed-by: Brad Fitzpatrick --- src/os/signal/signal_test.go | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/src/os/signal/signal_test.go b/src/os/signal/signal_test.go index 7aa3d7805b..ee884bc632 100644 --- a/src/os/signal/signal_test.go +++ b/src/os/signal/signal_test.go @@ -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 -- 2.48.1