2022-07-28 07:39:51 +00:00
|
|
|
import pytest
|
|
|
|
|
2022-04-13 13:09:08 +00:00
|
|
|
from bopytex.message import Message
|
2022-04-08 19:24:43 +00:00
|
|
|
from bopytex.scheduler import Scheduler
|
2022-07-28 07:39:51 +00:00
|
|
|
from bopytex.tasks import Task
|
|
|
|
|
2022-04-13 13:09:08 +00:00
|
|
|
from .fakes.dispatcher import fake_dispatcher
|
2022-04-08 19:24:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_schedule_append():
|
2022-04-13 13:09:08 +00:00
|
|
|
scheduler = Scheduler(dispatcher=fake_dispatcher)
|
2022-04-09 04:56:57 +00:00
|
|
|
tasks = [
|
2022-04-13 13:09:08 +00:00
|
|
|
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
|
|
|
]
|
2022-04-08 19:24:43 +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"}
|
2022-04-08 19:24:43 +00:00
|
|
|
|
2022-04-08 19:35:55 +00:00
|
|
|
|
2022-04-08 19:24:43 +00:00
|
|
|
def test_schedule_one_task():
|
2022-04-13 13:09:08 +00:00
|
|
|
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()
|
2022-04-13 13:09:08 +00:00
|
|
|
assert result.status == 0
|
|
|
|
assert result.out == ["FAKE - {} - [] - end"]
|
|
|
|
assert result.err == []
|
|
|
|
|
2022-04-08 19:24:43 +00:00
|
|
|
assert scheduler.tasks == []
|
2022-04-13 13:20:34 +00:00
|
|
|
assert scheduler.output_done == ["end"]
|
2022-04-08 19:35:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_schedule_one_task_with_args():
|
2022-04-13 13:09:08 +00:00
|
|
|
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()
|
|
|
|
|
2022-04-13 13:09:08 +00:00
|
|
|
assert result.status == 0
|
|
|
|
assert result.out == ["FAKE - {'task': 'one'} - [] - one"]
|
|
|
|
assert result.err == []
|
|
|
|
|
2022-04-08 19:35:55 +00:00
|
|
|
assert scheduler.tasks == []
|
2022-04-13 13:20:34 +00:00
|
|
|
assert scheduler.output_done == ["one"]
|
2022-04-08 19:41:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_schedule_multiple_tasks():
|
2022-04-13 13:09:08 +00:00
|
|
|
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()
|
2022-04-13 13:09:08 +00:00
|
|
|
assert result.status == 0
|
2022-04-08 19:41:29 +00:00
|
|
|
assert scheduler.tasks == [t2, t3]
|
2022-04-13 13:20:34 +00:00
|
|
|
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()
|
2022-04-13 13:09:08 +00:00
|
|
|
assert result.status == 0
|
2022-04-08 19:41:29 +00:00
|
|
|
assert scheduler.tasks == [t3]
|
2022-04-13 13:20:34 +00:00
|
|
|
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()
|
2022-04-13 13:09:08 +00:00
|
|
|
assert result.status == 0
|
2022-04-08 19:41:29 +00:00
|
|
|
assert scheduler.tasks == []
|
2022-04-13 13:20:34 +00:00
|
|
|
assert scheduler.output_done == ["one", "two", "three"]
|
2022-04-08 19:49:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_schedule_multiple_tasks_with_dependencies():
|
2022-04-13 13:09:08 +00:00
|
|
|
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-08 19:49:18 +00:00
|
|
|
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()
|
2022-04-13 13:09:08 +00:00
|
|
|
assert result.status == 0
|
2022-04-08 19:49:18 +00:00
|
|
|
assert scheduler.tasks == [t1, t2]
|
2022-04-13 13:20:34 +00:00
|
|
|
assert scheduler.output_done == ["three"]
|
2022-04-09 15:08:05 +00:00
|
|
|
assert scheduler.doable_tasks == [t1]
|
2022-04-08 19:49:18 +00:00
|
|
|
|
2022-04-09 19:46:24 +00:00
|
|
|
result = scheduler.next_task()
|
2022-04-13 13:09:08 +00:00
|
|
|
assert result.status == 0
|
2022-04-08 19:49:18 +00:00
|
|
|
assert scheduler.tasks == [t2]
|
2022-04-13 13:20:34 +00:00
|
|
|
assert scheduler.output_done == ["three", "one"]
|
2022-04-09 15:08:05 +00:00
|
|
|
assert scheduler.doable_tasks == [t2]
|
2022-04-08 19:49:18 +00:00
|
|
|
|
2022-04-09 19:46:24 +00:00
|
|
|
result = scheduler.next_task()
|
2022-04-13 13:09:08 +00:00
|
|
|
assert result.status == 0
|
2022-04-08 19:49:18 +00:00
|
|
|
assert scheduler.tasks == []
|
2022-04-13 13:20:34 +00:00
|
|
|
assert scheduler.output_done == ["three", "one", "two"]
|
2022-04-08 19:49:18 +00:00
|
|
|
|
2022-04-13 13:09:08 +00:00
|
|
|
|
2022-04-09 19:46:24 +00:00
|
|
|
def test_schedule_multiple_tasks_with_dependencies_loop():
|
2022-04-13 13:09:08 +00:00
|
|
|
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():
|
2022-04-13 13:09:08 +00:00
|
|
|
pass
|
|
|
|
|
2022-04-13 13:20:34 +00:00
|
|
|
assert scheduler.output_done == ["three", "one", "two"]
|
2022-04-13 13:09:08 +00:00
|
|
|
|
2022-04-08 20:04:47 +00:00
|
|
|
|
|
|
|
def test_schedule_empty_task():
|
2022-04-13 13:09:08 +00:00
|
|
|
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():
|
2022-04-13 13:09:08 +00:00
|
|
|
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]
|
2022-04-13 13:20:34 +00:00
|
|
|
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 == []
|