|
2 | 2 |
|
3 | 3 | import pytest |
4 | 4 |
|
5 | | -from dflockd_client.sharding import DEFAULT_SERVERS, stable_hash_shard |
| 5 | +from dflockd_client.sharding import ( |
| 6 | + DEFAULT_SERVERS, |
| 7 | + _validate_server_endpoint, |
| 8 | + _validate_servers, |
| 9 | + stable_hash_shard, |
| 10 | +) |
6 | 11 |
|
7 | 12 |
|
8 | 13 | class TestStableHashShard: |
@@ -31,3 +36,43 @@ def test_distribution(self): |
31 | 36 |
|
32 | 37 | def test_default_servers(): |
33 | 38 | assert DEFAULT_SERVERS == (("127.0.0.1", 6388),) |
| 39 | + |
| 40 | + |
| 41 | +class TestValidateServerEndpoint: |
| 42 | + def test_valid(self): |
| 43 | + assert _validate_server_endpoint("127.0.0.1", 6388) == ("127.0.0.1", 6388) |
| 44 | + |
| 45 | + @pytest.mark.parametrize("host", ["", "bad host", "bad\nhost"]) |
| 46 | + def test_rejects_bad_host(self, host): |
| 47 | + with pytest.raises(ValueError): |
| 48 | + _validate_server_endpoint(host, 6388) |
| 49 | + |
| 50 | + def test_rejects_non_string_host(self): |
| 51 | + with pytest.raises(TypeError): |
| 52 | + _validate_server_endpoint(127001, 6388) |
| 53 | + |
| 54 | + @pytest.mark.parametrize("port", [0, -1, 65536]) |
| 55 | + def test_rejects_out_of_range_port(self, port): |
| 56 | + with pytest.raises(ValueError): |
| 57 | + _validate_server_endpoint("127.0.0.1", port) |
| 58 | + |
| 59 | + @pytest.mark.parametrize("port", [True, "6388"]) |
| 60 | + def test_rejects_non_int_port(self, port): |
| 61 | + with pytest.raises(TypeError): |
| 62 | + _validate_server_endpoint("127.0.0.1", port) |
| 63 | + |
| 64 | + |
| 65 | +class TestValidateServers: |
| 66 | + def test_valid_list(self): |
| 67 | + _validate_servers([("127.0.0.1", 6388)]) |
| 68 | + |
| 69 | + def test_valid_tuple(self): |
| 70 | + _validate_servers((("127.0.0.1", 6388),)) |
| 71 | + |
| 72 | + def test_rejects_empty(self): |
| 73 | + with pytest.raises(ValueError, match="non-empty"): |
| 74 | + _validate_servers([]) |
| 75 | + |
| 76 | + def test_rejects_single_endpoint_tuple(self): |
| 77 | + with pytest.raises(TypeError, match="\\(host, port\\)"): |
| 78 | + _validate_servers(("127.0.0.1", 6388)) |
0 commit comments