2
0

expire.tcl 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. start_server {tags {"expire"}} {
  2. test {EXPIRE - set timeouts multiple times} {
  3. r set x foobar
  4. set v1 [r expire x 5]
  5. set v2 [r ttl x]
  6. set v3 [r expire x 10]
  7. set v4 [r ttl x]
  8. r expire x 2
  9. list $v1 $v2 $v3 $v4
  10. } {1 [45] 1 10}
  11. test {EXPIRE - It should be still possible to read 'x'} {
  12. r get x
  13. } {foobar}
  14. tags {"slow"} {
  15. test {EXPIRE - After 2.1 seconds the key should no longer be here} {
  16. after 2100
  17. list [r get x] [r exists x]
  18. } {{} 0}
  19. }
  20. test {EXPIRE - write on expire should work} {
  21. r del x
  22. r lpush x foo
  23. r expire x 1000
  24. r lpush x bar
  25. r lrange x 0 -1
  26. } {bar foo}
  27. test {EXPIREAT - Check for EXPIRE alike behavior} {
  28. r del x
  29. r set x foo
  30. r expireat x [expr [clock seconds]+15]
  31. r ttl x
  32. } {1[345]}
  33. test {SETEX - Set + Expire combo operation. Check for TTL} {
  34. r setex x 12 test
  35. r ttl x
  36. } {1[012]}
  37. test {SETEX - Check value} {
  38. r get x
  39. } {test}
  40. test {SETEX - Overwrite old key} {
  41. r setex y 1 foo
  42. r get y
  43. } {foo}
  44. tags {"slow"} {
  45. test {SETEX - Wait for the key to expire} {
  46. after 1100
  47. r get y
  48. } {}
  49. }
  50. test {SETEX - Wrong time parameter} {
  51. catch {r setex z -10 foo} e
  52. set _ $e
  53. } {*invalid expire*}
  54. test {PERSIST can undo an EXPIRE} {
  55. r set x foo
  56. r expire x 50
  57. list [r ttl x] [r persist x] [r ttl x] [r get x]
  58. } {50 1 -1 foo}
  59. test {PERSIST returns 0 against non existing or non volatile keys} {
  60. r set x foo
  61. list [r persist foo] [r persist nokeyatall]
  62. } {0 0}
  63. test {EXPIRE pricision is now the millisecond} {
  64. # This test is very likely to do a false positive if the
  65. # server is under pressure, so if it does not work give it a few more
  66. # chances.
  67. for {set j 0} {$j < 3} {incr j} {
  68. r del x
  69. r setex x 1 somevalue
  70. after 900
  71. set a [r get x]
  72. after 1100
  73. set b [r get x]
  74. if {$a eq {somevalue} && $b eq {}} break
  75. }
  76. list $a $b
  77. } {somevalue {}}
  78. test {PEXPIRE/PSETEX/PEXPIREAT can set sub-second expires} {
  79. # This test is very likely to do a false positive if the
  80. # server is under pressure, so if it does not work give it a few more
  81. # chances.
  82. for {set j 0} {$j < 3} {incr j} {
  83. r del x y z
  84. r psetex x 100 somevalue
  85. after 80
  86. set a [r get x]
  87. after 120
  88. set b [r get x]
  89. r set x somevalue
  90. r pexpire x 100
  91. after 80
  92. set c [r get x]
  93. after 120
  94. set d [r get x]
  95. r set x somevalue
  96. r pexpireat x [expr ([clock seconds]*1000)+100]
  97. after 80
  98. set e [r get x]
  99. after 120
  100. set f [r get x]
  101. if {$a eq {somevalue} && $b eq {} &&
  102. $c eq {somevalue} && $d eq {} &&
  103. $e eq {somevalue} && $f eq {}} break
  104. }
  105. list $a $b
  106. } {somevalue {}}
  107. test {TTL returns tiem to live in seconds} {
  108. r del x
  109. r setex x 10 somevalue
  110. set ttl [r ttl x]
  111. assert {$ttl > 8 && $ttl <= 10}
  112. }
  113. test {PTTL returns time to live in milliseconds} {
  114. r del x
  115. r setex x 1 somevalue
  116. set ttl [r pttl x]
  117. assert {$ttl > 900 && $ttl <= 1000}
  118. }
  119. test {TTL / PTTL return -1 if key has no expire} {
  120. r del x
  121. r set x hello
  122. list [r ttl x] [r pttl x]
  123. } {-1 -1}
  124. test {TTL / PTTL return -2 if key does not exit} {
  125. r del x
  126. list [r ttl x] [r pttl x]
  127. } {-2 -2}
  128. test {Redis should actively expire keys incrementally} {
  129. r flushdb
  130. r psetex key1 500 a
  131. r psetex key2 500 a
  132. r psetex key3 500 a
  133. set size1 [r dbsize]
  134. # Redis expires random keys ten times every second so we are
  135. # fairly sure that all the three keys should be evicted after
  136. # one second.
  137. after 1000
  138. set size2 [r dbsize]
  139. list $size1 $size2
  140. } {3 0}
  141. test {Redis should lazy expire keys} {
  142. r flushdb
  143. r debug set-active-expire 0
  144. r psetex key1 500 a
  145. r psetex key2 500 a
  146. r psetex key3 500 a
  147. set size1 [r dbsize]
  148. # Redis expires random keys ten times every second so we are
  149. # fairly sure that all the three keys should be evicted after
  150. # one second.
  151. after 1000
  152. set size2 [r dbsize]
  153. r mget key1 key2 key3
  154. set size3 [r dbsize]
  155. r debug set-active-expire 1
  156. list $size1 $size2 $size3
  157. } {3 3 0}
  158. test {EXPIRE should not resurrect keys (issue #1026)} {
  159. r debug set-active-expire 0
  160. r set foo bar
  161. r pexpire foo 500
  162. after 1000
  163. r expire foo 10
  164. r debug set-active-expire 1
  165. r exists foo
  166. } {0}
  167. test {5 keys in, 5 keys out} {
  168. r flushdb
  169. r set a c
  170. r expire a 5
  171. r set t c
  172. r set e c
  173. r set s c
  174. r set foo b
  175. lsort [r keys *]
  176. } {a e foo s t}
  177. test {EXPIRE with empty string as TTL should report an error} {
  178. r set foo bar
  179. catch {r expire foo ""} e
  180. set e
  181. } {*not an integer*}
  182. }