]> Cypherpunks.ru repositories - gostls13.git/commitdiff
os/signal: remove some arbitrary timeouts in tests
authorBryan C. Mills <bcmills@google.com>
Thu, 9 Nov 2023 13:52:17 +0000 (08:52 -0500)
committerBryan Mills <bcmills@google.com>
Thu, 9 Nov 2023 15:34:56 +0000 (15:34 +0000)
This should fix the test flake found in
https://build.golang.org/log/48ffb18e85dda480b7a67e8305dd03ee8337f170.

For #58901.

Change-Id: I1fcdd713a78e6b7c81e38133ce5f42f7f448a1a3
Reviewed-on: https://go-review.googlesource.com/c/go/+/541115
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Michael Pratt <mpratt@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Bryan Mills <bcmills@google.com>

src/os/signal/example_unix_test.go
src/os/signal/signal_test.go

index b7047ac45cdf2c5f83c11582394d4aa36e652e0d..583d4e4089f52f82ada6218ca7362b9ed4ffbe23 100644 (file)
@@ -12,9 +12,10 @@ import (
        "log"
        "os"
        "os/signal"
-       "time"
 )
 
+var neverReady = make(chan struct{}) // never closed
+
 // This example passes a context with a signal to tell a blocking function that
 // it should abandon its work after a signal is received.
 func ExampleNotifyContext() {
@@ -35,8 +36,8 @@ func ExampleNotifyContext() {
        }
 
        select {
-       case <-time.After(time.Second):
-               fmt.Println("missed signal")
+       case <-neverReady:
+               fmt.Println("ready")
        case <-ctx.Done():
                fmt.Println(ctx.Err()) // prints "context canceled"
                stop()                 // stop receiving signal notifications as soon as possible.
index 23040dc443774de520582a5a6b521e4872d88e38..d54787bc199a3b362202330d6947973ef0dbe9c4 100644 (file)
@@ -797,13 +797,9 @@ func TestNotifyContextStop(t *testing.T) {
        }
 
        stop()
-       select {
-       case <-c.Done():
-               if got := c.Err(); got != context.Canceled {
-                       t.Errorf("c.Err() = %q, want %q", got, context.Canceled)
-               }
-       case <-time.After(time.Second):
-               t.Errorf("timed out waiting for context to be done after calling stop")
+       <-c.Done()
+       if got := c.Err(); got != context.Canceled {
+               t.Errorf("c.Err() = %q, want %q", got, context.Canceled)
        }
 }
 
@@ -818,13 +814,9 @@ func TestNotifyContextCancelParent(t *testing.T) {
        }
 
        cancelParent()
-       select {
-       case <-c.Done():
-               if got := c.Err(); got != context.Canceled {
-                       t.Errorf("c.Err() = %q, want %q", got, context.Canceled)
-               }
-       case <-time.After(time.Second):
-               t.Errorf("timed out waiting for parent context to be canceled")
+       <-c.Done()
+       if got := c.Err(); got != context.Canceled {
+               t.Errorf("c.Err() = %q, want %q", got, context.Canceled)
        }
 }
 
@@ -840,13 +832,9 @@ func TestNotifyContextPrematureCancelParent(t *testing.T) {
                t.Errorf("c.String() = %q, want %q", got, want)
        }
 
-       select {
-       case <-c.Done():
-               if got := c.Err(); got != context.Canceled {
-                       t.Errorf("c.Err() = %q, want %q", got, context.Canceled)
-               }
-       case <-time.After(time.Second):
-               t.Errorf("timed out waiting for parent context to be canceled")
+       <-c.Done()
+       if got := c.Err(); got != context.Canceled {
+               t.Errorf("c.Err() = %q, want %q", got, context.Canceled)
        }
 }
 
@@ -868,13 +856,9 @@ func TestNotifyContextSimultaneousStop(t *testing.T) {
                }()
        }
        wg.Wait()
-       select {
-       case <-c.Done():
-               if got := c.Err(); got != context.Canceled {
-                       t.Errorf("c.Err() = %q, want %q", got, context.Canceled)
-               }
-       case <-time.After(time.Second):
-               t.Errorf("expected context to be canceled")
+       <-c.Done()
+       if got := c.Err(); got != context.Canceled {
+               t.Errorf("c.Err() = %q, want %q", got, context.Canceled)
        }
 }
 
@@ -920,7 +904,6 @@ func TestSignalTrace(t *testing.T) {
                if err := trace.Start(buf); err != nil {
                        t.Fatalf("[%d] failed to start tracing: %v", i, err)
                }
-               time.After(1 * time.Microsecond)
                trace.Stop()
                size := buf.Len()
                if size == 0 {