|
| 1 | +# License: MIT |
| 2 | +# Copyright © 2023 Frequenz Energy-as-a-Service GmbH |
| 3 | + |
| 4 | +"""Simple tests for the actor runner.""" |
| 5 | + |
| 6 | +import asyncio |
| 7 | +import time |
| 8 | +from typing import Iterator |
| 9 | + |
| 10 | +import async_solipsism |
| 11 | +import pytest |
| 12 | +import time_machine |
| 13 | + |
| 14 | +from frequenz.sdk.actor import actor, run |
| 15 | + |
| 16 | + |
| 17 | +# Setting 'autouse' has no effect as this method replaces the event loop for all tests in the file. |
| 18 | +@pytest.fixture() |
| 19 | +def event_loop() -> Iterator[async_solipsism.EventLoop]: |
| 20 | + """Replace the loop with one that doesn't interact with the outside world.""" |
| 21 | + loop = async_solipsism.EventLoop() |
| 22 | + yield loop |
| 23 | + loop.close() |
| 24 | + |
| 25 | + |
| 26 | +@pytest.fixture |
| 27 | +def fake_time() -> Iterator[time_machine.Coordinates]: |
| 28 | + """Replace real time with a time machine that doesn't automatically tick.""" |
| 29 | + with time_machine.travel(0, tick=False) as traveller: |
| 30 | + yield traveller |
| 31 | + |
| 32 | + |
| 33 | +@actor |
| 34 | +class FaultyActor: |
| 35 | + """A test faulty actor.""" |
| 36 | + |
| 37 | + def __init__(self, name: str) -> None: |
| 38 | + """Initialize the faulty actor. |
| 39 | +
|
| 40 | + Args: |
| 41 | + name: the name of the faulty actor. |
| 42 | + """ |
| 43 | + self.name = name |
| 44 | + self.is_cancelled = False |
| 45 | + |
| 46 | + async def run(self) -> None: |
| 47 | + """Run the faulty actor. |
| 48 | +
|
| 49 | + Raises: |
| 50 | + CancelledError: the exception causes the actor to be cancelled |
| 51 | + """ |
| 52 | + self.is_cancelled = True |
| 53 | + raise asyncio.CancelledError(f"Faulty Actor {self.name} failed") |
| 54 | + |
| 55 | + |
| 56 | +@actor |
| 57 | +class SleepyActor: |
| 58 | + """A test actor that sleeps a short time.""" |
| 59 | + |
| 60 | + def __init__(self, name: str, sleep_duration: float) -> None: |
| 61 | + """Initialize the sleepy actor. |
| 62 | +
|
| 63 | + Args: |
| 64 | + name: the name of the sleepy actor. |
| 65 | + sleep_duration: the virtual duration to sleep while running. |
| 66 | + """ |
| 67 | + self.name = name |
| 68 | + self.sleep_duration = sleep_duration |
| 69 | + self.is_joined = False |
| 70 | + |
| 71 | + async def run(self) -> None: |
| 72 | + """Run the sleepy actor.""" |
| 73 | + while time.time() < self.sleep_duration: |
| 74 | + await asyncio.sleep(0.1) |
| 75 | + |
| 76 | + self.is_joined = True |
| 77 | + |
| 78 | + |
| 79 | +# pylint: disable=redefined-outer-name |
| 80 | +async def test_all_actors_done(fake_time: time_machine.Coordinates) -> None: |
| 81 | + """Test the completion of all actors.""" |
| 82 | + |
| 83 | + sleepy_actor_1 = SleepyActor("sleepy_actor_1", sleep_duration=1.0) |
| 84 | + sleepy_actor_2 = SleepyActor("sleepy_actor_2", sleep_duration=2.0) |
| 85 | + |
| 86 | + test_task = asyncio.create_task(run(sleepy_actor_1, sleepy_actor_2)) |
| 87 | + |
| 88 | + sleep_duration = time.time() |
| 89 | + |
| 90 | + assert sleep_duration == 0 |
| 91 | + assert sleepy_actor_1.is_joined is False |
| 92 | + assert sleepy_actor_2.is_joined is False |
| 93 | + |
| 94 | + while not test_task.done(): |
| 95 | + if sleep_duration < 1: |
| 96 | + assert sleepy_actor_1.is_joined is False |
| 97 | + assert sleepy_actor_2.is_joined is False |
| 98 | + elif sleep_duration < 2: |
| 99 | + assert sleepy_actor_1.is_joined is True |
| 100 | + assert sleepy_actor_2.is_joined is False |
| 101 | + elif sleep_duration == 2: |
| 102 | + assert sleepy_actor_1.is_joined is True |
| 103 | + assert sleepy_actor_2.is_joined is True |
| 104 | + |
| 105 | + fake_time.shift(0.5) |
| 106 | + sleep_duration = time.time() |
| 107 | + await asyncio.sleep(1) |
| 108 | + |
| 109 | + assert sleepy_actor_1.is_joined |
| 110 | + assert sleepy_actor_2.is_joined |
| 111 | + |
| 112 | + |
| 113 | +async def test_actors_cancelled() -> None: |
| 114 | + """Test the completion of actors being cancelled.""" |
| 115 | + |
| 116 | + faulty_actors = [FaultyActor(f"faulty_actor_{idx}") for idx in range(5)] |
| 117 | + |
| 118 | + await asyncio.wait_for(run(*faulty_actors), timeout=1.0) |
| 119 | + |
| 120 | + for faulty_actor in faulty_actors: |
| 121 | + assert faulty_actor.is_cancelled |
0 commit comments