Skip to content

Commit 8e25ec7

Browse files
author
Sergio García Prado
committed
ISSUE #209
* Log exception on `Port` start errors.
1 parent 084d6c3 commit 8e25ec7

File tree

16 files changed

+131
-79
lines changed

16 files changed

+131
-79
lines changed

packages/core/minos-microservice-aggregate/tests/utils.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,20 +73,20 @@ def setUp(self) -> None:
7373
self.snapshot_repository,
7474
],
7575
)
76-
self.injector.wire()
76+
self.injector.wire_injections()
7777

7878
async def asyncSetUp(self):
7979
await super().asyncSetUp()
8080

81-
await self.injector.setup()
81+
await self.injector.setup_injections()
8282

8383
async def asyncTearDown(self):
84-
await self.injector.destroy()
84+
await self.injector.destroy_injections()
8585

8686
await super().asyncTearDown()
8787

8888
def tearDown(self) -> None:
89-
self.injector.unwire()
89+
self.injector.unwire_injections()
9090
super().tearDown()
9191

9292

packages/core/minos-microservice-common/minos/common/injections/injectors.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,24 +77,24 @@ def _fn(raw: Union[_InjectableSetupMixin, Type[_InjectableSetupMixin], str]) ->
7777

7878
return injections
7979

80-
async def wire_and_setup(self, *args, **kwargs) -> None:
80+
async def wire_and_setup_injections(self, *args, **kwargs) -> None:
8181
"""Connect the configuration.
8282
8383
:return: This method does not return anything.
8484
"""
8585

86-
self.wire(*args, **kwargs)
87-
await self.setup()
86+
self.wire_injections(*args, **kwargs)
87+
await self.setup_injections()
8888

89-
async def unwire_and_destroy(self) -> None:
89+
async def unwire_and_destroy_injections(self) -> None:
9090
"""Disconnect the configuration.
9191
9292
:return: This method does not return anything.
9393
"""
94-
await self.destroy()
95-
self.unwire()
94+
await self.destroy_injections()
95+
self.unwire_injections()
9696

