mirror of https://github.com/fluffle/goirc
Make mockNetConn more likely to expose racy conditions.
Use buffered channels so that Close() returns without waiting. Use an array of three channels rather than appending to a slice.
This commit is contained in:
parent
dc524420b0
commit
6a54622df9
|
@ -8,12 +8,18 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
mockReadCloser = iota
|
||||||
|
mockInCloser
|
||||||
|
mockOutCloser
|
||||||
|
)
|
||||||
|
|
||||||
type mockNetConn struct {
|
type mockNetConn struct {
|
||||||
*testing.T
|
*testing.T
|
||||||
|
|
||||||
In, Out chan string
|
In, Out chan string
|
||||||
in, out chan []byte
|
in, out chan []byte
|
||||||
closers []chan bool
|
closers [3]chan bool
|
||||||
rc chan bool
|
rc chan bool
|
||||||
|
|
||||||
closed bool
|
closed bool
|
||||||
|
@ -23,22 +29,15 @@ type mockNetConn struct {
|
||||||
func MockNetConn(t *testing.T) *mockNetConn {
|
func MockNetConn(t *testing.T) *mockNetConn {
|
||||||
// Our mock connection is a testing object
|
// Our mock connection is a testing object
|
||||||
m := &mockNetConn{T: t}
|
m := &mockNetConn{T: t}
|
||||||
m.closers = make([]chan bool, 0, 3)
|
|
||||||
|
|
||||||
// set known values for conn info
|
|
||||||
m.closed = false
|
|
||||||
m.rt = 0
|
|
||||||
m.wt = 0
|
|
||||||
|
|
||||||
// buffer input
|
// buffer input
|
||||||
m.In = make(chan string, 20)
|
m.In = make(chan string, 20)
|
||||||
m.in = make(chan []byte)
|
m.in = make(chan []byte)
|
||||||
ic := make(chan bool)
|
m.closers[mockInCloser] = make(chan bool, 1)
|
||||||
m.closers = append(m.closers, ic)
|
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-ic:
|
case <-m.closers[mockInCloser]:
|
||||||
return
|
return
|
||||||
case s := <-m.In:
|
case s := <-m.In:
|
||||||
m.in <- []byte(s)
|
m.in <- []byte(s)
|
||||||
|
@ -49,12 +48,11 @@ func MockNetConn(t *testing.T) *mockNetConn {
|
||||||
// buffer output
|
// buffer output
|
||||||
m.Out = make(chan string)
|
m.Out = make(chan string)
|
||||||
m.out = make(chan []byte, 20)
|
m.out = make(chan []byte, 20)
|
||||||
oc := make(chan bool)
|
m.closers[mockOutCloser] = make(chan bool, 1)
|
||||||
m.closers = append(m.closers, oc)
|
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-oc:
|
case <-m.closers[mockOutCloser]:
|
||||||
return
|
return
|
||||||
case b := <-m.out:
|
case b := <-m.out:
|
||||||
m.Out <- string(b)
|
m.Out <- string(b)
|
||||||
|
@ -63,8 +61,7 @@ func MockNetConn(t *testing.T) *mockNetConn {
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// Set up channel to force EOF to Read() on close.
|
// Set up channel to force EOF to Read() on close.
|
||||||
m.rc = make(chan bool)
|
m.closers[mockReadCloser] = make(chan bool, 1)
|
||||||
m.closers = append(m.closers, m.rc)
|
|
||||||
|
|
||||||
return m
|
return m
|
||||||
}
|
}
|
||||||
|
@ -108,7 +105,7 @@ func (m *mockNetConn) Read(b []byte) (int, os.Error) {
|
||||||
case s := <-m.in:
|
case s := <-m.in:
|
||||||
l = len(s)
|
l = len(s)
|
||||||
copy(b, s)
|
copy(b, s)
|
||||||
case <-m.rc:
|
case <-m.closers[mockReadCloser]:
|
||||||
return 0, os.EOF
|
return 0, os.EOF
|
||||||
}
|
}
|
||||||
return l, nil
|
return l, nil
|
||||||
|
|
Loading…
Reference in New Issue