-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathconn_test.go
48 lines (44 loc) · 974 Bytes
/
conn_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Copyright 2016 Mikio Hara. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package tcp_test
import (
"net"
"testing"
"github.com/mikioh/tcp"
"golang.org/x/net/nettest"
)
func TestConn(t *testing.T) {
nettest.TestConn(t, func() (net.Conn, net.Conn, func(), error) {
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
return nil, nil, nil, err
}
defer ln.Close()
type pasv struct {
net.Conn
error
}
ch := make(chan pasv)
go func() {
var p pasv
p.Conn, p.error = ln.Accept()
ch <- p
}()
c, err := net.Dial(ln.Addr().Network(), ln.Addr().String())
if err != nil {
return nil, nil, nil, err
}
tc, err := tcp.NewConn(c)
if err != nil {
c.Close()
return nil, nil, nil, err
}
p := <-ch
if p.error != nil {
tc.Close()
return nil, nil, nil, err
}
return tc, p.Conn, func() { tc.Close(); p.Conn.Close() }, nil
})
}