Files
Salvatore SanfilippoandGitHub 7e06329f6c Fix vrand ping pong (#14183)
VRANDMEMBER had a bug when exactly two elements where present in the
vector set: we selected a fixed number of random paths to take, and this
will lead always to the same element. This PR should be kindly
back-ported to Redis 8.x.
2025-07-18 18:19:14 +08:00

36 lines
1.3 KiB
Python

from test import TestCase, generate_random_vector
import struct
class VRANDMEMBERPingPongRegressionTest(TestCase):
def getname(self):
return "[regression] VRANDMEMBER ping-pong"
def test(self):
"""
This test ensures that when only two vectors exist, VRANDMEMBER
does not get stuck returning only one of them due to the "ping-pong" issue.
"""
self.redis.delete(self.test_key) # Clean up before test
dim = 4
# Add exactly two vectors
vec1_name = "vec1"
vec1_data = generate_random_vector(dim)
self.redis.execute_command('VADD', self.test_key, 'VALUES', dim, *vec1_data, vec1_name)
vec2_name = "vec2"
vec2_data = generate_random_vector(dim)
self.redis.execute_command('VADD', self.test_key, 'VALUES', dim, *vec2_data, vec2_name)
# Call VRANDMEMBER many times and check for distribution
iterations = 100
results = []
for _ in range(iterations):
member = self.redis.execute_command('VRANDMEMBER', self.test_key)
results.append(member.decode())
# Verify that both members were returned, proving it's not stuck
unique_results = set(results)
assert len(unique_results) == 2, f"Ping-pong test failed: should have returned 2 unique members, but got {len(unique_results)}."