From 851db8f563995ab558a46251767e96cd1c78094f Mon Sep 17 00:00:00 2001 From: amirhnajafiz Date: Wed, 12 Oct 2022 13:22:16 +0330 Subject: [PATCH] update: tests --- test/mock_test.go | 7 +++++++ test/server_test.go | 14 ++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/test/mock_test.go b/test/mock_test.go index 98707e0..b8e52cd 100644 --- a/test/mock_test.go +++ b/test/mock_test.go @@ -6,23 +6,30 @@ import ( "github.com/official-stallion/stallion" ) +// TestMock +// testing stallion mock client. func TestMock(t *testing.T) { + // creating a mock client mock := stallion.NewMockClient() if mock == nil { t.Error("failed to create mock client") } + // first we subscribe over a topic mock.Subscribe("topic", func(bytes []byte) { t.Log("successful subscribe") }) + // we should be able to publish over that topic err := mock.Publish("topic", []byte("message")) if err != nil { t.Error(err) } + // now we test unsubscribing mock.Unsubscribe("topic") + // we should get an error when we publish over this topic again err = mock.Publish("topic", []byte("message")) if err == nil { t.Error("failed to unsubscribe") diff --git a/test/server_test.go b/test/server_test.go index e86cec6..3207aa4 100644 --- a/test/server_test.go +++ b/test/server_test.go @@ -6,54 +6,68 @@ import ( "github.com/official-stallion/stallion" ) +// TestServer +// testing stallion server. func TestServer(t *testing.T) { + // creating a server on port 6000 go func() { if err := stallion.NewServer(":6000"); err != nil { t.Errorf("server failed to start: %v", err) } }() + // client does not give a valid url, so we should get error c, err := stallion.NewClient("localhost:6000") if err == nil { t.Error(err) } + // client should connect c, err = stallion.NewClient("st://localhost:6000") if err != nil { t.Error(err) } + // subscribe over a topic c.Subscribe("topic", func(bytes []byte) { t.Log("success subscribe") }) + // we should be able to subscribe err = c.Publish("topic", []byte("message")) if err != nil { t.Error(err) } } +// TestAuthServer +// testing stallion server with auth. func TestAuthServer(t *testing.T) { + // creating a stallion server on port 6001 with user and pass go func() { if err := stallion.NewServer(":6001", "root", "password"); err != nil { t.Errorf("server failed to start: %v", err) } }() + // client is not authorized we shoud get error c, err := stallion.NewClient("st://r:pass@localhost:6001") if err == nil { t.Error(err) } + // client should connect c, err = stallion.NewClient("st://root:password@localhost:6001") if err != nil { t.Error(err) } + // subscribe over a topic c.Subscribe("topic", func(bytes []byte) { t.Log("success subscribe") }) + // we should be able to subscribe err = c.Publish("topic", []byte("message")) if err != nil { t.Error(err)