Skip to content

Commit ef3caaa

Browse files
author
Sergio García Prado
committed
ISSUE #334
* Fix bug related with incomprehensible error messages related with failures on setup.
1 parent 72cd513 commit ef3caaa

File tree

2 files changed

+57
-23
lines changed

2 files changed

+57
-23
lines changed

packages/core/minos-microservice-common/minos/common/launchers.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ def launch(self) -> NoReturn:
134134
logger.info("Stopping microservice...")
135135
exception = exc
136136
except Exception as exc: # pragma: no cover
137+
logger.exception("Stopping microservice due to an unhandled exception...")
137138
exception = exc
138139
finally:
139140
self.graceful_shutdown(exception)
@@ -143,14 +144,14 @@ def graceful_launch(self) -> None:
143144
144145
:return: This method does not return anything.
145146
"""
146-
self.loop.run_until_complete(gather(self.setup(), self.entrypoint.__aenter__()))
147+
self.loop.run_until_complete(self.setup())
147148

148149
def graceful_shutdown(self, err: Exception = None) -> None:
149150
"""Shutdown the execution gracefully.
150151
151152
:return: This method does not return anything.
152153
"""
153-
self.loop.run_until_complete(gather(self.entrypoint.__aexit__(None, err, None), self.destroy()))
154+
self.loop.run_until_complete(self.destroy())
154155

155156
@cached_property
156157
def entrypoint(self) -> Entrypoint:
@@ -199,9 +200,10 @@ async def _setup(self) -> None:
199200
200201
:return: This method does not return anything.
201202
"""
202-
await self.injector.wire_and_setup_injections(
203-
modules=self._external_modules + self._internal_modules, packages=self._external_packages
204-
)
203+
modules = self._external_modules + self._internal_modules
204+
packages = self._external_packages
205+
self.injector.wire_injections(modules=modules, packages=packages)
206+
await gather(self.injector.setup_injections(), self.entrypoint.__aenter__())
205207

206208
@property
207209
def _internal_modules(self) -> list[ModuleType]:
@@ -212,7 +214,8 @@ async def _destroy(self) -> None:
212214
213215
:return: This method does not return anything.
214216
"""
215-
await self.injector.unwire_and_destroy_injections()
217+
await gather(self.entrypoint.__aexit__(None, None, None), self.injector.destroy_injections())
218+
self.injector.unwire_injections()
216219

217220
@property
218221
def injections(self) -> dict[str, InjectableMixin]:

packages/core/minos-microservice-common/tests/test_common/test_launchers.py

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import warnings
33
from unittest.mock import (
44
AsyncMock,
5+
MagicMock,
56
call,
67
patch,
78
)
@@ -106,37 +107,67 @@ async def test_loop(self):
106107
self.assertEqual(call(), mock_loop.call_args)
107108

108109
async def test_setup(self):
109-
mock = AsyncMock()
110-
self.launcher.injector.wire_and_setup_injections = mock
111-
await self.launcher.setup()
110+
wire_mock = MagicMock()
111+
setup_mock = AsyncMock()
112+
mock_entrypoint_aenter = AsyncMock()
113+
114+
self.launcher.injector.wire_injections = wire_mock
115+
self.launcher.injector.setup_injections = setup_mock
116+
117+
with patch("minos.common.launchers._create_loop") as mock_loop:
118+
loop = FakeLoop()
119+
mock_loop.return_value = loop
120+
with patch("minos.common.launchers._create_entrypoint") as mock_entrypoint:
121+
entrypoint = FakeEntrypoint()
122+
mock_entrypoint.return_value = entrypoint
123+
124+
entrypoint.__aenter__ = mock_entrypoint_aenter
125+
126+
await self.launcher.setup()
112127

113-
self.assertEqual(1, mock.call_count)
128+
self.assertEqual(1, wire_mock.call_count)
114129
import tests
115130
from minos import (
116131
common,
117132
)
118133

119-
self.assertEqual(0, len(mock.call_args.args))
120-
self.assertEqual(2, len(mock.call_args.kwargs))
121-
observed = mock.call_args.kwargs["modules"]
134+
self.assertEqual(0, len(wire_mock.call_args.args))
135+
self.assertEqual(2, len(wire_mock.call_args.kwargs))
136+
observed = wire_mock.call_args.kwargs["modules"]
122137

123138
self.assertIn(tests, observed)
124139
self.assertIn(common, observed)
125140

126-
self.assertEqual(["tests"], mock.call_args.kwargs["packages"])
141+
self.assertEqual(["tests"], wire_mock.call_args.kwargs["packages"])
127142

128-
await self.launcher.destroy()
143+
self.assertEqual(1, setup_mock.call_count)
144+
self.assertEqual(1, mock_entrypoint_aenter.call_count)
129145

130146
async def test_destroy(self):
131-
self.launcher.injector.wire_and_setup_injections = AsyncMock()
147+
self.launcher._setup = AsyncMock()
132148
await self.launcher.setup()
133149

134-
mock = AsyncMock()
135-
self.launcher.injector.unwire_and_destroy_injections = mock
136-
await self.launcher.destroy()
150+
destroy_mock = AsyncMock()
151+
unwire_mock = MagicMock()
152+
mock_entrypoint_aexit = AsyncMock()
153+
154+
self.launcher.injector.destroy_injections = destroy_mock
155+
self.launcher.injector.unwire_injections = unwire_mock
156+
157+
with patch("minos.common.launchers._create_loop") as mock_loop:
158+
loop = FakeLoop()
159+
mock_loop.return_value = loop
160+
with patch("minos.common.launchers._create_entrypoint") as mock_entrypoint:
161+
entrypoint = FakeEntrypoint()
162+
mock_entrypoint.return_value = entrypoint
163+
164+
entrypoint.__aexit__ = mock_entrypoint_aexit
165+
166+
await self.launcher.destroy()
137167

138-
self.assertEqual(1, mock.call_count)
139-
self.assertEqual(call(), mock.call_args)
168+
self.assertEqual(1, unwire_mock.call_count)
169+
self.assertEqual(1, destroy_mock.call_count)
170+
self.assertEqual(1, mock_entrypoint_aexit.call_count)
140171

141172
def test_launch(self):
142173
mock_setup = AsyncMock()
@@ -157,8 +188,8 @@ def test_launch(self):
157188

158189
self.launcher.launch()
159190

160-
self.assertEqual(1, mock_entrypoint.call_count)
161-
self.assertEqual(1, mock_loop.call_count)
191+
self.assertEqual(1, mock_setup.call_count)
192+
self.assertEqual(1, mock_destroy.call_count)
162193

163194

164195
class TestEntryPointLauncherLoop(unittest.TestCase):

0 commit comments

Comments
 (0)