tracking.tcl 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. start_server {tags {"tracking"}} {
  2. # Create a deferred client we'll use to redirect invalidation
  3. # messages to.
  4. set rd1 [redis_deferring_client]
  5. $rd1 client id
  6. set redir [$rd1 read]
  7. $rd1 subscribe __redis__:invalidate
  8. $rd1 read ; # Consume the SUBSCRIBE reply.
  9. # Create another client as well in order to test NOLOOP
  10. set rd2 [redis_deferring_client]
  11. test {Clients are able to enable tracking and redirect it} {
  12. r CLIENT TRACKING on REDIRECT $redir
  13. } {*OK}
  14. test {The other connection is able to get invalidations} {
  15. r SET a 1
  16. r GET a
  17. r INCR a
  18. r INCR b ; # This key should not be notified, since it wasn't fetched.
  19. set keys [lindex [$rd1 read] 2]
  20. assert {[llength $keys] == 1}
  21. assert {[lindex $keys 0] eq {a}}
  22. }
  23. test {The client is now able to disable tracking} {
  24. # Make sure to add a few more keys in the tracking list
  25. # so that we can check for leaks, as a side effect.
  26. r MGET a b c d e f g
  27. r CLIENT TRACKING off
  28. }
  29. test {Clients can enable the BCAST mode with the empty prefix} {
  30. r CLIENT TRACKING on BCAST REDIRECT $redir
  31. } {*OK*}
  32. test {The connection gets invalidation messages about all the keys} {
  33. r MSET a 1 b 2 c 3
  34. set keys [lsort [lindex [$rd1 read] 2]]
  35. assert {$keys eq {a b c}}
  36. }
  37. test {Clients can enable the BCAST mode with prefixes} {
  38. r CLIENT TRACKING off
  39. r CLIENT TRACKING on BCAST REDIRECT $redir PREFIX a: PREFIX b:
  40. r MULTI
  41. r INCR a:1
  42. r INCR a:2
  43. r INCR b:1
  44. r INCR b:2
  45. r EXEC
  46. # Because of the internals, we know we are going to receive
  47. # two separated notifications for the two different prefixes.
  48. set keys1 [lsort [lindex [$rd1 read] 2]]
  49. set keys2 [lsort [lindex [$rd1 read] 2]]
  50. set keys [lsort [list {*}$keys1 {*}$keys2]]
  51. assert {$keys eq {a:1 a:2 b:1 b:2}}
  52. }
  53. test {Adding prefixes to BCAST mode works} {
  54. r CLIENT TRACKING on BCAST REDIRECT $redir PREFIX c:
  55. r INCR c:1234
  56. set keys [lsort [lindex [$rd1 read] 2]]
  57. assert {$keys eq {c:1234}}
  58. }
  59. test {Tracking NOLOOP mode in standard mode works} {
  60. r CLIENT TRACKING off
  61. r CLIENT TRACKING on REDIRECT $redir NOLOOP
  62. r MGET otherkey1 loopkey otherkey2
  63. $rd2 SET otherkey1 1; # We should get this
  64. r SET loopkey 1 ; # We should not get this
  65. $rd2 SET otherkey2 1; # We should get this
  66. # Because of the internals, we know we are going to receive
  67. # two separated notifications for the two different prefixes.
  68. set keys1 [lsort [lindex [$rd1 read] 2]]
  69. set keys2 [lsort [lindex [$rd1 read] 2]]
  70. set keys [lsort [list {*}$keys1 {*}$keys2]]
  71. assert {$keys eq {otherkey1 otherkey2}}
  72. }
  73. test {Tracking NOLOOP mode in BCAST mode works} {
  74. r CLIENT TRACKING off
  75. r CLIENT TRACKING on BCAST REDIRECT $redir NOLOOP
  76. $rd2 SET otherkey1 1; # We should get this
  77. r SET loopkey 1 ; # We should not get this
  78. $rd2 SET otherkey2 1; # We should get this
  79. # Because of the internals, we know we are going to receive
  80. # two separated notifications for the two different prefixes.
  81. set keys1 [lsort [lindex [$rd1 read] 2]]
  82. set keys2 [lsort [lindex [$rd1 read] 2]]
  83. set keys [lsort [list {*}$keys1 {*}$keys2]]
  84. assert {$keys eq {otherkey1 otherkey2}}
  85. }
  86. test {Tracking gets notification of expired keys} {
  87. r CLIENT TRACKING off
  88. r CLIENT TRACKING on BCAST REDIRECT $redir NOLOOP
  89. r SET mykey myval px 1
  90. r SET mykeyotherkey myval ; # We should not get it
  91. after 1000
  92. # Because of the internals, we know we are going to receive
  93. # two separated notifications for the two different prefixes.
  94. set keys1 [lsort [lindex [$rd1 read] 2]]
  95. set keys [lsort [list {*}$keys1]]
  96. assert {$keys eq {mykey}}
  97. }
  98. test {Tracking gets notification on tracking table key eviction} {
  99. r CLIENT TRACKING off
  100. r CLIENT TRACKING on REDIRECT $redir NOLOOP
  101. r MSET key1 1 key2 2
  102. # Let the server track the two keys for us
  103. r MGET key1 key2
  104. # Force the eviction of all the keys but one:
  105. r config set tracking-table-max-keys 1
  106. # Note that we may have other keys in the table for this client,
  107. # since we disabled/enabled tracking multiple time with the same
  108. # ID, and tracking does not do ID cleanups for performance reasons.
  109. # So we check that eventually we'll receive one or the other key,
  110. # otherwise the test will die for timeout.
  111. while 1 {
  112. set keys [lindex [$rd1 read] 2]
  113. if {$keys eq {key1} || $keys eq {key2}} break
  114. }
  115. # We should receive an expire notification for one of
  116. # the two keys (only one must remain)
  117. assert {$keys eq {key1} || $keys eq {key2}}
  118. }
  119. $rd1 close
  120. }