Skip to content

Commit b7f889e

Browse files
committed
Make MockMicrogrid an async context manager
It will start when the context is entered and stop when it is left. This makes the `MockMicrogrid` easier to work with while ensure proper finalization. Signed-off-by: Leandro Lucarella <[email protected]>
1 parent d2bb6d4 commit b7f889e

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

tests/timeseries/mock_microgrid.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
"""A configurable mock microgrid for testing logical meter formulas."""
55

6+
from __future__ import annotations
67

78
import asyncio
89
from collections.abc import Callable
@@ -569,3 +570,12 @@ async def cleanup(self) -> None:
569570
await cancel_and_await(task)
570571
microgrid.connection_manager._CONNECTION_MANAGER = None
571572
# pylint: enable=protected-access
573+
574+
async def __aenter__(self) -> MockMicrogrid:
575+
"""Enter context manager."""
576+
await self.start()
577+
return self
578+
579+
async def __aexit__(self, exc_type: None, exc_val: None, exc_tb: None) -> None:
580+
"""Exit context manager."""
581+
await self.cleanup()

0 commit comments

Comments
 (0)