| 1 |
package mock |
| 2 |
|
| 3 |
import ( |
| 4 |
"net" |
| 5 |
"time" |
| 6 |
) |
| 7 |
|
| 8 |
type MockConn struct { |
| 9 |
t int |
| 10 |
err error |
| 11 |
block bool |
| 12 |
readIndex int |
| 13 |
first bool |
| 14 |
readings [][]byte |
| 15 |
Closed bool |
| 16 |
Written [][]byte |
| 17 |
} |
| 18 |
|
| 19 |
// Creates a new mock object which satisfies the net.Conn interface |
| 20 |
func Conn() *MockConn { |
| 21 |
return &MockConn{ |
| 22 |
first: true, |
| 23 |
Written: make([][]byte, 0, 4), |
| 24 |
readings: make([][]byte, 0, 4), |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
func (c *MockConn) Read(out []byte) (int, error) { |
| 29 |
if c.first { |
| 30 |
c.first = false |
| 31 |
if c.err != nil { |
| 32 |
return 0, c.err |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
if c.readIndex == len(c.readings) { |
| 37 |
if c.block == false { |
| 38 |
return 0, nil |
| 39 |
} |
| 40 |
time.Sleep(time.Minute) |
| 41 |
} |
| 42 |
bytes := c.readings[c.readIndex] |
| 43 |
end := len(bytes) |
| 44 |
if end > len(out) { |
| 45 |
end = len(out) |
| 46 |
} |
| 47 |
copy(out, bytes[:end]) |
| 48 |
length := len(bytes[:end]) |
| 49 |
c.readings[c.readIndex] = bytes[end:] |
| 50 |
if len(bytes[end:]) == 0 { |
| 51 |
c.readIndex++ |
| 52 |
} else { |
| 53 |
c.readings[c.readIndex] = bytes[end:] |
| 54 |
} |
| 55 |
return length, nil |
| 56 |
} |
| 57 |
|
| 58 |
func (c *MockConn) Write(b []byte) (int, error) { |
| 59 |
if c.err != nil { |
| 60 |
return 0, c.err |
| 61 |
} |
| 62 |
c.Written = append(c.Written, b) |
| 63 |
return len(b), nil |
| 64 |
} |
| 65 |
|
| 66 |
func (c *MockConn) Close() error { |
| 67 |
c.Closed = true |
| 68 |
return nil |
| 69 |
} |
| 70 |
|
| 71 |
func (c *MockConn) LocalAddr() net.Addr { |
| 72 |
return nil |
| 73 |
} |
| 74 |
|
| 75 |
func (c *MockConn) RemoteAddr() net.Addr { |
| 76 |
return nil |
| 77 |
} |
| 78 |
|
| 79 |
func (c *MockConn) SetDeadline(t time.Time) error { |
| 80 |
c.SetReadDeadline(t) |
| 81 |
c.SetWriteDeadline(t) |
| 82 |
return nil |
| 83 |
} |
| 84 |
|
| 85 |
func (c *MockConn) SetReadDeadline(t time.Time) error { |
| 86 |
return nil |
| 87 |
} |
| 88 |
|
| 89 |
// noop |
| 90 |
func (c *MockConn) SetWriteDeadline(t time.Time) error { |
| 91 |
return nil |
| 92 |
} |
| 93 |
|
| 94 |
func (c *MockConn) Error(err error) *MockConn { |
| 95 |
c.err = err |
| 96 |
return c |
| 97 |
} |
| 98 |
|
| 99 |
// Define what each call to Read() will read |
| 100 |
func (c *MockConn) Reading(data ...[]byte) *MockConn { |
| 101 |
for _, d := range data { |
| 102 |
c.readings = append(c.readings, d) |
| 103 |
} |
| 104 |
return c |
| 105 |
} |
| 106 |
|
| 107 |
func (c *MockConn) Block() *MockConn { |
| 108 |
c.block = true |
| 109 |
return c |
| 110 |
} |
| 111 |
|
| 112 |
// Returns whatever data hasn't been read yet |
| 113 |
func (c *MockConn) Drain() []byte { |
| 114 |
left := 0 |
| 115 |
for i := c.readIndex; i < len(c.readings); i++ { |
| 116 |
left += len(c.readings[i]) |
| 117 |
} |
| 118 |
data := make([]byte, 0, left) |
| 119 |
for i := c.readIndex; i < len(c.readings); i++ { |
| 120 |
data = append(data, c.readings[i]...) |
| 121 |
} |
| 122 |
return data |
| 123 |
} |
| 124 |
|
| 125 |
type TimeoutError struct { |
| 126 |
error |
| 127 |
} |
| 128 |
|
| 129 |
func (t *TimeoutError) Timeout() bool { |
| 130 |
return true |
| 131 |
} |
| 132 |
|
| 133 |
func (t *TimeoutError) Temporary() bool { |
| 134 |
return true |
| 135 |
} |