|
13 | 13 |
|
14 | 14 | from frequenz.channels import Bidirectional, Broadcast, Receiver, Sender |
15 | 15 | from google.protobuf.empty_pb2 import Empty # pylint: disable=no-name-in-module |
| 16 | +from pytest_mock import MockerFixture |
16 | 17 |
|
17 | 18 | from frequenz.sdk.actor.power_distributing import ( |
18 | 19 | PowerDistributingActor, |
19 | 20 | Request, |
20 | 21 | Result, |
| 22 | + _BrokenComponents, |
21 | 23 | ) |
22 | 24 | from frequenz.sdk.microgrid._graph import _MicrogridComponentGraph |
23 | 25 | from frequenz.sdk.microgrid.client import Connection |
@@ -738,3 +740,43 @@ async def test_power_distributor_stale_all_components_message( |
738 | 740 | assert result.status == Result.Status.ERROR |
739 | 741 | # User is interested in batteries only. |
740 | 742 | assert result.error_message == "No data for the given batteries {106, 206}" |
| 743 | + |
| 744 | + |
| 745 | +class TestBrokenComponents: |
| 746 | + """Test if BrokenComponents class is working as expected.""" |
| 747 | + |
| 748 | + def test_broken_components(self, mocker: MockerFixture) -> None: |
| 749 | + """Check if components are blocked for 30 seconds. |
| 750 | +
|
| 751 | + Args: |
| 752 | + mocker: pytest mocker |
| 753 | + """ |
| 754 | + datetime_mock = mocker.patch("frequenz.sdk.actor.power_distributing.datetime") |
| 755 | + |
| 756 | + expected_datetime = [ |
| 757 | + datetime.fromisoformat("2001-01-01T00:00:00+00:00"), |
| 758 | + datetime.fromisoformat("2001-01-01T00:00:10+00:00"), |
| 759 | + datetime.fromisoformat("2001-01-01T00:00:20+00:00"), |
| 760 | + ] |
| 761 | + expected_datetime.extend( |
| 762 | + 20 * [datetime.fromisoformat("2001-01-01T00:00:31+00:00")] |
| 763 | + ) |
| 764 | + |
| 765 | + datetime_mock.now.side_effect = expected_datetime |
| 766 | + |
| 767 | + # After 30 seconds components should be considered as working |
| 768 | + broken = _BrokenComponents(30) |
| 769 | + |
| 770 | + for component_id in range(3): |
| 771 | + broken.mark_as_broken(component_id) |
| 772 | + |
| 773 | + # Component 0 was marked as broken 30 seconds ago. Other components, not. |
| 774 | + assert not broken.is_broken(0) |
| 775 | + assert broken.is_broken(1) |
| 776 | + assert broken.is_broken(2) |
| 777 | + assert broken.get_working_subset({0, 1}) == {0} |
| 778 | + assert broken.get_working_subset({0}) == {0} |
| 779 | + |
| 780 | + # If all requested components are marked as broken, |
| 781 | + # then we should mark them as working to not block user command. |
| 782 | + assert broken.get_working_subset({1, 2}) == {1, 2} |
0 commit comments