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