1 start_server
{tags
{"pubsub"}} {
2 proc __consume_subscribe_messages
{client type channels
} {
6 for {set i
[llength $channels]} {$i > 0} {incr i
-1} {
8 assert_equal
$type [lindex $msg 0]
10 # when receiving subscribe messages the channels names
11 # are ordered. when receiving unsubscribe messages
13 set idx
[lsearch -exact $channels [lindex $msg 1]]
14 if {[string match
"*unsubscribe" $type]} {
19 set channels
[lreplace $channels $idx $idx]
21 # aggregate the subscription count to return to the caller
22 lappend counts
[lindex $msg 2]
25 # we should have received messages for channels
26 assert
{[llength $channels] == 0}
30 proc subscribe
{client channels
} {
31 $client subscribe
{*}$channels
32 __consume_subscribe_messages
$client subscribe
$channels
35 proc unsubscribe
{client
{channels
{}}} {
36 $client unsubscribe
{*}$channels
37 __consume_subscribe_messages
$client unsubscribe
$channels
40 proc psubscribe
{client channels
} {
41 $client psubscribe
{*}$channels
42 __consume_subscribe_messages
$client psubscribe
$channels
45 proc punsubscribe
{client
{channels
{}}} {
46 $client punsubscribe
{*}$channels
47 __consume_subscribe_messages
$client punsubscribe
$channels
50 test
"PUBLISH/SUBSCRIBE basics" {
51 set rd1
[redis_deferring_client
]
53 # subscribe to two channels
54 assert_equal
{1 2} [subscribe
$rd1 {chan1 chan2
}]
55 assert_equal
1 [r publish chan1 hello
]
56 assert_equal
1 [r publish chan2 world
]
57 assert_equal
{message chan1 hello
} [$rd1 read]
58 assert_equal
{message chan2 world
} [$rd1 read]
60 # unsubscribe from one of the channels
61 unsubscribe
$rd1 {chan1
}
62 assert_equal
0 [r publish chan1 hello
]
63 assert_equal
1 [r publish chan2 world
]
64 assert_equal
{message chan2 world
} [$rd1 read]
66 # unsubscribe from the remaining channel
67 unsubscribe
$rd1 {chan2
}
68 assert_equal
0 [r publish chan1 hello
]
69 assert_equal
0 [r publish chan2 world
]
75 test
"PUBLISH/SUBSCRIBE with two clients" {
76 set rd1
[redis_deferring_client
]
77 set rd2
[redis_deferring_client
]
79 assert_equal
{1} [subscribe
$rd1 {chan1
}]
80 assert_equal
{1} [subscribe
$rd2 {chan1
}]
81 assert_equal
2 [r publish chan1 hello
]
82 assert_equal
{message chan1 hello
} [$rd1 read]
83 assert_equal
{message chan1 hello
} [$rd2 read]
90 test
"PUBLISH/SUBSCRIBE after UNSUBSCRIBE without arguments" {
91 set rd1
[redis_deferring_client
]
92 assert_equal
{1 2 3} [subscribe
$rd1 {chan1 chan2 chan3
}]
94 assert_equal
0 [r publish chan1 hello
]
95 assert_equal
0 [r publish chan2 hello
]
96 assert_equal
0 [r publish chan3 hello
]
102 test
"SUBSCRIBE to one channel more than once" {
103 set rd1
[redis_deferring_client
]
104 assert_equal
{1 1 1} [subscribe
$rd1 {chan1 chan1 chan1
}]
105 assert_equal
1 [r publish chan1 hello
]
106 assert_equal
{message chan1 hello
} [$rd1 read]
112 test
"UNSUBSCRIBE from non-subscribed channels" {
113 set rd1
[redis_deferring_client
]
114 assert_equal
{0 0 0} [unsubscribe
$rd1 {foo bar quux
}]
120 test
"PUBLISH/PSUBSCRIBE basics" {
121 set rd1
[redis_deferring_client
]
123 # subscribe to two patterns
124 assert_equal
{1 2} [psubscribe
$rd1 {foo.
* bar.
*}]
125 assert_equal
1 [r publish foo
.1 hello
]
126 assert_equal
1 [r publish bar
.1 hello
]
127 assert_equal
0 [r publish foo1 hello
]
128 assert_equal
0 [r publish barfoo
.1 hello
]
129 assert_equal
0 [r publish qux
.1 hello
]
130 assert_equal
{pmessage foo.
* foo
.1 hello
} [$rd1 read]
131 assert_equal
{pmessage bar.
* bar
.1 hello
} [$rd1 read]
133 # unsubscribe from one of the patterns
134 assert_equal
{1} [punsubscribe
$rd1 {foo.
*}]
135 assert_equal
0 [r publish foo
.1 hello
]
136 assert_equal
1 [r publish bar
.1 hello
]
137 assert_equal
{pmessage bar.
* bar
.1 hello
} [$rd1 read]
139 # unsubscribe from the remaining pattern
140 assert_equal
{0} [punsubscribe
$rd1 {bar.
*}]
141 assert_equal
0 [r publish foo
.1 hello
]
142 assert_equal
0 [r publish bar
.1 hello
]
148 test
"PUBLISH/PSUBSCRIBE with two clients" {
149 set rd1
[redis_deferring_client
]
150 set rd2
[redis_deferring_client
]
152 assert_equal
{1} [psubscribe
$rd1 {chan.
*}]
153 assert_equal
{1} [psubscribe
$rd2 {chan.
*}]
154 assert_equal
2 [r publish chan.foo hello
]
155 assert_equal
{pmessage chan.
* chan.foo hello
} [$rd1 read]
156 assert_equal
{pmessage chan.
* chan.foo hello
} [$rd2 read]
163 test
"PUBLISH/PSUBSCRIBE after PUNSUBSCRIBE without arguments" {
164 set rd1
[redis_deferring_client
]
165 assert_equal
{1 2 3} [psubscribe
$rd1 {chan1.
* chan2.
* chan3.
*}]
167 assert_equal
0 [r publish chan1.hi hello
]
168 assert_equal
0 [r publish chan2.hi hello
]
169 assert_equal
0 [r publish chan3.hi hello
]
175 test
"PUNSUBSCRIBE from non-subscribed channels" {
176 set rd1
[redis_deferring_client
]
177 assert_equal
{0 0 0} [punsubscribe
$rd1 {foo.
* bar.
* quux.
*}]
183 test
"Mix SUBSCRIBE and PSUBSCRIBE" {
184 set rd1
[redis_deferring_client
]
185 assert_equal
{1} [subscribe
$rd1 {foo.bar
}]
186 assert_equal
{2} [psubscribe
$rd1 {foo.
*}]
188 assert_equal
2 [r publish foo.bar hello
]
189 assert_equal
{message foo.bar hello
} [$rd1 read]
190 assert_equal
{pmessage foo.
* foo.bar hello
} [$rd1 read]