Skip to content

Commit

Permalink
Add is connection open method to interface
Browse files Browse the repository at this point in the history
Signed-off-by: odedva <[email protected]>
  • Loading branch information
odedva committed May 18, 2018
1 parent 9ec68b7 commit 4ca4023
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
18 changes: 18 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ type Client interface {
// IsConnected returns a bool signifying whether
// the client is connected or not.
IsConnected() bool
// IsConnectionOpen return a bool signifying wether the client has an active
// connection to mqtt broker, i.e not in disconnected or reconnect mode
IsConnectionOpen() bool
// Connect will create a connection to the message broker, by default
// it will attempt to connect at v3.1.1 and auto retry at v3.1 if that
// fails
Expand Down Expand Up @@ -163,6 +166,21 @@ func (c *client) IsConnected() bool {
}
}

// IsConnectionOpen return a bool signifying whether the client has an active
// connection to mqtt broker, i.e not in disconnected or reconnect mode
func (c *client) IsConnectionOpen() bool {
c.RLock()
defer c.RUnlock()
status := atomic.LoadUint32(&c.status)
switch {
case status == connected:
return true
default:
return false
}
}


func (c *client) connectionStatus() uint32 {
c.RLock()
defer c.RUnlock()
Expand Down
28 changes: 28 additions & 0 deletions unit_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,31 @@ func Test_NewClient_optionsReader(t *testing.T) {
}

}

func Test_isConnection(t *testing.T) {
ops := NewClientOptions()
c := NewClient(ops)

c.(*client).setConnected(connected)
if !c.IsConnectionOpen() {
t.Fail()
}
}

func Test_isConnectionOpenNegative(t *testing.T) {
ops := NewClientOptions()
c := NewClient(ops)

c.(*client).setConnected(reconnecting)
if c.IsConnectionOpen() {
t.Fail()
}
c.(*client).setConnected(connecting)
if c.IsConnectionOpen() {
t.Fail()
}
c.(*client).setConnected(disconnected)
if c.IsConnectionOpen() {
t.Fail()
}
}

0 comments on commit 4ca4023

Please sign in to comment.