2011-08-21 12:38:12 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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
|
2011-08-23 09:42:54 +00:00
|
|
|
closers []chan bool
|
|
|
|
rc chan bool
|
2011-08-21 12:38:12 +00:00
|
|
|
|
|
|
|
closed bool
|
|
|
|
rt, wt int64
|
|
|
|
}
|
|
|
|
|
|
|
|
func MockNetConn(t *testing.T) (*mockNetConn) {
|
|
|
|
// Our mock connection is a testing object
|
|
|
|
m := &mockNetConn{T: t}
|
2011-08-23 09:42:54 +00:00
|
|
|
m.closers = make([]chan bool, 0, 3)
|
2011-08-21 12:38:12 +00:00
|
|
|
|
|
|
|
// set known values for conn info
|
|
|
|
m.closed = false
|
|
|
|
m.rt = 0
|
|
|
|
m.wt = 0
|
|
|
|
|
|
|
|
// buffer input
|
|
|
|
m.In = make(chan string, 20)
|
|
|
|
m.in = make(chan []byte)
|
2011-08-23 09:42:54 +00:00
|
|
|
ic := make(chan bool)
|
|
|
|
m.closers = append(m.closers, ic)
|
2011-08-21 12:38:12 +00:00
|
|
|
go func() {
|
2011-08-23 09:42:54 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ic:
|
|
|
|
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)
|
2011-08-23 09:42:54 +00:00
|
|
|
oc := make(chan bool)
|
|
|
|
m.closers = append(m.closers, oc)
|
2011-08-21 12:38:12 +00:00
|
|
|
go func() {
|
2011-08-23 09:42:54 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-oc:
|
|
|
|
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
|
|
|
|
|
|
|
// Set up channel to force EOF to Read() on close.
|
|
|
|
m.rc = make(chan bool)
|
|
|
|
m.closers = append(m.closers, m.rc)
|
|
|
|
|
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
|
|
|
t := time.NewTimer(5e6)
|
|
|
|
select {
|
|
|
|
case <-t.C:
|
|
|
|
m.Errorf("Mock connection did not receive expected output.\n\t" +
|
|
|
|
"Expected: '%s', got nothing.", e)
|
|
|
|
case s := <-m.Out:
|
|
|
|
t.Stop()
|
2011-08-24 11:45:32 +00:00
|
|
|
s = strings.Trim(s, "\r\n")
|
|
|
|
if e != s {
|
2011-08-23 09:42:54 +00:00
|
|
|
m.Errorf("Mock connection received unexpected value.\n\t" +
|
|
|
|
"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() {
|
|
|
|
t := time.NewTimer(5e6)
|
|
|
|
select {
|
|
|
|
case <-t.C:
|
|
|
|
case s := <-m.Out:
|
|
|
|
t.Stop()
|
2011-08-24 11:45:32 +00:00
|
|
|
s = strings.Trim(s, "\r\n")
|
2011-08-23 09:44:14 +00:00
|
|
|
m.Errorf("Mock connection received unexpected output.\n\t" +
|
|
|
|
"Expected nothing, got: '%s'", s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-21 12:38:12 +00:00
|
|
|
// Implement net.Conn interface
|
|
|
|
func (m *mockNetConn) Read(b []byte) (int, os.Error) {
|
2011-08-22 22:22:46 +00:00
|
|
|
if m.closed {
|
2011-08-23 09:43:48 +00:00
|
|
|
return 0, os.EINVAL
|
2011-08-22 22:22:46 +00:00
|
|
|
}
|
2011-08-23 09:42:54 +00:00
|
|
|
select {
|
|
|
|
case s := <-m.in:
|
|
|
|
copy(b, s)
|
|
|
|
case <-m.rc:
|
|
|
|
return 0, os.EOF
|
|
|
|
}
|
|
|
|
return len(b), nil
|
2011-08-21 12:38:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockNetConn) Write(s []byte) (int, os.Error) {
|
2011-08-22 22:22:46 +00:00
|
|
|
if m.closed {
|
2011-08-23 09:43:48 +00:00
|
|
|
return 0, os.EINVAL
|
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
|
|
|
|
return len(b), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockNetConn) Close() os.Error {
|
2011-08-23 09:42:54 +00:00
|
|
|
if m.closed {
|
|
|
|
return os.EINVAL
|
|
|
|
}
|
|
|
|
// Shut down *ALL* the goroutines!
|
|
|
|
// This will trigger an EOF event in Read() too
|
|
|
|
for _, c := range m.closers {
|
|
|
|
c <- true
|
|
|
|
}
|
2011-08-21 12:38:12 +00:00
|
|
|
m.closed = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockNetConn) LocalAddr() net.Addr {
|
|
|
|
return &net.IPAddr{net.IPv4(127,0,0,1)}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockNetConn) RemoteAddr() net.Addr {
|
|
|
|
return &net.IPAddr{net.IPv4(127,0,0,1)}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockNetConn) SetTimeout(ns int64) os.Error {
|
|
|
|
m.rt = ns
|
|
|
|
m.wt = ns
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockNetConn) SetReadTimeout(ns int64) os.Error {
|
|
|
|
m.rt = ns
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockNetConn) SetWriteTimeout(ns int64) os.Error {
|
|
|
|
m.wt = ns
|
|
|
|
return nil
|
|
|
|
}
|