]> git.saurik.com Git - redis.git/blame - tests/unit/maxmemory.tcl
Test SDIFF with first set empty.
[redis.git] / tests / unit / maxmemory.tcl
CommitLineData
243b783f 1start_server {tags {"maxmemory"}} {
2 foreach policy {
3 allkeys-random allkeys-lru volatile-lru volatile-random volatile-ttl
4 } {
5 test "maxmemory - is the memory limit honoured? (policy $policy)" {
6 # make sure to start with a blank instance
7 r flushall
8 # Get the current memory limit and calculate a new limit.
9 # We just add 100k to the current memory size so that it is
10 # fast for us to reach that limit.
11 set used [s used_memory]
12 set limit [expr {$used+100*1024}]
13 r config set maxmemory $limit
14 r config set maxmemory-policy $policy
15 # Now add keys until the limit is almost reached.
16 set numkeys 0
17 while 1 {
18 r setex [randomKey] 10000 x
19 incr numkeys
20 if {[s used_memory]+4096 > $limit} {
21 assert {$numkeys > 10}
22 break
23 }
24 }
25 # If we add the same number of keys already added again, we
26 # should still be under the limit.
27 for {set j 0} {$j < $numkeys} {incr j} {
28 r setex [randomKey] 10000 x
29 }
30 assert {[s used_memory] < ($limit+4096)}
31 }
32 }
33
34 foreach policy {
35 allkeys-random allkeys-lru volatile-lru volatile-random volatile-ttl
36 } {
37 test "maxmemory - only allkeys-* should remove non-volatile keys ($policy)" {
38 # make sure to start with a blank instance
39 r flushall
40 # Get the current memory limit and calculate a new limit.
41 # We just add 100k to the current memory size so that it is
42 # fast for us to reach that limit.
43 set used [s used_memory]
44 set limit [expr {$used+100*1024}]
45 r config set maxmemory $limit
46 r config set maxmemory-policy $policy
47 # Now add keys until the limit is almost reached.
48 set numkeys 0
49 while 1 {
50 r set [randomKey] x
51 incr numkeys
52 if {[s used_memory]+4096 > $limit} {
53 assert {$numkeys > 10}
54 break
55 }
56 }
57 # If we add the same number of keys already added again and
58 # the policy is allkeys-* we should still be under the limit.
59 # Otherwise we should see an error reported by Redis.
60 set err 0
61 for {set j 0} {$j < $numkeys} {incr j} {
62 if {[catch {r set [randomKey] x} e]} {
63 if {[string match {*used memory*} $e]} {
64 set err 1
65 }
66 }
67 }
68 if {[string match allkeys-* $policy]} {
69 assert {[s used_memory] < ($limit+4096)}
70 } else {
71 assert {$err == 1}
72 }
73 }
74 }
75
76 foreach policy {
77 volatile-lru volatile-random volatile-ttl
78 } {
79 test "maxmemory - policy $policy should only remove volatile keys." {
80 # make sure to start with a blank instance
81 r flushall
82 # Get the current memory limit and calculate a new limit.
83 # We just add 100k to the current memory size so that it is
84 # fast for us to reach that limit.
85 set used [s used_memory]
86 set limit [expr {$used+100*1024}]
87 r config set maxmemory $limit
88 r config set maxmemory-policy $policy
89 # Now add keys until the limit is almost reached.
90 set numkeys 0
91 while 1 {
92 # Odd keys are volatile
93 # Even keys are non volatile
94 if {$numkeys % 2} {
95 r setex "key:$numkeys" 10000 x
96 } else {
97 r set "key:$numkeys" x
98 }
99 if {[s used_memory]+4096 > $limit} {
100 assert {$numkeys > 10}
101 break
102 }
103 incr numkeys
104 }
105 # Now we add the same number of volatile keys already added.
106 # We expect Redis to evict only volatile keys in order to make
107 # space.
108 set err 0
109 for {set j 0} {$j < $numkeys} {incr j} {
110 catch {r setex "foo:$j" 10000 x}
111 }
112 # We should still be under the limit.
113 assert {[s used_memory] < ($limit+4096)}
114 # However all our non volatile keys should be here.
115 for {set j 0} {$j < $numkeys} {incr j 2} {
116 assert {[r exists "key:$j"]}
117 }
118 }
119 }
120}