summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorPieter Noordhuis <pcnoordhuis@gmail.com>2011-06-13 21:06:44 +0200
committerantirez <antirez@gmail.com>2011-06-14 16:59:50 +0200
commiteb48b107383c98ff547c810ca67ac360cf077064 (patch)
tree9e24fa1dd62d04f6baddad6dafc0ae61e891813d /tests
parent307868e1496f31ad7513181b83086229de5848cc (diff)
downloadredis-eb48b107383c98ff547c810ca67ac360cf077064.tar.gz
SETNX regression tests.
Diffstat (limited to 'tests')
-rw-r--r--tests/unit/basic.tcl49
1 files changed, 38 insertions, 11 deletions
diff --git a/tests/unit/basic.tcl b/tests/unit/basic.tcl
index 5098b0109..a7267fd8f 100644
--- a/tests/unit/basic.tcl
+++ b/tests/unit/basic.tcl
@@ -138,21 +138,48 @@ start_server {tags {"basic"}} {
r decrby novar 17179869185
} {-1}
- test {SETNX target key missing} {
- r setnx novar2 foobared
- r get novar2
- } {foobared}
+ test "SETNX target key missing" {
+ r del novar
+ assert_equal 1 [r setnx novar foobared]
+ assert_equal "foobared" [r get novar]
+ }
- test {SETNX target key exists} {
- r setnx novar2 blabla
- r get novar2
- } {foobared}
+ test "SETNX target key exists" {
+ r set novar foobared
+ assert_equal 0 [r setnx novar blabla]
+ assert_equal "foobared" [r get novar]
+ }
- test {SETNX against volatile key} {
+ test "SETNX against not-expired volatile key" {
r set x 10
r expire x 10000
- list [r setnx x 20] [r get x]
- } {0 10}
+ assert_equal 0 [r setnx x 20]
+ assert_equal 10 [r get x]
+ }
+
+ test "SETNX against expired volatile key" {
+ # Make it very unlikely for the key this test uses to be expired by the
+ # active expiry cycle. This is tightly coupled to the implementation of
+ # active expiry and dbAdd() but currently the only way to test that
+ # SETNX expires a key when it should have been.
+ for {set x 0} {$x < 9999} {incr x} {
+ r setex key-$x 3600 value
+ }
+
+ # This will be one of 10000 expiring keys. A cycle is executed every
+ # 100ms, sampling 10 keys for being expired or not. This key will be
+ # expired for at most 1s when we wait 2s, resulting in a total sample
+ # of 100 keys. The probability of the success of this test being a
+ # false positive is therefore approx. 1%.
+ r set x 10
+ r expire x 1
+
+ # Wait for the key to expire
+ after 2000
+
+ assert_equal 1 [r setnx x 20]
+ assert_equal 20 [r get x]
+ }
test {EXISTS} {
set res {}