-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbroker_test.go
91 lines (70 loc) · 1.67 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
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
package pubsub_test
import (
"fmt"
"testing"
"github.com/stretchr/testify/require"
"github.com/tinh-tinh/pubsub"
)
func Test_Broker(t *testing.T) {
broker := pubsub.NewBroker()
sub := broker.AddSubscriber()
sub2 := broker.AddSubscriber()
topic := "topic"
broker.Subscribe(sub, topic)
broker.Subscribe(sub2, topic)
require.NotEmpty(t, sub.GetTopic())
require.NotEmpty(t, sub2.GetTopic())
require.Equal(t, broker.GetSubscribers(topic), 2)
broker.RemoveSubscriber(sub2)
require.Equal(t, broker.GetSubscribers(topic), 1)
broker.Unsubscribe(sub, topic)
require.Empty(t, sub.GetTopic())
}
func Test_Pubsub(t *testing.T) {
broker := pubsub.NewBroker()
sub := broker.AddSubscriber()
sub2 := broker.AddSubscriber()
topic := "topic"
broker.Subscribe(sub, topic)
topic2 := "topic2"
broker.Subscribe(sub2, topic2)
require.NotEmpty(t, sub.GetTopic())
require.NotEmpty(t, sub2.GetTopic())
go sub.Listen()
go sub2.Listen()
go (func() {
broker.Publish(topic, "hello")
})()
go (func() {
broker.Broadcast("hello")
})()
require.NotNil(t, sub.GetMessages())
require.NotNil(t, sub2.GetMessages())
fmt.Println(sub.GetMessages())
}
func Test_MaxSubscribers(t *testing.T) {
broker := pubsub.NewBroker(pubsub.BrokerOptions{
MaxSubscribers: 10,
})
for i := 0; i < 15; i++ {
s := broker.AddSubscriber()
if i < 10 {
require.NotNil(t, s)
} else {
require.Nil(t, s)
}
}
}
func Test_Pattern(t *testing.T) {
broker := pubsub.NewBroker(pubsub.BrokerOptions{
Wildcard: true,
Delimiter: ".",
})
sub := broker.AddSubscriber()
topic := "orders.*"
broker.Subscribe(sub, topic)
go sub.Listen()
go (func() {
broker.Publish("orders.created", "hello")
})()
}