forked from zenazn/goji
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconn_test.go
63 lines (56 loc) · 1.21 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
package graceful
import (
"net"
"time"
)
// Stub out a net.Conn. This is going to be painful.
type fakeAddr struct{}
func (f fakeAddr) Network() string {
return "fake"
}
func (f fakeAddr) String() string {
return "fake"
}
type fakeConn struct {
onRead, onWrite, onClose, onLocalAddr, onRemoteAddr func()
onSetDeadline, onSetReadDeadline, onSetWriteDeadline func()
}
// Here's my number, so...
func callMeMaybe(f func()) {
// I apologize for nothing.
if f != nil {
f()
}
}
func (f fakeConn) Read(b []byte) (int, error) {
callMeMaybe(f.onRead)
return len(b), nil
}
func (f fakeConn) Write(b []byte) (int, error) {
callMeMaybe(f.onWrite)
return len(b), nil
}
func (f fakeConn) Close() error {
callMeMaybe(f.onClose)
return nil
}
func (f fakeConn) LocalAddr() net.Addr {
callMeMaybe(f.onLocalAddr)
return fakeAddr{}
}
func (f fakeConn) RemoteAddr() net.Addr {
callMeMaybe(f.onRemoteAddr)
return fakeAddr{}
}
func (f fakeConn) SetDeadline(t time.Time) error {
callMeMaybe(f.onSetDeadline)
return nil
}
func (f fakeConn) SetReadDeadline(t time.Time) error {
callMeMaybe(f.onSetReadDeadline)
return nil
}
func (f fakeConn) SetWriteDeadline(t time.Time) error {
callMeMaybe(f.onSetWriteDeadline)
return nil
}