Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions taskiq/receiver/receiver.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ async def callback( # noqa: C901, PLR0912
exc,
exc_info=True,
)
if ack_controller.is_ackable:
await ack_controller.ack()
return
logger.debug(f"Received message: {taskiq_msg}")
task = self.broker.find_task(taskiq_msg.task_name)
Expand All @@ -138,6 +140,8 @@ async def callback( # noqa: C901, PLR0912
'task "%s" is not found. Maybe you forgot to import it?',
taskiq_msg.task_name,
)
if ack_controller.is_ackable:
await ack_controller.ack()
return
logger.debug(
"Function for task %s is resolved. Executing...",
Expand Down
50 changes: 50 additions & 0 deletions tests/receiver/test_receiver.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,56 @@ def ack_callback() -> None:
assert acked


async def test_callback_acks_unknown_task() -> None:
"""Test that a message for an unknown task is acked, not left pending."""
broker = InMemoryBroker()
acked = False

def ack_callback() -> None:
nonlocal acked
acked = True

receiver = get_receiver(broker)

broker_message = broker.formatter.dumps(
TaskiqMessage(
task_id="task_id",
task_name="unknown_task_name",
labels={},
args=[],
kwargs={},
),
)

await receiver.callback(
AckableMessage(
data=broker_message.message,
ack=ack_callback,
),
)
assert acked


async def test_callback_acks_unparsable_message() -> None:
"""Test that an unparsable message is acked, not left pending."""
broker = InMemoryBroker()
acked = False

def ack_callback() -> None:
nonlocal acked
acked = True

receiver = get_receiver(broker)

await receiver.callback(
AckableMessage(
data=b"not a valid taskiq message",
ack=ack_callback,
),
)
assert acked


async def test_callback_success_ackable_async() -> None:
"""Test that acks work with async functions."""
broker = InMemoryBroker()
Expand Down
Loading