]> git.saurik.com Git - redis.git/commitdiff
Merge remote-tracking branch 'origin/unstable' into unstable
authorantirez <antirez@gmail.com>
Thu, 28 Jul 2011 12:40:47 +0000 (14:40 +0200)
committerantirez <antirez@gmail.com>
Thu, 28 Jul 2011 12:40:47 +0000 (14:40 +0200)
src/networking.c
tests/support/test.tcl
tests/test_helper.tcl
tests/unit/maxmemory.tcl [new file with mode: 0644]

index 629267d1cad2d025fe958407569c1a88043a810c..7f9b96f7da01c7ebdfcbd1202f510fffa4eec1f6 100644 (file)
@@ -793,6 +793,9 @@ int processMultibulkBuffer(redisClient *c) {
 void processInputBuffer(redisClient *c) {
     /* Keep processing while there is something in the input buffer */
     while(sdslen(c->querybuf)) {
+        /* Immediately abort if the client is in the middle of something. */
+        if (c->flags & REDIS_BLOCKED) return;
+
         /* REDIS_CLOSE_AFTER_REPLY closes the connection once the reply is
          * written to the client. Make sure to not let the reply grow after
          * this flag has been set (i.e. don't process more commands). */
index 4e68905a5a245304ace9020c140c4d18ae89dd84..c875cfd8030755c752f9aa72a9aa034419393512 100644 (file)
@@ -5,7 +5,7 @@ set ::tests_failed {}
 
 proc assert {condition} {
     if {![uplevel 1 expr $condition]} {
-        error "assertion:Expected '$value' to be true"
+        error "assertion:Expected condition '$condition' to be true"
     }
 }
 
index 559d026471073977eba18815d190eb1e9569b3ab..4f3cf01ec27eca0c5fb47751e870bc08273056e7 100644 (file)
@@ -32,6 +32,7 @@ set ::all_tests {
     unit/pubsub
     unit/slowlog
     unit/scripting
+    unit/maxmemory
 }
 # Index to the next test to run in the ::all_tests list.
 set ::next_test 0
diff --git a/tests/unit/maxmemory.tcl b/tests/unit/maxmemory.tcl
new file mode 100644 (file)
index 0000000..2cde1d8
--- /dev/null
@@ -0,0 +1,120 @@
+start_server {tags {"maxmemory"}} {
+    foreach policy {
+        allkeys-random allkeys-lru volatile-lru volatile-random volatile-ttl
+    } {
+        test "maxmemory - is the memory limit honoured? (policy $policy)" {
+            # make sure to start with a blank instance
+            r flushall 
+            # Get the current memory limit and calculate a new limit.
+            # We just add 100k to the current memory size so that it is
+            # fast for us to reach that limit.
+            set used [s used_memory]
+            set limit [expr {$used+100*1024}]
+            r config set maxmemory $limit
+            r config set maxmemory-policy $policy
+            # Now add keys until the limit is almost reached.
+            set numkeys 0
+            while 1 {
+                r setex [randomKey] 10000 x
+                incr numkeys
+                if {[s used_memory]+4096 > $limit} {
+                    assert {$numkeys > 10}
+                    break
+                }
+            }
+            # If we add the same number of keys already added again, we
+            # should still be under the limit.
+            for {set j 0} {$j < $numkeys} {incr j} {
+                r setex [randomKey] 10000 x
+            }
+            assert {[s used_memory] < ($limit+4096)}
+        }
+    }
+
+    foreach policy {
+        allkeys-random allkeys-lru volatile-lru volatile-random volatile-ttl
+    } {
+        test "maxmemory - only allkeys-* should remove non-volatile keys ($policy)" {
+            # make sure to start with a blank instance
+            r flushall 
+            # Get the current memory limit and calculate a new limit.
+            # We just add 100k to the current memory size so that it is
+            # fast for us to reach that limit.
+            set used [s used_memory]
+            set limit [expr {$used+100*1024}]
+            r config set maxmemory $limit
+            r config set maxmemory-policy $policy
+            # Now add keys until the limit is almost reached.
+            set numkeys 0
+            while 1 {
+                r set [randomKey] x
+                incr numkeys
+                if {[s used_memory]+4096 > $limit} {
+                    assert {$numkeys > 10}
+                    break
+                }
+            }
+            # If we add the same number of keys already added again and
+            # the policy is allkeys-* we should still be under the limit.
+            # Otherwise we should see an error reported by Redis.
+            set err 0
+            for {set j 0} {$j < $numkeys} {incr j} {
+                if {[catch {r set [randomKey] x} e]} {
+                    if {[string match {*used memory*} $e]} {
+                        set err 1
+                    }
+                }
+            }
+            if {[string match allkeys-* $policy]} {
+                assert {[s used_memory] < ($limit+4096)}
+            } else {
+                assert {$err == 1}
+            }
+        }
+    }
+
+    foreach policy {
+        volatile-lru volatile-random volatile-ttl
+    } {
+        test "maxmemory - policy $policy should only remove volatile keys." {
+            # make sure to start with a blank instance
+            r flushall 
+            # Get the current memory limit and calculate a new limit.
+            # We just add 100k to the current memory size so that it is
+            # fast for us to reach that limit.
+            set used [s used_memory]
+            set limit [expr {$used+100*1024}]
+            r config set maxmemory $limit
+            r config set maxmemory-policy $policy
+            # Now add keys until the limit is almost reached.
+            set numkeys 0
+            while 1 {
+                # Odd keys are volatile
+                # Even keys are non volatile
+                if {$numkeys % 2} {
+                    r setex "key:$numkeys" 10000 x
+                } else {
+                    r set "key:$numkeys" x
+                }
+                if {[s used_memory]+4096 > $limit} {
+                    assert {$numkeys > 10}
+                    break
+                }
+                incr numkeys
+            }
+            # Now we add the same number of volatile keys already added.
+            # We expect Redis to evict only volatile keys in order to make
+            # space.
+            set err 0
+            for {set j 0} {$j < $numkeys} {incr j} {
+                catch {r setex "foo:$j" 10000 x}
+            }
+            # We should still be under the limit.
+            assert {[s used_memory] < ($limit+4096)}
+            # However all our non volatile keys should be here.
+            for {set j 0} {$j < $numkeys} {incr j 2} {
+                assert {[r exists "key:$j"]}
+            }
+        }
+    }
+}