forked from jcelliott/turnpike
-
Notifications
You must be signed in to change notification settings - Fork 3
/
broker_test.go
63 lines (51 loc) · 1.73 KB
/
broker_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
package turnpike
import (
"testing"
. "github.com/smartystreets/goconvey/convey"
)
type TestSender struct {
received Message
}
func (s *TestSender) Send(msg Message) error { s.received = msg; return nil }
func TestSubscribe(t *testing.T) {
Convey("Subscribing to a topic", t, func() {
broker := NewDefaultBroker().(*defaultBroker)
subscriber := &TestSender{}
testTopic := URI("turnpike.test.topic")
msg := &Subscribe{Request: 123, Topic: testTopic}
broker.Subscribe(subscriber, msg)
Convey("The subscriber should have received a SUBSCRIBED message", func() {
sub := subscriber.received.(*Subscribed).Subscription
So(sub, ShouldNotEqual, 0)
})
Convey("The broker should have created the subscription", func() {
sub := subscriber.received.(*Subscribed).Subscription
topic, ok := broker.subscriptions[sub]
So(ok, ShouldBeTrue)
So(topic, ShouldEqual, testTopic)
})
// TODO: multiple subscribe requests?
})
}
func TestUnsubscribe(t *testing.T) {
broker := NewDefaultBroker().(*defaultBroker)
subscriber := &TestSender{}
testTopic := URI("turnpike.test.topic")
msg := &Subscribe{Request: 123, Topic: testTopic}
broker.Subscribe(subscriber, msg)
sub := subscriber.received.(*Subscribed).Subscription
Convey("Unsubscribing from a topic", t, func() {
msg := &Unsubscribe{Request: 124, Subscription: sub}
broker.Unsubscribe(subscriber, msg)
Convey("The peer should have received an UNSUBSCRIBED message", func() {
unsub := subscriber.received.(*Unsubscribed).Request
So(unsub, ShouldNotEqual, 0)
})
Convey("The broker should have removed the subscription", func() {
_, ok := broker.subscriptions[sub]
So(ok, ShouldBeFalse)
_, ok = broker.routes[testTopic]
So(ok, ShouldBeFalse)
})
})
}