aboutsummaryrefslogtreecommitdiff
path: root/src/net/timeout_test.go
diff options
context:
space:
mode:
authorIan Lance Taylor <iant@golang.org>2020-04-17 15:42:12 -0700
committerIan Lance Taylor <iant@golang.org>2020-04-25 00:26:48 +0000
commitd422f54619b5b6e6301eaa3e9f22cfa7b65063c8 (patch)
tree23c599e3a59f33116cb9caad4aaa91a87ec03c0b /src/net/timeout_test.go
parent396833caef83b20f38199f9d74cb3e768b2fd478 (diff)
downloadgo-d422f54619b5b6e6301eaa3e9f22cfa7b65063c8.tar.xz
os, net: define and use os.ErrDeadlineExceeded
If an I/O operation fails because a deadline was exceeded, return os.ErrDeadlineExceeded. We used to return poll.ErrTimeout, an internal error, and told users to check the Timeout method. However, there are other errors with a Timeout method that returns true, notably syscall.ETIMEDOUT which is returned for a keep-alive timeout. Checking errors.Is(err, os.ErrDeadlineExceeded) should permit code to reliably tell why it failed. This change does not affect the handling of net.Dialer.Deadline, nor does it change the handling of net.DialContext when the context deadline is exceeded. Those cases continue to return an error reported as "i/o timeout" for which Timeout is true, but that error is not os.ErrDeadlineExceeded. Fixes #31449 Change-Id: I0323f42e944324c6f2578f00c3ac90c24fe81177 Reviewed-on: https://go-review.googlesource.com/c/go/+/228645 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Filippo Valsorda <filippo@golang.org>
Diffstat (limited to 'src/net/timeout_test.go')
-rw-r--r--src/net/timeout_test.go58
1 files changed, 38 insertions, 20 deletions
diff --git a/src/net/timeout_test.go b/src/net/timeout_test.go
index 51123dfbc4..ad14cd79ac 100644
--- a/src/net/timeout_test.go
+++ b/src/net/timeout_test.go
@@ -7,12 +7,13 @@
package net
import (
+ "errors"
"fmt"
- "internal/poll"
"internal/testenv"
"io"
"io/ioutil"
"net/internal/socktest"
+ "os"
"runtime"
"sync"
"testing"
@@ -148,9 +149,9 @@ var acceptTimeoutTests = []struct {
}{
// Tests that accept deadlines in the past work, even if
// there's incoming connections available.
- {-5 * time.Second, [2]error{poll.ErrTimeout, poll.ErrTimeout}},
+ {-5 * time.Second, [2]error{os.ErrDeadlineExceeded, os.ErrDeadlineExceeded}},
- {50 * time.Millisecond, [2]error{nil, poll.ErrTimeout}},
+ {50 * time.Millisecond, [2]error{nil, os.ErrDeadlineExceeded}},
}
func TestAcceptTimeout(t *testing.T) {
@@ -194,7 +195,7 @@ func TestAcceptTimeout(t *testing.T) {
if perr := parseAcceptError(err); perr != nil {
t.Errorf("#%d/%d: %v", i, j, perr)
}
- if nerr, ok := err.(Error); !ok || !nerr.Timeout() {
+ if !isDeadlineExceeded(err) {
t.Fatalf("#%d/%d: %v", i, j, err)
}
}
@@ -250,7 +251,7 @@ func TestAcceptTimeoutMustReturn(t *testing.T) {
if perr := parseAcceptError(err); perr != nil {
t.Error(perr)
}
- if nerr, ok := err.(Error); !ok || !nerr.Timeout() {
+ if !isDeadlineExceeded(err) {
t.Fatal(err)
}
}
@@ -302,9 +303,9 @@ var readTimeoutTests = []struct {
}{
// Tests that read deadlines work, even if there's data ready
// to be read.
- {-5 * time.Second, [2]error{poll.ErrTimeout, poll.ErrTimeout}},
+ {-5 * time.Second, [2]error{os.ErrDeadlineExceeded, os.ErrDeadlineExceeded}},
- {50 * time.Millisecond, [2]error{nil, poll.ErrTimeout}},
+ {50 * time.Millisecond, [2]error{nil, os.ErrDeadlineExceeded}},
}
func TestReadTimeout(t *testing.T) {
@@ -344,7 +345,7 @@ func TestReadTimeout(t *testing.T) {
if perr := parseReadError(err); perr != nil {
t.Errorf("#%d/%d: %v", i, j, perr)
}
- if nerr, ok := err.(Error); !ok || !nerr.Timeout() {
+ if !isDeadlineExceeded(err) {
t.Fatalf("#%d/%d: %v", i, j, err)
}
}
@@ -423,9 +424,9 @@ var readFromTimeoutTests = []struct {
}{
// Tests that read deadlines work, even if there's data ready
// to be read.
- {-5 * time.Second, [2]error{poll.ErrTimeout, poll.ErrTimeout}},
+ {-5 * time.Second, [2]error{os.ErrDeadlineExceeded, os.ErrDeadlineExceeded}},
- {50 * time.Millisecond, [2]error{nil, poll.ErrTimeout}},
+ {50 * time.Millisecond, [2]error{nil, os.ErrDeadlineExceeded}},
}
func TestReadFromTimeout(t *testing.T) {
@@ -468,7 +469,7 @@ func TestReadFromTimeout(t *testing.T) {
if perr := parseReadError(err); perr != nil {
t.Errorf("#%d/%d: %v", i, j, perr)
}
- if nerr, ok := err.(Error); !ok || !nerr.Timeout() {
+ if !isDeadlineExceeded(err) {
t.Fatalf("#%d/%d: %v", i, j, err)
}
}
@@ -491,9 +492,9 @@ var writeTimeoutTests = []struct {
}{
// Tests that write deadlines work, even if there's buffer
// space available to write.
- {-5 * time.Second, [2]error{poll.ErrTimeout, poll.ErrTimeout}},
+ {-5 * time.Second, [2]error{os.ErrDeadlineExceeded, os.ErrDeadlineExceeded}},
- {10 * time.Millisecond, [2]error{nil, poll.ErrTimeout}},
+ {10 * time.Millisecond, [2]error{nil, os.ErrDeadlineExceeded}},
}
func TestWriteTimeout(t *testing.T) {
@@ -522,7 +523,7 @@ func TestWriteTimeout(t *testing.T) {
if perr := parseWriteError(err); perr != nil {
t.Errorf("#%d/%d: %v", i, j, perr)
}
- if nerr, ok := err.(Error); !ok || !nerr.Timeout() {
+ if !isDeadlineExceeded(err) {
t.Fatalf("#%d/%d: %v", i, j, err)
}
}
@@ -605,9 +606,9 @@ var writeToTimeoutTests = []struct {
}{
// Tests that write deadlines work, even if there's buffer
// space available to write.
- {-5 * time.Second, [2]error{poll.ErrTimeout, poll.ErrTimeout}},
+ {-5 * time.Second, [2]error{os.ErrDeadlineExceeded, os.ErrDeadlineExceeded}},
- {10 * time.Millisecond, [2]error{nil, poll.ErrTimeout}},
+ {10 * time.Millisecond, [2]error{nil, os.ErrDeadlineExceeded}},
}
func TestWriteToTimeout(t *testing.T) {
@@ -641,7 +642,7 @@ func TestWriteToTimeout(t *testing.T) {
if perr := parseWriteError(err); perr != nil {
t.Errorf("#%d/%d: %v", i, j, perr)
}
- if nerr, ok := err.(Error); !ok || !nerr.Timeout() {
+ if !isDeadlineExceeded(err) {
t.Fatalf("#%d/%d: %v", i, j, err)
}
}
@@ -685,7 +686,7 @@ func TestReadTimeoutFluctuation(t *testing.T) {
if perr := parseReadError(err); perr != nil {
t.Error(perr)
}
- if nerr, ok := err.(Error); !ok || !nerr.Timeout() {
+ if !isDeadlineExceeded(err) {
t.Fatal(err)
}
}
@@ -718,7 +719,7 @@ func TestReadFromTimeoutFluctuation(t *testing.T) {
if perr := parseReadError(err); perr != nil {
t.Error(perr)
}
- if nerr, ok := err.(Error); !ok || !nerr.Timeout() {
+ if !isDeadlineExceeded(err) {
t.Fatal(err)
}
}
@@ -760,7 +761,7 @@ func TestWriteTimeoutFluctuation(t *testing.T) {
if perr := parseWriteError(err); perr != nil {
t.Error(perr)
}
- if nerr, ok := err.(Error); !ok || !nerr.Timeout() {
+ if !isDeadlineExceeded(err) {
t.Fatal(err)
}
}
@@ -1073,3 +1074,20 @@ func TestConcurrentSetDeadline(t *testing.T) {
}
wg.Wait()
}
+
+// isDeadlineExceeded reports whether err is or wraps os.ErrDeadlineExceeded.
+// We also check that the error implements net.Error, and that the
+// Timeout method returns true.
+func isDeadlineExceeded(err error) bool {
+ nerr, ok := err.(Error)
+ if !ok {
+ return false
+ }
+ if !nerr.Timeout() {
+ return false
+ }
+ if !errors.Is(err, os.ErrDeadlineExceeded) {
+ return false
+ }
+ return true
+}