[tests] adds node context lifecycle test.(

This commit is contained in:
Romain Dorgueil
2017-11-04 13:36:54 +01:00
parent 83fc1743fc
commit 25e919ab96

View File

@ -1,4 +1,9 @@
from unittest.mock import MagicMock
import pytest
from bonobo import Bag, Graph
from bonobo.execution.contexts.node import NodeExecutionContext
from bonobo.execution.strategies import NaiveStrategy
from bonobo.util.testing import BufferingNodeExecutionContext, BufferingGraphExecutionContext
@ -179,3 +184,46 @@ def test_node_tuple_dict():
assert len(output) == 2
assert output[0] == ('foo', 'bar', {'id': 1})
assert output[1] == ('foo', 'baz', {'id': 2})
def test_node_lifecycle_natural():
func = MagicMock()
ctx = NodeExecutionContext(func)
assert not any((ctx.started, ctx.stopped, ctx.killed, ctx.alive))
# cannot stop before start
with pytest.raises(RuntimeError):
ctx.stop()
assert not any((ctx.started, ctx.stopped, ctx.killed, ctx.alive))
# turn the key
ctx.start()
assert all((ctx.started, ctx.alive)) and not any((ctx.stopped, ctx.killed))
ctx.stop()
assert all((ctx.started, ctx.stopped)) and not any((ctx.alive, ctx.killed))
def test_node_lifecycle_with_kill():
func = MagicMock()
ctx = NodeExecutionContext(func)
assert not any((ctx.started, ctx.stopped, ctx.killed, ctx.alive))
# cannot kill before start
with pytest.raises(RuntimeError):
ctx.kill()
assert not any((ctx.started, ctx.stopped, ctx.killed, ctx.alive))
# turn the key
ctx.start()
assert all((ctx.started, ctx.alive)) and not any((ctx.stopped, ctx.killed))
ctx.kill()
assert all((ctx.started, ctx.killed, ctx.alive)) and not ctx.stopped
ctx.stop()
assert all((ctx.started, ctx.killed, ctx.stopped)) and not ctx.alive