-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from official-stallion/7-unit-testing
7 unit testing
- Loading branch information
Showing
3 changed files
with
113 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |