-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpubsub_test.go
66 lines (50 loc) · 909 Bytes
/
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 gr_test
import (
"log"
"strconv"
"testing"
"time"
"github.com/xzip/gr"
)
func TestPubSubBegin(t *testing.T) {
log.Println("[Testing PubSub]")
}
func TestPubSub(t *testing.T) {
test := func() {
for i := 0; i < 10; i++ {
go pub(strconv.Itoa(i))
}
sub(false)
}
safeTestContext(test)
}
func TestPubSubPattern(t *testing.T) {
test := func() {
for i := 0; i < 10; i++ {
go pub(strconv.Itoa(i))
}
sub(true)
}
safeTestContext(test)
}
func pub(goThread string) {
time.Sleep(500 * time.Millisecond)
for i := 0; i < 100; i++ {
redis.Publish("gr::mychannel", "hello:"+goThread+":"+strconv.Itoa(i))
}
}
func sub(pattern bool) {
f := func(ps *gr.PubSub) {
for i := 0; i < 1000; i++ {
<-ps.Message
}
}
if pattern {
redis.PSubscribe(f, "gr::my*")
} else {
redis.Subscribe(f, "gr::mychannel")
}
}
func TestPubSubEnd(t *testing.T) {
println("[OK]")
}