2016-07-16 20:59:07 -07:00
|
|
|
package nats
|
|
|
|
|
|
|
|
import (
|
2016-11-07 19:55:43 +01:00
|
|
|
"errors"
|
2016-07-16 20:59:07 -07:00
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hybridgroup/gobot"
|
|
|
|
"github.com/hybridgroup/gobot/gobottest"
|
2016-12-02 09:56:37 +01:00
|
|
|
"github.com/nats-io/nats"
|
2016-07-16 20:59:07 -07:00
|
|
|
)
|
|
|
|
|
2016-10-01 17:23:40 +02:00
|
|
|
var _ gobot.Adaptor = (*Adaptor)(nil)
|
2016-07-16 20:59:07 -07:00
|
|
|
|
2016-12-02 09:56:37 +01:00
|
|
|
func initTestNatsAdaptor() *Adaptor {
|
2016-10-01 17:23:40 +02:00
|
|
|
a := NewAdaptor("localhost:4222", 9999)
|
2016-12-02 09:56:37 +01:00
|
|
|
a.connect = func() (*nats.Conn, error) {
|
|
|
|
c := &nats.Conn{}
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNatsAdaptorReturnsHost(t *testing.T) {
|
|
|
|
a := initTestNatsAdaptor()
|
2016-10-01 17:23:40 +02:00
|
|
|
gobottest.Assert(t, a.Host, "localhost:4222")
|
2016-07-17 15:53:12 -07:00
|
|
|
}
|
|
|
|
|
2016-12-02 09:56:37 +01:00
|
|
|
// TODO: implement this test without requiring actual server connection
|
2016-07-16 20:59:07 -07:00
|
|
|
func TestNatsAdaptorPublishWhenConnected(t *testing.T) {
|
2016-12-02 09:56:37 +01:00
|
|
|
t.Skip("TODO: implement this test without requiring actual server connection")
|
|
|
|
a := initTestNatsAdaptor()
|
2016-07-16 20:59:07 -07:00
|
|
|
a.Connect()
|
|
|
|
data := []byte("o")
|
|
|
|
gobottest.Assert(t, a.Publish("test", data), true)
|
|
|
|
}
|
|
|
|
|
2016-12-02 09:56:37 +01:00
|
|
|
// TODO: implement this test without requiring actual server connection
|
2016-07-16 20:59:07 -07:00
|
|
|
func TestNatsAdaptorOnWhenConnected(t *testing.T) {
|
2016-12-02 09:56:37 +01:00
|
|
|
t.Skip("TODO: implement this test without requiring actual server connection")
|
|
|
|
a := initTestNatsAdaptor()
|
2016-07-17 14:39:34 -07:00
|
|
|
a.Connect()
|
|
|
|
gobottest.Assert(t, a.On("hola", func(data []byte) {
|
|
|
|
fmt.Println("hola")
|
|
|
|
}), true)
|
|
|
|
}
|
|
|
|
|
2016-12-02 09:56:37 +01:00
|
|
|
// TODO: implement this test without requiring actual server connection
|
2016-07-17 14:39:34 -07:00
|
|
|
func TestNatsAdaptorPublishWhenConnectedWithAuth(t *testing.T) {
|
2016-12-02 09:56:37 +01:00
|
|
|
t.Skip("TODO: implement this test without requiring actual server connection")
|
2016-10-01 17:23:40 +02:00
|
|
|
a := NewAdaptorWithAuth("localhost:4222", 9999, "test", "testwd")
|
2016-07-17 14:39:34 -07:00
|
|
|
a.Connect()
|
|
|
|
data := []byte("o")
|
|
|
|
gobottest.Assert(t, a.Publish("test", data), true)
|
|
|
|
}
|
|
|
|
|
2016-12-02 09:56:37 +01:00
|
|
|
// TODO: implement this test without requiring actual server connection
|
2016-07-17 14:39:34 -07:00
|
|
|
func TestNatsAdaptorOnWhenConnectedWithAuth(t *testing.T) {
|
2016-12-02 09:56:37 +01:00
|
|
|
t.Skip("TODO: implement this test without requiring actual server connection")
|
2016-10-01 17:23:40 +02:00
|
|
|
a := NewAdaptorWithAuth("localhost:4222", 9999, "test", "testwd")
|
2016-07-16 20:59:07 -07:00
|
|
|
a.Connect()
|
|
|
|
gobottest.Assert(t, a.On("hola", func(data []byte) {
|
|
|
|
fmt.Println("hola")
|
|
|
|
}), true)
|
|
|
|
}
|
|
|
|
|
2016-12-02 09:56:37 +01:00
|
|
|
func TestNatsAdaptorFailedConnect(t *testing.T) {
|
2016-10-01 17:23:40 +02:00
|
|
|
a := NewAdaptor("localhost:9999", 9999)
|
2016-12-02 09:56:37 +01:00
|
|
|
gobottest.Assert(t, a.Connect(), errors.New("nats: no servers available for connection"))
|
2016-07-16 20:59:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNatsAdaptorFinalize(t *testing.T) {
|
2016-10-01 17:23:40 +02:00
|
|
|
a := NewAdaptor("localhost:9999", 9999)
|
2016-11-07 19:55:43 +01:00
|
|
|
gobottest.Assert(t, a.Finalize(), nil)
|
2016-07-16 20:59:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNatsAdaptorCannotPublishUnlessConnected(t *testing.T) {
|
2016-10-01 17:23:40 +02:00
|
|
|
a := NewAdaptor("localhost:9999", 9999)
|
2016-07-16 20:59:07 -07:00
|
|
|
data := []byte("o")
|
|
|
|
gobottest.Assert(t, a.Publish("test", data), false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNatsAdaptorCannotOnUnlessConnected(t *testing.T) {
|
2016-10-01 17:23:40 +02:00
|
|
|
a := NewAdaptor("localhost:9999", 9999)
|
2016-07-16 20:59:07 -07:00
|
|
|
gobottest.Assert(t, a.On("hola", func(data []byte) {
|
|
|
|
fmt.Println("hola")
|
|
|
|
}), false)
|
|
|
|
}
|