2014-11-04 10:30:56 +08:00
|
|
|
package mqtt
|
|
|
|
|
|
|
|
import (
|
2014-11-05 00:33:05 +08:00
|
|
|
"fmt"
|
2014-11-04 10:56:33 +08:00
|
|
|
"testing"
|
2014-11-07 04:28:04 +08:00
|
|
|
|
2016-07-14 01:01:36 +08:00
|
|
|
"github.com/hybridgroup/gobot"
|
2016-02-22 13:21:24 +08:00
|
|
|
"github.com/hybridgroup/gobot/gobottest"
|
2014-11-04 10:30:56 +08:00
|
|
|
)
|
|
|
|
|
2016-07-14 01:01:36 +08:00
|
|
|
var _ gobot.Adaptor = (*MqttAdaptor)(nil)
|
|
|
|
|
2014-11-04 10:30:56 +08:00
|
|
|
func initTestMqttAdaptor() *MqttAdaptor {
|
2014-11-07 04:28:04 +08:00
|
|
|
return NewMqttAdaptor("mqtt", "localhost:1883", "client")
|
2014-11-04 10:30:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMqttAdaptorConnect(t *testing.T) {
|
2014-11-04 10:56:33 +08:00
|
|
|
a := initTestMqttAdaptor()
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, a.Connect()[0].Error(), "Network Error : Unknown protocol")
|
2014-11-04 10:30:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMqttAdaptorFinalize(t *testing.T) {
|
2014-11-04 10:56:33 +08:00
|
|
|
a := initTestMqttAdaptor()
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, len(a.Finalize()), 0)
|
2014-11-04 10:30:56 +08:00
|
|
|
}
|
2014-11-05 00:33:05 +08:00
|
|
|
|
|
|
|
func TestMqttAdaptorCannotPublishUnlessConnected(t *testing.T) {
|
|
|
|
a := initTestMqttAdaptor()
|
|
|
|
data := []byte("o")
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, a.Publish("test", data), false)
|
2014-11-05 00:33:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMqttAdaptorPublishWhenConnected(t *testing.T) {
|
|
|
|
a := initTestMqttAdaptor()
|
|
|
|
a.Connect()
|
|
|
|
data := []byte("o")
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, a.Publish("test", data), true)
|
2014-11-05 00:33:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMqttAdaptorCannotOnUnlessConnected(t *testing.T) {
|
|
|
|
a := initTestMqttAdaptor()
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, a.On("hola", func(data []byte) {
|
2014-11-05 00:33:05 +08:00
|
|
|
fmt.Println("hola")
|
|
|
|
}), false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMqttAdaptorOnWhenConnected(t *testing.T) {
|
|
|
|
a := initTestMqttAdaptor()
|
|
|
|
a.Connect()
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, a.On("hola", func(data []byte) {
|
2014-11-05 00:33:05 +08:00
|
|
|
fmt.Println("hola")
|
|
|
|
}), true)
|
|
|
|
}
|