-
Notifications
You must be signed in to change notification settings - Fork 12
/
tests.py
79 lines (60 loc) · 1.95 KB
/
tests.py
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import socket
from unittest.mock import patch
import pytest
from redis import Redis
from redis_hashring import RingNode
TEST_KEY = "hashring-test"
@pytest.fixture
def redis():
redis = Redis()
yield redis
redis.delete(TEST_KEY)
def get_node(redis, n_replicas, total_replicas):
node = RingNode(redis, TEST_KEY, n_replicas=n_replicas)
assert len(node._replicas) == n_replicas
assert redis.zcard(TEST_KEY) == total_replicas - n_replicas
node.heartbeat()
assert redis.zcard(TEST_KEY) == total_replicas
assert len(node.get_ranges()) == 0
return node
def test_node(redis):
with patch.object(socket, "gethostname", return_value="host1"):
node1 = get_node(redis, 1, 1)
node1.update()
assert len(node1.get_ranges()) == 1
assert node1.get_node_count() == 1
with patch.object(socket, "gethostname", return_value="host2"):
node2 = get_node(redis, 1, 2)
node1.update()
node2.update()
assert len(node1.get_ranges()) + len(node2.get_ranges()) == 3
assert node1.get_node_count() == 2
assert node2.get_node_count() == 2
with patch.object(socket, "gethostname", return_value="host3"):
node3 = get_node(redis, 2, 4)
node1.update()
node2.update()
node3.update()
assert (
len(node1.get_ranges())
+ len(node2.get_ranges())
+ len(node3.get_ranges())
== 5
)
assert node1.get_node_count() == 3
assert node2.get_node_count() == 3
assert node3.get_node_count() == 3
node1.remove()
node2.update()
node3.update()
assert len(node1.get_ranges()) == 0
assert node1.get_node_count() == 0
assert len(node2.get_ranges()) + len(node3.get_ranges()) == 4
assert node2.get_node_count() == 2
assert node3.get_node_count() == 2
def test_contains(redis):
node1 = get_node(redis, 1, 1)
node1.update()
assert node1.contains("item") is True
node1.remove()
assert node1.contains("item") is False