]> Cypherpunks.ru repositories - goircd.git/blobdiff - common_test.go
Many fixes and additions
[goircd.git] / common_test.go
index 870852f6181d0a93d72f8bcfd732e11c99ea508d..9750b86c36cf926af681e0fbe0b83017e00e6302 100644 (file)
@@ -18,22 +18,23 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 package main
 
 import (
+       "io"
        "net"
        "time"
 )
 
-// Testing network connection that satisfies net.Conn interface
-// Can send predefined messages and store all written ones
 type TestingConn struct {
        inbound  chan string
        outbound chan string
-       closed   bool
+       closed   chan struct{}
 }
 
 func NewTestingConn() *TestingConn {
-       inbound := make(chan string, 8)
-       outbound := make(chan string, 8)
-       return &TestingConn{inbound: inbound, outbound: outbound}
+       return &TestingConn{
+               inbound:  make(chan string, 8),
+               outbound: make(chan string, 8),
+               closed:   make(chan struct{}),
+       }
 }
 
 func (conn TestingConn) Error() string {
@@ -41,14 +42,18 @@ func (conn TestingConn) Error() string {
 }
 
 func (conn *TestingConn) Read(b []byte) (n int, err error) {
-       msg := <-conn.inbound
-       if msg == "" {
-               return 0, conn
-       }
-       for n, bt := range append([]byte(msg), CRLF...) {
-               b[n] = bt
+       select {
+       case msg := <-conn.inbound:
+               if msg == "" {
+                       return 0, conn
+               }
+               for n, bt := range append([]byte(msg), CRLF...) {
+                       b[n] = bt
+               }
+               return len(msg) + 2, nil
+       case <-conn.closed:
+               return 0, io.EOF
        }
-       return len(msg) + 2, nil
 }
 
 type MyAddr struct{}
@@ -66,7 +71,7 @@ func (conn *TestingConn) Write(b []byte) (n int, err error) {
 }
 
 func (conn *TestingConn) Close() error {
-       conn.closed = true
+       close(conn.closed)
        close(conn.outbound)
        return nil
 }