|  | 
|  | 1 | +from unittest import mock | 
|  | 2 | + | 
|  | 3 | +import pytest | 
|  | 4 | + | 
|  | 5 | +from kazoo import retry | 
|  | 6 | +from kazoo.handlers import threading | 
|  | 7 | +from kazoo.protocol import connection | 
|  | 8 | +from kazoo.protocol import states | 
|  | 9 | + | 
|  | 10 | + | 
|  | 11 | +@mock.patch("kazoo.protocol.connection.ConnectionHandler._expand_client_hosts") | 
|  | 12 | +def test_retry_logic(mock_expand): | 
|  | 13 | +    mock_client = mock.Mock() | 
|  | 14 | +    mock_client._state = states.KeeperState.CLOSED | 
|  | 15 | +    mock_client._session_id = None | 
|  | 16 | +    mock_client._session_passwd = b"\x00" * 16 | 
|  | 17 | +    mock_client._stopped.is_set.return_value = False | 
|  | 18 | +    mock_client.handler.timeout_exception = threading.KazooTimeoutError | 
|  | 19 | +    mock_client.handler.create_connection.side_effect = ( | 
|  | 20 | +        threading.KazooTimeoutError() | 
|  | 21 | +    ) | 
|  | 22 | +    test_retry = retry.KazooRetry( | 
|  | 23 | +        max_tries=6, | 
|  | 24 | +        delay=1.0, | 
|  | 25 | +        backoff=2, | 
|  | 26 | +        max_delay=30.0, | 
|  | 27 | +        max_jitter=0.0, | 
|  | 28 | +        sleep_func=lambda _x: None, | 
|  | 29 | +    ) | 
|  | 30 | +    test_cnx = connection.ConnectionHandler( | 
|  | 31 | +        client=mock_client, | 
|  | 32 | +        retry_sleeper=test_retry, | 
|  | 33 | +    ) | 
|  | 34 | +    mock_expand.return_value = [ | 
|  | 35 | +        ("a", "1.1.1.1", 2181), | 
|  | 36 | +        ("b", "2.2.2.2", 2181), | 
|  | 37 | +        ("c", "3.3.3.3", 2181), | 
|  | 38 | +    ] | 
|  | 39 | + | 
|  | 40 | +    with pytest.raises(retry.RetryFailedError): | 
|  | 41 | +        test_retry(test_cnx._connect_loop, test_retry) | 
|  | 42 | + | 
|  | 43 | +    assert mock_client.handler.create_connection.call_args_list[:3] == [ | 
|  | 44 | +        mock.call( | 
|  | 45 | +            address=("1.1.1.1", 2181), | 
|  | 46 | +            timeout=1.0, | 
|  | 47 | +            use_ssl=mock.ANY, | 
|  | 48 | +            keyfile=mock.ANY, | 
|  | 49 | +            certfile=mock.ANY, | 
|  | 50 | +            ca=mock.ANY, | 
|  | 51 | +            keyfile_password=mock.ANY, | 
|  | 52 | +            verify_certs=mock.ANY, | 
|  | 53 | +        ), | 
|  | 54 | +        mock.call( | 
|  | 55 | +            address=("2.2.2.2", 2181), | 
|  | 56 | +            timeout=1.0, | 
|  | 57 | +            use_ssl=mock.ANY, | 
|  | 58 | +            keyfile=mock.ANY, | 
|  | 59 | +            certfile=mock.ANY, | 
|  | 60 | +            ca=mock.ANY, | 
|  | 61 | +            keyfile_password=mock.ANY, | 
|  | 62 | +            verify_certs=mock.ANY, | 
|  | 63 | +        ), | 
|  | 64 | +        mock.call( | 
|  | 65 | +            address=("3.3.3.3", 2181), | 
|  | 66 | +            timeout=1.0, | 
|  | 67 | +            use_ssl=mock.ANY, | 
|  | 68 | +            keyfile=mock.ANY, | 
|  | 69 | +            certfile=mock.ANY, | 
|  | 70 | +            ca=mock.ANY, | 
|  | 71 | +            keyfile_password=mock.ANY, | 
|  | 72 | +            verify_certs=mock.ANY, | 
|  | 73 | +        ), | 
|  | 74 | +    ], "All hosts are first tried with the lowest timeout value" | 
|  | 75 | +    assert mock_client.handler.create_connection.call_args_list[-3:] == [ | 
|  | 76 | +        mock.call( | 
|  | 77 | +            address=("1.1.1.1", 2181), | 
|  | 78 | +            timeout=30.0, | 
|  | 79 | +            use_ssl=mock.ANY, | 
|  | 80 | +            keyfile=mock.ANY, | 
|  | 81 | +            certfile=mock.ANY, | 
|  | 82 | +            ca=mock.ANY, | 
|  | 83 | +            keyfile_password=mock.ANY, | 
|  | 84 | +            verify_certs=mock.ANY, | 
|  | 85 | +        ), | 
|  | 86 | +        mock.call( | 
|  | 87 | +            address=("2.2.2.2", 2181), | 
|  | 88 | +            timeout=30.0, | 
|  | 89 | +            use_ssl=mock.ANY, | 
|  | 90 | +            keyfile=mock.ANY, | 
|  | 91 | +            certfile=mock.ANY, | 
|  | 92 | +            ca=mock.ANY, | 
|  | 93 | +            keyfile_password=mock.ANY, | 
|  | 94 | +            verify_certs=mock.ANY, | 
|  | 95 | +        ), | 
|  | 96 | +        mock.call( | 
|  | 97 | +            address=("3.3.3.3", 2181), | 
|  | 98 | +            timeout=30.0, | 
|  | 99 | +            use_ssl=mock.ANY, | 
|  | 100 | +            keyfile=mock.ANY, | 
|  | 101 | +            certfile=mock.ANY, | 
|  | 102 | +            ca=mock.ANY, | 
|  | 103 | +            keyfile_password=mock.ANY, | 
|  | 104 | +            verify_certs=mock.ANY, | 
|  | 105 | +        ), | 
|  | 106 | +    ], "All hosts are last tried with the highest timeout value" | 
0 commit comments