2014-11-03 18:30:56 -08:00
|
|
|
package mqtt
|
|
|
|
|
|
|
|
import (
|
2014-11-04 08:33:05 -08:00
|
|
|
"fmt"
|
2014-11-03 18:56:33 -08:00
|
|
|
"testing"
|
2014-11-06 12:28:04 -08:00
|
|
|
|
|
|
|
"github.com/hybridgroup/gobot"
|
2014-11-03 18:30:56 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
func initTestMqttAdaptor() *MqttAdaptor {
|
2014-11-06 12:28:04 -08:00
|
|
|
return NewMqttAdaptor("mqtt", "localhost:1883", "client")
|
2014-11-03 18:30:56 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMqttAdaptorConnect(t *testing.T) {
|
2014-11-03 18:56:33 -08:00
|
|
|
a := initTestMqttAdaptor()
|
2015-04-06 15:35:11 -07:00
|
|
|
gobot.Assert(t, a.Connect()[0].Error(), "Network Error : Unknown protocol")
|
2014-11-03 18:30:56 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMqttAdaptorFinalize(t *testing.T) {
|
2014-11-03 18:56:33 -08:00
|
|
|
a := initTestMqttAdaptor()
|
2014-11-19 23:21:19 -08:00
|
|
|
gobot.Assert(t, len(a.Finalize()), 0)
|
2014-11-03 18:30:56 -08:00
|
|
|
}
|
2014-11-04 08:33:05 -08:00
|
|
|
|
|
|
|
func TestMqttAdaptorCannotPublishUnlessConnected(t *testing.T) {
|
|
|
|
a := initTestMqttAdaptor()
|
|
|
|
data := []byte("o")
|
|
|
|
gobot.Assert(t, a.Publish("test", data), false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMqttAdaptorPublishWhenConnected(t *testing.T) {
|
|
|
|
a := initTestMqttAdaptor()
|
|
|
|
a.Connect()
|
|
|
|
data := []byte("o")
|
|
|
|
gobot.Assert(t, a.Publish("test", data), true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMqttAdaptorCannotOnUnlessConnected(t *testing.T) {
|
|
|
|
a := initTestMqttAdaptor()
|
2014-11-06 12:28:04 -08:00
|
|
|
gobot.Assert(t, a.On("hola", func(data []byte) {
|
2014-11-04 08:33:05 -08:00
|
|
|
fmt.Println("hola")
|
|
|
|
}), false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMqttAdaptorOnWhenConnected(t *testing.T) {
|
|
|
|
a := initTestMqttAdaptor()
|
|
|
|
a.Connect()
|
2014-11-06 12:28:04 -08:00
|
|
|
gobot.Assert(t, a.On("hola", func(data []byte) {
|
2014-11-04 08:33:05 -08:00
|
|
|
fmt.Println("hola")
|
|
|
|
}), true)
|
|
|
|
}
|