-
Notifications
You must be signed in to change notification settings - Fork 85
/
common_test.go
65 lines (57 loc) · 1.07 KB
/
common_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
package beanstalk
import (
"fmt"
"io"
"strings"
)
type mockError struct {
exp []byte
got []byte
}
func (e mockError) Error() string {
return fmt.Sprintf(
"mock error: exp %#v, got %#v",
string(e.exp),
string(e.got),
)
}
type mockIO struct {
recv *strings.Reader
send *strings.Reader
}
func mock(recv, send string) io.ReadWriteCloser {
return &mockIO{strings.NewReader(recv), strings.NewReader(send)}
}
func (m mockIO) Read(b []byte) (int, error) {
return m.send.Read(b)
}
func (m mockIO) Write(got []byte) (n int, err error) {
exp := make([]byte, len(got))
n, err = m.recv.Read(exp)
if err != nil {
return n, err
}
exp = exp[:n]
for i := range exp {
if exp[i] != got[i] {
return i, mockError{exp, got}
}
}
if n != len(got) {
return n, mockError{exp, got}
}
return n, err
}
func (m mockIO) Close() error {
if m.recv.Len() == 0 && m.send.Len() == 0 {
return nil
}
if m.recv.Len() > 0 {
b := make([]byte, m.recv.Len())
m.recv.Read(b)
return mockError{b, nil}
}
b := make([]byte, m.send.Len())
m.send.Read(b)
return mockError{b, nil}
}