1
0
mirror of https://github.com/golang/go synced 2024-11-18 05:04:47 -07:00

net: relax error checking in TestAcceptIgnoreSomeErrors

TestAcceptIgnoreSomeErrors was created to test that network
accept function ignores some errors. But conditions created
by the test also affects network reads. Change the test to
ignore these read errors when acceptable.

Fixes #10785

Change-Id: I3da85cb55bd3e78c1980ad949e53e82391f9b41e
Reviewed-on: https://go-review.googlesource.com/9942
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
Alex Brainman 2015-05-12 11:34:23 +10:00
parent 7bbd4f780b
commit 71bf182028

View File

@ -15,16 +15,31 @@ import (
"time"
)
func toErrno(err error) (syscall.Errno, bool) {
operr, ok := err.(*OpError)
if !ok {
return 0, false
}
syserr, ok := operr.Err.(*os.SyscallError)
if !ok {
return 0, false
}
errno, ok := syserr.Err.(syscall.Errno)
if !ok {
return 0, false
}
return errno, true
}
// TestAcceptIgnoreSomeErrors tests that windows TCPListener.AcceptTCP
// handles broken connections. It verifies that broken connections do
// not affect future connections.
func TestAcceptIgnoreSomeErrors(t *testing.T) {
recv := func(ln Listener) (string, error) {
recv := func(ln Listener, ignoreSomeReadErrors bool) (string, error) {
c, err := ln.Accept()
if err != nil {
// Display windows errno in error message.
operr, ok := err.(*OpError)
if !ok {
return "", err
}
errno, ok := operr.Err.(syscall.Errno)
errno, ok := toErrno(err)
if !ok {
return "", err
}
@ -34,10 +49,14 @@ func TestAcceptIgnoreSomeErrors(t *testing.T) {
b := make([]byte, 100)
n, err := c.Read(b)
if err != nil && err != io.EOF {
return "", err
if err == nil || err == io.EOF {
return string(b[:n]), nil
}
return string(b[:n]), nil
errno, ok := toErrno(err)
if ok && ignoreSomeReadErrors && (errno == syscall.ERROR_NETNAME_DELETED || errno == syscall.WSAECONNRESET) {
return "", nil
}
return "", err
}
send := func(addr string, data string) error {
@ -121,13 +140,13 @@ func TestAcceptIgnoreSomeErrors(t *testing.T) {
}()
// Receive first or second connection.
s, err := recv(ln)
s, err := recv(ln, true)
if err != nil {
t.Fatalf("recv failed: %v", err)
}
switch s {
case "":
// First connection data is received, lets get second connection data.
// First connection data is received, let's get second connection data.
case "abc":
// First connection is lost forever, but that is ok.
return
@ -136,7 +155,7 @@ func TestAcceptIgnoreSomeErrors(t *testing.T) {
}
// Get second connection data.
s, err = recv(ln)
s, err = recv(ln, false)
if err != nil {
t.Fatalf("recv failed: %v", err)
}