File: test_hashring.py

package info (click to toggle)
django-redis 5.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 476 kB
  • sloc: python: 2,904; makefile: 6; sh: 6
file content (34 lines) | stat: -rw-r--r-- 668 bytes parent folder | download | duplicates (2)
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
import pytest

from django_redis.hash_ring import HashRing


class Node:
    def __init__(self, id):
        self.id = id

    def __str__(self):
        return f"node:{self.id}"

    def __repr__(self):
        return f"<Node {self.id}>"


@pytest.fixture
def hash_ring():
    return HashRing([Node(i) for i in range(3)])


def test_hashring(hash_ring):
    ids = []

    for key in [f"test{x}" for x in range(10)]:
        node = hash_ring.get_node(key)
        ids.append(node.id)

    assert ids == [0, 2, 1, 2, 2, 2, 2, 0, 1, 1]


def test_hashring_brute_force(hash_ring):
    for key in (f"test{x}" for x in range(10000)):
        assert hash_ring.get_node(key)