|
19 | 19 | # pylint: disable=no-member |
20 | 20 |
|
21 | 21 |
|
22 | | -class TestDataSourcingActor: |
| 22 | +async def test_data_sourcing_actor() -> None: |
23 | 23 | """Tests for the DataSourcingActor.""" |
24 | | - |
25 | | - async def test_data_sourcing_actor(self) -> None: |
26 | | - """Tests for the DataSourcingActor.""" |
27 | | - servicer = mock_api.MockMicrogridServicer() |
28 | | - server = mock_api.MockGrpcServer(servicer, port=57899) |
29 | | - await server.start() |
30 | | - |
31 | | - servicer.add_component( |
32 | | - 1, components_pb.ComponentCategory.COMPONENT_CATEGORY_GRID |
| 24 | + servicer = mock_api.MockMicrogridServicer() |
| 25 | + server = mock_api.MockGrpcServer(servicer, port=57899) |
| 26 | + await server.start() |
| 27 | + |
| 28 | + servicer.add_component(1, components_pb.ComponentCategory.COMPONENT_CATEGORY_GRID) |
| 29 | + servicer.add_component(4, components_pb.ComponentCategory.COMPONENT_CATEGORY_METER) |
| 30 | + servicer.add_component(7, components_pb.ComponentCategory.COMPONENT_CATEGORY_METER) |
| 31 | + servicer.add_component( |
| 32 | + 8, components_pb.ComponentCategory.COMPONENT_CATEGORY_INVERTER |
| 33 | + ) |
| 34 | + servicer.add_component( |
| 35 | + 9, components_pb.ComponentCategory.COMPONENT_CATEGORY_BATTERY |
| 36 | + ) |
| 37 | + |
| 38 | + servicer.add_connection(1, 4) |
| 39 | + servicer.add_connection(1, 7) |
| 40 | + servicer.add_connection(7, 8) |
| 41 | + servicer.add_connection(8, 9) |
| 42 | + |
| 43 | + await connection_manager.initialize("[::1]", 57899) |
| 44 | + |
| 45 | + req_chan = Broadcast[ComponentMetricRequest](name="data_sourcing_requests") |
| 46 | + req_sender = req_chan.new_sender() |
| 47 | + |
| 48 | + registry = ChannelRegistry(name="test-registry") |
| 49 | + |
| 50 | + async with DataSourcingActor(req_chan.new_receiver(), registry): |
| 51 | + active_power_request = ComponentMetricRequest( |
| 52 | + "test-namespace", 4, ComponentMetricId.ACTIVE_POWER, None |
33 | 53 | ) |
34 | | - servicer.add_component( |
35 | | - 4, components_pb.ComponentCategory.COMPONENT_CATEGORY_METER |
| 54 | + active_power_recv = registry.get_or_create( |
| 55 | + Sample[Quantity], active_power_request.get_channel_name() |
| 56 | + ).new_receiver() |
| 57 | + await req_sender.send(active_power_request) |
| 58 | + |
| 59 | + reactive_power_request = ComponentMetricRequest( |
| 60 | + "test-namespace", 4, ComponentMetricId.REACTIVE_POWER, None |
36 | 61 | ) |
37 | | - servicer.add_component( |
38 | | - 7, components_pb.ComponentCategory.COMPONENT_CATEGORY_METER |
| 62 | + _ = registry.get_or_create( |
| 63 | + Sample[Quantity], reactive_power_request.get_channel_name() |
| 64 | + ).new_receiver() |
| 65 | + await req_sender.send(reactive_power_request) |
| 66 | + |
| 67 | + soc_request = ComponentMetricRequest( |
| 68 | + "test-namespace", 9, ComponentMetricId.SOC, None |
39 | 69 | ) |
40 | | - servicer.add_component( |
41 | | - 8, components_pb.ComponentCategory.COMPONENT_CATEGORY_INVERTER |
| 70 | + soc_recv = registry.get_or_create( |
| 71 | + Sample[Quantity], soc_request.get_channel_name() |
| 72 | + ).new_receiver() |
| 73 | + await req_sender.send(soc_request) |
| 74 | + |
| 75 | + soc2_request = ComponentMetricRequest( |
| 76 | + "test-namespace", 9, ComponentMetricId.SOC, None |
42 | 77 | ) |
43 | | - servicer.add_component( |
44 | | - 9, components_pb.ComponentCategory.COMPONENT_CATEGORY_BATTERY |
| 78 | + soc2_recv = registry.get_or_create( |
| 79 | + Sample[Quantity], soc2_request.get_channel_name() |
| 80 | + ).new_receiver() |
| 81 | + await req_sender.send(soc2_request) |
| 82 | + |
| 83 | + for _ in range(3): |
| 84 | + sample = await soc_recv.receive() |
| 85 | + assert sample.value is not None |
| 86 | + assert 9.0 == sample.value.base_value |
| 87 | + |
| 88 | + sample = await soc2_recv.receive() |
| 89 | + assert sample.value is not None |
| 90 | + assert 9.0 == sample.value.base_value |
| 91 | + |
| 92 | + sample = await active_power_recv.receive() |
| 93 | + assert sample.value is not None |
| 94 | + assert 100.0 == sample.value.base_value |
| 95 | + |
| 96 | + assert await server.graceful_shutdown() |
| 97 | + connection_manager._CONNECTION_MANAGER = ( # pylint: disable=protected-access |
| 98 | + None |
45 | 99 | ) |
46 | | - |
47 | | - servicer.add_connection(1, 4) |
48 | | - servicer.add_connection(1, 7) |
49 | | - servicer.add_connection(7, 8) |
50 | | - servicer.add_connection(8, 9) |
51 | | - |
52 | | - await connection_manager.initialize("[::1]", 57899) |
53 | | - |
54 | | - req_chan = Broadcast[ComponentMetricRequest](name="data_sourcing_requests") |
55 | | - req_sender = req_chan.new_sender() |
56 | | - |
57 | | - registry = ChannelRegistry(name="test-registry") |
58 | | - |
59 | | - async with DataSourcingActor(req_chan.new_receiver(), registry): |
60 | | - active_power_request = ComponentMetricRequest( |
61 | | - "test-namespace", 4, ComponentMetricId.ACTIVE_POWER, None |
62 | | - ) |
63 | | - active_power_recv = registry.get_or_create( |
64 | | - Sample[Quantity], active_power_request.get_channel_name() |
65 | | - ).new_receiver() |
66 | | - await req_sender.send(active_power_request) |
67 | | - |
68 | | - reactive_power_request = ComponentMetricRequest( |
69 | | - "test-namespace", 4, ComponentMetricId.REACTIVE_POWER, None |
70 | | - ) |
71 | | - _ = registry.get_or_create( |
72 | | - Sample[Quantity], reactive_power_request.get_channel_name() |
73 | | - ).new_receiver() |
74 | | - await req_sender.send(reactive_power_request) |
75 | | - |
76 | | - soc_request = ComponentMetricRequest( |
77 | | - "test-namespace", 9, ComponentMetricId.SOC, None |
78 | | - ) |
79 | | - soc_recv = registry.get_or_create( |
80 | | - Sample[Quantity], soc_request.get_channel_name() |
81 | | - ).new_receiver() |
82 | | - await req_sender.send(soc_request) |
83 | | - |
84 | | - soc2_request = ComponentMetricRequest( |
85 | | - "test-namespace", 9, ComponentMetricId.SOC, None |
86 | | - ) |
87 | | - soc2_recv = registry.get_or_create( |
88 | | - Sample[Quantity], soc2_request.get_channel_name() |
89 | | - ).new_receiver() |
90 | | - await req_sender.send(soc2_request) |
91 | | - |
92 | | - for _ in range(3): |
93 | | - sample = await soc_recv.receive() |
94 | | - assert sample.value is not None |
95 | | - assert 9.0 == sample.value.base_value |
96 | | - |
97 | | - sample = await soc2_recv.receive() |
98 | | - assert sample.value is not None |
99 | | - assert 9.0 == sample.value.base_value |
100 | | - |
101 | | - sample = await active_power_recv.receive() |
102 | | - assert sample.value is not None |
103 | | - assert 100.0 == sample.value.base_value |
104 | | - |
105 | | - assert await server.graceful_shutdown() |
106 | | - connection_manager._CONNECTION_MANAGER = ( # pylint: disable=protected-access |
107 | | - None |
108 | | - ) |
0 commit comments