-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconn_test.go
98 lines (78 loc) · 1.61 KB
/
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package sirencast
import (
"io"
"net"
"strings"
"testing"
"time"
)
// TestConnReaderSwap tests the read-swap of Conn when the first reader
// is exhausted.
func TestConnReaderSwap(t *testing.T) {
startContent, connContent := "hello", "world"
start := strings.NewReader(startContent)
end := strings.NewReader(connContent)
c := &fakeConn{Reader: end}
conn := Conn{
conn: c,
start: start,
reader: start,
handler: nil,
}
b := make([]byte, 5)
_, err := conn.Read(b)
if err != nil {
t.Fatal("reading start failed:", err)
}
if string(b) != startContent {
t.Fatal("reading start returned different content:", string(b), "!=", startContent)
}
_, err = conn.Read(b)
if err != nil {
t.Fatal("reading conn failed:", err)
}
if string(b) != connContent {
t.Fatal("reading conn returned different content:", string(b), "!=", connContent)
}
return
}
type fakeConn struct {
io.Reader
io.Writer
io.Closer
RemoteAddr_ net.Addr
LocalAddr_ net.Addr
closer chan struct{}
closed bool
}
func (fc *fakeConn) Wait() {
<-fc.closer
}
func (fc *fakeConn) Close() error {
if !fc.closed {
close(fc.closer)
fc.closed = true
}
return fc.Closer.Close()
}
func (fc *fakeConn) RemoteAddr() net.Addr {
if fc.RemoteAddr_ == nil {
return fakeAddr{}
}
return fc.RemoteAddr_
}
func (fc *fakeConn) LocalAddr() net.Addr {
if fc.LocalAddr_ == nil {
return fakeAddr{}
}
return fc.LocalAddr_
}
func (fc *fakeConn) SetDeadline(t time.Time) error {
return nil
}
func (fc *fakeConn) SetReadDeadline(t time.Time) error {
return nil
}
func (fc *fakeConn) SetWriteDeadline(t time.Time) error {
return nil
}