Skip to content

Commit

Permalink
nats: refactor and skip tests that require actual server connection
Browse files Browse the repository at this point in the history
Signed-off-by: deadprogram <ron@hybridgroup.com>
  • Loading branch information
deadprogram committed Dec 2, 2016
1 parent da4a195 commit 5cbd352
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 30 deletions.
6 changes: 0 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@ before_install:
- sudo add-apt-repository -y ppa:zoogie/sdl2-snapshots
- sudo apt-get update
- sudo apt-get install --force-yes libcv-dev libcvaux-dev libhighgui-dev libopencv-dev libsdl2-dev libsdl2-image-dev libsdl2 libusb-dev xvfb unzip libgtk2.0-0
- mkdir -p gnatsd
- cd gnatsd
- "wget https://github.com/nats-io/gnatsd/releases/download/v0.9.4/gnatsd-v0.9.4-linux-amd64.zip"
- unzip -j gnatsd-v0.9.4-linux-amd64.zip
- ./gnatsd -p 4222 &
- ./gnatsd -p 4223 --user test --pass testwd &
- cd $HOME/gopath/src/github.com/hybridgroup/gobot
- go get github.com/axw/gocov/gocov
- go get github.com/mattn/goveralls
Expand Down
25 changes: 9 additions & 16 deletions platforms/nats/nats_adaptor.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package nats

import (
multierror "github.com/hashicorp/go-multierror"
"github.com/nats-io/nats"
)
import "github.com/nats-io/nats"

// Adaptor is a configuration struct for interacting with a NATS server.
// Name is a logical name for the adaptor/nats server connection.
Expand All @@ -16,6 +13,7 @@ type Adaptor struct {
username string
password string
client *nats.Conn
connect func() (*nats.Conn, error)
}

// NewAdaptor populates a new NATS Adaptor.
Expand All @@ -24,6 +22,9 @@ func NewAdaptor(host string, clientID int) *Adaptor {
name: "NATS",
Host: host,
clientID: clientID,
connect: func() (*nats.Conn, error) {
return nats.Connect("nats://" + host)
},
}
}

Expand All @@ -34,6 +35,9 @@ func NewAdaptorWithAuth(host string, clientID int, username string, password str
clientID: clientID,
username: username,
password: password,
connect: func() (*nats.Conn, error) {
return nats.Connect("nats://" + username + ":" + password + "@" + host)
},
}
}

Expand All @@ -45,18 +49,7 @@ func (a *Adaptor) SetName(n string) { a.name = n }

// Connect makes a connection to the Nats server.
func (a *Adaptor) Connect() (err error) {
auth := ""
if a.username != "" && a.password != "" {
auth = a.username + ":" + a.password + "@"
}

defaultURL := "nats://" + auth + a.Host

var e error
a.client, e = nats.Connect(defaultURL)
if e != nil {
err = multierror.Append(err, e)
}
a.client, err = a.connect()
return
}

Expand Down
31 changes: 23 additions & 8 deletions platforms/nats/nats_adaptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,53 +5,68 @@ import (
"fmt"
"testing"

multierror "github.com/hashicorp/go-multierror"
"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/gobottest"
"github.com/nats-io/nats"
)

var _ gobot.Adaptor = (*Adaptor)(nil)

func TestNatsAdaptorReturnsHost(t *testing.T) {
func initTestNatsAdaptor() *Adaptor {
a := NewAdaptor("localhost:4222", 9999)
a.connect = func() (*nats.Conn, error) {
c := &nats.Conn{}
return c, nil
}
return a
}

func TestNatsAdaptorReturnsHost(t *testing.T) {
a := initTestNatsAdaptor()
gobottest.Assert(t, a.Host, "localhost:4222")
}

// TODO: implement this test without requiring actual server connection
func TestNatsAdaptorPublishWhenConnected(t *testing.T) {
a := NewAdaptor("localhost:4222", 9999)
t.Skip("TODO: implement this test without requiring actual server connection")
a := initTestNatsAdaptor()
a.Connect()
data := []byte("o")
gobottest.Assert(t, a.Publish("test", data), true)
}

// TODO: implement this test without requiring actual server connection
func TestNatsAdaptorOnWhenConnected(t *testing.T) {
a := NewAdaptor("localhost:4222", 9999)
t.Skip("TODO: implement this test without requiring actual server connection")
a := initTestNatsAdaptor()
a.Connect()
gobottest.Assert(t, a.On("hola", func(data []byte) {
fmt.Println("hola")
}), true)
}

// TODO: implement this test without requiring actual server connection
func TestNatsAdaptorPublishWhenConnectedWithAuth(t *testing.T) {
t.Skip("TODO: implement this test without requiring actual server connection")
a := NewAdaptorWithAuth("localhost:4222", 9999, "test", "testwd")
a.Connect()
data := []byte("o")
gobottest.Assert(t, a.Publish("test", data), true)
}

// TODO: implement this test without requiring actual server connection
func TestNatsAdaptorOnWhenConnectedWithAuth(t *testing.T) {
t.Skip("TODO: implement this test without requiring actual server connection")
a := NewAdaptorWithAuth("localhost:4222", 9999, "test", "testwd")
a.Connect()
gobottest.Assert(t, a.On("hola", func(data []byte) {
fmt.Println("hola")
}), true)
}

func TestNatsAdaptorConnect(t *testing.T) {
func TestNatsAdaptorFailedConnect(t *testing.T) {
a := NewAdaptor("localhost:9999", 9999)
var expected error
expected = multierror.Append(expected, errors.New("nats: no servers available for connection"))
gobottest.Assert(t, a.Connect(), expected)
gobottest.Assert(t, a.Connect(), errors.New("nats: no servers available for connection"))
}

func TestNatsAdaptorFinalize(t *testing.T) {
Expand Down

0 comments on commit 5cbd352

Please sign in to comment.