Skip to content

Commit

Permalink
Add tests for RESP3 responce of ZINTER and ZRANGE (#8391)
Browse files Browse the repository at this point in the history
It was confusing as to why these don't return a map type.
the reason is that order matters, so we need to make sure the client
library knows to respect it.
Added comments in the implementation and tests to cover it.
  • Loading branch information
oranagra committed Jan 26, 2021
1 parent 1aad55b commit 9e56d39
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/t_zset.c
Original file line number Diff line number Diff line change
Expand Up @@ -2751,6 +2751,9 @@ void zunionInterDiffGenericCommand(client *c, robj *dstkey, int numkeysIndex, in
unsigned long length = dstzset->zsl->length;
zskiplist *zsl = dstzset->zsl;
zskiplistNode *zn = zsl->header->level[0].forward;
/* In case of WITHSCORES, respond with a single array in RESP2, and
* nested arrays in RESP3. We can't use a map response type since the
* client library needs to know to respect the order. */
if (withscores && c->resp == 2)
addReplyArrayLen(c, length*2);
else
Expand Down Expand Up @@ -2868,6 +2871,9 @@ static void zrangeResultEmitLongLongToClient(zrange_result_handler *handler,
static void zrangeResultFinalizeClient(zrange_result_handler *handler,
size_t result_count)
{
/* In case of WITHSCORES, respond with a single array in RESP2, and
* nested arrays in RESP3. We can't use a map response type since the
* client library needs to know to respect the order. */
if (handler->withscores && (handler->client->resp == 2)) {
result_count *= 2;
}
Expand Down
1 change: 1 addition & 0 deletions tests/support/redis.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ proc ::redis::redis_read_reply {id fd} {
_ {redis_read_null $fd}
: -
+ {redis_read_line $fd}
, {expr {double([redis_read_line $fd])}}
- {return -code error [redis_read_line $fd]}
$ {redis_bulk_read $fd}
> -
Expand Down
12 changes: 12 additions & 0 deletions tests/unit/type/zset.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,12 @@ start_server {tags {"zset"}} {
assert_equal {b 3 c 5} [r zinter 2 zseta zsetb withscores]
}

test "ZINTER RESP3 - $encoding" {
r hello 3
assert_equal {{b 3.0} {c 5.0}} [r zinter 2 zseta zsetb withscores]
}
r hello 2

test "ZINTERSTORE with weights - $encoding" {
assert_equal 2 [r zinterstore zsetc 2 zseta zsetb weights 2 3]
assert_equal {b 7 c 12} [r zrange zsetc 0 -1 withscores]
Expand Down Expand Up @@ -1481,6 +1487,12 @@ start_server {tags {"zset"}} {
r zrange z2 0 -1 withscores
} {a 1 b 2 c 3 d 4}

test {ZRANGESTORE RESP3} {
r hello 3
r zrange z2 0 -1 withscores
} {{a 1.0} {b 2.0} {c 3.0} {d 4.0}}
r hello 2

test {ZRANGESTORE range} {
set res [r zrangestore z2 z1 1 2]
assert_equal $res 2
Expand Down

0 comments on commit 9e56d39

Please sign in to comment.