Skip to content

Commit 744fe8e

Browse files
committed
Do not fork actor execution with snactor fixture
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 <vfeenstr@redhat.com>
1 parent add5eca commit 744fe8e

2 files changed

Lines changed: 23 additions & 15 deletions

File tree

leapp/repository/actor_definition.py

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class ActorCallContext(object):
3333
"""
3434
Wraps the actor execution into child process.
3535
"""
36-
def __init__(self, definition, logger, messaging):
36+
def __init__(self, definition, logger, messaging, within_pytest=False):
3737
"""
3838
:param definition: Actor definition
3939
:type definition: :py:class:`leapp.repository.actor_definition.ActorDefinition`
@@ -45,6 +45,7 @@ def __init__(self, definition, logger, messaging):
4545
self.definition = definition
4646
self.logger = logger
4747
self.messaging = messaging
48+
self.within_pytest = within_pytest
4849

4950
@staticmethod
5051
def _do_run(stdin, logger, messaging, definition, args, kwargs):
@@ -53,6 +54,10 @@ def _do_run(stdin, logger, messaging, definition, args, kwargs):
5354
sys.stdin = os.fdopen(stdin)
5455
except OSError:
5556
pass
57+
self._do_run_impl(logger, messaging, definition, args, kwargs)
58+
59+
@staticmethod
60+
def _do_run_impl(logger, messaging, definition, args, kwargs)
5661
definition.load()
5762
with definition.injected_context():
5863
target_actor = [actor for actor in get_actors() if actor.name == definition.name][0]
@@ -62,17 +67,20 @@ def run(self, *args, **kwargs):
6267
"""
6368
Performs the actor execution in the child process.
6469
"""
65-
try:
66-
stdin = sys.stdin.fileno()
67-
except UnsupportedOperation:
68-
stdin = None
69-
p = Process(target=self._do_run, args=(stdin, self.logger, self.messaging, self.definition, args, kwargs))
70-
p.start()
71-
p.join()
72-
if p.exitcode != 0:
73-
raise LeappRuntimeError(
74-
'Actor {actorname} unexpectedly terminated with exit code: {exitcode}'
75-
.format(actorname=self.definition.name, exitcode=p.exitcode))
70+
if self.within_pytest:
71+
self._do_run_impl(self.logger, self.messaging, self.definition, args, kwargs)
72+
else:
73+
try:
74+
stdin = sys.stdin.fileno()
75+
except UnsupportedOperation:
76+
stdin = None
77+
p = Process(target=self._do_run, args=(stdin, self.logger, self.messaging, self.definition, args, kwargs))
78+
p.start()
79+
p.join()
80+
if p.exitcode != 0:
81+
raise LeappRuntimeError(
82+
'Actor {actorname} unexpectedly terminated with exit code: {exitcode}'
83+
.format(actorname=self.definition.name, exitcode=p.exitcode))
7684

7785

7886
class ActorDefinition(object):
@@ -168,8 +176,8 @@ def discover(self):
168176
tag.actors += (self,)
169177
return self._discovery
170178

171-
def __call__(self, messaging=None, logger=None):
172-
return ActorCallContext(definition=self, messaging=messaging, logger=logger)
179+
def __call__(self, messaging=None, logger=None, within_pytest=False):
180+
return ActorCallContext(definition=self, messaging=messaging, logger=logger, within_pytest=within_pytest)
173181

174182
@property
175183
def dialogs(self):

leapp/snactor/fixture.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ def run(self):
101101
102102
:return: None
103103
"""
104-
self._actor(messaging=self._messaging).run()
104+
self._actor(messaging=self._messaging, within_pytest=True).run()
105105

106106
def messages(self):
107107
"""

0 commit comments

Comments
 (0)