From 7d0692e274a697264b7522334dddf0bc8c334bb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=B0=D0=B4=D0=B8=D0=BC=20=D0=9A=D0=BE=D0=B7=D1=8B?= =?UTF-8?q?=D1=80=D0=B5=D0=B2=D1=81=D0=BA=D0=B8=D0=B9?= Date: Mon, 23 Nov 2026 14:04:51 +0300 Subject: [PATCH 1/3] docs: document the transport capability matrix Readers need one place for subscribe behavior, per-backend ordering and dead-letter limits, and the InMemoryTransport test path. Co-authored-by: Cursor --- docs/bootstrap/message_brokers.md | 25 ++++++++++++++-- docs/testing/index.md | 8 +++++- docs/testing/transport.md | 47 +++++++++++++++++++++++++++++++ mkdocs.yml | 1 + 4 files changed, 77 insertions(+), 4 deletions(-) create mode 100644 docs/testing/transport.md diff --git a/docs/bootstrap/message_brokers.md b/docs/bootstrap/message_brokers.md index af086ba..8a00957 100644 --- a/docs/bootstrap/message_brokers.md +++ b/docs/bootstrap/message_brokers.md @@ -41,12 +41,31 @@ Message brokers are used to publish `NotificationEvent` and `ECSTEvent` events t +### Transport versus MessageBroker + +A `MessageBroker` publishes. `send_message` is the only publish method. `KafkaMessageBroker`, `AMQPMessageBroker`, and `EventProducer` are that path. + +A `Transport` is a `MessageBroker` that can also subscribe. Use it when this process receives the message, or when a test should swap the backend. `bootstrap(..., message_broker=transport)` and `test_harness(message_broker=transport)` need no new argument. Subscribe on the transport. The harness still records `published`. + +`InMemoryTransport` runs the full command, saga, notification, and subscriber path in one process. `await transport.drain()` waits until handlers have acked. `delivery="inline"` makes `send_message` wait for that message and for anything the handlers published before they returned. The body a subscriber reads is bytes (`message_wire_bytes`), not a `Message` object. See [Transport](../testing/transport.md). + +| Backend | Ordering per key | Native DLQ | Delayed redelivery | Headers | What `topic` means | +| --- | --- | --- | --- | --- | --- | +| In-memory | yes | no, republish to `{topic}.dlq` | yes (`redelivery_delay`) | yes | logical name shared by publisher and subscribers | +| Kafka | yes, when `key` is set (partition key) | no, republish to `{topic}.dlq` | no | yes | Kafka topic. `group` is the consumer `group_id` | +| AMQP | no. `message.key` is header `key` and does not change the route | no, republish to `{topic}.dlq` | no | yes | direct-exchange routing key. `group` is the queue name. Optional `?exchange=` on the URL | +| Redis | no. `key` is a stream field | no, republish to `{topic}.dlq` | yes (`XCLAIM` / `XAUTOCLAIM`) | yes | Redis stream key. `group` is the consumer group | +| NATS | no. `key` is a header only. An in-process per-key lock is not a cross-process guarantee | no, republish to `{topic}.dlq` | no | yes | JetStream subject as given. `group` is the durable consumer name | +| SQS | no on a standard queue. A name ending in `.fifo` also sends `MessageGroupId` | no. Emulated dead-letter queue is `{topic}-dlq` (hyphen, not a dot) | yes (visibility timeout) | yes (message attributes) | queue name, unchanged. Group fan-out on this instance is `{name}-{group}`. Dots in the name are not rewritten, and creation fails | + +`create_transport` builds these from `kafka://`, `amqp://`, `redis://`, `nats://`, and `sqs://`. Install `python-cqrs[kafka]`, `[rabbit]`, `[redis]`, `[nats]`, `[sqs]`, or all of them with `[transports]`. `KafkaMessageBroker` and `AMQPMessageBroker` stay publish-only: the AMQP broker still declares a queue named `message.topic`, and `AMQPTransport` does not replace it. + ### DevnullMessageBroker -The `DevnullMessageBroker` is a no-op broker used for testing. It doesn't actually send messages anywhere but logs warnings. +The `DevnullMessageBroker` records a message and does not deliver it. It logs a warning. -!!! warning "Development Only" - Never use `DevnullMessageBroker` in production. It's designed for testing and development only. +!!! warning "Deprecated when a subscriber must receive the message" + Do not read `DevnullMessageBroker.MESSAGE_BUS`. That list does not deliver. Use `InMemoryTransport` when the message must reach a subscriber in a test or a local process. Do not use `DevnullMessageBroker` in production. ```python from cqrs.message_brokers import devnull diff --git a/docs/testing/index.md b/docs/testing/index.md index 804c377..66ba07f 100644 --- a/docs/testing/index.md +++ b/docs/testing/index.md @@ -59,7 +59,7 @@ The library keeps each page's test in [`examples/testing/`](https://github.com/v `TestContainer` is `register(type, instance)` and `async resolve`. For `ScopeStrategy.SEND`, pass `di.Container`, not `TestContainer`. -If the mediator holds `DevnullMessageBroker`, the harness replaces it with `CapturingMessageBroker` for the block and restores it afterwards. Do not read `DevnullMessageBroker.MESSAGE_BUS`. +If the mediator holds `DevnullMessageBroker`, the harness replaces it with `CapturingMessageBroker` for the block and restores it afterwards. Do not read `DevnullMessageBroker.MESSAGE_BUS`. When a subscriber in the same test must receive the notice, pass `InMemoryTransport` as `message_broker` and `await transport.drain()`. See [Transport](transport.md). `contains` is a boolean. `assert_contains`, `assert_one`, and `assert_empty` raise `AssertionError` and list what was recorded. There is no pytest plugin. The same calls work under unittest. @@ -327,6 +327,12 @@ Three delivery channels stay separate. A test that asks `published` for a domain [:octicons-arrow-right-24: Read More](kafka_amqp.md) +- :material-message-processing: **Transport** + + InMemoryTransport delivers to a subscriber in the unit test. drain waits for ack. The body is bytes. + + [:octicons-arrow-right-24: Read More](transport.md) + ## Outside the harness diff --git a/docs/testing/transport.md b/docs/testing/transport.md new file mode 100644 index 0000000..68a681c --- /dev/null +++ b/docs/testing/transport.md @@ -0,0 +1,47 @@ +--- +title: Transport +description: InMemoryTransport delivers a notification to a subscriber in a unit test. drain waits for ack. The body is bytes. +--- + +# Transport + +
+ +- :material-home: **Back to Testing Overview** + + Return to the Testing overview page with all topics. + + [:octicons-arrow-left-24: Back to Overview](index.md) + +
+ +`InMemoryTransport` is a `MessageBroker` that also subscribes. Pass it to `test_harness(message_broker=transport)`. The harness records `published` and still calls `send_message` on the transport, so a subscriber on that same instance receives the notice. Do not read `DevnullMessageBroker.MESSAGE_BUS`. + +The subscriber sees bytes, not a `Message` and not a decoded event. Those bytes are `message_wire_bytes`: the codec payload when one was set, otherwise the JSON of `payload`. The application deserializes them. The library does not map a topic to a type. + +`await transport.drain()` waits until queues are empty, no handler is running, and no redelivery is waiting. `close` stops the transport and does not deliver what is still queued. `delivery="inline"` makes `send_message` wait for this message and for anything the handlers published before they finished. + +```python +import cqrs +from cqrs.testing import test_harness + +transport = cqrs.InMemoryTransport() + +async def on_event(message: cqrs.IncomingMessage) -> None: + bodies.append(message.body) + await message.ack() + +async with test_harness(message_broker=transport, commands_mapper=commands_mapper) as harness: + await transport.subscribe("order_events", on_event, cqrs.SubscribeOptions(group="billing")) + await harness.mediator.send(CreateOrder(sku="book")) + await transport.drain() + + harness.published.assert_one(OrderCreatedNotice, topic="order_events") + assert len(bodies) == 1 +``` + +`SubscribeOptions(group=...)` is required. Two groups on one topic each get a copy. Two subscribers in one group share one copy. `nack(requeue=True)` tries again until `max_deliveries`, then the message is published to `{topic}.dlq`. + +Library examples: [`examples/transport/event_flow.py`](https://github.com/vadikko2/python-cqrs/blob/master/examples/transport/event_flow.py) and [`examples/transport/inbox_flow.py`](https://github.com/vadikko2/python-cqrs/blob/master/examples/transport/inbox_flow.py). + +`KafkaTransport`, `AMQPTransport`, `RedisTransport`, `NATSTransport`, and `SQSTransport` are the same port on a broker. `create_transport` builds one from a URL. Ordering, dead letters, and what `topic` means are in the [capability matrix](../bootstrap/message_brokers.md#transport-versus-messagebroker). A NATS per-key lock stays inside one process. The example above stays on `InMemoryTransport` and does not start Docker. diff --git a/mkdocs.yml b/mkdocs.yml index 3b34235..c4fb1ec 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -144,6 +144,7 @@ nav: - dishka: testing/dishka.md - dependency-injector: testing/dependency_injector.md - Kafka and AMQP: testing/kafka_amqp.md + - Transport: testing/transport.md - Outside the harness: - Mermaid: testing/mermaid.md - Compressor: testing/compressor.md From a28edac80368aecce8aeeda94eac7969e09f3de5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=B0=D0=B4=D0=B8=D0=BC=20=D0=9A=D0=BE=D0=B7=D1=8B?= =?UTF-8?q?=D1=80=D0=B5=D0=B2=D1=81=D0=BA=D0=B8=D0=B9?= Date: Mon, 23 Nov 2026 15:53:21 +0300 Subject: [PATCH 2/3] docs: install each broker extra on its own The aggregate transports extra is gone, so the broker page names kafka, rabbit, redis, nats, and sqs separately. Co-authored-by: Cursor --- docs/bootstrap/message_brokers.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/bootstrap/message_brokers.md b/docs/bootstrap/message_brokers.md index 8a00957..bdd9b67 100644 --- a/docs/bootstrap/message_brokers.md +++ b/docs/bootstrap/message_brokers.md @@ -58,7 +58,7 @@ A `Transport` is a `MessageBroker` that can also subscribe. Use it when this pro | NATS | no. `key` is a header only. An in-process per-key lock is not a cross-process guarantee | no, republish to `{topic}.dlq` | no | yes | JetStream subject as given. `group` is the durable consumer name | | SQS | no on a standard queue. A name ending in `.fifo` also sends `MessageGroupId` | no. Emulated dead-letter queue is `{topic}-dlq` (hyphen, not a dot) | yes (visibility timeout) | yes (message attributes) | queue name, unchanged. Group fan-out on this instance is `{name}-{group}`. Dots in the name are not rewritten, and creation fails | -`create_transport` builds these from `kafka://`, `amqp://`, `redis://`, `nats://`, and `sqs://`. Install `python-cqrs[kafka]`, `[rabbit]`, `[redis]`, `[nats]`, `[sqs]`, or all of them with `[transports]`. `KafkaMessageBroker` and `AMQPMessageBroker` stay publish-only: the AMQP broker still declares a queue named `message.topic`, and `AMQPTransport` does not replace it. +`create_transport` builds these from `kafka://`, `amqp://`, `redis://`, `nats://`, and `sqs://`. Install `python-cqrs[kafka]`, `[rabbit]`, `[redis]`, `[nats]`, or `[sqs]`. `KafkaMessageBroker` and `AMQPMessageBroker` stay publish-only: the AMQP broker still declares a queue named `message.topic`, and `AMQPTransport` does not replace it. ### DevnullMessageBroker From 253282235cabf35db899853461c61782ab428179 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=B0=D0=B4=D0=B8=D0=BC=20=D0=9A=D0=BE=D0=B7=D1=8B?= =?UTF-8?q?=D1=80=D0=B5=D0=B2=D1=81=D0=BA=D0=B8=D0=B9?= Date: Wed, 25 Nov 2026 12:00:24 +0300 Subject: [PATCH 3/3] docs: document per-module create_transport imports Point readers at each broker module instead of a single factory entry point. Co-authored-by: Cursor --- docs/bootstrap/message_brokers.md | 2 +- docs/testing/transport.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/bootstrap/message_brokers.md b/docs/bootstrap/message_brokers.md index bdd9b67..eebc404 100644 --- a/docs/bootstrap/message_brokers.md +++ b/docs/bootstrap/message_brokers.md @@ -58,7 +58,7 @@ A `Transport` is a `MessageBroker` that can also subscribe. Use it when this pro | NATS | no. `key` is a header only. An in-process per-key lock is not a cross-process guarantee | no, republish to `{topic}.dlq` | no | yes | JetStream subject as given. `group` is the durable consumer name | | SQS | no on a standard queue. A name ending in `.fifo` also sends `MessageGroupId` | no. Emulated dead-letter queue is `{topic}-dlq` (hyphen, not a dot) | yes (visibility timeout) | yes (message attributes) | queue name, unchanged. Group fan-out on this instance is `{name}-{group}`. Dots in the name are not rewritten, and creation fails | -`create_transport` builds these from `kafka://`, `amqp://`, `redis://`, `nats://`, and `sqs://`. Install `python-cqrs[kafka]`, `[rabbit]`, `[redis]`, `[nats]`, or `[sqs]`. `KafkaMessageBroker` and `AMQPMessageBroker` stay publish-only: the AMQP broker still declares a queue named `message.topic`, and `AMQPTransport` does not replace it. +Import `create_transport` from the backend module (`cqrs.transport.memory` or `cqrs.message_brokers.kafka`, `amqp`, `redis`, `nats`, or `sqs`). Each builds a transport from that scheme's URL (`kafka://`, `amqp://`, `redis://`, `nats://`, `sqs://`, or `memory://`). Install `python-cqrs[kafka]`, `[rabbit]`, `[redis]`, `[nats]`, or `[sqs]` before importing a broker module. `KafkaMessageBroker` and `AMQPMessageBroker` stay publish-only: the AMQP broker still declares a queue named `message.topic`, and `AMQPTransport` does not replace it. ### DevnullMessageBroker diff --git a/docs/testing/transport.md b/docs/testing/transport.md index 68a681c..494b18c 100644 --- a/docs/testing/transport.md +++ b/docs/testing/transport.md @@ -44,4 +44,4 @@ async with test_harness(message_broker=transport, commands_mapper=commands_mappe Library examples: [`examples/transport/event_flow.py`](https://github.com/vadikko2/python-cqrs/blob/master/examples/transport/event_flow.py) and [`examples/transport/inbox_flow.py`](https://github.com/vadikko2/python-cqrs/blob/master/examples/transport/inbox_flow.py). -`KafkaTransport`, `AMQPTransport`, `RedisTransport`, `NATSTransport`, and `SQSTransport` are the same port on a broker. `create_transport` builds one from a URL. Ordering, dead letters, and what `topic` means are in the [capability matrix](../bootstrap/message_brokers.md#transport-versus-messagebroker). A NATS per-key lock stays inside one process. The example above stays on `InMemoryTransport` and does not start Docker. +`KafkaTransport`, `AMQPTransport`, `RedisTransport`, `NATSTransport`, and `SQSTransport` are the same port on a broker. Import `create_transport` from that backend's module (`cqrs.message_brokers.kafka`, `amqp`, `redis`, `nats`, or `sqs`) to build one from a URL. Ordering, dead letters, and what `topic` means are in the [capability matrix](../bootstrap/message_brokers.md#transport-versus-messagebroker). A NATS per-key lock stays inside one process. The example above stays on `InMemoryTransport` and does not start Docker.