]> git.saurik.com Git - redis.git/commitdiff
Added a new test able to stress a lot the snapshotting engine
authorantirez <antirez@gmail.com>
Tue, 15 Dec 2009 15:55:13 +0000 (10:55 -0500)
committerantirez <antirez@gmail.com>
Tue, 15 Dec 2009 15:55:13 +0000 (10:55 -0500)
redis.c
test-redis.tcl

diff --git a/redis.c b/redis.c
index f0b69edd946517b4485e22189c9dfaa5aafab341..b166d9d1be330d68426a46be5b64eaf4559f70e6 100644 (file)
--- a/redis.c
+++ b/redis.c
@@ -2034,6 +2034,7 @@ static void addReplyBulkLen(redisClient *c, robj *obj) {
     } else {
         long n = (long)obj->ptr;
 
+        /* Compute how many bytes will take this integer as a radix 10 string */
         len = 1;
         if (n < 0) {
             len++;
index 8568a60d27a10b3bf631d6116a6e194796ca08cd..f002dfa771056e9599e1e4871e647cd7485aea43 100644 (file)
@@ -65,6 +65,110 @@ proc waitForBgsave r {
     }
 }
 
+proc randomInt {max} {
+    expr {int(rand()*$max)}
+}
+
+proc randpath args {
+    set path [expr {int(rand()*[llength $args])}]
+    uplevel 1 [lindex $args $path]
+}
+
+proc randomValue {} {
+    randpath {
+        # Small enough to likely collide
+        randomInt 1000
+    } {
+        # 32 bit compressible signed/unsigned
+        randpath {randomInt 2000000000} {randomInt 4000000000}
+    } {
+        # 64 bit
+        randpath {randomInt 1000000000000}
+    } {
+        # Random string
+        randpath {randstring 0 256 alpha} \
+                {randstring 0 256 compr} \
+                {randstring 0 256 binary}
+    }
+}
+
+proc randomKey {} {
+    randpath {
+        # Small enough to likely collide
+        randomInt 1000
+    } {
+        # 32 bit compressible signed/unsigned
+        randpath {randomInt 2000000000} {randomInt 4000000000}
+    } {
+        # 64 bit
+        randpath {randomInt 1000000000000}
+    } {
+        # Random string
+        randpath {randstring 1 256 alpha} \
+                {randstring 1 256 compr}
+    }
+}
+
+proc createComplexDataset {r ops} {
+    for {set j 0} {$j < $ops} {incr j} {
+        set k [randomKey]
+        set v [randomValue]
+        set d [expr {rand()}]
+        set t [$r type $k]
+
+        if {$t eq {none}} {
+            randpath {
+                $r set $k $v
+            } {
+                $r lpush $k $v
+            } {
+                $r sadd $k $v
+            } {
+                $r zadd $k $d $v
+            }
+            set t [$r type $k]
+        }
+
+        switch $t {
+            {string} {
+                # Nothing to do
+            }
+            {list} {
+                randpath {$r lpush $k $v} \
+                        {$r rpush $k $v} \
+                        {$r lrem $k 0 $v} \
+                        {$r rpop $k} \
+                        {$r lpop $k}
+            }
+            {set} {
+                randpath {$r sadd $k $v} \
+                        {$r srem $k $v}
+            }
+            {zset} {
+                randpath {$r zadd $k $d $v} \
+                        {$r zrem $k $v}
+            }
+        }
+    }
+}
+
+proc datasetDigest r {
+    set keys [lsort [split [$r keys *] " "]]
+    set digest [::sha1::sha1 -hex $keys]
+    foreach k $keys {
+        set t [$r type $k]
+        switch t {
+            {string} {set aux [::sha1::sha1 -hex [$r get $k]]} \
+            {list} {set aux [::sha1::sha1 -hex [$r lrange $k 0 -1]]} \
+            {set} {set aux [::sha1::sha1 -hex [$r smembers $k]]} \
+            {zset} {set aux [::sha1::sha1 -hex [$r zrange $k 0 -1]]}
+        }
+        append aux $digest
+        set digest [::sha1::sha1 -hex $aux]
+    }
+    return $digest
+}
+
 proc main {server port} {
     set r [redis $server $port]
     $r select 9
@@ -1279,6 +1383,17 @@ proc main {server port} {
         set _ $err
     } {*invalid*}
 
+    if {![catch {package require sha1}]} {
+        test {Check consistency of different data types after a reload} {
+            $r flushdb
+            createComplexDataset $r 10000
+            set sha1 [datasetDigest $r]
+            $r debug reload
+            set sha1_after [datasetDigest $r]
+            expr {$sha1 eq $sha1_after}
+        } {1}
+    }
+
     # Leave the user with a clean DB before to exit
     test {FLUSHDB} {
         set aux {}