-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathtest_resolver.py
85 lines (67 loc) · 2.49 KB
/
test_resolver.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
80
81
82
83
84
85
from typing import Set
import pytest
from arango.resolver import (
FallbackHostResolver,
PeriodicHostResolver,
RandomHostResolver,
RoundRobinHostResolver,
SingleHostResolver,
)
def test_bad_resolver():
with pytest.raises(ValueError):
RandomHostResolver(3, 2)
def test_resolver_single_host():
resolver = SingleHostResolver()
for _ in range(20):
assert resolver.get_host_index() == 0
def test_resolver_random_host():
resolver = RandomHostResolver(10)
for _ in range(20):
assert 0 <= resolver.get_host_index() < 10
resolver = RandomHostResolver(3)
indexes_to_filter: Set[int] = set()
index_a = resolver.get_host_index()
indexes_to_filter.add(index_a)
index_b = resolver.get_host_index(indexes_to_filter)
indexes_to_filter.add(index_b)
assert index_b != index_a
index_c = resolver.get_host_index(indexes_to_filter)
indexes_to_filter.clear()
indexes_to_filter.add(index_c)
assert index_c not in [index_a, index_b]
def test_resolver_round_robin():
resolver = RoundRobinHostResolver(10)
assert resolver.get_host_index() == 0
assert resolver.get_host_index() == 1
assert resolver.get_host_index() == 2
assert resolver.get_host_index() == 3
assert resolver.get_host_index() == 4
assert resolver.get_host_index() == 5
assert resolver.get_host_index() == 6
assert resolver.get_host_index() == 7
assert resolver.get_host_index() == 8
assert resolver.get_host_index() == 9
assert resolver.get_host_index() == 0
def test_resolver_periodic():
resolver = PeriodicHostResolver(3, requests_period=3)
assert resolver.get_host_index() == 0
assert resolver.get_host_index() == 0
assert resolver.get_host_index() == 1
assert resolver.get_host_index() == 1
assert resolver.get_host_index() == 1
assert resolver.get_host_index() == 2
assert resolver.get_host_index() == 2
assert resolver.get_host_index() == 2
assert resolver.get_host_index() == 0
assert resolver.get_host_index() == 0
assert resolver.get_host_index({0}) == 1
assert resolver.get_host_index() == 1
def test_resolver_fallback():
resolver = FallbackHostResolver(4)
assert resolver.get_host_index() == 0
assert resolver.get_host_index() == 0
assert resolver.get_host_index({0, 1, 3}) == 2
assert resolver.get_host_index({1, 2, 3}) == 0
assert resolver.get_host_index({0}) == 1
assert resolver.get_host_index({0}) == 1
assert resolver.get_host_index() == 1