Skip to content

Commit b5c0dba

Browse files
author
Bryan C. Mills
committed
net: eliminate arbitrary timeout in TestVariousDeadlines
When we set a timeout, we don't actually have a guarantee one how long the OS will take to notice it. Moreover, if the test deadlocks completely (for example, due to a deadline never taking effect), it would be more useful to get a full goroutine dump instead of the current "client stuck in Dial+Copy" failure message. For #37883 For #41863 Change-Id: I9f712ef1c620f97a5ab69baac45deb71134b99bc Reviewed-on: https://go-review.googlesource.com/c/go/+/371994 Trust: Bryan Mills <[email protected]> Run-TryBot: Bryan Mills <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent 9d0ca26 commit b5c0dba

File tree

1 file changed

+11
-23
lines changed

1 file changed

+11
-23
lines changed

src/net/timeout_test.go

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -947,35 +947,23 @@ func testVariousDeadlines(t *testing.T) {
947947
name := fmt.Sprintf("%v %d/%d", timeout, run, numRuns)
948948
t.Log(name)
949949

950-
tooSlow := time.NewTimer(5 * time.Second)
951-
defer tooSlow.Stop()
952-
953950
c, err := Dial(ls.Listener.Addr().Network(), ls.Listener.Addr().String())
954951
if err != nil {
955952
t.Fatal(err)
956953
}
957954

958-
ch := make(chan result, 1)
959-
go func() {
960-
t0 := time.Now()
961-
if err := c.SetDeadline(t0.Add(timeout)); err != nil {
962-
t.Error(err)
963-
}
964-
n, err := io.Copy(io.Discard, c)
965-
dt := time.Since(t0)
966-
c.Close()
967-
ch <- result{n, err, dt}
968-
}()
955+
t0 := time.Now()
956+
if err := c.SetDeadline(t0.Add(timeout)); err != nil {
957+
t.Error(err)
958+
}
959+
n, err := io.Copy(io.Discard, c)
960+
dt := time.Since(t0)
961+
c.Close()
969962

970-
select {
971-
case res := <-ch:
972-
if nerr, ok := res.err.(Error); ok && nerr.Timeout() {
973-
t.Logf("%v: good timeout after %v; %d bytes", name, res.d, res.n)
974-
} else {
975-
t.Fatalf("%v: Copy = %d, %v; want timeout", name, res.n, res.err)
976-
}
977-
case <-tooSlow.C:
978-
t.Fatalf("%v: client stuck in Dial+Copy", name)
963+
if nerr, ok := err.(Error); ok && nerr.Timeout() {
964+
t.Logf("%v: good timeout after %v; %d bytes", name, dt, n)
965+
} else {
966+
t.Fatalf("%v: Copy = %d, %v; want timeout", name, n, err)
979967
}
980968
}
981969
}

0 commit comments

Comments
 (0)