Skip to content

Commit

Permalink
Move off of outbound errors for start/stop
Browse files Browse the repository at this point in the history
  • Loading branch information
willhug committed Nov 8, 2016
1 parent 2d7059c commit ab9c17f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
16 changes: 16 additions & 0 deletions internal/errors/peers.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,19 @@ type ErrInvalidPeerType struct {
func (e ErrInvalidPeerType) Error() string {
return fmt.Sprintf("expected peer type (%s) but got peer (%v)", e.ExpectedType, e.PeerIdentifier)
}

// ErrPeerListAlreadyStarted represents a failure because Start() was already
// called on the peerlist.
type ErrPeerListAlreadyStarted string

func (e ErrPeerListAlreadyStarted) Error() string {
return fmt.Sprintf("%s has already been started", string(e))
}

// ErrPeerListNotStarted represents a failure because Start() was not called
// on a peerlist or if Stop() was called.
type ErrPeerListNotStarted string

func (e ErrPeerListNotStarted) Error() string {
return fmt.Sprintf("%s has not been started or was stopped", string(e))
}
6 changes: 3 additions & 3 deletions transport/peer/peerlist/single.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func NewSingle(pi transport.PeerIdentifier, agent transport.PeerAgent) transport

func (pl *single) Start() error {
if pl.started.Swap(true) {
return errors.ErrOutboundAlreadyStarted("single")
return errors.ErrPeerListAlreadyStarted("single")
}
peer, err := pl.agent.RetainPeer(pl.peerID, pl)
if err != nil {
Expand All @@ -39,7 +39,7 @@ func (pl *single) Start() error {

func (pl *single) Stop() error {
if !pl.started.Swap(false) {
return errors.ErrOutboundNotStarted("single")
return errors.ErrPeerListNotStarted("single")
}
err := pl.agent.ReleasePeer(pl.peerID, pl)
if err != nil {
Expand All @@ -52,7 +52,7 @@ func (pl *single) Stop() error {

func (pl *single) ChoosePeer(context.Context, *transport.Request) (transport.Peer, error) {
if !pl.started.Load() {
return nil, errors.ErrOutboundNotStarted("peerlist was not started")
return nil, errors.ErrPeerListNotStarted("single")
}
return pl.peer, nil
}
Expand Down
6 changes: 3 additions & 3 deletions transport/peer/peerlist/single_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func TestSingle(t *testing.T) {
return pl.Stop()
}

s.expectedErr = errors.ErrOutboundNotStarted("single")
s.expectedErr = errors.ErrPeerListNotStarted("single")
s.expectedPeerID = s.pid
s.expectedAgent = s.agent
s.expectedStarted = false
Expand All @@ -78,7 +78,7 @@ func TestSingle(t *testing.T) {
s.expectedStarted = false
s.expectedChooseResults = []expectedChooseResult{{
peer: nil,
err: errors.ErrOutboundNotStarted("peerlist was not started"),
err: errors.ErrPeerListNotStarted("single"),
}}
return
}(),
Expand Down Expand Up @@ -133,7 +133,7 @@ func TestSingle(t *testing.T) {
return pl.Start()
}

s.expectedErr = errors.ErrOutboundAlreadyStarted("single")
s.expectedErr = errors.ErrPeerListAlreadyStarted("single")
s.expectedPeerID = s.pid
s.expectedAgent = s.agent
s.expectedStarted = true
Expand Down

0 comments on commit ab9c17f

Please sign in to comment.