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 <oran@redislabs.com>
This commit is contained in:
Binbin 2021-07-05 15:41:57 +08:00 committed by GitHub
parent 5a3de81925
commit a418a2d3fc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 47 additions and 4 deletions

View File

@ -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 [<count> WITHVALUES] */
/* HRANDFIELD key [<count> [WITHVALUES]] */
void hrandfieldCommand(client *c) {
long l;
int withvalues = 0;

View File

@ -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 [<count> WITHSCORES] */
/* ZRANDMEMBER key [<count> [WITHSCORES]] */
void zrandmemberCommand(client *c) {
long l;
int withscores = 0;

View File

@ -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 <count> 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}} " {

View File

@ -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 <count> 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 <count> 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

View File

@ -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 <count> 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} " {