File: test_implementation.py

package info (click to toggle)
python-uhashring 1.2-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 156 kB
  • sloc: python: 668; makefile: 21
file content (240 lines) | stat: -rw-r--r-- 6,274 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
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# -*- coding: utf-8 -*-
"""
"""
import sys
import types
from collections import Counter
from uuid import uuid4

import pytest

from uhashring import HashRing

PY3 = sys.version_info >= (3,)


@pytest.fixture
def ring():
    ring = HashRing(nodes={"node1": 1, "node2": 1, "node3": 1}, hash_fn="ketama")
    return ring


@pytest.fixture
def ring_fast():
    ring = HashRing(nodes={"node1": 1, "node2": 1, "node3": 1}, compat=False)
    return ring


def weight_fn(**conf):
    print(conf)
    return int(conf["nodename"][-1])


def test_ring_size(ring, ring_fast):
    assert ring.size == len(ring.get_nodes()) * 160
    assert ring.size == len(ring._keys)
    assert ring.size == len(ring._ring)
    assert ring.size == len(ring.get_points())

    assert ring_fast.size == len(ring_fast.get_nodes()) * 160
    assert ring_fast.size == len(ring_fast._keys)
    assert ring_fast.size == len(ring_fast._ring)
    assert ring_fast.size == len(ring_fast.get_points())

    assert ring.size == ring_fast.size


def test_consistency(ring):
    coconut_node = ring.get("coconut")
    zoro_node = ring.get("zoro")

    del ring["node3"]

    with pytest.raises(KeyError):
        del ring["node3"]

    assert coconut_node == ring.get("coconut")
    assert zoro_node != ring.get("zoro")

    ring.add_node("node3", 1)

    assert zoro_node == ring.get("zoro")


def test_aliases(ring):
    assert ring.add_node == ring.__setitem__
    assert ring.get_node_instance == ring.__getitem__
    assert ring.remove_node == ring.__delitem__
    assert ring.continuum == ring.ring
    assert ring.nodes == ring.conf


def test_ketama_ring_hashi(ring):
    h = ring.hashi("test")
    assert h == 3446378249 == ring.get_key("test")


def test_faster_ring_hashi(ring_fast):
    h = ring_fast.hashi("test")
    assert h == 12707736894140473154801792860916528374


# XXX https://docs.python.org/3/whatsnew/3.0.html#ordering-comparisons
@pytest.mark.skipif(PY3, reason="requires python26, python27")
def test_range(ring):
    r = list(ring.range("test"))
    r.sort()
    c = ring.conf.values()
    c.sort()
    assert r == c

    r = list(ring.range("test", size=2, unique=True))
    assert len(r) == 2

    r = list(ring.range("test", size=2, unique=False))
    assert len(r) == 2

    r = list(ring.range("1800"))  # 1800 is at position 0
    r = list(ring.range("849"))  # 849 is at position -2

    r = list(ring.range("test", size=None, unique=False))
    assert len(r) == ring.size

    n = []
    for node in ring.iterate_nodes("test"):
        n.append(node)
    assert len(n) == 3

    ring_empty = HashRing()
    for node in ring_empty.iterate_nodes("test"):
        assert node is None


def test_conf():
    ring_1 = HashRing({"node1": 1})
    ring_2 = HashRing("node1")
    ring_3 = HashRing(["node1"])
    ring_4 = HashRing({"node1": {"weight": 1}})

    assert ring_1.ring == ring_2.ring == ring_3.ring == ring_4.ring

    with pytest.raises(ValueError):
        HashRing({"node1": "fail"})

    with pytest.raises(ValueError):
        HashRing(None)


def test_empty_ring():
    ring = HashRing()
    assert ring.get("test") is None


def test_methods_return_types(ring):
    assert isinstance(ring["test"], type(None))
    assert isinstance(ring.get("test"), dict)
    assert isinstance(ring.get_instances(), list)
    assert isinstance(ring.get_node("test"), str)
    assert isinstance(ring.get_nodes(), type({}.keys()) if PY3 else list)
    assert isinstance(ring.get_node_hostname("test"), str)
    assert isinstance(ring.get_node_port("test"), type(None))
    assert isinstance(ring.get_node_pos("test"), int)
    assert isinstance(ring.get_node_weight("test"), int)
    assert isinstance(ring.get_points(), list)
    assert isinstance(ring.get_server("test"), tuple)
    assert isinstance(ring.iterate_nodes("test"), types.GeneratorType)
    assert isinstance(ring.range("test"), types.GeneratorType)
    assert isinstance(ring.distribution, Counter)


def test_print_without_error(ring):
    assert ring.print_continuum() is None
    ring = HashRing()
    assert ring.print_continuum() is None


def test_with_non_str_objects(ring):
    uid = uuid4()
    ring.get_node(uid)


def test_weight_fn():
    ring = HashRing(
        nodes={"node1": 1, "node2": 1, "node3": 1},
        replicas=4,
        vnodes=40,
        hash_fn="ketama",
        weight_fn=weight_fn,
    )

    assert ring.distribution["node1"] == 80
    assert ring.distribution["node2"] == 160
    assert ring.distribution["node3"] == 240

    ring.regenerate

    assert ring.distribution["node1"] == 80
    assert ring.distribution["node2"] == 160
    assert ring.distribution["node3"] == 240

    with pytest.raises(TypeError):
        ring = HashRing(
            nodes={"node1": 1, "node2": 1, "node3": 1},
            replicas=4,
            vnodes=40,
            hash_fn="ketama",
            weight_fn=12,
        )

    with pytest.raises(TypeError):
        ring = HashRing(
            nodes={"node1": 1, "node2": 1, "node3": 1},
            replicas=4,
            vnodes=40,
            hash_fn="ketama",
            weight_fn="coconut",
        )


def test_ring_growth_ketama(ring):
    add_ring = HashRing(hash_fn="ketama")
    for nodename in ring.nodes:
        add_ring.add_node(nodename)

    assert ring._nodes == add_ring._nodes
    assert ring.ring == add_ring.ring
    assert ring.distribution == add_ring.distribution


def test_ring_growth_meta(ring_fast):
    add_ring = HashRing(compat=False)
    for nodename in ring_fast.nodes:
        add_ring.add_node(nodename)

    assert ring_fast._nodes == add_ring._nodes
    assert ring_fast.ring == add_ring.ring
    assert ring_fast.distribution == add_ring.distribution


def test_ketama_ring_shrink_collision():
    """
    see issue #6 thanks to @bjhockley
    """
    nodes = ["172.31.1.0", "172.31.1.125", "172.31.1.202"]
    ring = HashRing(nodes, hash_fn="ketama")
    ring.remove_node(nodes[1])
    ring.remove_node(nodes[2])
    ring.remove_node(nodes[0])
    assert ring.ring == {}


def test_hash_fn():
    """
    """

    def hash_fn(k):
        return k + "_hash"

    nodes = ["172.31.1.0", "172.31.1.125", "172.31.1.202"]
    ring = HashRing(nodes, hash_fn=hash_fn)
    assert ring.hashi("coconut") == "coconut_hash"