Skip to content

Commit 034ed05

Browse files
committed
Use a context manager to temporarily change the restart limit
A new utility `actor_restart_limit` context manager is added to be able to temporarily change the actor's restart limit, and it is now used in the `disable_actor_auto_restart` fixture. Signed-off-by: Leandro Lucarella <[email protected]>
1 parent aa8411c commit 034ed05

File tree

1 file changed

+29
-4
lines changed

1 file changed

+29
-4
lines changed

tests/conftest.py

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
# Copyright © 2022 Frequenz Energy-as-a-Service GmbH
33

44
"""Setup for all the tests."""
5+
import collections.abc
6+
import contextlib
7+
58
import pytest
69

710
from frequenz.sdk.actor import _actor
@@ -11,11 +14,33 @@
1114
SAFETY_TIMEOUT = 10.0
1215

1316

14-
@pytest.fixture(scope="session", autouse=True)
15-
def disable_actor_auto_restart() -> collections.abc.Iterator[None]:
16-
"""Disable auto-restart of actors while running tests."""
17+
@contextlib.contextmanager
18+
def actor_restart_limit(limit: int) -> collections.abc.Iterator[None]:
19+
"""Temporarily set the actor restart limit to a given value.
20+
21+
Example:
22+
```python
23+
with actor_restart_limit(0): # No restart
24+
async with MyActor() as actor:
25+
# Do something with actor
26+
```
27+
28+
Args:
29+
limit: The new limit.
30+
"""
1731
# pylint: disable=protected-access
1832
original_limit = _actor.Actor._restart_limit
19-
_actor.Actor._restart_limit = 0
33+
print(
34+
f"<actor_restart_limit> Changing the restart limit from {original_limit} to {limit}"
35+
)
36+
_actor.Actor._restart_limit = limit
2037
yield
38+
print(f"<actor_restart_limit> Resetting restart limit to {original_limit}")
2139
_actor.Actor._restart_limit = original_limit
40+
41+
42+
@pytest.fixture(scope="session", autouse=True)
43+
def disable_actor_auto_restart() -> collections.abc.Iterator[None]:
44+
"""Disable auto-restart of actors while running tests."""
45+
with actor_restart_limit(0):
46+
yield

0 commit comments

Comments
 (0)