From 79cd2f013cf474fb6122288b406bd3e8a11a2e94 Mon Sep 17 00:00:00 2001 From: Joe Pratt Date: Wed, 18 Nov 2026 11:37:06 -0500 Subject: [PATCH 1/2] [EventHub][ServiceBus] Do not announce a 1 MB max message size on pyAMQP receiver links The sender link already sends 0 (no limit) since #43462 and #43383. #42814 asks for the receiver link to match. --- ...eventhub-receiver-link-max-message-size.md | 7 +++ ...rvicebus-receiver-link-max-message-size.md | 7 +++ .../azure/eventhub/_pyamqp/client.py | 2 +- .../test_client_max_message_size_async.py | 34 ++++++++++++ .../unittest/test_client_max_message_size.py | 32 +++++++++++ .../test_producer_max_message_size_async.py | 53 +++++++++++++++++++ .../test_producer_max_message_size.py | 50 +++++++++++++++++ .../azure/servicebus/_pyamqp/client.py | 2 +- .../test_pyamqp_client_max_message_size.py | 41 ++++++++++++++ 9 files changed, 226 insertions(+), 2 deletions(-) create mode 100644 .chronus/changes/eventhub-receiver-link-max-message-size.md create mode 100644 .chronus/changes/servicebus-receiver-link-max-message-size.md create mode 100644 sdk/eventhub/azure-eventhub/tests/pyamqp_tests/unittest/asynctests/test_client_max_message_size_async.py create mode 100644 sdk/eventhub/azure-eventhub/tests/pyamqp_tests/unittest/test_client_max_message_size.py create mode 100644 sdk/eventhub/azure-eventhub/tests/unittest/asynctests/test_producer_max_message_size_async.py create mode 100644 sdk/eventhub/azure-eventhub/tests/unittest/test_producer_max_message_size.py create mode 100644 sdk/servicebus/azure-servicebus/tests/unittests/test_pyamqp_client_max_message_size.py diff --git a/.chronus/changes/eventhub-receiver-link-max-message-size.md b/.chronus/changes/eventhub-receiver-link-max-message-size.md new file mode 100644 index 000000000000..018c73db0ffe --- /dev/null +++ b/.chronus/changes/eventhub-receiver-link-max-message-size.md @@ -0,0 +1,7 @@ +--- +changeKind: internal +packages: + - azure-eventhub +--- + +The pyAMQP receiver link now sends a max message size of 0 (no limit) on its attach frame by default, matching the sender link. ([#42814](https://github.com/Azure/azure-sdk-for-python/issues/42814)) diff --git a/.chronus/changes/servicebus-receiver-link-max-message-size.md b/.chronus/changes/servicebus-receiver-link-max-message-size.md new file mode 100644 index 000000000000..0e80a06ddf9a --- /dev/null +++ b/.chronus/changes/servicebus-receiver-link-max-message-size.md @@ -0,0 +1,7 @@ +--- +changeKind: internal +packages: + - azure-servicebus +--- + +The pyAMQP receiver link now sends a max message size of 0 (no limit) on its attach frame by default, matching the sender link. ([#42814](https://github.com/Azure/azure-sdk-for-python/issues/42814)) diff --git a/sdk/eventhub/azure-eventhub/azure/eventhub/_pyamqp/client.py b/sdk/eventhub/azure-eventhub/azure/eventhub/_pyamqp/client.py index 657c9bd8a879..af17a82c23d4 100644 --- a/sdk/eventhub/azure-eventhub/azure/eventhub/_pyamqp/client.py +++ b/sdk/eventhub/azure-eventhub/azure/eventhub/_pyamqp/client.py @@ -806,7 +806,7 @@ def __init__(self, hostname, source, **kwargs): self._message_received_callback = kwargs.pop("message_received_callback", None) # Sender and Link settings - self._max_message_size = kwargs.pop("max_message_size", MAX_FRAME_SIZE_BYTES) + self._max_message_size = kwargs.pop("max_message_size", LINK_MAX_MESSAGE_SIZE) self._link_properties = kwargs.pop("link_properties", None) self._link_credit = kwargs.pop("link_credit", 300) diff --git a/sdk/eventhub/azure-eventhub/tests/pyamqp_tests/unittest/asynctests/test_client_max_message_size_async.py b/sdk/eventhub/azure-eventhub/tests/pyamqp_tests/unittest/asynctests/test_client_max_message_size_async.py new file mode 100644 index 000000000000..851c3160de0e --- /dev/null +++ b/sdk/eventhub/azure-eventhub/tests/pyamqp_tests/unittest/asynctests/test_client_max_message_size_async.py @@ -0,0 +1,34 @@ +# -------------------------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License.txt in the project root for license information. +# -------------------------------------------------------------------------------------------- +from unittest.mock import AsyncMock, Mock + +import pytest + +from azure.eventhub._pyamqp.aio import SendClientAsync, ReceiveClientAsync +from azure.eventhub._pyamqp.constants import LINK_MAX_MESSAGE_SIZE + + +# shadows conftest's autouse amqpproxy, which skips without a live Event Hub +@pytest.fixture(autouse=True) +def amqpproxy(): + yield + + +@pytest.mark.asyncio +@pytest.mark.parametrize( + "kwargs, expected", + [({}, LINK_MAX_MESSAGE_SIZE), ({"max_message_size": 5 * 1024 * 1024}, 5 * 1024 * 1024)], +) +@pytest.mark.parametrize( + "cls, factory", + [(SendClientAsync, "create_sender_link"), (ReceiveClientAsync, "create_receiver_link")], +) +async def test_link_max_message_size_async(cls, factory, kwargs, expected): + client = cls("fake.host.com", "fake_eh", auth="my_fake_auth", **kwargs) + client._session = Mock() + getattr(client._session, factory).return_value = AsyncMock() + + assert await client._client_ready_async() is False + assert getattr(client._session, factory).call_args.kwargs["max_message_size"] == expected diff --git a/sdk/eventhub/azure-eventhub/tests/pyamqp_tests/unittest/test_client_max_message_size.py b/sdk/eventhub/azure-eventhub/tests/pyamqp_tests/unittest/test_client_max_message_size.py new file mode 100644 index 000000000000..bb0ee0db1c7a --- /dev/null +++ b/sdk/eventhub/azure-eventhub/tests/pyamqp_tests/unittest/test_client_max_message_size.py @@ -0,0 +1,32 @@ +# -------------------------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License.txt in the project root for license information. +# -------------------------------------------------------------------------------------------- +from unittest.mock import Mock + +import pytest + +from azure.eventhub._pyamqp import SendClient, ReceiveClient +from azure.eventhub._pyamqp.constants import LINK_MAX_MESSAGE_SIZE + + +# shadows conftest's autouse amqpproxy, which skips without a live Event Hub +@pytest.fixture(autouse=True) +def amqpproxy(): + yield + + +@pytest.mark.parametrize( + "kwargs, expected", + [({}, LINK_MAX_MESSAGE_SIZE), ({"max_message_size": 5 * 1024 * 1024}, 5 * 1024 * 1024)], +) +@pytest.mark.parametrize( + "cls, factory", + [(SendClient, "create_sender_link"), (ReceiveClient, "create_receiver_link")], +) +def test_link_max_message_size(cls, factory, kwargs, expected): + client = cls("fake.host.com", "fake_eh", auth="my_fake_auth", **kwargs) + client._session = Mock() + + assert client._client_ready() is False + assert getattr(client._session, factory).call_args.kwargs["max_message_size"] == expected diff --git a/sdk/eventhub/azure-eventhub/tests/unittest/asynctests/test_producer_max_message_size_async.py b/sdk/eventhub/azure-eventhub/tests/unittest/asynctests/test_producer_max_message_size_async.py new file mode 100644 index 000000000000..efebcb25adcb --- /dev/null +++ b/sdk/eventhub/azure-eventhub/tests/unittest/asynctests/test_producer_max_message_size_async.py @@ -0,0 +1,53 @@ +# -------------------------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License.txt in the project root for license information. +# -------------------------------------------------------------------------------------------- +from unittest.mock import AsyncMock, Mock + +import pytest + +from azure.eventhub import EventData +from azure.eventhub.aio import EventHubProducerClient +from azure.eventhub._constants import ALL_PARTITIONS, MAX_MESSAGE_LENGTH_BYTES + +MB = 1024 * 1024 + + +# shadows conftest's autouse amqpproxy, which skips without a live Event Hub +@pytest.fixture(autouse=True) +def amqpproxy(): + yield + + +def _client_with_remote_max_message_size(remote): + client = EventHubProducerClient("fake.host.com", "fake_eh", None) + inner = client._producers[ALL_PARTITIONS] + inner._open_with_retry = AsyncMock() + inner._handler = Mock() + inner._handler._link.remote_max_message_size = remote + return client + + +@pytest.mark.asyncio +async def test_create_batch_uses_remote_max_message_size_async(): + client = _client_with_remote_max_message_size(20 * MB) + + batch = await client.create_batch() + assert batch.max_size_in_bytes == 20 * MB + batch.add(EventData(b"a" * (2 * MB))) + assert batch.size_in_bytes > MB + + assert (await client.create_batch(max_size_in_bytes=5 * MB)).max_size_in_bytes == 5 * MB + await client.create_batch(max_size_in_bytes=20 * MB) + with pytest.raises(ValueError): + await client.create_batch(max_size_in_bytes=20 * MB + 1) + + +@pytest.mark.asyncio +@pytest.mark.parametrize("remote", [None, 0]) +async def test_create_batch_falls_back_to_1mb_async(remote): + client = _client_with_remote_max_message_size(remote) + + assert (await client.create_batch()).max_size_in_bytes == MAX_MESSAGE_LENGTH_BYTES + with pytest.raises(ValueError): + await client.create_batch(max_size_in_bytes=MB + 1) diff --git a/sdk/eventhub/azure-eventhub/tests/unittest/test_producer_max_message_size.py b/sdk/eventhub/azure-eventhub/tests/unittest/test_producer_max_message_size.py new file mode 100644 index 000000000000..bf8379a12067 --- /dev/null +++ b/sdk/eventhub/azure-eventhub/tests/unittest/test_producer_max_message_size.py @@ -0,0 +1,50 @@ +# -------------------------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License.txt in the project root for license information. +# -------------------------------------------------------------------------------------------- +from unittest.mock import Mock + +import pytest + +from azure.eventhub import EventData, EventHubProducerClient +from azure.eventhub._constants import ALL_PARTITIONS, MAX_MESSAGE_LENGTH_BYTES + +MB = 1024 * 1024 + + +# shadows conftest's autouse amqpproxy, which skips without a live Event Hub +@pytest.fixture(autouse=True) +def amqpproxy(): + yield + + +def _client_with_remote_max_message_size(remote): + client = EventHubProducerClient("fake.host.com", "fake_eh", None) + inner = client._producers[ALL_PARTITIONS] + inner._open_with_retry = Mock() + inner._handler = Mock() + inner._handler._link.remote_max_message_size = remote + return client + + +def test_create_batch_uses_remote_max_message_size(): + client = _client_with_remote_max_message_size(20 * MB) + + batch = client.create_batch() + assert batch.max_size_in_bytes == 20 * MB + batch.add(EventData(b"a" * (2 * MB))) + assert batch.size_in_bytes > MB + + assert client.create_batch(max_size_in_bytes=5 * MB).max_size_in_bytes == 5 * MB + client.create_batch(max_size_in_bytes=20 * MB) + with pytest.raises(ValueError): + client.create_batch(max_size_in_bytes=20 * MB + 1) + + +@pytest.mark.parametrize("remote", [None, 0]) +def test_create_batch_falls_back_to_1mb(remote): + client = _client_with_remote_max_message_size(remote) + + assert client.create_batch().max_size_in_bytes == MAX_MESSAGE_LENGTH_BYTES + with pytest.raises(ValueError): + client.create_batch(max_size_in_bytes=MB + 1) diff --git a/sdk/servicebus/azure-servicebus/azure/servicebus/_pyamqp/client.py b/sdk/servicebus/azure-servicebus/azure/servicebus/_pyamqp/client.py index c48d2f1e14f8..d87700acea21 100644 --- a/sdk/servicebus/azure-servicebus/azure/servicebus/_pyamqp/client.py +++ b/sdk/servicebus/azure-servicebus/azure/servicebus/_pyamqp/client.py @@ -829,7 +829,7 @@ def __init__(self, hostname, source, **kwargs): self._message_received_callback = kwargs.pop("message_received_callback", None) # Sender and Link settings - self._max_message_size = kwargs.pop("max_message_size", MAX_FRAME_SIZE_BYTES) + self._max_message_size = kwargs.pop("max_message_size", LINK_MAX_MESSAGE_SIZE) self._link_properties = kwargs.pop("link_properties", None) self._link_credit = kwargs.pop("link_credit", 300) diff --git a/sdk/servicebus/azure-servicebus/tests/unittests/test_pyamqp_client_max_message_size.py b/sdk/servicebus/azure-servicebus/tests/unittests/test_pyamqp_client_max_message_size.py new file mode 100644 index 000000000000..644b73c6782f --- /dev/null +++ b/sdk/servicebus/azure-servicebus/tests/unittests/test_pyamqp_client_max_message_size.py @@ -0,0 +1,41 @@ +# -------------------------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License.txt in the project root for license information. +# -------------------------------------------------------------------------------------------- +from unittest.mock import AsyncMock, Mock + +import pytest + +from azure.servicebus._pyamqp import SendClient, ReceiveClient +from azure.servicebus._pyamqp.aio import SendClientAsync, ReceiveClientAsync +from azure.servicebus._pyamqp.constants import LINK_MAX_MESSAGE_SIZE + +SIZE_CASES = [({}, LINK_MAX_MESSAGE_SIZE), ({"max_message_size": 5 * 1024 * 1024}, 5 * 1024 * 1024)] + + +@pytest.mark.parametrize("kwargs, expected", SIZE_CASES) +@pytest.mark.parametrize( + "cls, factory", + [(SendClient, "create_sender_link"), (ReceiveClient, "create_receiver_link")], +) +def test_link_max_message_size(cls, factory, kwargs, expected): + client = cls("fake.host.com", "fake_sb", auth="my_fake_auth", **kwargs) + client._session = Mock() + + assert client._client_ready() is False + assert getattr(client._session, factory).call_args.kwargs["max_message_size"] == expected + + +@pytest.mark.asyncio +@pytest.mark.parametrize("kwargs, expected", SIZE_CASES) +@pytest.mark.parametrize( + "cls, factory", + [(SendClientAsync, "create_sender_link"), (ReceiveClientAsync, "create_receiver_link")], +) +async def test_link_max_message_size_async(cls, factory, kwargs, expected): + client = cls("fake.host.com", "fake_sb", auth="my_fake_auth", **kwargs) + client._session = Mock() + getattr(client._session, factory).return_value = AsyncMock() + + assert await client._client_ready_async() is False + assert getattr(client._session, factory).call_args.kwargs["max_message_size"] == expected From 246c141a181bfee09d7cdac2c8b55ae6df32565c Mon Sep 17 00:00:00 2001 From: Joe Pratt Date: Fri, 20 Nov 2026 20:46:15 -0500 Subject: [PATCH 2/2] [EventHub][ServiceBus] Test the max message size on the receiver link ATTACH frame The existing tests stop at the mocked link creation and compare against LINK_MAX_MESSAGE_SIZE, so they would not catch the constant changing. --- .../test_client_max_message_size_async.py | 18 ++++++++++++ .../unittest/test_client_max_message_size.py | 17 +++++++++++ .../test_pyamqp_client_max_message_size.py | 29 +++++++++++++++++++ 3 files changed, 64 insertions(+) diff --git a/sdk/eventhub/azure-eventhub/tests/pyamqp_tests/unittest/asynctests/test_client_max_message_size_async.py b/sdk/eventhub/azure-eventhub/tests/pyamqp_tests/unittest/asynctests/test_client_max_message_size_async.py index 851c3160de0e..55b0a6d702c5 100644 --- a/sdk/eventhub/azure-eventhub/tests/pyamqp_tests/unittest/asynctests/test_client_max_message_size_async.py +++ b/sdk/eventhub/azure-eventhub/tests/pyamqp_tests/unittest/asynctests/test_client_max_message_size_async.py @@ -7,7 +7,9 @@ import pytest from azure.eventhub._pyamqp.aio import SendClientAsync, ReceiveClientAsync +from azure.eventhub._pyamqp.aio._session_async import Session from azure.eventhub._pyamqp.constants import LINK_MAX_MESSAGE_SIZE +from azure.eventhub._pyamqp.performatives import AttachFrame # shadows conftest's autouse amqpproxy, which skips without a live Event Hub @@ -32,3 +34,19 @@ async def test_link_max_message_size_async(cls, factory, kwargs, expected): assert await client._client_ready_async() is False assert getattr(client._session, factory).call_args.kwargs["max_message_size"] == expected + + +@pytest.mark.asyncio +@pytest.mark.parametrize( + "kwargs, expected", + [({}, 0), ({"max_message_size": 5 * 1024 * 1024}, 5 * 1024 * 1024)], +) +async def test_receiver_attach_frame_max_message_size_async(kwargs, expected): + client = ReceiveClientAsync("fake.host.com", "fake_eh", auth="my_fake_auth", **kwargs) + connection = AsyncMock() + client._session = Session(connection, 0, network_trace=False, network_trace_params={}) + + assert await client._client_ready_async() is False + frame = connection._process_outgoing_frame.call_args.args[1] + assert isinstance(frame, AttachFrame) + assert frame.max_message_size == expected diff --git a/sdk/eventhub/azure-eventhub/tests/pyamqp_tests/unittest/test_client_max_message_size.py b/sdk/eventhub/azure-eventhub/tests/pyamqp_tests/unittest/test_client_max_message_size.py index bb0ee0db1c7a..25ef2a6adf37 100644 --- a/sdk/eventhub/azure-eventhub/tests/pyamqp_tests/unittest/test_client_max_message_size.py +++ b/sdk/eventhub/azure-eventhub/tests/pyamqp_tests/unittest/test_client_max_message_size.py @@ -8,6 +8,8 @@ from azure.eventhub._pyamqp import SendClient, ReceiveClient from azure.eventhub._pyamqp.constants import LINK_MAX_MESSAGE_SIZE +from azure.eventhub._pyamqp.performatives import AttachFrame +from azure.eventhub._pyamqp.session import Session # shadows conftest's autouse amqpproxy, which skips without a live Event Hub @@ -30,3 +32,18 @@ def test_link_max_message_size(cls, factory, kwargs, expected): assert client._client_ready() is False assert getattr(client._session, factory).call_args.kwargs["max_message_size"] == expected + + +@pytest.mark.parametrize( + "kwargs, expected", + [({}, 0), ({"max_message_size": 5 * 1024 * 1024}, 5 * 1024 * 1024)], +) +def test_receiver_attach_frame_max_message_size(kwargs, expected): + client = ReceiveClient("fake.host.com", "fake_eh", auth="my_fake_auth", **kwargs) + connection = Mock() + client._session = Session(connection, 0, network_trace=False, network_trace_params={}) + + assert client._client_ready() is False + frame = connection._process_outgoing_frame.call_args.args[1] + assert isinstance(frame, AttachFrame) + assert frame.max_message_size == expected diff --git a/sdk/servicebus/azure-servicebus/tests/unittests/test_pyamqp_client_max_message_size.py b/sdk/servicebus/azure-servicebus/tests/unittests/test_pyamqp_client_max_message_size.py index 644b73c6782f..97c1d5e29246 100644 --- a/sdk/servicebus/azure-servicebus/tests/unittests/test_pyamqp_client_max_message_size.py +++ b/sdk/servicebus/azure-servicebus/tests/unittests/test_pyamqp_client_max_message_size.py @@ -8,9 +8,13 @@ from azure.servicebus._pyamqp import SendClient, ReceiveClient from azure.servicebus._pyamqp.aio import SendClientAsync, ReceiveClientAsync +from azure.servicebus._pyamqp.aio._session_async import Session as SessionAsync from azure.servicebus._pyamqp.constants import LINK_MAX_MESSAGE_SIZE +from azure.servicebus._pyamqp.performatives import AttachFrame +from azure.servicebus._pyamqp.session import Session SIZE_CASES = [({}, LINK_MAX_MESSAGE_SIZE), ({"max_message_size": 5 * 1024 * 1024}, 5 * 1024 * 1024)] +ATTACH_SIZE_CASES = [({}, 0), ({"max_message_size": 5 * 1024 * 1024}, 5 * 1024 * 1024)] @pytest.mark.parametrize("kwargs, expected", SIZE_CASES) @@ -39,3 +43,28 @@ async def test_link_max_message_size_async(cls, factory, kwargs, expected): assert await client._client_ready_async() is False assert getattr(client._session, factory).call_args.kwargs["max_message_size"] == expected + + +@pytest.mark.parametrize("kwargs, expected", ATTACH_SIZE_CASES) +def test_receiver_attach_frame_max_message_size(kwargs, expected): + client = ReceiveClient("fake.host.com", "fake_sb", auth="my_fake_auth", **kwargs) + connection = Mock() + client._session = Session(connection, 0, network_trace=False, network_trace_params={}) + + assert client._client_ready() is False + frame = connection._process_outgoing_frame.call_args.args[1] + assert isinstance(frame, AttachFrame) + assert frame.max_message_size == expected + + +@pytest.mark.asyncio +@pytest.mark.parametrize("kwargs, expected", ATTACH_SIZE_CASES) +async def test_receiver_attach_frame_max_message_size_async(kwargs, expected): + client = ReceiveClientAsync("fake.host.com", "fake_sb", auth="my_fake_auth", **kwargs) + connection = AsyncMock() + client._session = SessionAsync(connection, 0, network_trace=False, network_trace_params={}) + + assert await client._client_ready_async() is False + frame = connection._process_outgoing_frame.call_args.args[1] + assert isinstance(frame, AttachFrame) + assert frame.max_message_size == expected