Skip to content
Merged
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
51 changes: 50 additions & 1 deletion tests/test_fundamentals.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

import logging
from datetime import datetime, timedelta
from typing import Annotated, Callable
from logging import LoggerAdapter
from typing import Annotated, Callable
from unittest.mock import AsyncMock
from uuid import uuid4

Expand Down Expand Up @@ -473,3 +473,52 @@ async def the_task(a: str, b: str, logger: LoggerAdapter = TaskLogger()):
assert called
assert "Task is running" in caplog.text
assert "docket.task.the_task" in caplog.text


async def test_self_perpetuating_immediate_tasks(
docket: Docket, worker: Worker, now: Callable[[], datetime]
):
"""docket should support self-perpetuating tasks"""

calls: dict[str, list[int]] = {
"first": [],
"second": [],
}

async def the_task(start: int, iteration: int, key: str = TaskKey()):
calls[key].append(start + iteration)
if iteration < 3:
await docket.add(the_task, key=key)(start, iteration + 1)

await docket.add(the_task, key="first")(10, 1)
await docket.add(the_task, key="second")(20, 1)

await worker.run_until_finished()

assert calls["first"] == [11, 12, 13]
assert calls["second"] == [21, 22, 23]


async def test_self_perpetuating_scheduled_tasks(
docket: Docket, worker: Worker, now: Callable[[], datetime]
):
"""docket should support self-perpetuating tasks"""

calls: dict[str, list[int]] = {
"first": [],
"second": [],
}

async def the_task(start: int, iteration: int, key: str = TaskKey()):
calls[key].append(start + iteration)
if iteration < 3:
soon = now() + timedelta(milliseconds=100)
await docket.add(the_task, key=key, when=soon)(start, iteration + 1)

await docket.add(the_task, key="first")(10, 1)
await docket.add(the_task, key="second")(20, 1)

await worker.run_until_finished()

assert calls["first"] == [11, 12, 13]
assert calls["second"] == [21, 22, 23]