-
Notifications
You must be signed in to change notification settings - Fork 39
/
pubsub_test.go
66 lines (53 loc) · 1.87 KB
/
pubsub_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
package redeo
import (
. "github.com/bsm/ginkgo/v2"
. "github.com/bsm/gomega"
"github.com/bsm/redeo/v2/redeotest"
"github.com/bsm/redeo/v2/resp"
)
var _ = Describe("PubSubBroker", func() {
var subject *PubSubBroker
BeforeEach(func() {
subject = NewPubSubBroker()
})
var publish = func(name, msg string) int64 {
c := resp.NewCommand("subscribe", resp.CommandArgument(name), resp.CommandArgument(msg))
w := redeotest.NewRecorder()
subject.Publish().ServeRedeo(w, c)
n, err := w.Response()
Expect(err).NotTo(HaveOccurred())
return n.(int64)
}
It("should publish/subscribe", func() {
sub1 := redeotest.NewRecorder()
sub2 := redeotest.NewRecorder()
subc := resp.NewCommand("subscribe", resp.CommandArgument("chan"))
Expect(subject.channels).To(HaveLen(0))
Expect(publish("chan", "msg1")).To(Equal(int64(0)))
subject.Subscribe().ServeRedeo(sub1, subc)
Expect(sub1.Responses()).To(Equal([]interface{}{
[]interface{}{"subscribe", "chan", int64(1)},
}))
Expect(subject.channels).To(HaveLen(1))
Expect(subject.channels).To(HaveKey("chan"))
Expect(subject.channels["chan"].subscribers).To(HaveLen(1))
Expect(publish("chan", "msg2")).To(Equal(int64(1)))
subject.Subscribe().ServeRedeo(sub2, subc)
Expect(sub2.Responses()).To(Equal([]interface{}{
[]interface{}{"subscribe", "chan", int64(1)},
}))
Expect(subject.channels).To(HaveLen(1))
Expect(subject.channels).To(HaveKey("chan"))
Expect(subject.channels["chan"].subscribers).To(HaveLen(2))
Expect(publish("chan", "msg3")).To(Equal(int64(2)))
Expect(sub1.Responses()).To(Equal([]interface{}{
[]interface{}{"subscribe", "chan", int64(1)},
[]interface{}{"message", "chan", "msg2"},
[]interface{}{"message", "chan", "msg3"},
}))
Expect(sub2.Responses()).To(Equal([]interface{}{
[]interface{}{"subscribe", "chan", int64(1)},
[]interface{}{"message", "chan", "msg3"},
}))
})
})