Skip to content

Commit 1b03ec8

Browse files
author
Bryan C. Mills
committed
os/signal: remove some arbitrary timeouts in tests
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 <[email protected]> Reviewed-by: Michael Pratt <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Bryan Mills <[email protected]>
1 parent 291ffcb commit 1b03ec8

File tree

2 files changed

+16
-32
lines changed

2 files changed

+16
-32
lines changed

src/os/signal/example_unix_test.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ import (
1212
"log"
1313
"os"
1414
"os/signal"
15-
"time"
1615
)
1716

17+
var neverReady = make(chan struct{}) // never closed
18+
1819
// This example passes a context with a signal to tell a blocking function that
1920
// it should abandon its work after a signal is received.
2021
func ExampleNotifyContext() {
@@ -35,8 +36,8 @@ func ExampleNotifyContext() {
3536
}
3637

3738
select {
38-
case <-time.After(time.Second):
39-
fmt.Println("missed signal")
39+
case <-neverReady:
40+
fmt.Println("ready")
4041
case <-ctx.Done():
4142
fmt.Println(ctx.Err()) // prints "context canceled"
4243
stop() // stop receiving signal notifications as soon as possible.

src/os/signal/signal_test.go

+12-29
Original file line numberDiff line numberDiff line change
@@ -797,13 +797,9 @@ func TestNotifyContextStop(t *testing.T) {
797797
}
798798

799799
stop()
800-
select {
801-
case <-c.Done():
802-
if got := c.Err(); got != context.Canceled {
803-
t.Errorf("c.Err() = %q, want %q", got, context.Canceled)
804-
}
805-
case <-time.After(time.Second):
806-
t.Errorf("timed out waiting for context to be done after calling stop")
800+
<-c.Done()
801+
if got := c.Err(); got != context.Canceled {
802+
t.Errorf("c.Err() = %q, want %q", got, context.Canceled)
807803
}
808804
}
809805

@@ -818,13 +814,9 @@ func TestNotifyContextCancelParent(t *testing.T) {
818814
}
819815

820816
cancelParent()
821-
select {
822-
case <-c.Done():
823-
if got := c.Err(); got != context.Canceled {
824-
t.Errorf("c.Err() = %q, want %q", got, context.Canceled)
825-
}
826-
case <-time.After(time.Second):
827-
t.Errorf("timed out waiting for parent context to be canceled")
817+
<-c.Done()
818+
if got := c.Err(); got != context.Canceled {
819+
t.Errorf("c.Err() = %q, want %q", got, context.Canceled)
828820
}
829821
}
830822

@@ -840,13 +832,9 @@ func TestNotifyContextPrematureCancelParent(t *testing.T) {
840832
t.Errorf("c.String() = %q, want %q", got, want)
841833
}
842834

843-
select {
844-
case <-c.Done():
845-
if got := c.Err(); got != context.Canceled {
846-
t.Errorf("c.Err() = %q, want %q", got, context.Canceled)
847-
}
848-
case <-time.After(time.Second):
849-
t.Errorf("timed out waiting for parent context to be canceled")
835+
<-c.Done()
836+
if got := c.Err(); got != context.Canceled {
837+
t.Errorf("c.Err() = %q, want %q", got, context.Canceled)
850838
}
851839
}
852840

@@ -868,13 +856,9 @@ func TestNotifyContextSimultaneousStop(t *testing.T) {
868856
}()
869857
}
870858
wg.Wait()
871-
select {
872-
case <-c.Done():
873-
if got := c.Err(); got != context.Canceled {
874-
t.Errorf("c.Err() = %q, want %q", got, context.Canceled)
875-
}
876-
case <-time.After(time.Second):
877-
t.Errorf("expected context to be canceled")
859+
<-c.Done()
860+
if got := c.Err(); got != context.Canceled {
861+
t.Errorf("c.Err() = %q, want %q", got, context.Canceled)
878862
}
879863
}
880864

@@ -920,7 +904,6 @@ func TestSignalTrace(t *testing.T) {
920904
if err := trace.Start(buf); err != nil {
921905
t.Fatalf("[%d] failed to start tracing: %v", i, err)
922906
}
923-
time.After(1 * time.Microsecond)
924907
trace.Stop()
925908
size := buf.Len()
926909
if size == 0 {

0 commit comments

Comments
 (0)