forked from mirror/redcon
added signal
This commit is contained in:
parent
e9d886853f
commit
f549dc92e8
12
redcon.go
12
redcon.go
|
@ -93,14 +93,26 @@ func (s *Server) Close() error {
|
|||
|
||||
// ListenAndServe serves incoming connections.
|
||||
func (s *Server) ListenAndServe() error {
|
||||
return s.ListenServeAndSignal(nil)
|
||||
}
|
||||
|
||||
// ListenServeAndSignal serves incoming connections and passes nil or error
|
||||
// when listening. signal can be nil.
|
||||
func (s *Server) ListenServeAndSignal(signal chan error) error {
|
||||
var addr = s.addr
|
||||
var handler = s.handler
|
||||
var accept = s.accept
|
||||
var closed = s.closed
|
||||
ln, err := net.Listen("tcp", addr)
|
||||
if err != nil {
|
||||
if signal != nil {
|
||||
signal <- err
|
||||
}
|
||||
return err
|
||||
}
|
||||
if signal != nil {
|
||||
signal <- nil
|
||||
}
|
||||
s.ln = ln.(*net.TCPListener)
|
||||
defer func() {
|
||||
ln.Close()
|
||||
|
|
|
@ -292,7 +292,14 @@ func TestServer(t *testing.T) {
|
|||
t.Fatal("expecting array, got '%v'", res)
|
||||
}
|
||||
}()
|
||||
err := s.ListenAndServe()
|
||||
signal := make(chan error)
|
||||
go func() {
|
||||
err := s.ListenServeAndSignal(signal)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}()
|
||||
err := <-signal
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue