|
| 1 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | + |
| 3 | +"""Unit tests for the management server-timeout. |
| 4 | +
|
| 5 | +Management operations send the `com.microsoft:server-timeout` property, asking the service to |
| 6 | +bound the operation on its side. The value is the caller's remaining time less a one second |
| 7 | +buffer, so the service answers before the client gives up, or 60 seconds when the caller |
| 8 | +supplied no timeout. |
| 9 | +
|
| 10 | +Previously no bound was sent at all, so a stalled service could hold a management call until |
| 11 | +the AMQP link itself failed. Matches the .NET, Java and Go SDKs. |
| 12 | +""" |
| 13 | + |
| 14 | +from unittest.mock import MagicMock |
| 15 | + |
| 16 | +import struct |
| 17 | + |
| 18 | +import pytest |
| 19 | + |
| 20 | +from azure.servicebus._common import mgmt_handlers |
| 21 | +from azure.servicebus._common.constants import ( |
| 22 | + ERROR_CODE_TIMEOUT, |
| 23 | + MAX_SERVER_TIMEOUT_MS, |
| 24 | + MGMT_RESPONSE_MESSAGE_ERROR_CONDITION, |
| 25 | + REQUEST_RESPONSE_TIMEOUT, |
| 26 | +) |
| 27 | +from azure.servicebus._common.utils import get_server_timeout_ms |
| 28 | +from azure.servicebus._transport._pyamqp_transport import PyamqpTransport |
| 29 | +from azure.servicebus.exceptions import OperationTimeoutError |
| 30 | + |
| 31 | + |
| 32 | +class TestServerTimeoutMillis: |
| 33 | + """`get_server_timeout_ms` converts the caller's remaining time into the |
| 34 | + value advertised to the service.""" |
| 35 | + |
| 36 | + @pytest.mark.parametrize( |
| 37 | + "remaining_seconds,expected_ms", |
| 38 | + [ |
| 39 | + (None, 60000), # no caller timeout: the default, not "no bound at all" |
| 40 | + (120, 119000), |
| 41 | + (60, 59000), # equals the default, but must still take the buffer path |
| 42 | + (10, 9000), |
| 43 | + (1.5, 500), |
| 44 | + (1, 0), # at the buffer, nothing left to give the service |
| 45 | + (0.5, 0), |
| 46 | + (-5, 0), # deadline already passed: clamped, never a negative uint |
| 47 | + ], |
| 48 | + ) |
| 49 | + def test_remaining_time_less_buffer(self, remaining_seconds, expected_ms): |
| 50 | + assert get_server_timeout_ms(remaining_seconds) == expected_ms |
| 51 | + |
| 52 | + def test_wire_contract(self): |
| 53 | + # The key .NET and Java send, encoded as an unsigned int of milliseconds. |
| 54 | + assert REQUEST_RESPONSE_TIMEOUT == b"com.microsoft:server-timeout" |
| 55 | + encoded = PyamqpTransport.AMQP_UINT_VALUE(get_server_timeout_ms(None)) |
| 56 | + assert encoded == {"TYPE": "UINT", "VALUE": 60000} |
| 57 | + |
| 58 | + @pytest.mark.parametrize("remaining_seconds", [4294968, 5_000_000, 1e12, float("inf")]) |
| 59 | + def test_capped_at_the_amqp_uint_maximum(self, remaining_seconds): |
| 60 | + # `timeout` is only bounded at zero, so a large value would overflow the uint encoder. |
| 61 | + result = get_server_timeout_ms(remaining_seconds) |
| 62 | + assert result <= MAX_SERVER_TIMEOUT_MS |
| 63 | + struct.pack(">I", result) # raises if it does not fit |
| 64 | + |
| 65 | + def test_just_below_the_cap_is_not_clamped(self): |
| 66 | + assert get_server_timeout_ms(4294967) == 4294966000 |
| 67 | + |
| 68 | + |
| 69 | +class TestManagementRequestSetsServerTimeout: |
| 70 | + """The property must actually reach the outgoing management message, on both |
| 71 | + the associated-link and no-associated-link paths.""" |
| 72 | + |
| 73 | + def _make_handler(self): |
| 74 | + from azure.servicebus._base_handler import BaseHandler |
| 75 | + |
| 76 | + captured = {} |
| 77 | + |
| 78 | + def fake_create_mgmt_msg(message, application_properties, config, reply_to, **kwargs): |
| 79 | + captured.clear() |
| 80 | + captured.update(application_properties) |
| 81 | + return MagicMock() |
| 82 | + |
| 83 | + handler = BaseHandler.__new__(BaseHandler) |
| 84 | + handler._amqp_transport = MagicMock() |
| 85 | + handler._amqp_transport.create_mgmt_msg = fake_create_mgmt_msg |
| 86 | + handler._amqp_transport.AMQP_UINT_VALUE = PyamqpTransport.AMQP_UINT_VALUE |
| 87 | + handler._amqp_transport.get_handler_link_name = lambda h: "link-1" |
| 88 | + handler._amqp_transport.mgmt_client_request = lambda *args, **kwargs: "response" |
| 89 | + handler._amqp_transport.TIMEOUT_ERROR = TimeoutError |
| 90 | + handler._open = lambda: None |
| 91 | + handler._handler = MagicMock() |
| 92 | + handler._config = MagicMock(encoding="UTF-8") |
| 93 | + handler._mgmt_target = "queue/$management" |
| 94 | + return handler, captured |
| 95 | + |
| 96 | + def test_default_sent_when_caller_gave_no_timeout(self): |
| 97 | + # The gap this closes: previously no bound was sent at all. |
| 98 | + handler, captured = self._make_handler() |
| 99 | + handler._mgmt_request_response(b"op", {}, lambda *a: None, timeout=None) |
| 100 | + assert captured[REQUEST_RESPONSE_TIMEOUT] == {"TYPE": "UINT", "VALUE": 60000} |
| 101 | + |
| 102 | + def test_remaining_time_less_buffer_sent(self): |
| 103 | + handler, captured = self._make_handler() |
| 104 | + handler._mgmt_request_response(b"op", {}, lambda *a: None, timeout=10) |
| 105 | + assert captured[REQUEST_RESPONSE_TIMEOUT] == {"TYPE": "UINT", "VALUE": 9000} |
| 106 | + |
| 107 | + def test_clamped_below_buffer(self): |
| 108 | + handler, captured = self._make_handler() |
| 109 | + handler._mgmt_request_response(b"op", {}, lambda *a: None, timeout=0.4) |
| 110 | + assert captured[REQUEST_RESPONSE_TIMEOUT] == {"TYPE": "UINT", "VALUE": 0} |
| 111 | + |
| 112 | + def test_associated_link_name_preserved(self): |
| 113 | + handler, captured = self._make_handler() |
| 114 | + handler._mgmt_request_response(b"op", {}, lambda *a: None, timeout=None) |
| 115 | + assert b"associated-link-name" in captured |
| 116 | + assert REQUEST_RESPONSE_TIMEOUT in captured |
| 117 | + |
| 118 | + def test_sent_on_calls_without_an_associated_link(self): |
| 119 | + # list_sessions passes keep_alive_associated_link=False, starting from an empty map. |
| 120 | + handler, captured = self._make_handler() |
| 121 | + handler._mgmt_request_response( |
| 122 | + b"op", |
| 123 | + {}, |
| 124 | + lambda *a: None, |
| 125 | + keep_alive_associated_link=False, |
| 126 | + timeout=None, |
| 127 | + ) |
| 128 | + assert captured == {REQUEST_RESPONSE_TIMEOUT: {"TYPE": "UINT", "VALUE": 60000}} |
| 129 | + |
| 130 | + |
| 131 | +class TestServiceTimeoutResponse: |
| 132 | + """The response half: the service's answer must surface as a retryable |
| 133 | + `OperationTimeoutError`, which rests on `com.microsoft:timeout` in `errorCondition`.""" |
| 134 | + |
| 135 | + @staticmethod |
| 136 | + def _response(condition): |
| 137 | + message = MagicMock() |
| 138 | + message.application_properties = {MGMT_RESPONSE_MESSAGE_ERROR_CONDITION: condition} |
| 139 | + return message |
| 140 | + |
| 141 | + def test_timeout_condition_raises_retryable_operation_timeout_error(self): |
| 142 | + with pytest.raises(OperationTimeoutError) as exc_info: |
| 143 | + mgmt_handlers.default(408, self._response(ERROR_CODE_TIMEOUT), "The operation timed out.", PyamqpTransport) |
| 144 | + |
| 145 | + assert exc_info.value._retryable is True |
| 146 | + |
| 147 | + def test_success_returns_the_value_untouched(self): |
| 148 | + message = self._response(None) |
| 149 | + message.value = {"ok": True} |
| 150 | + assert mgmt_handlers.default(200, message, None, PyamqpTransport) == {"ok": True} |
| 151 | + |
| 152 | + def test_uamqp_maps_the_same_condition(self): |
| 153 | + # Skip on uamqp itself: the transport module imports without it, but UamqpTransport is not defined. |
| 154 | + pytest.importorskip("uamqp", reason="uamqp not installed") |
| 155 | + from azure.servicebus._transport._uamqp_transport import UamqpTransport |
| 156 | + |
| 157 | + with pytest.raises(OperationTimeoutError): |
| 158 | + mgmt_handlers.default( |
| 159 | + 408, |
| 160 | + self._response(ERROR_CODE_TIMEOUT), |
| 161 | + "The operation timed out.", |
| 162 | + UamqpTransport, |
| 163 | + ) |
| 164 | + |
| 165 | + |
| 166 | +class TestUamqpValueType: |
| 167 | + """uamqp is the one place the value type changes: pyamqp uses a plain dict, uamqp an `AMQPuInt`.""" |
| 168 | + |
| 169 | + def test_transport_supplied_type_is_what_reaches_the_message(self): |
| 170 | + |
| 171 | + from azure.servicebus._base_handler import BaseHandler |
| 172 | + |
| 173 | + sentinel = object() |
| 174 | + captured = {} |
| 175 | + |
| 176 | + def fake_create_mgmt_msg(message, application_properties, config, reply_to, **kwargs): |
| 177 | + captured.update(application_properties) |
| 178 | + return MagicMock() |
| 179 | + |
| 180 | + handler = BaseHandler.__new__(BaseHandler) |
| 181 | + handler._amqp_transport = MagicMock() |
| 182 | + handler._amqp_transport.create_mgmt_msg = fake_create_mgmt_msg |
| 183 | + handler._amqp_transport.AMQP_UINT_VALUE = lambda ms: sentinel |
| 184 | + handler._amqp_transport.get_handler_link_name = lambda h: "link-1" |
| 185 | + handler._amqp_transport.mgmt_client_request = lambda *args, **kwargs: "response" |
| 186 | + handler._amqp_transport.TIMEOUT_ERROR = TimeoutError |
| 187 | + handler._open = lambda: None |
| 188 | + handler._handler = MagicMock() |
| 189 | + handler._config = MagicMock(encoding="UTF-8") |
| 190 | + handler._mgmt_target = "queue/$management" |
| 191 | + |
| 192 | + handler._mgmt_request_response(b"op", {}, lambda *a: None, timeout=None) |
| 193 | + assert captured[REQUEST_RESPONSE_TIMEOUT] is sentinel |
| 194 | + |
| 195 | + def test_real_uamqp_type_encodes_in_application_properties(self): |
| 196 | + uamqp = pytest.importorskip("uamqp", reason="uamqp not installed") |
| 197 | + from azure.servicebus._transport._uamqp_transport import UamqpTransport |
| 198 | + |
| 199 | + value = UamqpTransport.AMQP_UINT_VALUE(get_server_timeout_ms(None)) |
| 200 | + assert isinstance(value, uamqp.types.AMQPuInt) |
| 201 | + |
| 202 | + message = UamqpTransport.create_mgmt_msg( |
| 203 | + message={"operation": "peek"}, |
| 204 | + application_properties={REQUEST_RESPONSE_TIMEOUT: value}, |
| 205 | + config=MagicMock(encoding="UTF-8"), |
| 206 | + reply_to="queue/$management", |
| 207 | + ) |
| 208 | + |
| 209 | + assert message.encode_message() |
| 210 | + |
| 211 | + |
| 212 | +class TestAsyncParity: |
| 213 | + """The async management path is a separate implementation and can drift from the sync one.""" |
| 214 | + |
| 215 | + @pytest.mark.asyncio |
| 216 | + async def test_async_management_sets_server_timeout(self): |
| 217 | + from azure.servicebus.aio._base_handler_async import BaseHandler as AsyncBaseHandler |
| 218 | + |
| 219 | + captured = {} |
| 220 | + |
| 221 | + def fake_create_mgmt_msg(message, application_properties, config, reply_to, **kwargs): |
| 222 | + captured.clear() |
| 223 | + captured.update(application_properties) |
| 224 | + return MagicMock() |
| 225 | + |
| 226 | + async def fake_request(*args, **kwargs): |
| 227 | + return "response" |
| 228 | + |
| 229 | + async def fake_open(): |
| 230 | + return None |
| 231 | + |
| 232 | + handler = AsyncBaseHandler.__new__(AsyncBaseHandler) |
| 233 | + handler._amqp_transport = MagicMock() |
| 234 | + handler._amqp_transport.create_mgmt_msg = fake_create_mgmt_msg |
| 235 | + handler._amqp_transport.AMQP_UINT_VALUE = PyamqpTransport.AMQP_UINT_VALUE |
| 236 | + handler._amqp_transport.get_handler_link_name = lambda h: "link-1" |
| 237 | + handler._amqp_transport.mgmt_client_request_async = fake_request |
| 238 | + handler._amqp_transport.TIMEOUT_ERROR = TimeoutError |
| 239 | + handler._open = fake_open |
| 240 | + handler._handler = MagicMock() |
| 241 | + handler._config = MagicMock(encoding="UTF-8") |
| 242 | + handler._mgmt_target = "queue/$management" |
| 243 | + |
| 244 | + await handler._mgmt_request_response(b"op", {}, lambda *a: None, timeout=None) |
| 245 | + assert captured[REQUEST_RESPONSE_TIMEOUT] == {"TYPE": "UINT", "VALUE": 60000} |
| 246 | + |
| 247 | + await handler._mgmt_request_response(b"op", {}, lambda *a: None, timeout=10) |
| 248 | + assert captured[REQUEST_RESPONSE_TIMEOUT] == {"TYPE": "UINT", "VALUE": 9000} |
0 commit comments