From a418a2d3fc0250c094802d7e8ea64d96eedfda07 Mon Sep 17 00:00:00 2001 From: Binbin Date: Mon, 5 Jul 2021 15:41:57 +0800 Subject: [PATCH] hrandfield and zrandmember with count should return emptyarray when key does not exist. (#9178) due to a copy-paste bug, it used to reply with null response rather than empty array. this commit includes new tests that are looking at the RESP response directly in order to be able to tell the difference between them. Co-authored-by: Oran Agra --- src/t_hash.c | 4 ++-- src/t_zset.c | 4 ++-- tests/unit/type/hash.tcl | 13 +++++++++++++ tests/unit/type/set.tcl | 17 +++++++++++++++++ tests/unit/type/zset.tcl | 13 +++++++++++++ 5 files changed, 47 insertions(+), 4 deletions(-) diff --git a/src/t_hash.c b/src/t_hash.c index 865145807..7a1215b13 100644 --- a/src/t_hash.c +++ b/src/t_hash.c @@ -989,7 +989,7 @@ void hrandfieldWithCountCommand(client *c, long l, int withvalues) { int uniq = 1; robj *hash; - if ((hash = lookupKeyReadOrReply(c,c->argv[1],shared.null[c->resp])) + if ((hash = lookupKeyReadOrReply(c,c->argv[1],shared.emptyarray)) == NULL || checkType(c,hash,OBJ_HASH)) return; size = hashTypeLength(hash); @@ -1178,7 +1178,7 @@ void hrandfieldWithCountCommand(client *c, long l, int withvalues) { } } -/* HRANDFIELD [ WITHVALUES] */ +/* HRANDFIELD key [ [WITHVALUES]] */ void hrandfieldCommand(client *c) { long l; int withvalues = 0; diff --git a/src/t_zset.c b/src/t_zset.c index 51a14989d..28f1df05e 100644 --- a/src/t_zset.c +++ b/src/t_zset.c @@ -4000,7 +4000,7 @@ void zrandmemberWithCountCommand(client *c, long l, int withscores) { int uniq = 1; robj *zsetobj; - if ((zsetobj = lookupKeyReadOrReply(c, c->argv[1], shared.null[c->resp])) + if ((zsetobj = lookupKeyReadOrReply(c, c->argv[1], shared.emptyarray)) == NULL || checkType(c, zsetobj, OBJ_ZSET)) return; size = zsetLength(zsetobj); @@ -4188,7 +4188,7 @@ void zrandmemberWithCountCommand(client *c, long l, int withscores) { zuiClearIterator(&src); } -/* ZRANDMEMBER [ WITHSCORES] */ +/* ZRANDMEMBER key [ [WITHSCORES]] */ void zrandmemberCommand(client *c) { long l; int withscores = 0; diff --git a/tests/unit/type/hash.tcl b/tests/unit/type/hash.tcl index 97c4c7d09..5f00a732e 100644 --- a/tests/unit/type/hash.tcl +++ b/tests/unit/type/hash.tcl @@ -72,6 +72,19 @@ start_server {tags {"hash"}} { r hrandfield nonexisting_key 100 } {} + # Make sure we can distinguish between an empty array and a null response + r readraw 1 + + test "HRANDFIELD count of 0 is handled correctly - emptyarray" { + r hrandfield myhash 0 + } {*0} + + test "HRANDFIELD with against non existing key - emptyarray" { + r hrandfield nonexisting_key 100 + } {*0} + + r readraw 0 + foreach {type contents} " hashtable {{a 1} {b 2} {c 3} {d 4} {e 5} {6 f} {7 g} {8 h} {9 i} {[randstring 70 90 alpha] 10}} ziplist {{a 1} {b 2} {c 3} {d 4} {e 5} {6 f} {7 g} {8 h} {9 i} {10 j}} " { diff --git a/tests/unit/type/set.tcl b/tests/unit/type/set.tcl index 8524ffc33..a12eae5b3 100644 --- a/tests/unit/type/set.tcl +++ b/tests/unit/type/set.tcl @@ -582,10 +582,27 @@ start_server { assert {[lsort $union] eq [lsort $content]} } + test "SRANDMEMBER count of 0 is handled correctly" { + r srandmember myset 0 + } {} + test "SRANDMEMBER with against non existing key" { r srandmember nonexisting_key 100 } {} + # Make sure we can distinguish between an empty array and a null response + r readraw 1 + + test "SRANDMEMBER count of 0 is handled correctly - emptyarray" { + r srandmember myset 0 + } {*0} + + test "SRANDMEMBER with against non existing key - emptyarray" { + r srandmember nonexisting_key 100 + } {*0} + + r readraw 0 + foreach {type contents} { hashtable { 1 5 10 50 125 50000 33959417 4775547 65434162 diff --git a/tests/unit/type/zset.tcl b/tests/unit/type/zset.tcl index e1e7e7882..1f2f00576 100644 --- a/tests/unit/type/zset.tcl +++ b/tests/unit/type/zset.tcl @@ -1717,6 +1717,19 @@ start_server {tags {"zset"}} { r zrandmember nonexisting_key 100 } {} + # Make sure we can distinguish between an empty array and a null response + r readraw 1 + + test "ZRANDMEMBER count of 0 is handled correctly - emptyarray" { + r zrandmember myzset 0 + } {*0} + + test "ZRANDMEMBER with against non existing key - emptyarray" { + r zrandmember nonexisting_key 100 + } {*0} + + r readraw 0 + foreach {type contents} " skiplist {1 a 2 b 3 c 4 d 5 e 6 f 7 g 7 h 9 i 10 [randstring 70 90 alpha]} ziplist {1 a 2 b 3 c 4 d 5 e 6 f 7 g 7 h 9 i 10 j} " {