/*
goircd -- minimalistic simple Internet Relay Chat (IRC) server
-Copyright (C) 2014-2016 Sergey Matveev <stargrave@stargrave.org>
+Copyright (C) 2014-2020 Sergey Matveev <stargrave@stargrave.org>
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
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 {
}
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{}
}
func (conn *TestingConn) Close() error {
- conn.closed = true
+ close(conn.closed)
close(conn.outbound)
return nil
}