-
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 #18 from official-stallion/8-mock
8 mock
- Loading branch information
Showing
2 changed files
with
61 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,46 @@ | ||
package stallion | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/official-stallion/stallion/internal" | ||
) | ||
|
||
// mockClient | ||
// mocks stallion client. | ||
type mockClient struct { | ||
// list of the topics with their handlers | ||
topics map[string]internal.MessageHandler | ||
} | ||
|
||
// NewMockClient | ||
// creates a new mock client. | ||
func NewMockClient() Client { | ||
return &mockClient{ | ||
topics: make(map[string]internal.MessageHandler), | ||
} | ||
} | ||
|
||
// Publish | ||
// send messages over mock client. | ||
func (m *mockClient) Publish(topic string, data []byte) error { | ||
if handler, ok := m.topics[topic]; ok { | ||
handler(data) | ||
|
||
return nil | ||
} | ||
|
||
return fmt.Errorf("failed to publish") | ||
} | ||
|
||
// Subscribe | ||
// over a topic. | ||
func (m *mockClient) Subscribe(topic string, handler internal.MessageHandler) { | ||
m.topics[topic] = handler | ||
} | ||
|
||
// Unsubscribe | ||
// from a topic. | ||
func (m *mockClient) Unsubscribe(topic string) { | ||
delete(m.topics, topic) | ||
} |