Bopytex/test/test_scheduler.py

118 lines
3.7 KiB
Python
Raw Normal View History

2022-04-09 04:58:44 +00:00
from bopytex.tasks import Task
from bopytex.scheduler import Scheduler
2022-04-08 20:04:47 +00:00
import pytest
2022-04-09 04:32:56 +00:00
def action_done(deps, args, output):
return f"{deps} - {args} - {output} - done"
actions = {"DO": action_done}
def test_schedule_append():
scheduler = Scheduler(actions)
2022-04-09 04:56:57 +00:00
tasks = [
Task(action="DO", args={}, deps=["dep1", "dep2"], output="end1"),
Task(action="DO", args={}, deps=["dep1", "dep3"], output="end2"),
]
scheduler.append(tasks)
assert scheduler.tasks == tasks
2022-04-09 04:56:57 +00:00
assert scheduler.all_deps == {"dep1", "dep2", "dep3"}
assert scheduler.all_output == {"end1", "end2"}
def test_schedule_dispatch():
scheduler = Scheduler(actions)
2022-04-09 04:32:56 +00:00
task = Task(action="DO", args={}, deps=[], output="end")
result = scheduler.dispatch(task)
2022-04-09 04:32:56 +00:00
assert result == "[] - {} - end - done"
def test_schedule_one_task():
scheduler = Scheduler(actions)
2022-04-09 04:32:56 +00:00
scheduler.append([Task(action="DO", args={}, deps=[], output="end")])
2022-04-08 20:04:47 +00:00
result = scheduler.next()
2022-04-09 04:32:56 +00:00
assert result == "[] - {} - end - done"
assert scheduler.tasks == []
2022-04-09 04:32:56 +00:00
assert scheduler.done == ["end"]
def test_schedule_one_task_with_args():
scheduler = Scheduler(actions)
2022-04-09 04:32:56 +00:00
scheduler.append([Task(action="DO", args={"task": "one"}, deps=[], output="one")])
2022-04-08 20:04:47 +00:00
result = scheduler.next()
2022-04-09 04:32:56 +00:00
assert result == "[] - {'task': 'one'} - one - done"
assert scheduler.tasks == []
2022-04-09 04:32:56 +00:00
assert scheduler.done == ["one"]
2022-04-08 19:41:29 +00:00
def test_schedule_multiple_tasks():
scheduler = Scheduler(actions)
2022-04-09 04:32:56 +00:00
t1 = Task(action="DO", args={"task": "one"}, deps=[], output="one")
t2 = Task(action="DO", args={"task": "two"}, deps=[], output="two")
t3 = Task(action="DO", args={"task": "three"}, deps=[], output="three")
2022-04-08 19:41:29 +00:00
scheduler.append([t1, t2, t3])
2022-04-09 04:56:57 +00:00
assert scheduler.is_finishable() == True
2022-04-08 20:04:47 +00:00
result = scheduler.next()
2022-04-09 04:32:56 +00:00
assert result == "[] - {'task': 'one'} - one - done"
2022-04-08 19:41:29 +00:00
assert scheduler.tasks == [t2, t3]
2022-04-09 04:32:56 +00:00
assert scheduler.done == ["one"]
2022-04-08 19:41:29 +00:00
2022-04-08 20:04:47 +00:00
result = scheduler.next()
2022-04-09 04:32:56 +00:00
assert result == "[] - {'task': 'two'} - two - done"
2022-04-08 19:41:29 +00:00
assert scheduler.tasks == [t3]
2022-04-09 04:32:56 +00:00
assert scheduler.done == ["one", "two"]
2022-04-08 19:41:29 +00:00
2022-04-08 20:04:47 +00:00
result = scheduler.next()
2022-04-09 04:32:56 +00:00
assert result == "[] - {'task': 'three'} - three - done"
2022-04-08 19:41:29 +00:00
assert scheduler.tasks == []
2022-04-09 04:32:56 +00:00
assert scheduler.done == ["one", "two", "three"]
def test_schedule_multiple_tasks_with_dependencies():
scheduler = Scheduler(actions)
2022-04-09 04:32:56 +00:00
t1 = Task(action="DO", args={"task": "one"}, deps=["three"], output="one")
t2 = Task(action="DO", args={"task": "two"}, deps=["one"], output="two")
t3 = Task(action="DO", args={"task": "three"}, deps=[], output="three")
scheduler.append([t1, t2, t3])
2022-04-09 04:56:57 +00:00
assert scheduler.is_finishable() == True
2022-04-08 20:04:47 +00:00
result = scheduler.next()
2022-04-09 04:32:56 +00:00
assert result == "[] - {'task': 'three'} - three - done"
assert scheduler.tasks == [t1, t2]
2022-04-09 04:32:56 +00:00
assert scheduler.done == ["three"]
2022-04-08 20:04:47 +00:00
result = scheduler.next()
2022-04-09 04:32:56 +00:00
assert result == "['three'] - {'task': 'one'} - one - done"
assert scheduler.tasks == [t2]
2022-04-09 04:32:56 +00:00
assert scheduler.done == ["three", "one"]
2022-04-08 20:04:47 +00:00
result = scheduler.next()
2022-04-09 04:32:56 +00:00
assert result == "['one'] - {'task': 'two'} - two - done"
assert scheduler.tasks == []
2022-04-09 04:32:56 +00:00
assert scheduler.done == ["three", "one", "two"]
2022-04-08 20:04:47 +00:00
def test_schedule_empty_task():
scheduler = Scheduler(actions)
scheduler.append([])
with pytest.raises(StopIteration):
scheduler.next()
def test_schedule_multiple_tasks_with_undoable_dependencies():
scheduler = Scheduler(actions)
2022-04-09 04:32:56 +00:00
t1 = Task(action="DO", args={"task": "one"}, deps=["three"], output="one")
t2 = Task(action="DO", args={"task": "two"}, deps=[], output="two")
2022-04-08 20:04:47 +00:00
scheduler.append([t1, t2])
2022-04-09 04:56:57 +00:00
assert scheduler.is_finishable() == False
2022-04-08 20:04:47 +00:00
scheduler.run()
assert scheduler.tasks == [t1]
2022-04-09 04:32:56 +00:00
assert scheduler.done == ["two"]