pubsub.tcl 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. start_server {tags {"pubsub"}} {
  2. proc __consume_subscribe_messages {client type channels} {
  3. set numsub -1
  4. set counts {}
  5. for {set i [llength $channels]} {$i > 0} {incr i -1} {
  6. set msg [$client read]
  7. assert_equal $type [lindex $msg 0]
  8. # when receiving subscribe messages the channels names
  9. # are ordered. when receiving unsubscribe messages
  10. # they are unordered
  11. set idx [lsearch -exact $channels [lindex $msg 1]]
  12. if {[string match "*unsubscribe" $type]} {
  13. assert {$idx >= 0}
  14. } else {
  15. assert {$idx == 0}
  16. }
  17. set channels [lreplace $channels $idx $idx]
  18. # aggregate the subscription count to return to the caller
  19. lappend counts [lindex $msg 2]
  20. }
  21. # we should have received messages for channels
  22. assert {[llength $channels] == 0}
  23. return $counts
  24. }
  25. proc subscribe {client channels} {
  26. $client subscribe {*}$channels
  27. __consume_subscribe_messages $client subscribe $channels
  28. }
  29. proc unsubscribe {client {channels {}}} {
  30. $client unsubscribe {*}$channels
  31. __consume_subscribe_messages $client unsubscribe $channels
  32. }
  33. proc psubscribe {client channels} {
  34. $client psubscribe {*}$channels
  35. __consume_subscribe_messages $client psubscribe $channels
  36. }
  37. proc punsubscribe {client {channels {}}} {
  38. $client punsubscribe {*}$channels
  39. __consume_subscribe_messages $client punsubscribe $channels
  40. }
  41. test "PUBLISH/SUBSCRIBE basics" {
  42. set rd1 [redis_deferring_client]
  43. # subscribe to two channels
  44. assert_equal {1 2} [subscribe $rd1 {chan1 chan2}]
  45. assert_equal 1 [r publish chan1 hello]
  46. assert_equal 1 [r publish chan2 world]
  47. assert_equal {message chan1 hello} [$rd1 read]
  48. assert_equal {message chan2 world} [$rd1 read]
  49. # unsubscribe from one of the channels
  50. unsubscribe $rd1 {chan1}
  51. assert_equal 0 [r publish chan1 hello]
  52. assert_equal 1 [r publish chan2 world]
  53. assert_equal {message chan2 world} [$rd1 read]
  54. # unsubscribe from the remaining channel
  55. unsubscribe $rd1 {chan2}
  56. assert_equal 0 [r publish chan1 hello]
  57. assert_equal 0 [r publish chan2 world]
  58. # clean up clients
  59. $rd1 close
  60. }
  61. test "PUBLISH/SUBSCRIBE with two clients" {
  62. set rd1 [redis_deferring_client]
  63. set rd2 [redis_deferring_client]
  64. assert_equal {1} [subscribe $rd1 {chan1}]
  65. assert_equal {1} [subscribe $rd2 {chan1}]
  66. assert_equal 2 [r publish chan1 hello]
  67. assert_equal {message chan1 hello} [$rd1 read]
  68. assert_equal {message chan1 hello} [$rd2 read]
  69. # clean up clients
  70. $rd1 close
  71. $rd2 close
  72. }
  73. test "PUBLISH/SUBSCRIBE after UNSUBSCRIBE without arguments" {
  74. set rd1 [redis_deferring_client]
  75. assert_equal {1 2 3} [subscribe $rd1 {chan1 chan2 chan3}]
  76. unsubscribe $rd1
  77. assert_equal 0 [r publish chan1 hello]
  78. assert_equal 0 [r publish chan2 hello]
  79. assert_equal 0 [r publish chan3 hello]
  80. # clean up clients
  81. $rd1 close
  82. }
  83. test "SUBSCRIBE to one channel more than once" {
  84. set rd1 [redis_deferring_client]
  85. assert_equal {1 1 1} [subscribe $rd1 {chan1 chan1 chan1}]
  86. assert_equal 1 [r publish chan1 hello]
  87. assert_equal {message chan1 hello} [$rd1 read]
  88. # clean up clients
  89. $rd1 close
  90. }
  91. test "UNSUBSCRIBE from non-subscribed channels" {
  92. set rd1 [redis_deferring_client]
  93. assert_equal {0 0 0} [unsubscribe $rd1 {foo bar quux}]
  94. # clean up clients
  95. $rd1 close
  96. }
  97. test "PUBLISH/PSUBSCRIBE basics" {
  98. set rd1 [redis_deferring_client]
  99. # subscribe to two patterns
  100. assert_equal {1 2} [psubscribe $rd1 {foo.* bar.*}]
  101. assert_equal 1 [r publish foo.1 hello]
  102. assert_equal 1 [r publish bar.1 hello]
  103. assert_equal 0 [r publish foo1 hello]
  104. assert_equal 0 [r publish barfoo.1 hello]
  105. assert_equal 0 [r publish qux.1 hello]
  106. assert_equal {pmessage foo.* foo.1 hello} [$rd1 read]
  107. assert_equal {pmessage bar.* bar.1 hello} [$rd1 read]
  108. # unsubscribe from one of the patterns
  109. assert_equal {1} [punsubscribe $rd1 {foo.*}]
  110. assert_equal 0 [r publish foo.1 hello]
  111. assert_equal 1 [r publish bar.1 hello]
  112. assert_equal {pmessage bar.* bar.1 hello} [$rd1 read]
  113. # unsubscribe from the remaining pattern
  114. assert_equal {0} [punsubscribe $rd1 {bar.*}]
  115. assert_equal 0 [r publish foo.1 hello]
  116. assert_equal 0 [r publish bar.1 hello]
  117. # clean up clients
  118. $rd1 close
  119. }
  120. test "PUBLISH/PSUBSCRIBE with two clients" {
  121. set rd1 [redis_deferring_client]
  122. set rd2 [redis_deferring_client]
  123. assert_equal {1} [psubscribe $rd1 {chan.*}]
  124. assert_equal {1} [psubscribe $rd2 {chan.*}]
  125. assert_equal 2 [r publish chan.foo hello]
  126. assert_equal {pmessage chan.* chan.foo hello} [$rd1 read]
  127. assert_equal {pmessage chan.* chan.foo hello} [$rd2 read]
  128. # clean up clients
  129. $rd1 close
  130. $rd2 close
  131. }
  132. test "PUBLISH/PSUBSCRIBE after PUNSUBSCRIBE without arguments" {
  133. set rd1 [redis_deferring_client]
  134. assert_equal {1 2 3} [psubscribe $rd1 {chan1.* chan2.* chan3.*}]
  135. punsubscribe $rd1
  136. assert_equal 0 [r publish chan1.hi hello]
  137. assert_equal 0 [r publish chan2.hi hello]
  138. assert_equal 0 [r publish chan3.hi hello]
  139. # clean up clients
  140. $rd1 close
  141. }
  142. test "PUNSUBSCRIBE from non-subscribed channels" {
  143. set rd1 [redis_deferring_client]
  144. assert_equal {0 0 0} [punsubscribe $rd1 {foo.* bar.* quux.*}]
  145. # clean up clients
  146. $rd1 close
  147. }
  148. test "Mix SUBSCRIBE and PSUBSCRIBE" {
  149. set rd1 [redis_deferring_client]
  150. assert_equal {1} [subscribe $rd1 {foo.bar}]
  151. assert_equal {2} [psubscribe $rd1 {foo.*}]
  152. assert_equal 2 [r publish foo.bar hello]
  153. assert_equal {message foo.bar hello} [$rd1 read]
  154. assert_equal {pmessage foo.* foo.bar hello} [$rd1 read]
  155. # clean up clients
  156. $rd1 close
  157. }
  158. test "PUNSUBSCRIBE and UNSUBSCRIBE should always reply" {
  159. # Make sure we are not subscribed to any channel at all.
  160. r punsubscribe
  161. r unsubscribe
  162. # Now check if the commands still reply correctly.
  163. set reply1 [r punsubscribe]
  164. set reply2 [r unsubscribe]
  165. concat $reply1 $reply2
  166. } {punsubscribe {} 0 unsubscribe {} 0}
  167. ### Keyspace events notification tests
  168. test "Keyspace notifications: we receive keyspace notifications" {
  169. r config set notify-keyspace-events KA
  170. set rd1 [redis_deferring_client]
  171. assert_equal {1} [psubscribe $rd1 *]
  172. r set foo bar
  173. assert_equal {pmessage * __keyspace@9__:foo set} [$rd1 read]
  174. $rd1 close
  175. }
  176. test "Keyspace notifications: we receive keyevent notifications" {
  177. r config set notify-keyspace-events EA
  178. set rd1 [redis_deferring_client]
  179. assert_equal {1} [psubscribe $rd1 *]
  180. r set foo bar
  181. assert_equal {pmessage * __keyevent@9__:set foo} [$rd1 read]
  182. $rd1 close
  183. }
  184. test "Keyspace notifications: we can receive both kind of events" {
  185. r config set notify-keyspace-events KEA
  186. set rd1 [redis_deferring_client]
  187. assert_equal {1} [psubscribe $rd1 *]
  188. r set foo bar
  189. assert_equal {pmessage * __keyspace@9__:foo set} [$rd1 read]
  190. assert_equal {pmessage * __keyevent@9__:set foo} [$rd1 read]
  191. $rd1 close
  192. }
  193. test "Keyspace notifications: we are able to mask events" {
  194. r config set notify-keyspace-events KEl
  195. r del mylist
  196. set rd1 [redis_deferring_client]
  197. assert_equal {1} [psubscribe $rd1 *]
  198. r set foo bar
  199. r lpush mylist a
  200. # No notification for set, because only list commands are enabled.
  201. assert_equal {pmessage * __keyspace@9__:mylist lpush} [$rd1 read]
  202. assert_equal {pmessage * __keyevent@9__:lpush mylist} [$rd1 read]
  203. $rd1 close
  204. }
  205. test "Keyspace notifications: general events test" {
  206. r config set notify-keyspace-events KEg
  207. set rd1 [redis_deferring_client]
  208. assert_equal {1} [psubscribe $rd1 *]
  209. r set foo bar
  210. r expire foo 1
  211. r del foo
  212. assert_equal {pmessage * __keyspace@9__:foo expire} [$rd1 read]
  213. assert_equal {pmessage * __keyevent@9__:expire foo} [$rd1 read]
  214. assert_equal {pmessage * __keyspace@9__:foo del} [$rd1 read]
  215. assert_equal {pmessage * __keyevent@9__:del foo} [$rd1 read]
  216. $rd1 close
  217. }
  218. test "Keyspace notifications: list events test" {
  219. r config set notify-keyspace-events KEl
  220. r del mylist
  221. set rd1 [redis_deferring_client]
  222. assert_equal {1} [psubscribe $rd1 *]
  223. r lpush mylist a
  224. r rpush mylist a
  225. r rpop mylist
  226. assert_equal {pmessage * __keyspace@9__:mylist lpush} [$rd1 read]
  227. assert_equal {pmessage * __keyevent@9__:lpush mylist} [$rd1 read]
  228. assert_equal {pmessage * __keyspace@9__:mylist rpush} [$rd1 read]
  229. assert_equal {pmessage * __keyevent@9__:rpush mylist} [$rd1 read]
  230. assert_equal {pmessage * __keyspace@9__:mylist rpop} [$rd1 read]
  231. assert_equal {pmessage * __keyevent@9__:rpop mylist} [$rd1 read]
  232. $rd1 close
  233. }
  234. test "Keyspace notifications: set events test" {
  235. r config set notify-keyspace-events Ks
  236. r del myset
  237. set rd1 [redis_deferring_client]
  238. assert_equal {1} [psubscribe $rd1 *]
  239. r sadd myset a b c d
  240. r srem myset x
  241. r sadd myset x y z
  242. r srem myset x
  243. assert_equal {pmessage * __keyspace@9__:myset sadd} [$rd1 read]
  244. assert_equal {pmessage * __keyspace@9__:myset sadd} [$rd1 read]
  245. assert_equal {pmessage * __keyspace@9__:myset srem} [$rd1 read]
  246. $rd1 close
  247. }
  248. test "Keyspace notifications: zset events test" {
  249. r config set notify-keyspace-events Kz
  250. r del myzset
  251. set rd1 [redis_deferring_client]
  252. assert_equal {1} [psubscribe $rd1 *]
  253. r zadd myzset 1 a 2 b
  254. r zrem myzset x
  255. r zadd myzset 3 x 4 y 5 z
  256. r zrem myzset x
  257. assert_equal {pmessage * __keyspace@9__:myzset zadd} [$rd1 read]
  258. assert_equal {pmessage * __keyspace@9__:myzset zadd} [$rd1 read]
  259. assert_equal {pmessage * __keyspace@9__:myzset zrem} [$rd1 read]
  260. $rd1 close
  261. }
  262. test "Keyspace notifications: hash events test" {
  263. r config set notify-keyspace-events Kh
  264. r del myhash
  265. set rd1 [redis_deferring_client]
  266. assert_equal {1} [psubscribe $rd1 *]
  267. r hmset myhash yes 1 no 0
  268. r hincrby myhash yes 10
  269. assert_equal {pmessage * __keyspace@9__:myhash hset} [$rd1 read]
  270. assert_equal {pmessage * __keyspace@9__:myhash hincrby} [$rd1 read]
  271. $rd1 close
  272. }
  273. test "Keyspace notifications: expired events (triggered expire)" {
  274. r config set notify-keyspace-events Ex
  275. r del foo
  276. set rd1 [redis_deferring_client]
  277. assert_equal {1} [psubscribe $rd1 *]
  278. r psetex foo 100 1
  279. wait_for_condition 50 100 {
  280. [r exists foo] == 0
  281. } else {
  282. fail "Key does not expire?!"
  283. }
  284. assert_equal {pmessage * __keyevent@9__:expired foo} [$rd1 read]
  285. $rd1 close
  286. }
  287. test "Keyspace notifications: expired events (background expire)" {
  288. r config set notify-keyspace-events Ex
  289. r del foo
  290. set rd1 [redis_deferring_client]
  291. assert_equal {1} [psubscribe $rd1 *]
  292. r psetex foo 100 1
  293. assert_equal {pmessage * __keyevent@9__:expired foo} [$rd1 read]
  294. $rd1 close
  295. }
  296. test "Keyspace notifications: evicted events" {
  297. r config set notify-keyspace-events Ee
  298. r config set maxmemory-policy allkeys-lru
  299. r flushdb
  300. set rd1 [redis_deferring_client]
  301. assert_equal {1} [psubscribe $rd1 *]
  302. r set foo bar
  303. r config set maxmemory 1
  304. assert_equal {pmessage * __keyevent@9__:evicted foo} [$rd1 read]
  305. r config set maxmemory 0
  306. $rd1 close
  307. }
  308. test "Keyspace notifications: test CONFIG GET/SET of event flags" {
  309. r config set notify-keyspace-events gKE
  310. assert_equal {gKE} [lindex [r config get notify-keyspace-events] 1]
  311. r config set notify-keyspace-events {$lshzxeKE}
  312. assert_equal {$lshzxeKE} [lindex [r config get notify-keyspace-events] 1]
  313. r config set notify-keyspace-events KA
  314. assert_equal {AK} [lindex [r config get notify-keyspace-events] 1]
  315. r config set notify-keyspace-events EA
  316. assert_equal {AE} [lindex [r config get notify-keyspace-events] 1]
  317. }
  318. }