-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmultiplexer_test.go
287 lines (258 loc) · 8.32 KB
/
multiplexer_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
// Copyright 2016 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package pubsub_test
import (
"time"
"github.com/juju/testing"
jc "github.com/juju/testing/checkers"
gc "gopkg.in/check.v1"
"github.com/juju/pubsub/v2"
)
type MultiplexerHubSuite struct {
testing.IsolationSuite
}
var _ = gc.Suite(&MultiplexerHubSuite{})
func (*MultiplexerHubSuite) TestNewMultiplexerStructuredHub(c *gc.C) {
hub := pubsub.NewStructuredHub(nil)
multi, err := hub.NewMultiplexer()
c.Assert(err, jc.ErrorIsNil)
defer multi.Unsubscribe()
c.Check(multi, gc.NotNil)
}
func (*MultiplexerHubSuite) TestMultiplexerAdd(c *gc.C) {
hub := pubsub.NewStructuredHub(nil)
multi, err := hub.NewMultiplexer()
c.Assert(err, jc.ErrorIsNil)
defer multi.Unsubscribe()
for i, test := range []struct {
description string
handler interface{}
err string
}{
{
description: "nil handler",
err: "nil handler not valid",
}, {
description: "string handler",
handler: "a string",
err: "handler of type string not valid",
}, {
description: "too few args",
handler: func(string) {},
err: "expected 2 or 3 args, got 1, incorrect handler signature not valid",
}, {
description: "too many args",
handler: func(string, string, string, string) {},
err: "expected 2 or 3 args, got 4, incorrect handler signature not valid",
}, {
description: "simple hub handler function",
handler: func(string, interface{}) {},
err: "second arg should be a structure or map\\[string\\]interface{} for data, incorrect handler signature not valid",
}, {
description: "bad return values in handler function",
handler: func(string, interface{}, error) error { return nil },
err: "expected no return values, got 1, incorrect handler signature not valid",
}, {
description: "bad first arg",
handler: func(int, map[string]interface{}, error) {},
err: "first arg should be a string, incorrect handler signature not valid",
}, {
description: "bad second arg",
handler: func(string, string, error) {},
err: "second arg should be a structure or map\\[string\\]interface{} for data, incorrect handler signature not valid",
}, {
description: "bad third arg",
handler: func(string, map[string]interface{}, error) {},
err: "data type of map\\[string\\]interface{} expects only 2 args, got 3, incorrect handler signature not valid",
}, {
description: "accept map[string]interface{}",
handler: func(string, map[string]interface{}) {},
}, {
description: "bad map[string]string",
handler: func(string, map[string]string, error) {},
err: "second arg should be a structure or map\\[string\\]interface{} for data, incorrect handler signature not valid",
}, {
description: "bad third arg",
handler: func(string, Emission, bool) {},
err: "third arg should be error for deserialization errors, incorrect handler signature not valid",
}, {
description: "accept struct value",
handler: func(string, Emission, error) {},
},
} {
c.Logf("test %d: %s", i, test.description)
err := multi.AddMatch(pubsub.MatchAll, test.handler)
if test.err == "" {
c.Check(err, jc.ErrorIsNil)
} else {
c.Check(err, gc.ErrorMatches, test.err)
}
}
}
func (*MultiplexerHubSuite) TestMatcher(c *gc.C) {
hub := pubsub.NewStructuredHub(nil)
multi, err := hub.NewMultiplexer()
c.Assert(err, jc.ErrorIsNil)
defer multi.Unsubscribe()
noopFunc := func(string, map[string]interface{}) {}
err = multi.Add(first, noopFunc)
c.Assert(err, jc.ErrorIsNil)
err = multi.AddMatch(pubsub.MatchRegexp("second.*"), noopFunc)
c.Assert(err, jc.ErrorIsNil)
c.Check(pubsub.MultiplexerMatch(multi, first), jc.IsTrue)
c.Check(pubsub.MultiplexerMatch(multi, firstdot), jc.IsFalse)
c.Check(pubsub.MultiplexerMatch(multi, second), jc.IsTrue)
c.Check(pubsub.MultiplexerMatch(multi, space), jc.IsFalse)
}
func (*MultiplexerHubSuite) TestCallback(c *gc.C) {
source := Emission{
Origin: "test",
Message: "hello world",
ID: 42,
}
var (
topic = "callback.topic"
originCalled bool
messageCalled bool
mapCalled bool
)
hub := pubsub.NewStructuredHub(nil)
multi, err := hub.NewMultiplexer()
c.Assert(err, jc.ErrorIsNil)
defer multi.Unsubscribe()
err = multi.Add(topic, func(top string, data JustOrigin, err error) {
c.Check(err, jc.ErrorIsNil)
c.Check(top, gc.Equals, topic)
c.Check(data.Origin, gc.Equals, source.Origin)
originCalled = true
})
c.Assert(err, jc.ErrorIsNil)
err = multi.Add(second, func(topic string, data MessageID, err error) {
c.Fail()
messageCalled = true
})
c.Assert(err, jc.ErrorIsNil)
err = multi.AddMatch(pubsub.MatchAll, func(top string, data map[string]interface{}) {
c.Check(top, gc.Equals, topic)
c.Check(data, jc.DeepEquals, map[string]interface{}{
"origin": "test",
"message": "hello world",
"id": float64(42), // ints are converted to floats through json.
})
mapCalled = true
})
c.Assert(err, jc.ErrorIsNil)
done, err := hub.Publish(topic, source)
c.Assert(err, jc.ErrorIsNil)
waitForPublishToComplete(c, done)
c.Check(originCalled, jc.IsTrue)
c.Check(messageCalled, jc.IsFalse)
c.Check(mapCalled, jc.IsTrue)
}
func (*MultiplexerHubSuite) TestCallbackCanPublish(c *gc.C) {
source := Emission{
Origin: "test",
Message: "hello world",
ID: 42,
}
var (
topic = "callback.topic"
originCalled bool
messageCalled bool
mapCalled bool
nestedPublish func()
)
hub := pubsub.NewStructuredHub(nil)
multi, err := hub.NewMultiplexer()
c.Assert(err, jc.ErrorIsNil)
defer multi.Unsubscribe()
err = multi.Add(topic, func(top string, data JustOrigin, err error) {
c.Check(err, jc.ErrorIsNil)
c.Check(top, gc.Equals, topic)
c.Check(data.Origin, gc.Equals, source.Origin)
originCalled = true
nestedPublish, err = hub.Publish(second, MessageID{
Message: "new message",
})
c.Check(err, jc.ErrorIsNil)
})
c.Assert(err, jc.ErrorIsNil)
err = multi.Add(second, func(topic string, data MessageID, err error) {
c.Check(data.Message, gc.Equals, "new message")
messageCalled = true
})
c.Assert(err, jc.ErrorIsNil)
err = multi.AddMatch(pubsub.MatchAll, func(top string, data map[string]interface{}) {
if top == second {
c.Check(data["message"], gc.Equals, "new message")
return
}
c.Check(top, gc.Equals, topic)
c.Check(data, jc.DeepEquals, map[string]interface{}{
"origin": "test",
"message": "hello world",
"id": float64(42), // ints are converted to floats through json.
})
mapCalled = true
})
c.Assert(err, jc.ErrorIsNil)
done, err := hub.Publish(topic, source)
c.Assert(err, jc.ErrorIsNil)
waitForPublishToComplete(c, done)
waitForPublishToComplete(c, nestedPublish)
c.Check(originCalled, jc.IsTrue)
c.Check(messageCalled, jc.IsTrue)
c.Check(mapCalled, jc.IsTrue)
}
func (s *MultiplexerHubSuite) TestUnsubscribeWhileMatch(c *gc.C) {
// The race we are trying do deal with is this:
// Publish is called, it acquires underlying mutex on hub
// Unsubscribe is called, it acquires the mutex on the multiplexer
// and awaits the mutex on the hub
// Publish proceeds and calls match on the multiplexer which wants
// the multiplexer mutex
// = deadlock
publishBlock := make(chan struct{})
publishReady := make(chan struct{})
unsubBlock := make(chan struct{})
unsubReady := make(chan struct{})
s.PatchValue(pubsub.PrePublishTestHook, func() {
close(publishReady)
<-publishBlock
})
s.PatchValue(pubsub.MultiUnsubscribeTestHook, func() {
close(unsubReady)
<-unsubBlock
})
hub := pubsub.NewStructuredHub(nil)
multi, err := hub.NewMultiplexer()
c.Assert(err, jc.ErrorIsNil)
err = multi.Add("a subject", func(string, map[string]interface{}) {})
c.Assert(err, jc.ErrorIsNil)
go func() {
hub.Publish("test", map[string]interface{}{})
}()
select {
case <-time.After(testing.LongWait):
c.Errorf("publish not called")
case <-publishReady:
}
unsubscribed := make(chan struct{})
go func() {
multi.Unsubscribe()
close(unsubscribed)
}()
select {
case <-time.After(testing.LongWait):
c.Errorf("unsubscribe not called")
case <-unsubReady:
}
// Now both functions are in the right place, let them continue.
close(publishBlock)
close(unsubBlock)
select {
case <-time.After(testing.LongWait):
c.Errorf("unsubscribe blocked")
case <-unsubscribed:
}
}