]> git.saurik.com Git - redis.git/blob - tests/unit/expire.tcl
Make an EXEC test more latency proof.
[redis.git] / tests / unit / expire.tcl
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
12 test {EXPIRE - It should be still possible to read 'x'} {
13 r get x
14 } {foobar}
15
16 tags {"slow"} {
17 test {EXPIRE - After 2.1 seconds the key should no longer be here} {
18 after 2100
19 list [r get x] [r exists x]
20 } {{} 0}
21 }
22
23 test {EXPIRE - write on expire should work} {
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
29 } {bar foo}
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
52 tags {"slow"} {
53 test {SETEX - Wait for the key to expire} {
54 after 1100
55 r get y
56 } {}
57 }
58
59 test {SETEX - Wrong time parameter} {
60 catch {r setex z -10 foo} e
61 set _ $e
62 } {*invalid expire*}
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}
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
82 after 900
83 set a [r get x]
84 after 1100
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
98 after 80
99 set a [r get x]
100 after 120
101 set b [r get x]
102
103 r set x somevalue
104 r pexpire x 100
105 after 80
106 set c [r get x]
107 after 120
108 set d [r get x]
109
110 r set x somevalue
111 r pexpireat x [expr ([clock seconds]*1000)+100]
112 after 80
113 set e [r get x]
114 after 120
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 }
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}
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}
155 }