-
Notifications
You must be signed in to change notification settings - Fork 9
/
simplehub_test.go
332 lines (279 loc) · 8.15 KB
/
simplehub_test.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
// Copyright 2016 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package pubsub_test
import (
"fmt"
"sync"
"time"
jc "github.com/juju/testing/checkers"
gc "gopkg.in/check.v1"
"github.com/juju/pubsub/v2"
)
type SimpleHubSuite struct{}
var _ = gc.Suite(&SimpleHubSuite{})
func waitForPublishToComplete(c *gc.C, done func()) {
waitForMessageHandlingToBeComplete(c, pubsub.Wait(done))
}
func waitForMessageHandlingToBeComplete(c *gc.C, done <-chan struct{}) {
select {
case <-done:
case <-time.After(time.Second):
// We expect message handling to be done in under 1ms
// so waiting for a second is 1000x as long.
c.Fatal("publish did not complete")
}
}
func (*SimpleHubSuite) TestPublishNoSubscribers(c *gc.C) {
hub := pubsub.NewSimpleHub(nil)
done := hub.Publish("topic", nil)
waitForPublishToComplete(c, done)
}
func (*SimpleHubSuite) TestPublishOneSubscriber(c *gc.C) {
var called bool
hub := pubsub.NewSimpleHub(nil)
hub.Subscribe("topic", func(topic string, data interface{}) {
c.Check(topic, gc.Equals, "topic")
c.Check(data, gc.IsNil)
called = true
})
done := hub.Publish("topic", nil)
waitForPublishToComplete(c, done)
c.Assert(called, jc.IsTrue)
}
func (*SimpleHubSuite) TestPublishCompleterWaits(c *gc.C) {
wait := make(chan struct{})
hub := pubsub.NewSimpleHub(nil)
hub.Subscribe("topic", func(topic string, data interface{}) {
<-wait
})
done := pubsub.Wait(hub.Publish("topic", nil))
select {
case <-done:
c.Fatal("didn't wait")
case <-time.After(time.Millisecond):
// Under normal circumstances, this would take ~3000ns
// See benchmark tests for speed.
}
close(wait)
waitForMessageHandlingToBeComplete(c, done)
}
func (*SimpleHubSuite) TestSubscriberExecsInOrder(c *gc.C) {
var calls []string
hub := pubsub.NewSimpleHub(nil)
hub.SubscribeMatch(pubsub.MatchRegexp("test.*"), func(topic string, data interface{}) {
calls = append(calls, topic)
})
var lastCall <-chan struct{}
for i := 0; i < 5; i++ {
lastCall = pubsub.Wait(hub.Publish(fmt.Sprintf("test.%v", i), nil))
}
waitForMessageHandlingToBeComplete(c, lastCall)
c.Assert(calls, jc.DeepEquals, []string{"test.0", "test.1", "test.2", "test.3", "test.4"})
}
func (*SimpleHubSuite) TestPublishNotBlockedByHandlerFunc(c *gc.C) {
wait := make(chan struct{})
hub := pubsub.NewSimpleHub(nil)
hub.Subscribe("topic", func(topic string, data interface{}) {
<-wait
})
var lastCall <-chan struct{}
for i := 0; i < 5; i++ {
lastCall = pubsub.Wait(hub.Publish("topic", nil))
}
// release the handlers
close(wait)
waitForMessageHandlingToBeComplete(c, lastCall)
}
func (*SimpleHubSuite) TestUnsubcribeWithPendingHandlersMarksDone(c *gc.C) {
wait := make(chan struct{})
var calls []string
hub := pubsub.NewSimpleHub(nil)
var unsubscribe func()
var err error
unsubscribe = hub.Subscribe("topic", func(topic string, data interface{}) {
<-wait
calls = append(calls, topic)
unsubscribe()
})
c.Assert(err, gc.IsNil)
var lastCall <-chan struct{}
for i := 0; i < 5; i++ {
lastCall = pubsub.Wait(hub.Publish("topic", nil))
}
// release the handlers
close(wait)
waitForMessageHandlingToBeComplete(c, lastCall)
// Only the first handler should execute as we unsubscribe in it.
c.Assert(calls, jc.DeepEquals, []string{"topic"})
}
func (*SimpleHubSuite) TestSubscribeMissingHandler(c *gc.C) {
hub := pubsub.NewSimpleHub(nil)
unsubscribe := hub.Subscribe("topic", nil)
c.Assert(unsubscribe, gc.NotNil)
// Calling it is fine.
unsubscribe()
}
func (*SimpleHubSuite) TestSubscribeMissingMatcher(c *gc.C) {
hub := pubsub.NewSimpleHub(nil)
unsubscribe := hub.SubscribeMatch(nil, func(string, interface{}) {})
c.Assert(unsubscribe, gc.NotNil)
// Calling it is fine.
unsubscribe()
}
func (*SimpleHubSuite) TestUnsubscribe(c *gc.C) {
var called bool
hub := pubsub.NewSimpleHub(nil)
unsub := hub.Subscribe("topic", func(topic string, data interface{}) {
called = true
})
unsub()
done := hub.Publish("topic", nil)
waitForPublishToComplete(c, done)
c.Assert(called, jc.IsFalse)
}
func (*SimpleHubSuite) TestSubscriberMultipleCallbacks(c *gc.C) {
firstCalled := false
secondCalled := false
thirdCalled := false
hub := pubsub.NewSimpleHub(nil)
hub.Subscribe("topic", func(string, interface{}) { firstCalled = true })
hub.Subscribe("topic", func(string, interface{}) { secondCalled = true })
hub.Subscribe("topic", func(string, interface{}) { thirdCalled = true })
done := hub.Publish("topic", nil)
waitForPublishToComplete(c, done)
c.Check(firstCalled, jc.IsTrue)
c.Check(secondCalled, jc.IsTrue)
c.Check(thirdCalled, jc.IsTrue)
}
func (*SimpleHubSuite) TestMetricsPublished(c *gc.C) {
metrics := newTestMetrics()
hub := pubsub.NewSimpleHub(&pubsub.SimpleHubConfig{
Metrics: metrics,
})
done := hub.Publish("topic", nil)
waitForPublishToComplete(c, done)
published, queue, consumed := metrics.Values()
c.Assert(published, gc.DeepEquals, map[string]int{
"topic": 1,
})
c.Assert(queue, gc.DeepEquals, map[string]int{})
c.Assert(consumed, gc.DeepEquals, map[string]time.Duration{})
}
func (*SimpleHubSuite) TestMetricsQueue(c *gc.C) {
metrics := newTestMetrics()
hub := pubsub.NewSimpleHub(&pubsub.SimpleHubConfig{
Metrics: metrics,
})
hub.Subscribe("topic", func(string, interface{}) {})
published, queue, consumed := metrics.Values()
c.Assert(published, gc.DeepEquals, map[string]int{})
c.Assert(queue, gc.DeepEquals, map[string]int{})
c.Assert(consumed, gc.DeepEquals, map[string]time.Duration{})
done := hub.Publish("topic", nil)
waitForPublishToComplete(c, done)
published, queue, consumed = metrics.Values()
c.Assert(published, gc.DeepEquals, map[string]int{
"topic": 1,
})
c.Assert(queue, gc.DeepEquals, map[string]int{
"TestMetricsQueue.func1": 0,
})
c.Assert(consumed, gc.HasLen, 1)
}
func (*SimpleHubSuite) TestMetricsDequeue(c *gc.C) {
metrics := newTestMetrics()
var (
first = true
waiting sync.WaitGroup
blocking sync.WaitGroup
)
waiting.Add(1)
blocking.Add(1)
hub := pubsub.NewSimpleHub(&pubsub.SimpleHubConfig{
Metrics: metrics,
})
hub.Subscribe("topic", func(string, interface{}) {
if first {
waiting.Done()
first = false
}
blocking.Wait()
})
published, queue, consumed := metrics.Values()
c.Assert(published, gc.DeepEquals, map[string]int{})
c.Assert(queue, gc.DeepEquals, map[string]int{})
c.Assert(consumed, gc.DeepEquals, map[string]time.Duration{})
hub.Publish("topic", nil)
done := hub.Publish("topic", nil)
waiting.Wait()
published, queue, consumed = metrics.Values()
c.Assert(published, gc.DeepEquals, map[string]int{
"topic": 2,
})
c.Assert(queue, gc.DeepEquals, map[string]int{
"TestMetricsDequeue.func1": 1,
})
c.Assert(consumed, gc.HasLen, 0)
blocking.Done()
waitForPublishToComplete(c, done)
published, queue, consumed = metrics.Values()
c.Assert(published, gc.DeepEquals, map[string]int{
"topic": 2,
})
c.Assert(queue, gc.DeepEquals, map[string]int{
"TestMetricsDequeue.func1": 0,
})
c.Assert(consumed, gc.HasLen, 1)
}
type testMetrics struct {
mutex sync.Mutex
published map[string]int
queue map[string]int
consumed map[string]time.Duration
}
func newTestMetrics() *testMetrics {
return &testMetrics{
published: make(map[string]int),
queue: make(map[string]int),
consumed: make(map[string]time.Duration),
}
}
func (m *testMetrics) Subscribed() {}
func (m *testMetrics) Unsubscribed() {}
func (m *testMetrics) Published(topic string) {
m.mutex.Lock()
m.published[topic]++
m.mutex.Unlock()
}
func (m *testMetrics) Enqueued(ident string) {
m.mutex.Lock()
m.queue[ident]++
m.mutex.Unlock()
}
func (m *testMetrics) Dequeued(ident string) {
m.mutex.Lock()
m.queue[ident]--
m.mutex.Unlock()
}
func (m *testMetrics) Consumed(ident string, duration time.Duration) {
m.mutex.Lock()
m.consumed[ident] = duration
m.mutex.Unlock()
}
func (m *testMetrics) Values() (map[string]int, map[string]int, map[string]time.Duration) {
m.mutex.Lock()
defer m.mutex.Unlock()
p := make(map[string]int, len(m.published))
for k, v := range m.published {
p[k] = v
}
q := make(map[string]int, len(m.queue))
for k, v := range m.queue {
q[k] = v
}
c := make(map[string]time.Duration, len(m.consumed))
for k, v := range m.consumed {
c[k] = v
}
return p, q, c
}