Skip to content

Commit

Permalink
add test_monitor_decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
superstar54 committed Dec 5, 2024
1 parent cc9ebbd commit cde38db
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions tests/test_awaitable_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import time


def test_awaitable_task(decorated_add):
def test_awaitable_decorator(decorated_add):
@task.awaitable()
async def awaitable_func(x, y):
n = 2
Expand All @@ -15,7 +15,7 @@ async def awaitable_func(x, y):
await asyncio.sleep(0.5)
return x + y

wg = WorkGraph(name="test_awaitable")
wg = WorkGraph(name="test_awaitable_decorator")
awaitable_func1 = wg.add_task(awaitable_func, "awaitable_func1", x=1, y=2)
add1 = wg.add_task(decorated_add, "add1", x=1, y=awaitable_func1.outputs["result"])
wg.run()
Expand All @@ -24,13 +24,36 @@ async def awaitable_func(x, y):
assert add1.outputs["result"].value == 4


def test_monitor_decorator():
@task.monitor()
def time_monitor(time: str):
"""Return True if the current time is greater than the given time."""
import datetime

# load the time string
time = datetime.datetime.strptime(time, "%Y-%m-%d %H:%M:%S.%f")

return datetime.datetime.now() > time

wg = WorkGraph(name="test_monitor_decorator")
wg.add_task(
time_monitor,
"time_monitor1",
time=str(datetime.datetime.now() + datetime.timedelta(seconds=5)),
)
wg.run()
report = get_workchain_report(wg.process, "REPORT")
assert "Waiting for child processes: time_monitor1" in report
assert wg.process.is_finished_ok is True


def test_time_monitor(decorated_add):
"""Test the time monitor task."""
wg = WorkGraph(name="test_time_monitor")
monitor1 = wg.add_task(
"workgraph.time_monitor",
"monitor1",
time=str(datetime.datetime.now() + datetime.timedelta(seconds=10)),
time=str(datetime.datetime.now() + datetime.timedelta(seconds=5)),
)
add1 = wg.add_task(decorated_add, "add1", x=1, y=2)
add1.waiting_on.add(monitor1)
Expand Down

0 comments on commit cde38db

Please sign in to comment.