X-Git-Url: https://git.saurik.com/redis.git/blobdiff_plain/2c572622fb99f32328de58f815953f17d4ad0e4d..0ee3f05518e081640c1c6f9ae52c3a414f0feace:/tests/unit/expire.tcl?ds=sidebyside diff --git a/tests/unit/expire.tcl b/tests/unit/expire.tcl index 5de907ab..56a59f76 100644 --- a/tests/unit/expire.tcl +++ b/tests/unit/expire.tcl @@ -5,17 +5,17 @@ start_server {tags {"expire"}} { set v2 [r ttl x] set v3 [r expire x 10] set v4 [r ttl x] - r expire x 4 + r expire x 2 list $v1 $v2 $v3 $v4 - } {1 5 1 10} + } {1 [45] 1 10} test {EXPIRE - It should be still possible to read 'x'} { r get x } {foobar} tags {"slow"} { - test {EXPIRE - After 6 seconds the key should no longer be here} { - after 6000 + test {EXPIRE - After 2.1 seconds the key should no longer be here} { + after 2100 list [r get x] [r exists x] } {{} 0} } @@ -51,7 +51,7 @@ start_server {tags {"expire"}} { tags {"slow"} { test {SETEX - Wait for the key to expire} { - after 3000 + after 1100 r get y } {} } @@ -60,4 +60,96 @@ start_server {tags {"expire"}} { catch {r setex z -10 foo} e set _ $e } {*invalid expire*} + + test {PERSIST can undo an EXPIRE} { + r set x foo + r expire x 50 + list [r ttl x] [r persist x] [r ttl x] [r get x] + } {50 1 -1 foo} + + test {PERSIST returns 0 against non existing or non volatile keys} { + r set x foo + list [r persist foo] [r persist nokeyatall] + } {0 0} + + test {EXPIRE pricision is now the millisecond} { + # This test is very likely to do a false positive if the + # server is under pressure, so if it does not work give it a few more + # chances. + for {set j 0} {$j < 3} {incr j} { + r del x + r setex x 1 somevalue + after 900 + set a [r get x] + after 1100 + set b [r get x] + if {$a eq {somevalue} && $b eq {}} break + } + list $a $b + } {somevalue {}} + + test {PEXPIRE/PSETEX/PEXPIREAT can set sub-second expires} { + # This test is very likely to do a false positive if the + # server is under pressure, so if it does not work give it a few more + # chances. + for {set j 0} {$j < 3} {incr j} { + r del x y z + r psetex x 100 somevalue + after 80 + set a [r get x] + after 120 + set b [r get x] + + r set x somevalue + r pexpire x 100 + after 80 + set c [r get x] + after 120 + set d [r get x] + + r set x somevalue + r pexpireat x [expr ([clock seconds]*1000)+100] + after 80 + set e [r get x] + after 120 + set f [r get x] + + if {$a eq {somevalue} && $b eq {} && + $c eq {somevalue} && $d eq {} && + $e eq {somevalue} && $f eq {}} break + } + list $a $b + } {somevalue {}} + + test {PTTL returns millisecond time to live} { + r del x + r setex x 1 somevalue + set ttl [r pttl x] + assert {$ttl > 900 && $ttl <= 1000} + } + + test {Redis should actively expire keys incrementally} { + r flushdb + r psetex key1 500 a + r psetex key2 500 a + r psetex key3 500 a + set size1 [r dbsize] + # Redis expires random keys ten times every second so we are + # fairly sure that all the three keys should be evicted after + # one second. + after 1000 + set size2 [r dbsize] + list $size1 $size2 + } {3 0} + + test {5 keys in, 5 keys out} { + r flushdb + r set a c + r expire a 5 + r set t c + r set e c + r set s c + r set foo b + lsort [r keys *] + } {a e foo s t} }