-
Notifications
You must be signed in to change notification settings - Fork 158
/
pubsub_test.go
76 lines (71 loc) · 1.57 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
67
68
69
70
71
72
73
74
75
76
package rueidis
import (
"testing"
"time"
)
func TestSubs_Publish(t *testing.T) {
defer ShouldNotLeaked(SetupLeakDetection())
t.Run("without subs", func(t *testing.T) {
s := newSubs()
s.Publish("aa", PubSubMessage{}) // just no block
})
t.Run("with multiple subs", func(t *testing.T) {
s := newSubs()
ch1, cancel1 := s.Subscribe([]string{"a"})
ch2, cancel2 := s.Subscribe([]string{"a"})
ch3, cancel3 := s.Subscribe([]string{"b"})
m1 := PubSubMessage{Pattern: "1", Channel: "2", Message: "3"}
m2 := PubSubMessage{Pattern: "11", Channel: "22", Message: "33"}
go func() {
s.Publish("a", m1)
s.Publish("b", m2)
}()
for m := range ch1 {
if m != m1 {
t.Fatalf("unexpected msg %v", m)
}
cancel1()
}
for m := range ch2 {
if m != m1 {
t.Fatalf("unexpected msg %v", m)
}
cancel2()
}
for m := range ch3 {
if m != m2 {
t.Fatalf("unexpected msg %v", m)
}
cancel3()
}
})
t.Run("drain ch", func(t *testing.T) {
s := newSubs()
ch, cancel := s.Subscribe([]string{"a"})
s.Publish("a", PubSubMessage{})
if len(ch) != 1 {
t.Fatalf("unexpected ch len %v", len(ch))
}
cancel()
for ; len(ch) != 0; time.Sleep(time.Millisecond * 100) {
t.Log("wait ch to be drain")
}
})
}
func TestSubs_Unsubscribe(t *testing.T) {
defer ShouldNotLeaked(SetupLeakDetection())
s := newSubs()
ch, _ := s.Subscribe([]string{"1", "2"})
go func() {
s.Publish("1", PubSubMessage{})
}()
_, ok := <-ch
if !ok {
t.Fatalf("unexpected ch closed")
}
s.Unsubscribe("1")
_, ok = <-ch
if ok {
t.Fatalf("unexpected ch unclosed")
}
}