File: conn.go

package info (click to toggle)
golang-github-karlseguin-expect 1.0.1%2Bgit20160716.12.5c2eadb-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 172 kB
  • sloc: makefile: 6
file content (135 lines) | stat: -rw-r--r-- 2,329 bytes parent folder | download | duplicates (2)
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package mock

import (
	"net"
	"time"
)

type MockConn struct {
	t         int
	err       error
	block     bool
	readIndex int
	first     bool
	readings  [][]byte
	Closed    bool
	Written   [][]byte
}

// Creates a new mock object which satisfies the net.Conn interface
func Conn() *MockConn {
	return &MockConn{
		first:    true,
		Written:  make([][]byte, 0, 4),
		readings: make([][]byte, 0, 4),
	}
}

func (c *MockConn) Read(out []byte) (int, error) {
	if c.first {
		c.first = false
		if c.err != nil {
			return 0, c.err
		}
	}

	if c.readIndex == len(c.readings) {
		if c.block == false {
			return 0, nil
		}
		time.Sleep(time.Minute)
	}
	bytes := c.readings[c.readIndex]
	end := len(bytes)
	if end > len(out) {
		end = len(out)
	}
	copy(out, bytes[:end])
	length := len(bytes[:end])
	c.readings[c.readIndex] = bytes[end:]
	if len(bytes[end:]) == 0 {
		c.readIndex++
	} else {
		c.readings[c.readIndex] = bytes[end:]
	}
	return length, nil
}

func (c *MockConn) Write(b []byte) (int, error) {
	if c.err != nil {
		return 0, c.err
	}
	c.Written = append(c.Written, b)
	return len(b), nil
}

func (c *MockConn) Close() error {
	c.Closed = true
	return nil
}

func (c *MockConn) LocalAddr() net.Addr {
	return nil
}

func (c *MockConn) RemoteAddr() net.Addr {
	return nil
}

func (c *MockConn) SetDeadline(t time.Time) error {
	c.SetReadDeadline(t)
	c.SetWriteDeadline(t)
	return nil
}

func (c *MockConn) SetReadDeadline(t time.Time) error {
	return nil
}

// noop
func (c *MockConn) SetWriteDeadline(t time.Time) error {
	return nil
}

func (c *MockConn) Error(err error) *MockConn {
	c.err = err
	return c
}

// Define what each call to Read() will read
func (c *MockConn) Reading(data ...[]byte) *MockConn {
	for _, d := range data {
		c.readings = append(c.readings, d)
	}
	return c
}

func (c *MockConn) Block() *MockConn {
	c.block = true
	return c
}

// Returns whatever data hasn't been read yet
func (c *MockConn) Drain() []byte {
	left := 0
	for i := c.readIndex; i < len(c.readings); i++ {
		left += len(c.readings[i])
	}
	data := make([]byte, 0, left)
	for i := c.readIndex; i < len(c.readings); i++ {
		data = append(data, c.readings[i]...)
	}
	return data
}

type TimeoutError struct {
	error
}

func (t *TimeoutError) Timeout() bool {
	return true
}

func (t *TimeoutError) Temporary() bool {
	return true
}