From 86c563324cefea2872cb0e94cc40fb25c590b7c3 Mon Sep 17 00:00:00 2001 From: "M. J. Fromberger" Date: Thu, 5 Aug 2021 11:10:24 -0700 Subject: [PATCH] Simplify the ServerStatus interface. Convert the Stopped and Closed methods into plain fields. Update the tests and documentation. This is a breaking change to the ServerStatus type. --- jrpc2_test.go | 4 ++-- server.go | 23 +++++++++++------------ 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/jrpc2_test.go b/jrpc2_test.go index 97ff5a7..e1e0cf7 100644 --- a/jrpc2_test.go +++ b/jrpc2_test.go @@ -1058,10 +1058,10 @@ func TestWaitStatus(t *testing.T) { if got, want := stat.Success(), wantErr == nil; got != want { t.Errorf("Status success: got %v, want %v", got, want) } - if got := stat.Closed(); got != closed { + if got := stat.Closed; got != closed { t.Errorf("Status closed: got %v, want %v", got, closed) } - if got := stat.Stopped(); got != stopped { + if got := stat.Stopped; got != stopped { t.Errorf("Status stopped: got %v, want %v", got, stopped) } if stat.Err != wantErr { diff --git a/server.go b/server.go index 0aea07e..9c9bdd3 100644 --- a/server.go +++ b/server.go @@ -474,18 +474,14 @@ func (s *Server) Stop() { type ServerStatus struct { Err error // the error that caused the server to stop (nil on success) - stopped bool // whether Stop was called + // On success, these flags explain the reason why the server stopped. + Stopped bool // server exited because Stop was called + Closed bool // server exited because the client channel closed } // Success reports whether the server exited without error. func (s ServerStatus) Success() bool { return s.Err == nil } -// Stopped reports whether the server exited due to Stop being called. -func (s ServerStatus) Stopped() bool { return s.Err == nil && s.stopped } - -// Closed reports whether the server exited due to a channel close. -func (s ServerStatus) Closed() bool { return s.Err == nil && !s.stopped } - // WaitStatus blocks until the server terminates, and returns the resulting // status. After WaitStatus returns, whether or not there was an error, it is // safe to call s.Start again to restart the server with a fresh channel. @@ -495,12 +491,15 @@ func (s *Server) WaitStatus() ServerStatus { if s.inq.Len() != 0 { panic("s.inq is not empty at shutdown") } - exitErr := s.err - // Don't remark on a closed channel or EOF as a noteworthy failure. - if s.err == io.EOF || channel.IsErrClosing(s.err) || s.err == errServerStopped { - exitErr = nil + stat := ServerStatus{Err: s.err} + if s.err == io.EOF || channel.IsErrClosing(s.err) { + stat.Err = nil + stat.Closed = true + } else if s.err == errServerStopped { + stat.Err = nil + stat.Stopped = true } - return ServerStatus{Err: exitErr, stopped: s.err == errServerStopped} + return stat } // Wait blocks until the server terminates and returns the resulting error.