|
| 1 | +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"). |
| 4 | +# You may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import pytest |
| 16 | + |
| 17 | +from aws_advanced_python_wrapper.host_availability import HostAvailability |
| 18 | +from aws_advanced_python_wrapper.host_selector import HighestWeightHostSelector |
| 19 | +from aws_advanced_python_wrapper.hostinfo import HostInfo, HostRole |
| 20 | +from aws_advanced_python_wrapper.utils.properties import Properties |
| 21 | + |
| 22 | +HOST_ROLE = HostRole.READER |
| 23 | + |
| 24 | + |
| 25 | +@pytest.mark.parametrize("execution_number", range(50)) |
| 26 | +def test_get_host_given_unavailable_host(execution_number): |
| 27 | + unavailable_host: HostInfo = HostInfo(host="some_unavailable_host", role=HOST_ROLE, availability=HostAvailability.UNAVAILABLE) |
| 28 | + available_host: HostInfo = HostInfo(host="some_available_host", role=HOST_ROLE, availability=HostAvailability.AVAILABLE) |
| 29 | + |
| 30 | + host_selector = HighestWeightHostSelector() |
| 31 | + actual_host = host_selector.get_host((unavailable_host, available_host), HOST_ROLE, Properties()) |
| 32 | + |
| 33 | + assert available_host == actual_host |
| 34 | + |
| 35 | + |
| 36 | +@pytest.mark.parametrize("execution_number", range(50)) |
| 37 | +def test_get_host_given_multiple_unavailable_hosts(execution_number): |
| 38 | + hosts = ( |
| 39 | + HostInfo(host="some_unavailable_host", role=HOST_ROLE, availability=HostAvailability.UNAVAILABLE), |
| 40 | + HostInfo(host="some_unavailable_host", role=HOST_ROLE, availability=HostAvailability.UNAVAILABLE), |
| 41 | + HostInfo(host="some_available_host", role=HOST_ROLE, availability=HostAvailability.AVAILABLE) |
| 42 | + ) |
| 43 | + |
| 44 | + host_selector = HighestWeightHostSelector() |
| 45 | + actual_host = host_selector.get_host(hosts, HOST_ROLE, Properties()) |
| 46 | + |
| 47 | + assert HostAvailability.AVAILABLE == actual_host.get_availability() |
| 48 | + |
| 49 | + |
| 50 | +@pytest.mark.parametrize("execution_number", range(50)) |
| 51 | +def test_get_host_given_different_weights(execution_number): |
| 52 | + |
| 53 | + highest_weight_host = HostInfo(host="some_available_host", role=HOST_ROLE, availability=HostAvailability.AVAILABLE, weight=3) |
| 54 | + |
| 55 | + hosts = ( |
| 56 | + HostInfo(host="some_unavailable_host", role=HOST_ROLE, availability=HostAvailability.UNAVAILABLE), |
| 57 | + HostInfo(host="some_unavailable_host", role=HOST_ROLE, availability=HostAvailability.UNAVAILABLE), |
| 58 | + HostInfo(host="some_available_host", role=HOST_ROLE, availability=HostAvailability.AVAILABLE, weight=1), |
| 59 | + HostInfo(host="some_available_host", role=HOST_ROLE, availability=HostAvailability.AVAILABLE, weight=2), |
| 60 | + highest_weight_host |
| 61 | + ) |
| 62 | + |
| 63 | + host_selector = HighestWeightHostSelector() |
| 64 | + actual_host = host_selector.get_host(hosts, HOST_ROLE, Properties()) |
| 65 | + |
| 66 | + assert actual_host == highest_weight_host |
0 commit comments