Bopytex/test/test_scheduler.py

130 lines
4.0 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
def test_schedule_append():
2022-04-09 19:46:24 +00:00
scheduler = Scheduler()
2022-04-09 04:56:57 +00:00
tasks = [
2022-04-09 19:46:24 +00:00
Task(action="FOO", args={}, deps=["dep1", "dep2"], output="end1"),
Task(action="FOO", args={}, deps=["dep1", "dep3"], output="end2"),
2022-04-09 04:56:57 +00:00
]
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_one_task():
2022-04-09 19:46:24 +00:00
scheduler = Scheduler()
tasks = [Task(action="FOO", args={}, deps=[], output="end")]
2022-04-09 15:08:05 +00:00
scheduler.append(tasks)
2022-04-09 19:46:24 +00:00
2022-04-09 15:08:05 +00:00
assert scheduler.doable_tasks == tasks
2022-04-09 19:46:24 +00:00
result = scheduler.next_task()
assert result == tasks[0]
assert scheduler.tasks == []
2022-04-09 04:32:56 +00:00
assert scheduler.done == ["end"]
def test_schedule_one_task_with_args():
2022-04-09 19:46:24 +00:00
scheduler = Scheduler()
tasks = [Task(action="FOO", args={"task": "one"}, deps=[], output="one")]
scheduler.append(tasks)
result = scheduler.next_task()
assert result == tasks[0]
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():
2022-04-09 19:46:24 +00:00
scheduler = Scheduler()
t1 = Task(action="FOO", args={"task": "one"}, deps=[], output="one")
t2 = Task(action="FOO", args={"task": "two"}, deps=[], output="two")
t3 = Task(action="FOO", 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
2022-04-09 15:08:05 +00:00
assert scheduler.doable_tasks == [t1, t2, t3]
assert scheduler.is_finishable()
2022-04-09 04:56:57 +00:00
2022-04-09 19:46:24 +00:00
result = scheduler.next_task()
assert result == t1
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-09 19:46:24 +00:00
result = scheduler.next_task()
assert result == t2
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-09 19:46:24 +00:00
result = scheduler.next_task()
assert result == t3
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():
2022-04-09 19:46:24 +00:00
scheduler = Scheduler()
t1 = Task(action="FOO", args={"task": "one"}, deps=["three"], output="one")
t2 = Task(action="FOO", args={"task": "two"}, deps=["one"], output="two")
t3 = Task(action="FOO", args={"task": "three"}, deps=[], output="three")
scheduler.append([t1, t2, t3])
2022-04-09 15:08:05 +00:00
assert scheduler.doable_tasks == [t3]
assert scheduler.is_finishable()
2022-04-09 04:56:57 +00:00
2022-04-09 19:46:24 +00:00
result = scheduler.next_task()
assert result == t3
assert scheduler.tasks == [t1, t2]
2022-04-09 04:32:56 +00:00
assert scheduler.done == ["three"]
2022-04-09 15:08:05 +00:00
assert scheduler.doable_tasks == [t1]
2022-04-09 19:46:24 +00:00
result = scheduler.next_task()
assert result == t1
assert scheduler.tasks == [t2]
2022-04-09 04:32:56 +00:00
assert scheduler.done == ["three", "one"]
2022-04-09 15:08:05 +00:00
assert scheduler.doable_tasks == [t2]
2022-04-09 19:46:24 +00:00
result = scheduler.next_task()
assert result == t2
assert scheduler.tasks == []
2022-04-09 04:32:56 +00:00
assert scheduler.done == ["three", "one", "two"]
2022-04-09 19:46:24 +00:00
def test_schedule_multiple_tasks_with_dependencies_loop():
scheduler = Scheduler()
t1 = Task(action="FOO", args={"task": "one"}, deps=["three"], output="one")
t2 = Task(action="FOO", args={"task": "two"}, deps=["one"], output="two")
t3 = Task(action="FOO", args={"task": "three"}, deps=[], output="three")
scheduler.append([t1, t2, t3])
ordered_tasks = []
for task in scheduler.backlog():
ordered_tasks.append(task)
assert ordered_tasks == [t3, t1, t2]
2022-04-08 20:04:47 +00:00
def test_schedule_empty_task():
2022-04-09 19:46:24 +00:00
scheduler = Scheduler()
2022-04-08 20:04:47 +00:00
scheduler.append([])
with pytest.raises(StopIteration):
2022-04-09 19:46:24 +00:00
scheduler.next_task()
2022-04-08 20:04:47 +00:00
def test_schedule_multiple_tasks_with_undoable_dependencies():
2022-04-09 19:46:24 +00:00
scheduler = Scheduler()
t1 = Task(action="FOO", args={"task": "one"}, deps=["three"], output="one")
t2 = Task(action="FOO", 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
2022-04-09 15:08:05 +00:00
assert scheduler.doable_tasks == [t2]
assert not scheduler.is_finishable()
2022-04-09 04:56:57 +00:00
2022-04-09 19:46:24 +00:00
for _ in scheduler.backlog():
pass
2022-04-08 20:04:47 +00:00
assert scheduler.tasks == [t1]
2022-04-09 04:32:56 +00:00
assert scheduler.done == ["two"]
2022-04-09 15:08:05 +00:00
assert scheduler.doable_tasks == []