Skip to content

Commit

Permalink
Feature: add NatsMessage ack_sync method #1906 (#1909)
Browse files Browse the repository at this point in the history
* Feature: add NatsMessage ack_sync method #1906

* Added Test case for the ack_sync method
Calling the ack_sync method in Msg

* Handle ack_sync and test methods as async

* docs: generate API References

---------

Co-authored-by: wpn10 <[email protected]>
Co-authored-by: Pastukhov Nikita <[email protected]>
  • Loading branch information
3 people authored Nov 15, 2024
1 parent 1c437d7 commit 2b2fced
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
5 changes: 5 additions & 0 deletions faststream/nats/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ async def ack(self) -> None:
await self.raw_message.ack()
await super().ack()

async def ack_sync(self) -> None:
if not self.raw_message._ackd:
await self.raw_message.ack_sync()
await super().ack()

async def nack(
self,
delay: Union[int, float, None] = None,
Expand Down
28 changes: 28 additions & 0 deletions tests/brokers/nats/test_consume.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,34 @@ async def handler(msg: NatsMessage):

assert event.is_set()

async def test_consume_ack_sync_manual(
self,
queue: str,
event: asyncio.Event,
stream: JStream,
):
consume_broker = self.get_broker(apply_types=True)

@consume_broker.subscriber(queue, stream=stream)
async def handler(msg: NatsMessage):
await msg.ack_sync()
event.set()

async with self.patch_broker(consume_broker) as br:
await br.start()

with patch.object(Msg, "ack_sync", spy_decorator(Msg.ack_sync)) as m:
await asyncio.wait(
(
asyncio.create_task(br.publish("hello", queue)),
asyncio.create_task(event.wait()),
),
timeout=3,
)
m.mock.assert_called_once()

assert event.is_set()

async def test_consume_ack_raise(
self,
queue: str,
Expand Down

0 comments on commit 2b2fced

Please sign in to comment.