Remove the sleep() in tick() that causes a minimum execution time of 2*PERIOD, more explicit status display and a small test case for console plugin.

This commit is contained in:
Romain Dorgueil
2017-11-04 13:01:43 +01:00
parent 0b8168f7da
commit 2c9729c4ca
4 changed files with 64 additions and 40 deletions

View File

@ -77,7 +77,7 @@ class GraphExecutionContext:
def start(self, starter=None):
self.register_plugins()
self.dispatch(events.START)
self.tick()
self.tick(pause=False)
for node in self.nodes:
if starter is None:
node.start()
@ -85,8 +85,9 @@ class GraphExecutionContext:
starter(node)
self.dispatch(events.STARTED)
def tick(self):
def tick(self, pause=True):
self.dispatch(events.TICK)
if pause:
sleep(self.TICK_PERIOD)
def kill(self):
@ -102,7 +103,7 @@ class GraphExecutionContext:
node_context.stop()
else:
stopper(node_context)
self.tick()
self.tick(pause=False)
self.dispatch(events.STOPPED)
self.unregister_plugins()

View File

@ -42,6 +42,8 @@ class NodeExecutionContext(WithStatistics, LoopingExecutionContext):
def get_flags_as_string(self):
if self.killed:
return '[killed]'
if self.stopped:
return '[done]'
return ''
def write(self, *messages):

View File

@ -30,7 +30,7 @@ class ConsoleOutputPlugin(Plugin):
_stdout = sys.stdout
_stderr = sys.stderr
# When the plugin is started, we'll set the real value of this.
# When the plugin is instanciated, we'll set the real value of this.
isatty = False
# Whether we're on windows, or a real operating system.
@ -50,6 +50,10 @@ class ConsoleOutputPlugin(Plugin):
dispatcher.remove_listener(events.START, self.setup)
def setup(self, event):
# TODO this wont work if one instance is registered with more than one context.
# Two options:
# - move state to context
# - forbid registering more than once
self.prefix = ''
self.counter = 0
self._append_cache = ''
@ -88,31 +92,12 @@ class ConsoleOutputPlugin(Plugin):
for i in context.graph.topologically_sorted_indexes:
node = context[i]
name_suffix = '({})'.format(i) if settings.DEBUG.get() else ''
if node.alive:
liveliness_color = alive_color if node.alive else dead_color
liveliness_prefix = ' {}{}{} '.format(liveliness_color, node.status, Style.RESET_ALL)
_line = ''.join(
(
' ',
alive_color,
'+',
Style.RESET_ALL,
' ',
node.name,
name_suffix,
' ',
node.get_statistics_as_string(),
' ',
node.get_flags_as_string(),
Style.RESET_ALL,
' ',
)
)
else:
_line = ''.join(
(
' ',
dead_color,
'-',
' ',
liveliness_prefix,
node.name,
name_suffix,
' ',

View File

@ -0,0 +1,36 @@
from unittest.mock import MagicMock
import bonobo
from bonobo.execution import events
from bonobo.execution.graph import GraphExecutionContext
from bonobo.plugins.console import ConsoleOutputPlugin
from whistle import EventDispatcher
def test_register_unregister():
plugin = ConsoleOutputPlugin()
dispatcher = EventDispatcher()
plugin.register(dispatcher)
assert plugin.setup in dispatcher.get_listeners(events.START)
assert plugin.tick in dispatcher.get_listeners(events.TICK)
assert plugin.teardown in dispatcher.get_listeners(events.STOPPED)
plugin.unregister(dispatcher)
assert plugin.setup not in dispatcher.get_listeners(events.START)
assert plugin.tick not in dispatcher.get_listeners(events.TICK)
assert plugin.teardown not in dispatcher.get_listeners(events.STOPPED)
def test_one_pass():
plugin = ConsoleOutputPlugin()
dispatcher = EventDispatcher()
plugin.register(dispatcher)
graph = bonobo.Graph()
context = MagicMock(spec=GraphExecutionContext(graph))
dispatcher.dispatch(events.START, events.ExecutionEvent(context))
dispatcher.dispatch(events.TICK, events.ExecutionEvent(context))
dispatcher.dispatch(events.STOPPED, events.ExecutionEvent(context))
plugin.unregister(dispatcher)