core: update MQTT platform to simply return error

Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
deadprogram 2016-11-07 19:52:28 +01:00
parent 5dff2aa169
commit 2cf61532bf
2 changed files with 9 additions and 6 deletions

View File

@ -1,6 +1,9 @@
package mqtt package mqtt
import paho "github.com/eclipse/paho.mqtt.golang" import (
paho "github.com/eclipse/paho.mqtt.golang"
multierror "github.com/hashicorp/go-multierror"
)
type Adaptor struct { type Adaptor struct {
name string name string
@ -34,10 +37,10 @@ func (a *Adaptor) Name() string { return a.name }
func (a *Adaptor) SetName(n string) { a.name = n } func (a *Adaptor) SetName(n string) { a.name = n }
// Connect returns true if connection to mqtt is established // Connect returns true if connection to mqtt is established
func (a *Adaptor) Connect() (errs []error) { func (a *Adaptor) Connect() (err error) {
a.client = paho.NewClient(createClientOptions(a.clientID, a.Host, a.username, a.password)) a.client = paho.NewClient(createClientOptions(a.clientID, a.Host, a.username, a.password))
if token := a.client.Connect(); token.Wait() && token.Error() != nil { if token := a.client.Connect(); token.Wait() && token.Error() != nil {
errs = append(errs, token.Error()) err = multierror.Append(err, token.Error())
} }
return return
@ -52,7 +55,7 @@ func (a *Adaptor) Disconnect() (err error) {
} }
// Finalize returns true if connection to mqtt is finalized successfully // Finalize returns true if connection to mqtt is finalized successfully
func (a *Adaptor) Finalize() (errs []error) { func (a *Adaptor) Finalize() (err error) {
a.Disconnect() a.Disconnect()
return return
} }

View File

@ -16,12 +16,12 @@ func initTestMqttAdaptor() *Adaptor {
func TestMqttAdaptorConnect(t *testing.T) { func TestMqttAdaptorConnect(t *testing.T) {
a := initTestMqttAdaptor() a := initTestMqttAdaptor()
gobottest.Assert(t, a.Connect()[0].Error(), "Network Error : Unknown protocol") gobottest.Assert(t, a.Connect().Error(), "1 error(s) occurred:\n\n* Network Error : Unknown protocol")
} }
func TestMqttAdaptorFinalize(t *testing.T) { func TestMqttAdaptorFinalize(t *testing.T) {
a := initTestMqttAdaptor() a := initTestMqttAdaptor()
gobottest.Assert(t, len(a.Finalize()), 0) gobottest.Assert(t, a.Finalize(), nil)
} }
func TestMqttAdaptorCannotPublishUnlessConnected(t *testing.T) { func TestMqttAdaptorCannotPublishUnlessConnected(t *testing.T) {