net: fix flaky *TimeoutMustNotReturn tests

The tester goroutine doesn't always gets a chance to run before the
timeout expires. Wait for the goroutine to start and set deadlines
before staring the timer.

Fixes #36796

Change-Id: Iffed6259de31340c3f66e34da473826a1d09fcde
Reviewed-on: https://go-review.googlesource.com/c/go/+/412858
Reviewed-by: Damien Neil <dneil@google.com>
Run-TryBot: Bryan Mills <bcmills@google.com>
Reviewed-by: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
This commit is contained in:
Dmitri Goutnik 2022-06-17 12:10:09 -05:00 committed by Damien Neil
parent 6c25ba624f
commit dd2d00f9d5

View file

@ -243,8 +243,7 @@ func TestAcceptTimeoutMustNotReturn(t *testing.T) {
ln := newLocalListener(t, "tcp")
defer ln.Close()
max := time.NewTimer(100 * time.Millisecond)
defer max.Stop()
maxch := make(chan *time.Timer)
ch := make(chan error)
go func() {
if err := ln.(*TCPListener).SetDeadline(time.Now().Add(-5 * time.Second)); err != nil {
@ -253,10 +252,14 @@ func TestAcceptTimeoutMustNotReturn(t *testing.T) {
if err := ln.(*TCPListener).SetDeadline(noDeadline); err != nil {
t.Error(err)
}
maxch <- time.NewTimer(100 * time.Millisecond)
_, err := ln.Accept()
ch <- err
}()
max := <-maxch
defer max.Stop()
select {
case err := <-ch:
if perr := parseAcceptError(err); perr != nil {
@ -348,8 +351,7 @@ func TestReadTimeoutMustNotReturn(t *testing.T) {
}
defer c.Close()
max := time.NewTimer(100 * time.Millisecond)
defer max.Stop()
maxch := make(chan *time.Timer)
ch := make(chan error)
go func() {
if err := c.SetDeadline(time.Now().Add(-5 * time.Second)); err != nil {
@ -361,11 +363,15 @@ func TestReadTimeoutMustNotReturn(t *testing.T) {
if err := c.SetReadDeadline(noDeadline); err != nil {
t.Error(err)
}
maxch <- time.NewTimer(100 * time.Millisecond)
var b [1]byte
_, err := c.Read(b[:])
ch <- err
}()
max := <-maxch
defer max.Stop()
select {
case err := <-ch:
if perr := parseReadError(err); perr != nil {
@ -517,8 +523,7 @@ func TestWriteTimeoutMustNotReturn(t *testing.T) {
}
defer c.Close()
max := time.NewTimer(100 * time.Millisecond)
defer max.Stop()
maxch := make(chan *time.Timer)
ch := make(chan error)
go func() {
if err := c.SetDeadline(time.Now().Add(-5 * time.Second)); err != nil {
@ -530,6 +535,7 @@ func TestWriteTimeoutMustNotReturn(t *testing.T) {
if err := c.SetWriteDeadline(noDeadline); err != nil {
t.Error(err)
}
maxch <- time.NewTimer(100 * time.Millisecond)
var b [1]byte
for {
if _, err := c.Write(b[:]); err != nil {
@ -539,6 +545,9 @@ func TestWriteTimeoutMustNotReturn(t *testing.T) {
}
}()
max := <-maxch
defer max.Stop()
select {
case err := <-ch:
if perr := parseWriteError(err); perr != nil {