Skip to content

Commit

Permalink
core: Refactor NATS platform for new Adaptor creation signature
Browse files Browse the repository at this point in the history
Signed-off-by: deadprogram <[email protected]>
  • Loading branch information
deadprogram committed Oct 1, 2016
1 parent 17cc57d commit 2c170cc
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 28 deletions.
33 changes: 17 additions & 16 deletions platforms/nats/nats_adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import (
"github.com/nats-io/nats"
)

// NatsAdaptor is a configuration struct for interacting with a nats server.
// Adaptor is a configuration struct for interacting with a NATS server.
// Name is a logical name for the adaptor/nats server connection.
// Host is in the form "localhost:4222" which is the hostname/ip and port of the nats server.
// ClientID is a unique identifier integer that specifies the identity of the client.
type NatsAdaptor struct {
type Adaptor struct {
name string
Host string
clientID int
Expand All @@ -17,19 +17,17 @@ type NatsAdaptor struct {
client *nats.Conn
}

// NewNatsAdaptor populates a new NatsAdaptor.
func NewNatsAdaptor(name string, host string, clientID int) *NatsAdaptor {
return &NatsAdaptor{
name: name,
// NewAdaptor populates a new NATS Adaptor.
func NewAdaptor(host string, clientID int) *Adaptor {
return &Adaptor{
Host: host,
clientID: clientID,
}
}

// NewNatsAdaptorWithAuth populates a NatsAdaptor including username and password.
func NewNatsAdaptorWithAuth(name string, host string, clientID int, username string, password string) *NatsAdaptor {
return &NatsAdaptor{
name: name,
// NewAdaptorWithAuth populates a NATS Adaptor including username and password.
func NewAdaptorWithAuth(host string, clientID int, username string, password string) *Adaptor {
return &Adaptor{
Host: host,
clientID: clientID,
username: username,
Expand All @@ -38,10 +36,13 @@ func NewNatsAdaptorWithAuth(name string, host string, clientID int, username str
}

// Name returns the logical client name.
func (a *NatsAdaptor) Name() string { return a.name }
func (a *Adaptor) Name() string { return a.name }

// SetName sets the logical client name.
func (a *Adaptor) SetName(n string) { a.name = n }

// Connect makes a connection to the Nats server.
func (a *NatsAdaptor) Connect() (errs []error) {
func (a *Adaptor) Connect() (errs []error) {

auth := ""
if a.username != "" && a.password != "" {
Expand All @@ -59,21 +60,21 @@ func (a *NatsAdaptor) Connect() (errs []error) {
}

// Disconnect from the nats server. Returns an error if the client doesn't exist.
func (a *NatsAdaptor) Disconnect() (err error) {
func (a *Adaptor) Disconnect() (err error) {
if a.client != nil {
a.client.Close()
}
return
}

// Finalize is simply a helper method for the disconnect.
func (a *NatsAdaptor) Finalize() (errs []error) {
func (a *Adaptor) Finalize() (errs []error) {
a.Disconnect()
return
}

// Publish sends a message with the particular topic to the nats server.
func (a *NatsAdaptor) Publish(topic string, message []byte) bool {
func (a *Adaptor) Publish(topic string, message []byte) bool {
if a.client == nil {
return false
}
Expand All @@ -83,7 +84,7 @@ func (a *NatsAdaptor) Publish(topic string, message []byte) bool {

// On is an event-handler style subscriber to a particular topic (named event).
// Supply a handler function to use the bytes returned by the server.
func (a *NatsAdaptor) On(event string, f func(s []byte)) bool {
func (a *Adaptor) On(event string, f func(s []byte)) bool {
if a.client == nil {
return false
}
Expand Down
24 changes: 12 additions & 12 deletions platforms/nats/nats_adaptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,61 +8,61 @@ import (
"github.com/hybridgroup/gobot/gobottest"
)

var _ gobot.Adaptor = (*NatsAdaptor)(nil)
var _ gobot.Adaptor = (*Adaptor)(nil)

func TestNatsAdaptorReturnsName(t *testing.T) {
a := NewNatsAdaptor("Nats", "localhost:4222", 9999)
gobottest.Assert(t, a.Name(), "Nats")
func TestNatsAdaptorReturnsHost(t *testing.T) {
a := NewAdaptor("localhost:4222", 9999)
gobottest.Assert(t, a.Host, "localhost:4222")
}

func TestNatsAdaptorPublishWhenConnected(t *testing.T) {
a := NewNatsAdaptor("Nats", "localhost:4222", 9999)
a := NewAdaptor("localhost:4222", 9999)
a.Connect()
data := []byte("o")
gobottest.Assert(t, a.Publish("test", data), true)
}

func TestNatsAdaptorOnWhenConnected(t *testing.T) {
a := NewNatsAdaptor("Nats", "localhost:4222", 9999)
a := NewAdaptor("localhost:4222", 9999)
a.Connect()
gobottest.Assert(t, a.On("hola", func(data []byte) {
fmt.Println("hola")
}), true)
}

func TestNatsAdaptorPublishWhenConnectedWithAuth(t *testing.T) {
a := NewNatsAdaptorWithAuth("Nats", "localhost:4222", 9999, "test", "testwd")
a := NewAdaptorWithAuth("localhost:4222", 9999, "test", "testwd")
a.Connect()
data := []byte("o")
gobottest.Assert(t, a.Publish("test", data), true)
}

func TestNatsAdaptorOnWhenConnectedWithAuth(t *testing.T) {
a := NewNatsAdaptorWithAuth("Nats", "localhost:4222", 9999, "test", "testwd")
a := NewAdaptorWithAuth("localhost:4222", 9999, "test", "testwd")
a.Connect()
gobottest.Assert(t, a.On("hola", func(data []byte) {
fmt.Println("hola")
}), true)
}

func TestNatsAdaptorConnect(t *testing.T) {
a := NewNatsAdaptor("Nats", "localhost:9999", 9999)
a := NewAdaptor("localhost:9999", 9999)
gobottest.Assert(t, a.Connect()[0].Error(), "nats: no servers available for connection")
}

func TestNatsAdaptorFinalize(t *testing.T) {
a := NewNatsAdaptor("Nats", "localhost:9999", 9999)
a := NewAdaptor("localhost:9999", 9999)
gobottest.Assert(t, len(a.Finalize()), 0)
}

func TestNatsAdaptorCannotPublishUnlessConnected(t *testing.T) {
a := NewNatsAdaptor("Nats", "localhost:9999", 9999)
a := NewAdaptor("localhost:9999", 9999)
data := []byte("o")
gobottest.Assert(t, a.Publish("test", data), false)
}

func TestNatsAdaptorCannotOnUnlessConnected(t *testing.T) {
a := NewNatsAdaptor("Nats", "localhost:9999", 9999)
a := NewAdaptor("localhost:9999", 9999)
gobottest.Assert(t, a.On("hola", func(data []byte) {
fmt.Println("hola")
}), false)
Expand Down

0 comments on commit 2c170cc

Please sign in to comment.