X-Git-Url: http://www.git.cypherpunks.ru/?p=goircd.git;a=blobdiff_plain;f=common_test.go;h=9750b86c36cf926af681e0fbe0b83017e00e6302;hp=36d70f2c1daafacb0d93480957d6fa84090ba919;hb=b7fb219307483d2c31b5dad1f559f325f2fd1a5e;hpb=ec62152429f64a625d58c0ee5e914d62061d062a diff --git a/common_test.go b/common_test.go index 36d70f2..9750b86 100644 --- a/common_test.go +++ b/common_test.go @@ -1,11 +1,10 @@ /* goircd -- minimalistic simple Internet Relay Chat (IRC) server -Copyright (C) 2014-2017 Sergey Matveev +Copyright (C) 2014-2020 Sergey Matveev This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. +the Free Software Foundation, version 3 of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of @@ -19,22 +18,23 @@ along with this program. If not, see . 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 { @@ -42,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{} @@ -67,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 }