2011-08-21 12:38:12 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2021-03-26 12:36:19 +00:00
|
|
|
"context"
|
2011-11-13 13:34:32 +00:00
|
|
|
"io"
|
2011-08-21 12:38:12 +00:00
|
|
|
"net"
|
|
|
|
"os"
|
2011-08-24 11:45:32 +00:00
|
|
|
"strings"
|
2011-08-21 12:38:12 +00:00
|
|
|
"testing"
|
2011-08-23 09:42:54 +00:00
|
|
|
"time"
|
2011-08-21 12:38:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type mockNetConn struct {
|
|
|
|
*testing.T
|
|
|
|
|
|
|
|
In, Out chan string
|
|
|
|
in, out chan []byte
|
2021-03-26 12:36:19 +00:00
|
|
|
die context.CancelFunc
|
2011-08-21 12:38:12 +00:00
|
|
|
|
|
|
|
closed bool
|
2012-02-04 01:16:19 +00:00
|
|
|
rt, wt time.Time
|
2011-08-21 12:38:12 +00:00
|
|
|
}
|
|
|
|
|
2011-09-12 22:25:09 +00:00
|
|
|
func MockNetConn(t *testing.T) *mockNetConn {
|
2011-08-21 12:38:12 +00:00
|
|
|
// Our mock connection is a testing object
|
2021-03-26 12:36:19 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
m := &mockNetConn{T: t, die: cancel}
|
2011-08-21 12:38:12 +00:00
|
|
|
|
|
|
|
// buffer input
|
|
|
|
m.In = make(chan string, 20)
|
|
|
|
m.in = make(chan []byte)
|
|
|
|
go func() {
|
2011-08-23 09:42:54 +00:00
|
|
|
for {
|
|
|
|
select {
|
2021-03-26 12:36:19 +00:00
|
|
|
case <-ctx.Done():
|
2011-08-23 09:42:54 +00:00
|
|
|
return
|
|
|
|
case s := <-m.In:
|
|
|
|
m.in <- []byte(s)
|
|
|
|
}
|
2011-08-21 12:38:12 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// buffer output
|
|
|
|
m.Out = make(chan string)
|
|
|
|
m.out = make(chan []byte, 20)
|
|
|
|
go func() {
|
2011-08-23 09:42:54 +00:00
|
|
|
for {
|
|
|
|
select {
|
2021-03-26 12:36:19 +00:00
|
|
|
case <-ctx.Done():
|
2011-08-23 09:42:54 +00:00
|
|
|
return
|
|
|
|
case b := <-m.out:
|
|
|
|
m.Out <- string(b)
|
|
|
|
}
|
2011-08-21 12:38:12 +00:00
|
|
|
}
|
|
|
|
}()
|
2011-08-23 09:42:54 +00:00
|
|
|
|
2011-08-21 12:38:12 +00:00
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2011-08-22 22:22:46 +00:00
|
|
|
// Test helpers
|
|
|
|
func (m *mockNetConn) Send(s string) {
|
|
|
|
m.In <- s + "\r\n"
|
|
|
|
}
|
|
|
|
|
2011-08-21 12:38:12 +00:00
|
|
|
func (m *mockNetConn) Expect(e string) {
|
2011-08-23 09:42:54 +00:00
|
|
|
select {
|
2013-02-16 00:17:31 +00:00
|
|
|
case <-time.After(time.Millisecond):
|
2011-09-12 22:25:09 +00:00
|
|
|
m.Errorf("Mock connection did not receive expected output.\n\t"+
|
2011-08-23 09:42:54 +00:00
|
|
|
"Expected: '%s', got nothing.", e)
|
|
|
|
case s := <-m.Out:
|
2011-08-24 11:45:32 +00:00
|
|
|
s = strings.Trim(s, "\r\n")
|
|
|
|
if e != s {
|
2011-09-12 22:25:09 +00:00
|
|
|
m.Errorf("Mock connection received unexpected value.\n\t"+
|
2011-08-23 09:42:54 +00:00
|
|
|
"Expected: '%s'\n\tGot: '%s'", e, s)
|
|
|
|
}
|
2011-08-21 12:38:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-23 09:44:14 +00:00
|
|
|
func (m *mockNetConn) ExpectNothing() {
|
|
|
|
select {
|
2013-02-16 00:17:31 +00:00
|
|
|
case <-time.After(time.Millisecond):
|
2011-08-23 09:44:14 +00:00
|
|
|
case s := <-m.Out:
|
2011-08-24 11:45:32 +00:00
|
|
|
s = strings.Trim(s, "\r\n")
|
2011-09-12 22:25:09 +00:00
|
|
|
m.Errorf("Mock connection received unexpected output.\n\t"+
|
2011-08-23 09:44:14 +00:00
|
|
|
"Expected nothing, got: '%s'", s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-21 12:38:12 +00:00
|
|
|
// Implement net.Conn interface
|
2011-11-13 13:34:32 +00:00
|
|
|
func (m *mockNetConn) Read(b []byte) (int, error) {
|
2013-09-30 12:06:52 +00:00
|
|
|
if m.Closed() {
|
2012-05-14 21:51:01 +00:00
|
|
|
return 0, os.ErrInvalid
|
2011-08-22 22:22:46 +00:00
|
|
|
}
|
2021-03-26 12:36:19 +00:00
|
|
|
s, ok := <-m.in
|
|
|
|
copy(b, s)
|
|
|
|
if !ok {
|
|
|
|
return len(s), io.EOF
|
2011-08-23 09:42:54 +00:00
|
|
|
}
|
2021-03-26 12:36:19 +00:00
|
|
|
return len(s), nil
|
2011-08-21 12:38:12 +00:00
|
|
|
}
|
|
|
|
|
2011-11-13 13:34:32 +00:00
|
|
|
func (m *mockNetConn) Write(s []byte) (int, error) {
|
2013-09-30 12:06:52 +00:00
|
|
|
if m.Closed() {
|
2012-05-14 21:51:01 +00:00
|
|
|
return 0, os.ErrInvalid
|
2011-08-22 22:22:46 +00:00
|
|
|
}
|
2011-08-21 12:38:12 +00:00
|
|
|
b := make([]byte, len(s))
|
|
|
|
copy(b, s)
|
|
|
|
m.out <- b
|
2011-11-11 08:34:55 +00:00
|
|
|
return len(s), nil
|
2011-08-21 12:38:12 +00:00
|
|
|
}
|
|
|
|
|
2011-11-13 13:34:32 +00:00
|
|
|
func (m *mockNetConn) Close() error {
|
2013-09-30 12:06:52 +00:00
|
|
|
if m.Closed() {
|
2012-05-14 21:51:01 +00:00
|
|
|
return os.ErrInvalid
|
2011-08-23 09:42:54 +00:00
|
|
|
}
|
2021-03-26 12:36:19 +00:00
|
|
|
m.closed = true
|
2011-08-23 09:42:54 +00:00
|
|
|
// Shut down *ALL* the goroutines!
|
|
|
|
// This will trigger an EOF event in Read() too
|
2021-03-26 12:36:19 +00:00
|
|
|
m.die()
|
|
|
|
close(m.in)
|
2011-08-21 12:38:12 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-09-30 12:06:52 +00:00
|
|
|
func (m *mockNetConn) Closed() bool {
|
2021-03-26 12:36:19 +00:00
|
|
|
return m.closed
|
2013-09-30 12:06:52 +00:00
|
|
|
}
|
|
|
|
|
2011-08-21 12:38:12 +00:00
|
|
|
func (m *mockNetConn) LocalAddr() net.Addr {
|
2014-06-19 13:19:37 +00:00
|
|
|
return &net.IPAddr{IP: net.IPv4(127, 0, 0, 1)}
|
2011-08-21 12:38:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockNetConn) RemoteAddr() net.Addr {
|
2014-06-19 13:19:37 +00:00
|
|
|
return &net.IPAddr{IP: net.IPv4(127, 0, 0, 1)}
|
2011-08-21 12:38:12 +00:00
|
|
|
}
|
|
|
|
|
2012-02-04 01:16:19 +00:00
|
|
|
func (m *mockNetConn) SetDeadline(t time.Time) error {
|
|
|
|
m.rt = t
|
|
|
|
m.wt = t
|
2011-08-21 12:38:12 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2012-02-04 01:16:19 +00:00
|
|
|
func (m *mockNetConn) SetReadDeadline(t time.Time) error {
|
|
|
|
m.rt = t
|
2011-08-21 12:38:12 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2012-02-04 01:16:19 +00:00
|
|
|
func (m *mockNetConn) SetWriteDeadline(t time.Time) error {
|
|
|
|
m.wt = t
|
2011-08-21 12:38:12 +00:00
|
|
|
return nil
|
|
|
|
}
|