/
mock.go
180 lines (141 loc) · 2.87 KB
/
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// Package mock provides a mock broker for testing
package mock
import (
"errors"
"sync"
"github.com/google/uuid"
"github.com/micro/go-micro/broker"
)
type mockBroker struct {
opts broker.Options
sync.RWMutex
connected bool
Subscribers map[string][]*mockSubscriber
}
type mockPublication struct {
topic string
message *broker.Message
}
type mockSubscriber struct {
id string
topic string
exit chan bool
handler broker.Handler
opts broker.SubscribeOptions
}
func (m *mockBroker) Options() broker.Options {
return m.opts
}
func (m *mockBroker) Address() string {
return ""
}
func (m *mockBroker) Connect() error {
m.Lock()
defer m.Unlock()
if m.connected {
return nil
}
m.connected = true
return nil
}
func (m *mockBroker) Disconnect() error {
m.Lock()
defer m.Unlock()
if !m.connected {
return nil
}
m.connected = false
return nil
}
func (m *mockBroker) Init(opts ...broker.Option) error {
for _, o := range opts {
o(&m.opts)
}
return nil
}
func (m *mockBroker) Publish(topic string, message *broker.Message, opts ...broker.PublishOption) error {
m.Lock()
defer m.Unlock()
if !m.connected {
return errors.New("not connected")
}
subs, ok := m.Subscribers[topic]
if !ok {
return nil
}
p := &mockPublication{
topic: topic,
message: message,
}
for _, sub := range subs {
if err := sub.handler(p); err != nil {
return err
}
}
return nil
}
func (m *mockBroker) Subscribe(topic string, handler broker.Handler, opts ...broker.SubscribeOption) (broker.Subscriber, error) {
m.Lock()
defer m.Unlock()
if !m.connected {
return nil, errors.New("not connected")
}
var options broker.SubscribeOptions
for _, o := range opts {
o(&options)
}
sub := &mockSubscriber{
exit: make(chan bool, 1),
id: uuid.New().String(),
topic: topic,
handler: handler,
opts: options,
}
m.Subscribers[topic] = append(m.Subscribers[topic], sub)
go func() {
<-sub.exit
m.Lock()
var newSubscribers []*mockSubscriber
for _, sb := range m.Subscribers[topic] {
if sb.id == sub.id {
continue
}
newSubscribers = append(newSubscribers, sb)
}
m.Subscribers[topic] = newSubscribers
m.Unlock()
}()
return sub, nil
}
func (m *mockBroker) String() string {
return "mock"
}
func (m *mockPublication) Topic() string {
return m.topic
}
func (m *mockPublication) Message() *broker.Message {
return m.message
}
func (m *mockPublication) Ack() error {
return nil
}
func (m *mockSubscriber) Options() broker.SubscribeOptions {
return m.opts
}
func (m *mockSubscriber) Topic() string {
return m.topic
}
func (m *mockSubscriber) Unsubscribe() error {
m.exit <- true
return nil
}
func NewBroker(opts ...broker.Option) broker.Broker {
var options broker.Options
for _, o := range opts {
o(&options)
}
return &mockBroker{
opts: options,
Subscribers: make(map[string][]*mockSubscriber),
}
}