Skip to content

Commit

Permalink
Merge pull request #812 from JosephWoodward/add-connection-status-str…
Browse files Browse the repository at this point in the history
…inger

[ADDED] Stringer for connection status
  • Loading branch information
kozlovic committed Sep 2, 2021
2 parents 51262a8 + c18a667 commit 2c959d6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
20 changes: 20 additions & 0 deletions nats.go
Expand Up @@ -196,6 +196,26 @@ const (
DRAINING_PUBS
)

func (s Status) String() string {
switch s {
case DISCONNECTED:
return "DISCONNECTED"
case CONNECTED:
return "CONNECTED"
case CLOSED:
return "CLOSED"
case RECONNECTING:
return "RECONNECTING"
case CONNECTING:
return "CONNECTING"
case DRAINING_SUBS:
return "DRAINING_SUBS"
case DRAINING_PUBS:
return "DRAINING_PUBS"
}
return "unknown status"
}

// ConnHandler is used for asynchronous events such as
// disconnected and closed connections.
type ConnHandler func(*Conn)
Expand Down
5 changes: 3 additions & 2 deletions test/conn_test.go
Expand Up @@ -49,14 +49,15 @@ func TestConnectionStatus(t *testing.T) {
nc := NewDefaultConnection(t)
defer nc.Close()

if nc.Status() != nats.CONNECTED {
if nc.Status() != nats.CONNECTED || nc.Status().String() != "CONNECTED" {
t.Fatal("Should have status set to CONNECTED")
}

if !nc.IsConnected() {
t.Fatal("Should have status set to CONNECTED")
}
nc.Close()
if nc.Status() != nats.CLOSED {
if nc.Status() != nats.CLOSED || nc.Status().String() != "CLOSED" {
t.Fatal("Should have status set to CLOSED")
}
if !nc.IsClosed() {
Expand Down

0 comments on commit 2c959d6

Please sign in to comment.