1 // Copyright 2012 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // +build darwin freebsd linux netbsd openbsd
15 // Issue 3590. netFd.AddFD should return an error
16 // from the underlying pollster rather than panicing.
17 func TestAddFDReturnsError(t
*testing
.T
) {
18 ln
:= newLocalListener(t
).(*TCPListener
)
20 connected
:= make(chan bool)
32 c
, err
:= DialTCP("tcp", nil, ln
.Addr().(*TCPAddr
))
39 // replace c's pollServer with a closed version.
40 ps
, err
:= newPollServer()
45 c
.conn
.fd
.pollServer
= ps
49 if err
, ok
:= err
.(*OpError
); ok
{
50 if err
.Op
== "addfd" {
53 if err
, ok
:= err
.Err
.(*OpError
); ok
{
54 // the err is sometimes wrapped by another OpError
55 if err
.Op
== "addfd" {
60 t
.Error("unexpected error:", err
)
63 var chkReadErrTests
= []struct {
70 {100, nil, &netFD
{sotype
: syscall
.SOCK_STREAM
}, nil},
71 {100, io
.EOF
, &netFD
{sotype
: syscall
.SOCK_STREAM
}, io
.EOF
},
72 {100, errClosing
, &netFD
{sotype
: syscall
.SOCK_STREAM
}, errClosing
},
73 {0, nil, &netFD
{sotype
: syscall
.SOCK_STREAM
}, io
.EOF
},
74 {0, io
.EOF
, &netFD
{sotype
: syscall
.SOCK_STREAM
}, io
.EOF
},
75 {0, errClosing
, &netFD
{sotype
: syscall
.SOCK_STREAM
}, errClosing
},
77 {100, nil, &netFD
{sotype
: syscall
.SOCK_DGRAM
}, nil},
78 {100, io
.EOF
, &netFD
{sotype
: syscall
.SOCK_DGRAM
}, io
.EOF
},
79 {100, errClosing
, &netFD
{sotype
: syscall
.SOCK_DGRAM
}, errClosing
},
80 {0, nil, &netFD
{sotype
: syscall
.SOCK_DGRAM
}, nil},
81 {0, io
.EOF
, &netFD
{sotype
: syscall
.SOCK_DGRAM
}, io
.EOF
},
82 {0, errClosing
, &netFD
{sotype
: syscall
.SOCK_DGRAM
}, errClosing
},
84 {100, nil, &netFD
{sotype
: syscall
.SOCK_SEQPACKET
}, nil},
85 {100, io
.EOF
, &netFD
{sotype
: syscall
.SOCK_SEQPACKET
}, io
.EOF
},
86 {100, errClosing
, &netFD
{sotype
: syscall
.SOCK_SEQPACKET
}, errClosing
},
87 {0, nil, &netFD
{sotype
: syscall
.SOCK_SEQPACKET
}, io
.EOF
},
88 {0, io
.EOF
, &netFD
{sotype
: syscall
.SOCK_SEQPACKET
}, io
.EOF
},
89 {0, errClosing
, &netFD
{sotype
: syscall
.SOCK_SEQPACKET
}, errClosing
},
91 {100, nil, &netFD
{sotype
: syscall
.SOCK_RAW
}, nil},
92 {100, io
.EOF
, &netFD
{sotype
: syscall
.SOCK_RAW
}, io
.EOF
},
93 {100, errClosing
, &netFD
{sotype
: syscall
.SOCK_RAW
}, errClosing
},
94 {0, nil, &netFD
{sotype
: syscall
.SOCK_RAW
}, nil},
95 {0, io
.EOF
, &netFD
{sotype
: syscall
.SOCK_RAW
}, io
.EOF
},
96 {0, errClosing
, &netFD
{sotype
: syscall
.SOCK_RAW
}, errClosing
},
99 func TestChkReadErr(t
*testing
.T
) {
100 for _
, tt
:= range chkReadErrTests
{
101 actual
:= chkReadErr(tt
.n
, tt
.err
, tt
.fd
)
102 if actual
!= tt
.expected
{
103 t
.Errorf("chkReadError(%v, %v, %v): expected %v, actual %v", tt
.n
, tt
.err
, tt
.fd
.sotype
, tt
.expected
, actual
)