Skip to content

Commit 6018792

Browse files
authored
Adds tests for self-perpetuating tasks (#31)
Just wanted to demonstrate that a task can be scheduled while it is currently running. This just uses `docket.add`, but later we could think about making self-perpetuating tasks first-class citizens. Closes #7
1 parent ca9d810 commit 6018792

File tree

1 file changed

+50
-1
lines changed

1 file changed

+50
-1
lines changed

tests/test_fundamentals.py

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77

88
import logging
99
from datetime import datetime, timedelta
10-
from typing import Annotated, Callable
1110
from logging import LoggerAdapter
11+
from typing import Annotated, Callable
1212
from unittest.mock import AsyncMock
1313
from uuid import uuid4
1414

@@ -473,3 +473,52 @@ async def the_task(a: str, b: str, logger: LoggerAdapter = TaskLogger()):
473473
assert called
474474
assert "Task is running" in caplog.text
475475
assert "docket.task.the_task" in caplog.text
476+
477+
478+
async def test_self_perpetuating_immediate_tasks(
479+
docket: Docket, worker: Worker, now: Callable[[], datetime]
480+
):
481+
"""docket should support self-perpetuating tasks"""
482+
483+
calls: dict[str, list[int]] = {
484+
"first": [],
485+
"second": [],
486+
}
487+
488+
async def the_task(start: int, iteration: int, key: str = TaskKey()):
489+
calls[key].append(start + iteration)
490+
if iteration < 3:
491+
await docket.add(the_task, key=key)(start, iteration + 1)
492+
493+
await docket.add(the_task, key="first")(10, 1)
494+
await docket.add(the_task, key="second")(20, 1)
495+
496+
await worker.run_until_finished()
497+
498+
assert calls["first"] == [11, 12, 13]
499+
assert calls["second"] == [21, 22, 23]
500+
501+
502+
async def test_self_perpetuating_scheduled_tasks(
503+
docket: Docket, worker: Worker, now: Callable[[], datetime]
504+
):
505+
"""docket should support self-perpetuating tasks"""
506+
507+
calls: dict[str, list[int]] = {
508+
"first": [],
509+
"second": [],
510+
}
511+
512+
async def the_task(start: int, iteration: int, key: str = TaskKey()):
513+
calls[key].append(start + iteration)
514+
if iteration < 3:
515+
soon = now() + timedelta(milliseconds=100)
516+
await docket.add(the_task, key=key, when=soon)(start, iteration + 1)
517+
518+
await docket.add(the_task, key="first")(10, 1)
519+
await docket.add(the_task, key="second")(20, 1)
520+
521+
await worker.run_until_finished()
522+
523+
assert calls["first"] == [11, 12, 13]
524+
assert calls["second"] == [21, 22, 23]

0 commit comments

Comments
 (0)