Skip to content

Commit 9b0ec9a

Browse files
authored
Speedup tests by lowering the actors restart delay (#828)
A restart delay was added by #741 and as a side effect the tests became almost 40% slower in total and the actor test became 28 times slower (locally this PR added around 27 seconds to the test run). This commit fixes this by adding a test fixture to automatically reduce the restart delay to 0 for tests. It also adds a context manager to temporarily change the restart delay in case a different value is needed.
2 parents 3906299 + 7e8e495 commit 9b0ec9a

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

tests/conftest.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,43 @@ def actor_auto_restart_once() -> Iterator[None]:
5757
yield
5858

5959

60+
@contextlib.contextmanager
61+
def actor_restart_delay(delay: float | timedelta) -> Iterator[None]:
62+
"""Temporarily set the actor restart delay to a given value.
63+
64+
Example:
65+
```python
66+
with actor_restart_delay(0.0): # No delay
67+
async with MyActor() as actor:
68+
# Do something with actor
69+
```
70+
71+
Args:
72+
delay: The new delay.
73+
"""
74+
if isinstance(delay, float):
75+
delay = timedelta(seconds=delay)
76+
original_delay = _actor.Actor.RESTART_DELAY
77+
print(
78+
f"<actor_restart_delay> Changing the `RESTART_DELAY` from "
79+
f"{original_delay} to {delay}"
80+
)
81+
82+
_actor.Actor.RESTART_DELAY = delay
83+
try:
84+
yield
85+
finally:
86+
print(f"<actor_restart_limit> Resetting restart limit to {original_delay}")
87+
_actor.Actor.RESTART_DELAY = original_delay
88+
89+
90+
@pytest.fixture(scope="session", autouse=True)
91+
def disable_actor_restart_delay() -> Iterator[None]:
92+
"""Disable auto-restart of actors while running tests."""
93+
with actor_restart_delay(0.0):
94+
yield
95+
96+
6097
@pytest.fixture
6198
def fake_time() -> Iterator[time_machine.Coordinates]:
6299
"""Replace real time with a time machine that doesn't automatically tick."""

0 commit comments

Comments
 (0)