Skip to content

Commit

Permalink
Simplify the ServerStatus interface.
Browse files Browse the repository at this point in the history
Convert the Stopped and Closed methods into plain fields.
Update the tests and documentation.
This is a breaking change to the ServerStatus type.
  • Loading branch information
creachadair committed Aug 5, 2021
1 parent a4a4fd8 commit 86c5633
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
4 changes: 2 additions & 2 deletions jrpc2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
23 changes: 11 additions & 12 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down

0 comments on commit 86c5633

Please sign in to comment.