pubsub.tcl 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. start_server {tags {"pubsub network"}} {
  2. if {$::singledb} {
  3. set db 0
  4. } else {
  5. set db 9
  6. }
  7. test "Pub/Sub PING" {
  8. set rd1 [redis_deferring_client]
  9. subscribe $rd1 somechannel
  10. # While subscribed to non-zero channels PING works in Pub/Sub mode.
  11. $rd1 ping
  12. $rd1 ping "foo"
  13. set reply1 [$rd1 read]
  14. set reply2 [$rd1 read]
  15. unsubscribe $rd1 somechannel
  16. # Now we are unsubscribed, PING should just return PONG.
  17. $rd1 ping
  18. set reply3 [$rd1 read]
  19. $rd1 close
  20. list $reply1 $reply2 $reply3
  21. } {{pong {}} {pong foo} PONG}
  22. test "PUBLISH/SUBSCRIBE basics" {
  23. set rd1 [redis_deferring_client]
  24. # subscribe to two channels
  25. assert_equal {1 2} [subscribe $rd1 {chan1 chan2}]
  26. assert_equal 1 [r publish chan1 hello]
  27. assert_equal 1 [r publish chan2 world]
  28. assert_equal {message chan1 hello} [$rd1 read]
  29. assert_equal {message chan2 world} [$rd1 read]
  30. # unsubscribe from one of the channels
  31. unsubscribe $rd1 {chan1}
  32. assert_equal 0 [r publish chan1 hello]
  33. assert_equal 1 [r publish chan2 world]
  34. assert_equal {message chan2 world} [$rd1 read]
  35. # unsubscribe from the remaining channel
  36. unsubscribe $rd1 {chan2}
  37. assert_equal 0 [r publish chan1 hello]
  38. assert_equal 0 [r publish chan2 world]
  39. # clean up clients
  40. $rd1 close
  41. }
  42. test "PUBLISH/SUBSCRIBE with two clients" {
  43. set rd1 [redis_deferring_client]
  44. set rd2 [redis_deferring_client]
  45. assert_equal {1} [subscribe $rd1 {chan1}]
  46. assert_equal {1} [subscribe $rd2 {chan1}]
  47. assert_equal 2 [r publish chan1 hello]
  48. assert_equal {message chan1 hello} [$rd1 read]
  49. assert_equal {message chan1 hello} [$rd2 read]
  50. # clean up clients
  51. $rd1 close
  52. $rd2 close
  53. }
  54. test "PUBLISH/SUBSCRIBE after UNSUBSCRIBE without arguments" {
  55. set rd1 [redis_deferring_client]
  56. assert_equal {1 2 3} [subscribe $rd1 {chan1 chan2 chan3}]
  57. unsubscribe $rd1
  58. after 10
  59. assert_equal 0 [r publish chan1 hello]
  60. assert_equal 0 [r publish chan2 hello]
  61. assert_equal 0 [r publish chan3 hello]
  62. # clean up clients
  63. $rd1 close
  64. }
  65. test "SUBSCRIBE to one channel more than once" {
  66. set rd1 [redis_deferring_client]
  67. assert_equal {1 1 1} [subscribe $rd1 {chan1 chan1 chan1}]
  68. assert_equal 1 [r publish chan1 hello]
  69. assert_equal {message chan1 hello} [$rd1 read]
  70. # clean up clients
  71. $rd1 close
  72. }
  73. test "UNSUBSCRIBE from non-subscribed channels" {
  74. set rd1 [redis_deferring_client]
  75. assert_equal {0 0 0} [unsubscribe $rd1 {foo bar quux}]
  76. # clean up clients
  77. $rd1 close
  78. }
  79. test "PUBLISH/PSUBSCRIBE basics" {
  80. set rd1 [redis_deferring_client]
  81. # subscribe to two patterns
  82. assert_equal {1 2} [psubscribe $rd1 {foo.* bar.*}]
  83. assert_equal 1 [r publish foo.1 hello]
  84. assert_equal 1 [r publish bar.1 hello]
  85. assert_equal 0 [r publish foo1 hello]
  86. assert_equal 0 [r publish barfoo.1 hello]
  87. assert_equal 0 [r publish qux.1 hello]
  88. assert_equal {pmessage foo.* foo.1 hello} [$rd1 read]
  89. assert_equal {pmessage bar.* bar.1 hello} [$rd1 read]
  90. # unsubscribe from one of the patterns
  91. assert_equal {1} [punsubscribe $rd1 {foo.*}]
  92. assert_equal 0 [r publish foo.1 hello]
  93. assert_equal 1 [r publish bar.1 hello]
  94. assert_equal {pmessage bar.* bar.1 hello} [$rd1 read]
  95. # unsubscribe from the remaining pattern
  96. assert_equal {0} [punsubscribe $rd1 {bar.*}]
  97. assert_equal 0 [r publish foo.1 hello]
  98. assert_equal 0 [r publish bar.1 hello]
  99. # clean up clients
  100. $rd1 close
  101. }
  102. test "PUBLISH/PSUBSCRIBE with two clients" {
  103. set rd1 [redis_deferring_client]
  104. set rd2 [redis_deferring_client]
  105. assert_equal {1} [psubscribe $rd1 {chan.*}]
  106. assert_equal {1} [psubscribe $rd2 {chan.*}]
  107. assert_equal 2 [r publish chan.foo hello]
  108. assert_equal {pmessage chan.* chan.foo hello} [$rd1 read]
  109. assert_equal {pmessage chan.* chan.foo hello} [$rd2 read]
  110. # clean up clients
  111. $rd1 close
  112. $rd2 close
  113. }
  114. test "PUBLISH/PSUBSCRIBE after PUNSUBSCRIBE without arguments" {
  115. set rd1 [redis_deferring_client]
  116. assert_equal {1 2 3} [psubscribe $rd1 {chan1.* chan2.* chan3.*}]
  117. punsubscribe $rd1
  118. assert_equal 0 [r publish chan1.hi hello]
  119. assert_equal 0 [r publish chan2.hi hello]
  120. assert_equal 0 [r publish chan3.hi hello]
  121. # clean up clients
  122. $rd1 close
  123. }
  124. test "PUNSUBSCRIBE from non-subscribed channels" {
  125. set rd1 [redis_deferring_client]
  126. assert_equal {0 0 0} [punsubscribe $rd1 {foo.* bar.* quux.*}]
  127. # clean up clients
  128. $rd1 close
  129. }
  130. test "NUMSUB returns numbers, not strings (#1561)" {
  131. r pubsub numsub abc def
  132. } {abc 0 def 0}
  133. test "NUMPATs returns the number of unique patterns" {
  134. set rd1 [redis_deferring_client]
  135. set rd2 [redis_deferring_client]
  136. # Three unique patterns and one that overlaps
  137. psubscribe $rd1 "foo*"
  138. psubscribe $rd2 "foo*"
  139. psubscribe $rd1 "bar*"
  140. psubscribe $rd2 "baz*"
  141. set patterns [r pubsub numpat]
  142. # clean up clients
  143. punsubscribe $rd1
  144. punsubscribe $rd2
  145. assert_equal 3 $patterns
  146. }
  147. test "Mix SUBSCRIBE and PSUBSCRIBE" {
  148. set rd1 [redis_deferring_client]
  149. assert_equal {1} [subscribe $rd1 {foo.bar}]
  150. assert_equal {2} [psubscribe $rd1 {foo.*}]
  151. assert_equal 2 [r publish foo.bar hello]
  152. assert_equal {message foo.bar hello} [$rd1 read]
  153. assert_equal {pmessage foo.* foo.bar hello} [$rd1 read]
  154. # clean up clients
  155. $rd1 close
  156. }
  157. test "PUNSUBSCRIBE and UNSUBSCRIBE should always reply" {
  158. # Make sure we are not subscribed to any channel at all.
  159. r punsubscribe
  160. r unsubscribe
  161. # Now check if the commands still reply correctly.
  162. set reply1 [r punsubscribe]
  163. set reply2 [r unsubscribe]
  164. concat $reply1 $reply2
  165. } {punsubscribe {} 0 unsubscribe {} 0}
  166. }