Skip to content

Commit

Permalink
Do not fork actor execution with snactor fixture
Browse files Browse the repository at this point in the history
Previously running an actor within a test caused the test to be executed
in a forked process and when it was executing the actor the actor was
also being executed in another forked process.

This patch is removing the latter fork from being done to allow easier
mocking and testing.

Signed-off-by: Vinzenz Feenstra <[email protected]>
  • Loading branch information
vinzenz committed Jan 9, 2019
1 parent add5eca commit 744fe8e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
36 changes: 22 additions & 14 deletions leapp/repository/actor_definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class ActorCallContext(object):
"""
Wraps the actor execution into child process.
"""
def __init__(self, definition, logger, messaging):
def __init__(self, definition, logger, messaging, within_pytest=False):
"""
:param definition: Actor definition
:type definition: :py:class:`leapp.repository.actor_definition.ActorDefinition`
Expand All @@ -45,6 +45,7 @@ def __init__(self, definition, logger, messaging):
self.definition = definition
self.logger = logger
self.messaging = messaging
self.within_pytest = within_pytest

@staticmethod
def _do_run(stdin, logger, messaging, definition, args, kwargs):
Expand All @@ -53,6 +54,10 @@ def _do_run(stdin, logger, messaging, definition, args, kwargs):
sys.stdin = os.fdopen(stdin)
except OSError:
pass
self._do_run_impl(logger, messaging, definition, args, kwargs)

@staticmethod
def _do_run_impl(logger, messaging, definition, args, kwargs)
definition.load()
with definition.injected_context():
target_actor = [actor for actor in get_actors() if actor.name == definition.name][0]
Expand All @@ -62,17 +67,20 @@ def run(self, *args, **kwargs):
"""
Performs the actor execution in the child process.
"""
try:
stdin = sys.stdin.fileno()
except UnsupportedOperation:
stdin = None
p = Process(target=self._do_run, args=(stdin, self.logger, self.messaging, self.definition, args, kwargs))
p.start()
p.join()
if p.exitcode != 0:
raise LeappRuntimeError(
'Actor {actorname} unexpectedly terminated with exit code: {exitcode}'
.format(actorname=self.definition.name, exitcode=p.exitcode))
if self.within_pytest:
self._do_run_impl(self.logger, self.messaging, self.definition, args, kwargs)
else:
try:
stdin = sys.stdin.fileno()
except UnsupportedOperation:
stdin = None
p = Process(target=self._do_run, args=(stdin, self.logger, self.messaging, self.definition, args, kwargs))
p.start()
p.join()
if p.exitcode != 0:
raise LeappRuntimeError(
'Actor {actorname} unexpectedly terminated with exit code: {exitcode}'
.format(actorname=self.definition.name, exitcode=p.exitcode))


class ActorDefinition(object):
Expand Down Expand Up @@ -168,8 +176,8 @@ def discover(self):
tag.actors += (self,)
return self._discovery

def __call__(self, messaging=None, logger=None):
return ActorCallContext(definition=self, messaging=messaging, logger=logger)
def __call__(self, messaging=None, logger=None, within_pytest=False):
return ActorCallContext(definition=self, messaging=messaging, logger=logger, within_pytest=within_pytest)

@property
def dialogs(self):
Expand Down
2 changes: 1 addition & 1 deletion leapp/snactor/fixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def run(self):
:return: None
"""
self._actor(messaging=self._messaging).run()
self._actor(messaging=self._messaging, within_pytest=True).run()

def messages(self):
"""
Expand Down

0 comments on commit 744fe8e

Please sign in to comment.