Skip to content

Commit

Permalink
docs: Add failed task snippet
Browse files Browse the repository at this point in the history
  • Loading branch information
HomelessDinosaur committed Oct 5, 2021
1 parent 7d76a1b commit 5f5f08f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
19 changes: 19 additions & 0 deletions examples/queues/failed.py
Original file line number Diff line number Diff line change
@@ -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]
23 changes: 23 additions & 0 deletions tests/examples/test_queues_example.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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()

0 comments on commit 5f5f08f

Please sign in to comment.