Skip to content

Commit

Permalink
Merge pull request #18 from official-stallion/8-mock
Browse files Browse the repository at this point in the history
8 mock
  • Loading branch information
amirhnajafiz authored Oct 12, 2022
2 parents ee516d2 + f4671e8 commit 6ab3bf3
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
16 changes: 15 additions & 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.0-red?style=for-the-badge&logo=github" alt="version" /><br />
<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/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 All @@ -22,6 +22,7 @@ Using no external libraries, just internal Golang libraries.
- [Publish](#publish-over-a-topic)
- [Unsubscribe](#unsubscribe-from-a-topic)
- [Auth](#creating-a-server-with-auth)
- [Mock](#mock)

## How to use?
Get package:
Expand Down Expand Up @@ -108,3 +109,16 @@ if err != nil {
}
```

## Mock
You can create a mock client to create a stallion client sample:
```go
package main

import "github.com/official-stallion/stallion"

func main() {
client := stallion.NewMockClient()

client.Publish("topic", []byte("message"))
}
```
46 changes: 46 additions & 0 deletions mock.go
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)
}

0 comments on commit 6ab3bf3

Please sign in to comment.