1
0

pubsub.tcl 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. assert_equal 0 [r publish chan1 hello]
  59. assert_equal 0 [r publish chan2 hello]
  60. assert_equal 0 [r publish chan3 hello]
  61. # clean up clients
  62. $rd1 close
  63. }
  64. test "SUBSCRIBE to one channel more than once" {
  65. set rd1 [redis_deferring_client]
  66. assert_equal {1 1 1} [subscribe $rd1 {chan1 chan1 chan1}]
  67. assert_equal 1 [r publish chan1 hello]
  68. assert_equal {message chan1 hello} [$rd1 read]
  69. # clean up clients
  70. $rd1 close
  71. }
  72. test "UNSUBSCRIBE from non-subscribed channels" {
  73. set rd1 [redis_deferring_client]
  74. assert_equal {0 0 0} [unsubscribe $rd1 {foo bar quux}]
  75. # clean up clients
  76. $rd1 close
  77. }
  78. test "PUBLISH/PSUBSCRIBE basics" {
  79. set rd1 [redis_deferring_client]
  80. # subscribe to two patterns
  81. assert_equal {1 2} [psubscribe $rd1 {foo.* bar.*}]
  82. assert_equal 1 [r publish foo.1 hello]
  83. assert_equal 1 [r publish bar.1 hello]
  84. assert_equal 0 [r publish foo1 hello]
  85. assert_equal 0 [r publish barfoo.1 hello]
  86. assert_equal 0 [r publish qux.1 hello]
  87. assert_equal {pmessage foo.* foo.1 hello} [$rd1 read]
  88. assert_equal {pmessage bar.* bar.1 hello} [$rd1 read]
  89. # unsubscribe from one of the patterns
  90. assert_equal {1} [punsubscribe $rd1 {foo.*}]
  91. assert_equal 0 [r publish foo.1 hello]
  92. assert_equal 1 [r publish bar.1 hello]
  93. assert_equal {pmessage bar.* bar.1 hello} [$rd1 read]
  94. # unsubscribe from the remaining pattern
  95. assert_equal {0} [punsubscribe $rd1 {bar.*}]
  96. assert_equal 0 [r publish foo.1 hello]
  97. assert_equal 0 [r publish bar.1 hello]
  98. # clean up clients
  99. $rd1 close
  100. }
  101. test "PUBLISH/PSUBSCRIBE with two clients" {
  102. set rd1 [redis_deferring_client]
  103. set rd2 [redis_deferring_client]
  104. assert_equal {1} [psubscribe $rd1 {chan.*}]
  105. assert_equal {1} [psubscribe $rd2 {chan.*}]
  106. assert_equal 2 [r publish chan.foo hello]
  107. assert_equal {pmessage chan.* chan.foo hello} [$rd1 read]
  108. assert_equal {pmessage chan.* chan.foo hello} [$rd2 read]
  109. # clean up clients
  110. $rd1 close
  111. $rd2 close
  112. }
  113. test "PUBLISH/PSUBSCRIBE after PUNSUBSCRIBE without arguments" {
  114. set rd1 [redis_deferring_client]
  115. assert_equal {1 2 3} [psubscribe $rd1 {chan1.* chan2.* chan3.*}]
  116. punsubscribe $rd1
  117. assert_equal 0 [r publish chan1.hi hello]
  118. assert_equal 0 [r publish chan2.hi hello]
  119. assert_equal 0 [r publish chan3.hi hello]
  120. # clean up clients
  121. $rd1 close
  122. }
  123. test "PUNSUBSCRIBE from non-subscribed channels" {
  124. set rd1 [redis_deferring_client]
  125. assert_equal {0 0 0} [punsubscribe $rd1 {foo.* bar.* quux.*}]
  126. # clean up clients
  127. $rd1 close
  128. }
  129. test "NUMSUB returns numbers, not strings (#1561)" {
  130. r pubsub numsub abc def
  131. } {abc 0 def 0}
  132. test "NUMPATs returns the number of unique patterns" {
  133. set rd1 [redis_deferring_client]
  134. set rd2 [redis_deferring_client]
  135. # Three unique patterns and one that overlaps
  136. psubscribe $rd1 "foo*"
  137. psubscribe $rd2 "foo*"
  138. psubscribe $rd1 "bar*"
  139. psubscribe $rd2 "baz*"
  140. set patterns [r pubsub numpat]
  141. # clean up clients
  142. punsubscribe $rd1
  143. punsubscribe $rd2
  144. assert_equal 3 $patterns
  145. }
  146. test "Mix SUBSCRIBE and PSUBSCRIBE" {
  147. set rd1 [redis_deferring_client]
  148. assert_equal {1} [subscribe $rd1 {foo.bar}]
  149. assert_equal {2} [psubscribe $rd1 {foo.*}]
  150. assert_equal 2 [r publish foo.bar hello]
  151. assert_equal {message foo.bar hello} [$rd1 read]
  152. assert_equal {pmessage foo.* foo.bar hello} [$rd1 read]
  153. # clean up clients
  154. $rd1 close
  155. }
  156. test "PUNSUBSCRIBE and UNSUBSCRIBE should always reply" {
  157. # Make sure we are not subscribed to any channel at all.
  158. r punsubscribe
  159. r unsubscribe
  160. # Now check if the commands still reply correctly.
  161. set reply1 [r punsubscribe]
  162. set reply2 [r unsubscribe]
  163. concat $reply1 $reply2
  164. } {punsubscribe {} 0 unsubscribe {} 0}
  165. }