From c6b3966d02833968c0e1886a2ae98879e260834d 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 (cherry picked from commit a418a2d3fc0250c094802d7e8ea64d96eedfda07) --- 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 d88b80b0f..ea0606fb0 100644 --- a/src/t_hash.c +++ b/src/t_hash.c @@ -986,7 +986,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); @@ -1175,7 +1175,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 7a3394d47..c501518a3 100644 --- a/src/t_zset.c +++ b/src/t_zset.c @@ -3992,7 +3992,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); @@ -4177,7 +4177,7 @@ void zrandmemberWithCountCommand(client *c, long l, int withscores) { } } -/* 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 fcf97eed7..f2a503722 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 5548ca3a2..c5a4c687f 100644 --- a/tests/unit/type/set.tcl +++ b/tests/unit/type/set.tcl @@ -403,10 +403,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 554d9c2c1..004eaf8a3 100644 --- a/tests/unit/type/zset.tcl +++ b/tests/unit/type/zset.tcl @@ -1681,6 +1681,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} " {