]> Cypherpunks repositories - gostls13.git/commitdiff
os/signal: derive TestAtomicStop timeout from overall test timeout
authorBryan C. Mills <bcmills@google.com>
Fri, 25 Oct 2019 17:55:10 +0000 (13:55 -0400)
committerBryan C. Mills <bcmills@google.com>
Fri, 25 Oct 2019 18:44:06 +0000 (18:44 +0000)
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>
src/os/signal/signal_test.go

index 7aa3d7805b28f0b1ef30b0480765d361e65f4993..ee884bc632d35ff952dc8e5b34fd476973c3495c 100644 (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