|
| 1 | +from unittest.mock import patch |
| 2 | + |
1 | 3 | from hashring.hashring import HashRing |
2 | 4 |
|
3 | 5 | ONE_NODE_LIST = ['localhost:2020'] |
@@ -60,3 +62,25 @@ def test_keys_do_not_change_if_add_existing_node(): |
60 | 62 | old_keys = ring.keys.copy() |
61 | 63 | ring.add_node(ONE_NODE_LIST[0]) |
62 | 64 | assert old_keys.items() == ring.keys.items() |
| 65 | + |
| 66 | + |
| 67 | +@patch('hashring.hashring.hash_to_32bit_int', lambda x: 150) |
| 68 | +def test_found_node_for_str_is_node_with_nearest_from_left_key_for_str_hash(): |
| 69 | + ring = HashRing(TWO_NODE_LIST, 0, 0) |
| 70 | + ring.keys[100] = 'localhost:2020' |
| 71 | + ring.keys[200] = 'localhost:2021' |
| 72 | + ring.keys[10000] = 'localhost:2020' |
| 73 | + ring.keys[100000] = 'localhost:2021' |
| 74 | + key = 'makaka' |
| 75 | + found_node = ring.find_node_for_string(key) |
| 76 | + assert found_node == 'localhost:2020' |
| 77 | + |
| 78 | + |
| 79 | +@patch('hashring.hashring.hash_to_32bit_int', lambda x: 99) |
| 80 | +def test_node_for_str_is_with_max_key_if_str_hash_less_than_all_keys(): |
| 81 | + ring = HashRing(TWO_NODE_LIST, 0, 0) |
| 82 | + ring.keys[100] = 'localhost:2020' |
| 83 | + ring.keys[200] = 'localhost:2021' |
| 84 | + key = 'makaka' |
| 85 | + found_node = ring.find_node_for_string(key) |
| 86 | + assert found_node == 'localhost:2021' |
0 commit comments