97-
def wire(self, modules=None, *args, **kwargs) -> None:
97+
def wire_injections(self, modules=None, *args, **kwargs) -> None:
9898
"""Connect the configuration.
9999
100100
:return: This method does not return anything.
@@ -109,17 +109,25 @@ def wire(self, modules=None, *args, **kwargs) -> None:
109109

110110
self.container.wire(modules=modules, *args, **kwargs)
111111

112-
def unwire(self) -> None:
112+
def unwire_injections(self) -> None:
113113
"""Disconnect the configuration.
114114
115115
:return: This method does not return anything.
116116
"""
117117
self.container.unwire()
118118

119-
async def setup(self):
119+
async def setup_injections(self) -> None:
120+
"""Set Up the injections.
121+
122+
:return: This method does not return anything.
123+
"""
120124
await gather(*(injection.setup() for injection in self.injections.values()))
121125

122-
async def destroy(self):
126+
async def destroy_injections(self) -> None:
127+
"""Destroy the injections
128+
129+
:return: This method does not return anything.
130+
"""
123131
await gather(*(injection.destroy() for injection in self.injections.values()))
124132

125133
@cached_property

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ async def _setup(self) -> None:
186186
187187
:return: This method does not return anything.
188188
"""
189-
await self.injector.wire_and_setup(
189+
await self.injector.wire_and_setup_injections(
190190
modules=self._external_modules + self._internal_modules, packages=self._external_packages
191191
)
192192

@@ -199,7 +199,7 @@ async def _destroy(self) -> None:
199199
200200
:return: This method does not return anything.
201201
"""
202-
await self.injector.unwire_and_destroy()
202+
await self.injector.unwire_and_destroy_injections()
203203

204204
@cached_property
205205
def injector(self) -> DependencyInjector:

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import logging
12
from abc import (
23
ABC,
34
abstractmethod,
@@ -7,21 +8,37 @@
78
Service,
89
)
910

11+
logger = logging.getLogger(__name__)
12+
1013

1114
class Port(Service, ABC):
1215
"""Port base class."""
1316

14-
@abstractmethod
1517
async def start(self) -> None:
1618
"""Start the port execution.
1719
1820
:return: This method does not return anything.
1921
"""
22+
try:
23+
return await self._start()
24+
except Exception as exc:
25+
logger.exception(f"Raised an exception on {self!r} start: {exc!r}")
2026

2127
@abstractmethod
28+
def _start(self):
29+
raise NotImplementedError
30+
2231
async def stop(self, err: Exception = None) -> None:
2332
"""Stop the port execution.
2433
2534
:param err: Optional exception that stopped the execution.
2635
:return: This method does not return anything.
2736
"""
37+
try:
38+
return await self._stop(err)
39+
except Exception as exc:
40+
logger.exception(f"Raised an exception on {self!r} stop: {exc!r}")
41+
42+
@abstractmethod
43+
async def _stop(self, err: Exception = None) -> None:
44+
raise NotImplementedError

packages/core/minos-microservice-common/tests/test_common/test_database/test_abc.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ async def test_pool(self):
3333

3434
async def test_pool_with_dependency_injections(self):
3535
injector = DependencyInjector(self.config, [PostgreSqlPool])
36-
await injector.wire_and_setup(modules=[sys.modules[__name__]])
36+
await injector.wire_and_setup_injections(modules=[sys.modules[__name__]])
3737

3838
async with PostgreSqlMinosDatabase(**self.repository_db) as database:
3939
self.assertEqual(injector.postgresql_pool, database.pool)
4040

41-
await injector.unwire_and_destroy()
41+
await injector.unwire_and_destroy_injections()
4242

4343
async def test_submit_query(self):
4444
async with PostgreSqlMinosDatabase(**self.repository_db) as database:

packages/core/minos-microservice-common/tests/test_common/test_injections/test_decorators.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,10 @@ def setUp(self) -> None:
8989
super().setUp()
9090
self.foo = _Foo(34)
9191
self.injector = DependencyInjector(None, [self.foo])
92-
self.injector.wire()
92+
self.injector.wire_injections()
9393

9494
def tearDown(self) -> None:
95-
self.injector.unwire()
95+
self.injector.unwire_injections()
9696
super().tearDown()
9797

9898
def test_decorator_sync(self):

packages/core/minos-microservice-common/tests/test_common/test_injections/test_injectors.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,13 @@ async def test_wire_unwire(self):
6666

6767
mock = MagicMock()
6868
injector.container.wire = mock
69-
await injector.wire_and_setup()
69+
await injector.wire_and_setup_injections()
7070
self.assertEqual(1, mock.call_count)
7171
self.assertEqual(call(modules=[decorators]), mock.call_args)
7272

7373
mock = MagicMock()
7474
injector.container.unwire = mock
75-
await injector.unwire_and_destroy()
75+
await injector.unwire_and_destroy_injections()
7676
self.assertEqual(1, mock.call_count)
7777

7878
async def test_wire_unwire_with_modules(self):
@@ -84,13 +84,13 @@ async def test_wire_unwire_with_modules(self):
8484

8585
mock = MagicMock()
8686
injector.container.wire = mock
87-
await injector.wire_and_setup(modules=[sys.modules[__name__]])
87+
await injector.wire_and_setup_injections(modules=[sys.modules[__name__]])
8888
self.assertEqual(1, mock.call_count)
8989
self.assertEqual(call(modules=[sys.modules[__name__], decorators]), mock.call_args)
9090

9191
mock = MagicMock()
9292
injector.container.unwire = mock
93-
await injector.unwire_and_destroy()
93+
await injector.unwire_and_destroy_injections()
9494
self.assertEqual(1, mock.call_count)
9595

9696

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ def __init__(self, **kwargs):
3030
super().__init__(**kwargs)
3131
self.kwargs = kwargs
3232

33-
async def start(self) -> None:
33+
async def _start(self) -> None:
3434
"""For testing purposes."""
3535

36-
async def stop(self, err: Exception = None) -> None:
36+
async def _stop(self, err: Exception = None) -> None:
3737
"""For testing purposes."""
3838

3939

@@ -99,7 +99,7 @@ async def test_loop(self):
9999

100100
async def test_setup(self):
101101
mock = AsyncMock()
102-
self.launcher.injector.wire_and_setup = mock
102+
self.launcher.injector.wire_and_setup_injections = mock
103103
await self.launcher.setup()
104104

105105
self.assertEqual(1, mock.call_count)
@@ -120,11 +120,11 @@ async def test_setup(self):
120120
await self.launcher.destroy()
121121

122122
async def test_destroy(self):
123-
self.launcher.injector.wire_and_setup = AsyncMock()
123+
self.launcher.injector.wire_and_setup_injections = AsyncMock()
124124
await self.launcher.setup()
125125

126126
mock = AsyncMock()
127-
self.launcher.injector.unwire_and_destroy = mock
127+
self.launcher.injector.unwire_and_destroy_injections = mock
128128
await self.launcher.destroy()
129129

130130
self.assertEqual(1, mock.call_count)

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

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
import unittest
2+
from unittest.mock import (
3+
AsyncMock,
4+
call,
5+
)
26

37
from aiomisc import (
48
Service,
@@ -9,13 +13,63 @@
913
)
1014

1115

12-
class TestPort(unittest.TestCase):
16+
class _Port(Port):
17+
"""For testing purposes."""
18+
19+
def _start(self):
20+
"""For testing purposes."""
21+
22+
async def _stop(self, err: Exception = None) -> None:
23+
"""For testing purposes."""
24+
25+
26+
class TestPort(unittest.IsolatedAsyncioTestCase):
1327
def test_is_subclass(self):
1428
self.assertTrue(issubclass(Port, Service))
1529

1630
def test_abstract(self):
1731
# noinspection PyUnresolvedReferences
18-
self.assertEqual({"start", "stop"}, Port.__abstractmethods__)
32+
self.assertEqual({"_start", "_stop"}, Port.__abstractmethods__)
33+
34+
async def test_start(self):
35+
port = _Port()
36+
37+
mock = AsyncMock()
38+
port._start = mock
39+
40+
await port.start()
41+
42+
self.assertEqual([call()], mock.call_args_list)
43+
44+
async def test_start_raises(self):
45+
port = _Port()
46+
47+
mock = AsyncMock(side_effect=ValueError)
48+
port._start = mock
49+
50+
await port.start()
51+
52+
self.assertEqual([call()], mock.call_args_list)
53+
54+
async def test_stop(self):
55+
port = _Port()
56+
57+
mock = AsyncMock()
58+
port._stop = mock
59+
60+
await port.stop()
61+
62+
self.assertEqual([call(None)], mock.call_args_list)
63+
64+
async def test_stop_raises(self):
65+
port = _Port()
66+
67+
mock = AsyncMock(side_effect=ValueError)
68+
port._stop = mock
69+
70+
await port.stop()
71+
72+
self.assertEqual([call(None)], mock.call_args_list)
1973

2074

2175
if __name__ == "__main__":

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,11 @@ def test_from_config_file_path(self):
7373
async def test_from_config_with_dependency_injection(self):
7474
config = Config(BASE_PATH / "test_config.yml")
7575
injector = DependencyInjector(config)
76-
await injector.wire_and_setup(modules=[sys.modules[__name__]])
76+
await injector.wire_and_setup_injections(modules=[sys.modules[__name__]])
7777

7878
_SetupMixin.from_config()
7979

80-
await injector.unwire_and_destroy()
80+
await injector.unwire_and_destroy_injections()
8181

8282
def test_from_config_raises(self):
8383
with self.assertRaises(NotProvidedException):

0 commit comments

Comments
 (0)