X-Git-Url: http://www.git.cypherpunks.ru/?a=blobdiff_plain;f=common_test.go;h=c6a2f3b4f723ec78a0eba9e6299490a538339cae;hb=HEAD;hp=fdb362587304c0d35035a4e68980c1cf204a3aa2;hpb=7a081293793cbf329b964ce0de6338425609374f;p=goircd.git diff --git a/common_test.go b/common_test.go index fdb3625..c6a2f3b 100644 --- a/common_test.go +++ b/common_test.go @@ -1,39 +1,38 @@ -/* -goircd -- minimalistic simple Internet Relay Chat (IRC) server -Copyright (C) 2014-2019 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, 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 -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program. If not, see . -*/ +// goircd -- minimalistic simple Internet Relay Chat (IRC) server +// Copyright (C) 2014-2024 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, 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 +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// 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 { @@ -41,14 +40,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 +69,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 }