diff --git a/examples/queues/failed.py b/examples/queues/failed.py new file mode 100644 index 0000000..75cbd69 --- /dev/null +++ b/examples/queues/failed.py @@ -0,0 +1,19 @@ +# [START import] +from typing import List +from nitric.api import Queues, Task +from nitric.api.queues import FailedTask +# [END import] +async def queues_failed(): +# [START snippet] + # Construct a new queue client with default settings + queues = Queues() + + payload = {"content": "of task"} + + # Publish tasks to queue + failed_task = await queues.queue("my-queue").send([Task(payload=payload) for i in range(2)]) + + # Process the failed task + for task in failed_task: + print(task.message) +# [END snippet] \ No newline at end of file diff --git a/tests/examples/test_queues_example.py b/tests/examples/test_queues_example.py index cccbc80..6a39ec8 100644 --- a/tests/examples/test_queues_example.py +++ b/tests/examples/test_queues_example.py @@ -1,3 +1,8 @@ +from typing import List + +from nitricapi.nitric.queue.v1 import NitricTask, QueueSendBatchResponse, QueueSendResponse, FailedTask + +from examples.queues.failed import queues_failed from examples.queues.receive import queues_receive from examples.queues.send import queues_send @@ -21,3 +26,21 @@ async def test_send_queue(self): await queues_send() mock_send.assert_called_once() + + async def test_failed_queue(self): + mock_failed = AsyncMock() + mock_failed.return_value = QueueSendBatchResponse( + failed_tasks=[ + FailedTask( + task=NitricTask( + id="1", + ), + message="failed to send in this test", + ) + ] + ) + + with patch("nitricapi.nitric.queue.v1.QueueServiceStub.send_batch", mock_failed): + await queues_failed() + + mock_failed.assert_called_once()