redict/tests/unit/expire.tcl

131 lines
3.4 KiB
Tcl
Raw Normal View History

2010-06-02 17:14:55 -04:00
start_server {tags {"expire"}} {
test {EXPIRE - set timeouts multiple times} {
r set x foobar
set v1 [r expire x 5]
set v2 [r ttl x]
set v3 [r expire x 10]
set v4 [r ttl x]
r expire x 2
list $v1 $v2 $v3 $v4
} {1 [45] 1 10}
test {EXPIRE - It should be still possible to read 'x'} {
r get x
} {foobar}
2010-06-02 18:16:10 -04:00
tags {"slow"} {
test {EXPIRE - After 2.1 seconds the key should no longer be here} {
after 2100
2010-06-02 18:16:10 -04:00
list [r get x] [r exists x]
} {{} 0}
}
test {EXPIRE - write on expire should work} {
r del x
r lpush x foo
r expire x 1000
r lpush x bar
r lrange x 0 -1
} {bar foo}
test {EXPIREAT - Check for EXPIRE alike behavior} {
r del x
r set x foo
r expireat x [expr [clock seconds]+15]
r ttl x
} {1[345]}
test {SETEX - Set + Expire combo operation. Check for TTL} {
r setex x 12 test
r ttl x
} {1[012]}
test {SETEX - Check value} {
r get x
} {test}
test {SETEX - Overwrite old key} {
r setex y 1 foo
r get y
} {foo}
2010-06-02 18:26:39 -04:00
tags {"slow"} {
test {SETEX - Wait for the key to expire} {
after 1100
2010-06-02 18:26:39 -04:00
r get y
} {}
}
test {SETEX - Wrong time parameter} {
catch {r setex z -10 foo} e
set _ $e
} {*invalid expire*}
2010-08-03 08:25:22 -04:00
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 997
set a [r get x]
after 1002
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 97
set a [r get x]
after 102
set b [r get x]
r set x somevalue
r pexpire x 100
after 97
set c [r get x]
after 102
set d [r get x]
r set x somevalue
r pexpireat x [expr ([clock seconds]*1000)+100]
after 97
set e [r get x]
after 102
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}
}
}