diff --git a/tests/execution/contexts/test_node.py b/tests/execution/contexts/test_node.py index 34a8ae1..648743b 100644 --- a/tests/execution/contexts/test_node.py +++ b/tests/execution/contexts/test_node.py @@ -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 + + + +