From aad642b9cc3cb69534d90619017e69370a7513a2 Mon Sep 17 00:00:00 2001 From: amirhnajafiz Date: Wed, 12 Oct 2022 13:11:26 +0330 Subject: [PATCH 1/6] add: server test --- test/server_test.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 test/server_test.go diff --git a/test/server_test.go b/test/server_test.go new file mode 100644 index 0000000..4327853 --- /dev/null +++ b/test/server_test.go @@ -0,0 +1,29 @@ +package test + +import ( + "testing" + + "github.com/official-stallion/stallion" +) + +func TestServer(t *testing.T) { + go func() { + if err := stallion.NewServer(":6000"); err != nil { + t.Errorf("server failed to start: %w", err) + } + }() + + c, err := stallion.NewClient("localhost:6000") + if err != nil { + t.Error(err) + } + + c.Subscribe("topic", func(bytes []byte) { + t.Log("success subscribe") + }) + + err = c.Publish("topic", []byte("message")) + if err != nil { + t.Error(err) + } +} \ No newline at end of file From 6e7d968f43765427af37c50e7a0d5ca11ee64b86 Mon Sep 17 00:00:00 2001 From: amirhnajafiz Date: Wed, 12 Oct 2022 13:13:04 +0330 Subject: [PATCH 2/6] fix: server test --- test/server_test.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/test/server_test.go b/test/server_test.go index 4327853..8ef2421 100644 --- a/test/server_test.go +++ b/test/server_test.go @@ -9,11 +9,16 @@ import ( func TestServer(t *testing.T) { go func() { if err := stallion.NewServer(":6000"); err != nil { - t.Errorf("server failed to start: %w", err) + t.Errorf("server failed to start: %v", err) } }() c, err := stallion.NewClient("localhost:6000") + if err == nil { + t.Error(err) + } + + c, err = stallion.NewClient("st://localhost:6000") if err != nil { t.Error(err) } @@ -26,4 +31,4 @@ func TestServer(t *testing.T) { if err != nil { t.Error(err) } -} \ No newline at end of file +} From bbbe3f7bec2524a9896cdb0b85cbfa152537a2fd Mon Sep 17 00:00:00 2001 From: amirhnajafiz Date: Wed, 12 Oct 2022 13:14:47 +0330 Subject: [PATCH 3/6] add: auth server test --- test/server_test.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/test/server_test.go b/test/server_test.go index 8ef2421..e86cec6 100644 --- a/test/server_test.go +++ b/test/server_test.go @@ -32,3 +32,30 @@ func TestServer(t *testing.T) { t.Error(err) } } + +func TestAuthServer(t *testing.T) { + go func() { + if err := stallion.NewServer(":6001", "root", "password"); err != nil { + t.Errorf("server failed to start: %v", err) + } + }() + + c, err := stallion.NewClient("st://r:pass@localhost:6001") + if err == nil { + t.Error(err) + } + + c, err = stallion.NewClient("st://root:password@localhost:6001") + if err != nil { + t.Error(err) + } + + c.Subscribe("topic", func(bytes []byte) { + t.Log("success subscribe") + }) + + err = c.Publish("topic", []byte("message")) + if err != nil { + t.Error(err) + } +} From bd3e75937e13578efde6a637bced100a54149229 Mon Sep 17 00:00:00 2001 From: amirhnajafiz Date: Wed, 12 Oct 2022 13:17:46 +0330 Subject: [PATCH 4/6] add: mock test --- test/mock_test.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 test/mock_test.go diff --git a/test/mock_test.go b/test/mock_test.go new file mode 100644 index 0000000..98707e0 --- /dev/null +++ b/test/mock_test.go @@ -0,0 +1,30 @@ +package test + +import ( + "testing" + + "github.com/official-stallion/stallion" +) + +func TestMock(t *testing.T) { + mock := stallion.NewMockClient() + if mock == nil { + t.Error("failed to create mock client") + } + + mock.Subscribe("topic", func(bytes []byte) { + t.Log("successful subscribe") + }) + + err := mock.Publish("topic", []byte("message")) + if err != nil { + t.Error(err) + } + + mock.Unsubscribe("topic") + + err = mock.Publish("topic", []byte("message")) + if err == nil { + t.Error("failed to unsubscribe") + } +} From 851db8f563995ab558a46251767e96cd1c78094f Mon Sep 17 00:00:00 2001 From: amirhnajafiz Date: Wed, 12 Oct 2022 13:22:16 +0330 Subject: [PATCH 5/6] 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) From 1eb90cdd41125d363adb84bbde909851eb07239b Mon Sep 17 00:00:00 2001 From: amirhnajafiz Date: Wed, 12 Oct 2022 13:22:41 +0330 Subject: [PATCH 6/6] update: readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index da207bb..e931013 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@

go version -version
+version
version version version