core: Refactor NATS platform for new Adaptor creation signature

Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
deadprogram 2016-10-01 17:23:40 +02:00
parent 17cc57d9d1
commit 2c170cc052
2 changed files with 29 additions and 28 deletions

View File

@ -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
@ -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,
@ -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 != "" {
@ -59,7 +60,7 @@ 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()
}
@ -67,13 +68,13 @@ func (a *NatsAdaptor) Disconnect() (err error) {
}
// 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
}
@ -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
}

View File

@ -8,22 +8,22 @@ 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")
@ -31,14 +31,14 @@ func TestNatsAdaptorOnWhenConnected(t *testing.T) {
}
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")
@ -46,23 +46,23 @@ func TestNatsAdaptorOnWhenConnectedWithAuth(t *testing.T) {
}
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)