File: vrand-ping-pong.py

package info (click to toggle)
redis 5%3A8.0.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 21,916 kB
  • sloc: ansic: 217,142; tcl: 51,874; sh: 4,625; perl: 4,214; cpp: 3,568; python: 3,165; makefile: 2,055; ruby: 639; javascript: 30; csh: 7
file content (35 lines) | stat: -rw-r--r-- 1,370 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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)}."