Skip to content

Commit

Permalink
update: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
amirhnajafiz committed Oct 12, 2022
1 parent bd3e759 commit 851db8f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
7 changes: 7 additions & 0 deletions test/mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
14 changes: 14 additions & 0 deletions test/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 851db8f

Please sign in to comment.