def test_joblist_from_todo_cycle(job_factory): """ joblist_from_todo detects cycles """ gen = _it.count(20).next class Depender(list): def __call__(self, value): self.append(value) class Todo(object): def __init__(self, *pre): self._succ = [] self._pre = pre def predecessors(self): return self._pre def successors(self): return self._succ job_factory.side_effect = lambda x: _test.Bunch( t=x, id=gen(), depend_on=Depender() ) todo = Todo() todo.successors().append(todo) with assert_raises(_job.DependencyCycle): _job.joblist_from_todo(todo) todo = Todo(1, 2) todo2 = Todo(1, 3) todo3 = Todo(5, 8) todo4 = Todo() todo5 = Todo(3, 8, 13) todo.successors().append(todo2) todo.successors().append(todo4) todo2.successors().append(todo3) todo4.successors().append(todo5) todo.successors().append(todo5) todo2.successors().append(todo5) todo3.successors().append(todo) with assert_raises(_job.DependencyCycle) as e: _job.joblist_from_todo(todo) assert_equals(e.exception.args[0], [todo, todo2, todo3])
def test_joblist_from_todo_simple(job_factory): """ joblist_from_todo works in the trivial case """ gen = _it.count(20).next job_factory.side_effect = lambda x: _test.Bunch(t=x, id=gen()) todo = _test.Bunch(predecessors=lambda: (), successors=lambda: ()) jobs = _job.joblist_from_todo(todo) assert_equals(map(_op.attrgetter('id', 't'), jobs), [(20, todo)])
def test_joblist_from_todo_dag(job_factory): """ joblist_from_todo works for a complex DAG """ gen = _it.count(20).next class Depender(list): def __call__(self, value): self.append(value) class Todo(object): def __init__(self, *pre): self._succ = [] self._pre = pre def predecessors(self): return self._pre def successors(self): return self._succ job_factory.side_effect = lambda x: _test.Bunch( t=x, id=gen(), depend_on=Depender() ) todo = Todo(1, 2) todo2 = Todo(1, 3) todo3 = Todo(5, 8) todo4 = Todo() todo5 = Todo(3, 8, 13) todo.successors().append(todo2) todo.successors().append(todo4) todo2.successors().append(todo3) todo4.successors().append(todo5) todo.successors().append(todo5) todo2.successors().append(todo5) jobs = _job.joblist_from_todo(todo) assert_equals(map(_op.attrgetter('id', 't', 'depend_on'), jobs), [ (20, todo, [1, 2]), (21, todo2, [1, 3, 20]), (22, todo4, [20]), (23, todo5, [3, 8, 13, 20, 21, 22]), (24, todo3, [5, 8, 21]), ])
def test_joblist_from_todo_tree(job_factory): """ joblist_from_todo works for a simple tree """ gen = _it.count(20).next class Depender(list): def __call__(self, value): self.append(value) class Todo(object): def __init__(self): self._succ = [] def predecessors(self): # pylint: disable = no-self-use return () def successors(self): return self._succ job_factory.side_effect = lambda x: _test.Bunch( t=x, id=gen(), depend_on=Depender() ) todo = Todo() todo2 = Todo() todo3 = Todo() todo4 = Todo() todo5 = Todo() todo.successors().append(todo2) todo.successors().append(todo4) todo2.successors().append(todo3) todo4.successors().append(todo5) jobs = _job.joblist_from_todo(todo) assert_equals(map(_op.attrgetter('id', 't', 'depend_on'), jobs), [ (20, todo, []), (21, todo2, [20]), (22, todo4, [20]), (23, todo3, [21]), (24, todo5, [22]), ])