From cca8b5624b77dca2043e2d30921b17cda318ae93 Mon Sep 17 00:00:00 2001 From: Cale Hoopes Date: Sun, 17 Jul 2016 14:39:34 -0700 Subject: [PATCH] Fixing tests, adding a few more, adding nats server to Travis CI for testing Signed-off-by: Cale Hoopes --- .travis.yml | 5 +++++ platforms/nats/nats_adaptor_test.go | 33 ++++++++++++++++++----------- 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/.travis.yml b/.travis.yml index dcee6d549..f1c7d8fbc 100644 --- a/.travis.yml +++ b/.travis.yml @@ -19,6 +19,11 @@ before_install: - sudo apt-get install --force-yes libcv-dev libcvaux-dev libhighgui-dev libopencv-dev libsdl2-dev libsdl2-image-dev libsdl2 libusb-dev xvfb libgtk2.0-0 - go get github.com/axw/gocov/gocov - go get github.com/mattn/goveralls + - mkdir -p gnatsd + - "wget https://github.com/nats-io/gnatsd/releases/download/v0.8.1/gnatsd-v0.8.1-linux-amd64.zip -qO - | tar -zxvC gnatsd/" + - export PATH=$PATH:$PWD/gnatsd/ + - gnatsd -p 4222 & + - gnatsd -p 4223 --user test --pass testwd & - if ! go get github.com/golang/tools/cmd/cover; then go get golang.org/x/tools/cmd/cover; fi install: - go get -d -v ./... diff --git a/platforms/nats/nats_adaptor_test.go b/platforms/nats/nats_adaptor_test.go index 44be78b3c..e7ea30db5 100644 --- a/platforms/nats/nats_adaptor_test.go +++ b/platforms/nats/nats_adaptor_test.go @@ -10,45 +10,54 @@ import ( var _ gobot.Adaptor = (*NatsAdaptor)(nil) -func initTestNatsAdaptor() *NatsAdaptor { - return NewNatsAdaptor("Nats", "localhost:4222", 9999) -} - -// These tests only succeed when there is a nats server available func TestNatsAdaptorPublishWhenConnected(t *testing.T) { - a := initTestNatsAdaptor() + a := NewNatsAdaptor("Nats", "localhost:4222", 9999) a.Connect() data := []byte("o") gobottest.Assert(t, a.Publish("test", data), true) } func TestNatsAdaptorOnWhenConnected(t *testing.T) { - a := initTestNatsAdaptor() + a := NewNatsAdaptor("Nats", "localhost:4222", 9999) + a.Connect() + gobottest.Assert(t, a.On("hola", func(data []byte) { + fmt.Println("hola") + }), true) +} + +func TestNatsAdaptorPublishWhenConnectedWithAuth(t *testing.T) { + a := NewNatsAdaptorWithAuth("Nats", "localhost:4222", 9999, "test", "testwd") + a.Connect() + data := []byte("o") + gobottest.Assert(t, a.Publish("test", data), true) +} + +func TestNatsAdaptorOnWhenConnectedWithAuth(t *testing.T) { + a := NewNatsAdaptorWithAuth("Nats", "localhost:4222", 9999, "test", "testwd") a.Connect() gobottest.Assert(t, a.On("hola", func(data []byte) { fmt.Println("hola") }), true) } -// These tests only succeed when there is no nats server available func TestNatsAdaptorConnect(t *testing.T) { - a := initTestNatsAdaptor() + a := NewNatsAdaptor("Nats", "localhost:9999", 9999) gobottest.Assert(t, a.Connect()[0].Error(), "nats: no servers available for connection") } func TestNatsAdaptorFinalize(t *testing.T) { - a := initTestNatsAdaptor() + a := NewNatsAdaptor("Nats", "localhost:9999", 9999) gobottest.Assert(t, len(a.Finalize()), 0) } func TestNatsAdaptorCannotPublishUnlessConnected(t *testing.T) { - a := initTestNatsAdaptor() + a := NewNatsAdaptor("Nats", "localhost:9999", 9999) data := []byte("o") gobottest.Assert(t, a.Publish("test", data), false) } func TestNatsAdaptorCannotOnUnlessConnected(t *testing.T) { - a := initTestNatsAdaptor() + a := NewNatsAdaptor("Nats", "localhost:9999", 9999) gobottest.Assert(t, a.On("hola", func(data []byte) { fmt.Println("hola") }), false)