Skip to content

Commit

Permalink
Merge pull request #19 from official-stallion/7-unit-testing
Browse files Browse the repository at this point in the history
7 unit testing
  • Loading branch information
amirhnajafiz authored Oct 12, 2022
2 parents 6ab3bf3 + 1eb90cd commit 81cce64
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<p align="center">
<img src="https://img.shields.io/badge/Golang-1.19-66ADD8?style=for-the-badge&logo=go" alt="go version" />
<img src="https://img.shields.io/badge/Version-1.2.1-red?style=for-the-badge&logo=github" alt="version" /><br />
<img src="https://img.shields.io/badge/Version-1.2.2-red?style=for-the-badge&logo=github" alt="version" /><br />
<img src="https://img.shields.io/badge/MacOS-black?style=for-the-badge&logo=apple" alt="version" />
<img src="https://img.shields.io/badge/Linux-white?style=for-the-badge&logo=linux" alt="version" />
<img src="https://img.shields.io/badge/Windows-blue?style=for-the-badge&logo=windows" alt="version" />
Expand Down
37 changes: 37 additions & 0 deletions test/mock_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package test

import (
"testing"

"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")
}
}
75 changes: 75 additions & 0 deletions test/server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package test

import (
"testing"

"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)
}
}

0 comments on commit 81cce64

Please sign in to comment.