-
Notifications
You must be signed in to change notification settings - Fork 0
/
mock.go
67 lines (56 loc) · 1.71 KB
/
mock.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
package irc
import (
"io"
"net"
"time"
)
// addr holds the internal data of the implementation of the net.Addr interface
type addr struct {
typ string
addr string
}
// Methods required by the net.Addr interface
func (a *addr) Network() string { return a.typ }
func (a *addr) String() string { return a.addr }
// mockConn holds the internal data structures needed to mock a TCP connection
type mockConn struct {
reader *io.PipeReader
writer *io.PipeWriter
}
// Methods required to satisfy the net.Conn interface
func (c mockConn) Read(data []byte) (int, error) { return c.reader.Read(data) }
func (c mockConn) Write(data []byte) (int, error) { return c.writer.Write(data) }
func (c mockConn) LocalAddr() net.Addr { return &addr{"tcp", "127.0.0.1"} }
func (c mockConn) RemoteAddr() net.Addr { return &addr{"tcp", "127.0.0.1"} }
func (c mockConn) SetDeadline(t time.Time) error { return nil }
func (c mockConn) SetReadDeadline(t time.Time) error { return nil }
func (c mockConn) SetWriteDeadline(t time.Time) error { return nil }
func (c mockConn) Close() error {
if err := c.reader.Close(); err != nil {
return err
}
if err := c.writer.Close(); err != nil {
return err
}
return nil
}
// mockComm contains the mocked connections for client -> server and server -> client communication
type mockComm struct {
Client *mockConn
Server *mockConn
}
// newMockComm creates a new mocked communications hub
func newMockComm() *mockComm {
clientRead, serverWrite := io.Pipe()
serverRead, clientWrite := io.Pipe()
return &mockComm{
Client: &mockConn{
reader: clientRead,
writer: clientWrite,
},
Server: &mockConn{
reader: serverRead,
writer: serverWrite,
},
}
}