Skip to content

Fix return value of HRANDFIELD With Values when count is negative #3017

@BalaKaza

Description

@BalaKaza

### Expected behavior

hrandfieldWithValues(String key, long count) should return the count equivalent entries in case of a negative (absolute value of count) or a positive count value.

Actual behavior

hrandfieldWithValues(String key, long count) currently returns a Map<String, String> which does not account for the duplicates that can be returned(https://redis.io/commands/hrandfield/) when a negative count is given. This results in an inaccurate number (less than the count) of random entries being returned when there is a negative count.

Steps to reproduce:

  1. Create a Hash Set with size 5
  2. Call hrandfieldWithValues(String key, long count) with a count of -3.
  3. Assert for the size of the result.
Map<String, String> hash = createHash(5); 
jedis.hset(KEY, hash); 
long count = -3L; 
Map<String, String> result = jedis.hrandfieldWithValues(KEY, count); 
assertThat(result.size()).isEqualTo(Math.abs(count));

Redis / Jedis Configuration

Jedis version: 3.7.0

Redis version: 6.2.0

Java version: 8

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions