added signal

This commit is contained in:
Josh Baker 2016-08-22 10:53:04 -07:00
parent e9d886853f
commit f549dc92e8
2 changed files with 20 additions and 1 deletions

View File

@ -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()

View File

@ -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)
}