-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmock.go
46 lines (37 loc) · 870 Bytes
/
mock.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package go_sdk
import (
"fmt"
"github.com/official-stallion/go-sdk/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)
}