Bopytex/test/test_scheduler.py

164 lines
5.4 KiB
Python
Raw Permalink Normal View History

2022-07-28 07:39:51 +00:00
import pytest
from bopytex.message import Message
from bopytex.scheduler import Scheduler
2022-07-28 07:39:51 +00:00
from bopytex.tasks import Task
from .fakes.dispatcher import fake_dispatcher
def test_schedule_append():
scheduler = Scheduler(dispatcher=fake_dispatcher)
2022-04-09 04:56:57 +00:00
tasks = [
Task(action="FAKE", args={}, deps=["dep1", "dep2"], output="end1"),
Task(action="FAKE", 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():
scheduler = Scheduler(dispatcher=fake_dispatcher)
tasks = [Task(action="FAKE", 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.status == 0
assert result.out == ["FAKE - {} - [] - end"]
assert result.err == []
assert scheduler.tasks == []
assert scheduler.output_done == ["end"]
def test_schedule_one_task_with_args():
scheduler = Scheduler(dispatcher=fake_dispatcher)
tasks = [Task(action="FAKE", args={"task": "one"}, deps=[], output="one")]
2022-04-09 19:46:24 +00:00
scheduler.append(tasks)
result = scheduler.next_task()
assert result.status == 0
assert result.out == ["FAKE - {'task': 'one'} - [] - one"]
assert result.err == []
assert scheduler.tasks == []
assert scheduler.output_done == ["one"]
2022-04-08 19:41:29 +00:00
def test_schedule_multiple_tasks():
scheduler = Scheduler(dispatcher=fake_dispatcher)
t1 = Task(action="FAKE", args={"task": "one"}, deps=[], output="one")
t2 = Task(action="FAKE", args={"task": "two"}, deps=[], output="two")
t3 = Task(action="FAKE", 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.status == 0
2022-04-08 19:41:29 +00:00
assert scheduler.tasks == [t2, t3]
assert scheduler.output_done == ["one"]
2022-04-08 19:41:29 +00:00
2022-04-09 19:46:24 +00:00
result = scheduler.next_task()
assert result.status == 0
2022-04-08 19:41:29 +00:00
assert scheduler.tasks == [t3]
assert scheduler.output_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.status == 0
2022-04-08 19:41:29 +00:00
assert scheduler.tasks == []
assert scheduler.output_done == ["one", "two", "three"]
def test_schedule_multiple_tasks_with_dependencies():
scheduler = Scheduler(dispatcher=fake_dispatcher)
t1 = Task(action="FAKE", args={"task": "one"}, deps=["three"], output="one")
t2 = Task(action="FAKE", args={"task": "two"}, deps=["one"], output="two")
t3 = Task(action="FAKE", 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.status == 0
assert scheduler.tasks == [t1, t2]
assert scheduler.output_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.status == 0
assert scheduler.tasks == [t2]
assert scheduler.output_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.status == 0
assert scheduler.tasks == []
assert scheduler.output_done == ["three", "one", "two"]
2022-04-09 19:46:24 +00:00
def test_schedule_multiple_tasks_with_dependencies_loop():
scheduler = Scheduler(dispatcher=fake_dispatcher)
t1 = Task(action="FAKE", args={"task": "one"}, deps=["three"], output="one")
t2 = Task(action="FAKE", args={"task": "two"}, deps=["one"], output="two")
t3 = Task(action="FAKE", args={"task": "three"}, deps=[], output="three")
2022-04-09 19:46:24 +00:00
scheduler.append([t1, t2, t3])
for task in scheduler.backlog():
pass
assert scheduler.output_done == ["three", "one", "two"]
2022-04-08 20:04:47 +00:00
def test_schedule_empty_task():
scheduler = Scheduler(dispatcher=fake_dispatcher)
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():
scheduler = Scheduler(dispatcher=fake_dispatcher)
t1 = Task(action="FAKE", args={"task": "one"}, deps=["three"], output="one")
t2 = Task(action="FAKE", 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]
assert scheduler.output_done == ["two"]
assert scheduler.doable_tasks == []
def test_schedule_multiple_tasks_with_failling_tasks():
scheduler = Scheduler(dispatcher=fake_dispatcher)
t1 = Task(action="FAILURE", args={"task": "one"}, deps=["three"], output="one")
t2 = Task(action="FAKE", args={"task": "two"}, deps=["one"], output="two")
t3 = Task(action="FAKE", args={"task": "three"}, deps=[], output="three")
t4 = Task(action="FAILURE", args={"task": "four"}, deps=[], output="four")
scheduler.append([t1, t2, t3, t4])
assert scheduler.doable_tasks == [t3, t4]
assert scheduler.is_finishable()
status = []
for message in scheduler.backlog():
status.append(message.status)
assert status == [0, 1, 1]
assert scheduler.tasks == [t2]
assert scheduler.failed_tasks == [t1, t4]
assert scheduler.output_done == ["three"]
2022-04-09 15:08:05 +00:00
assert scheduler.doable_tasks == []