3535# pylint: disable=missing-class-docstring,no-member
3636
3737
38+ # This incrementing port is a hack to avoid the inherent flakiness of the approach of
39+ # using a real GRPC (mock) server. The server seems to stay alive for a short time after
40+ # the test is finished, which causes the next test to fail because the port is already
41+ # in use.
42+ # This is a workaround until we have a better solution.
43+ # See https://github.com/frequenz-floss/frequenz-sdk-python/issues/662
44+ _CURRENT_PORT : int = 57897
45+
46+
3847@contextlib .asynccontextmanager
3948async def _gprc_server (
40- port : int ,
4149 servicer : mock_api .MockMicrogridServicer | None = None ,
4250) -> AsyncIterator [tuple [mock_api .MockMicrogridServicer , client .MicrogridApiClient ]]:
51+ global _CURRENT_PORT # pylint: disable=global-statement
52+ port = _CURRENT_PORT
53+ _CURRENT_PORT += 1
4354 if servicer is None :
4455 servicer = mock_api .MockMicrogridServicer ()
4556 server = mock_api .MockGrpcServer (servicer , port = port )
@@ -60,7 +71,7 @@ class TestMicrogridGrpcClient:
6071
6172 async def test_components (self ) -> None :
6273 """Test the components() method."""
63- async with _gprc_server (57899 ) as (servicer , microgrid ):
74+ async with _gprc_server () as (servicer , microgrid ):
6475 assert set (await microgrid .components ()) == set ()
6576
6677 servicer .add_component (
@@ -174,7 +185,7 @@ async def test_components(self) -> None:
174185
175186 async def test_connections (self ) -> None :
176187 """Test the connections() method."""
177- async with _gprc_server (57898 ) as (servicer , microgrid ):
188+ async with _gprc_server () as (servicer , microgrid ):
178189 assert set (await microgrid .connections ()) == set ()
179190
180191 servicer .add_connection (0 , 0 )
@@ -336,7 +347,7 @@ def ListAllComponents(
336347 ) -> microgrid_pb .ComponentList :
337348 return microgrid_pb .ComponentList (components = self ._components )
338349
339- async with _gprc_server (57897 , BadServicer ()) as (servicer , microgrid ):
350+ async with _gprc_server (BadServicer ()) as (servicer , microgrid ):
340351 assert list (await microgrid .connections ()) == []
341352 for component_id in [1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ]:
342353 servicer .add_component (
@@ -382,7 +393,7 @@ def ListAllComponents(
382393
383394 async def test_meter_data (self ) -> None :
384395 """Test the meter_data() method."""
385- async with _gprc_server (57899 ) as (servicer , microgrid ):
396+ async with _gprc_server () as (servicer , microgrid ):
386397 servicer .add_component (
387398 83 , components_pb .ComponentCategory .COMPONENT_CATEGORY_METER
388399 )
@@ -406,7 +417,7 @@ async def test_meter_data(self) -> None:
406417
407418 async def test_battery_data (self ) -> None :
408419 """Test the battery_data() method."""
409- async with _gprc_server (57899 ) as (servicer , microgrid ):
420+ async with _gprc_server () as (servicer , microgrid ):
410421 servicer .add_component (
411422 83 , components_pb .ComponentCategory .COMPONENT_CATEGORY_BATTERY
412423 )
@@ -430,7 +441,7 @@ async def test_battery_data(self) -> None:
430441
431442 async def test_inverter_data (self ) -> None :
432443 """Test the inverter_data() method."""
433- async with _gprc_server (57899 ) as (servicer , microgrid ):
444+ async with _gprc_server () as (servicer , microgrid ):
434445 servicer .add_component (
435446 83 , components_pb .ComponentCategory .COMPONENT_CATEGORY_INVERTER
436447 )
@@ -454,7 +465,7 @@ async def test_inverter_data(self) -> None:
454465
455466 async def test_ev_charger_data (self ) -> None :
456467 """Test the ev_charger_data() method."""
457- async with _gprc_server (57899 ) as (servicer , microgrid ):
468+ async with _gprc_server () as (servicer , microgrid ):
458469 servicer .add_component (
459470 83 , components_pb .ComponentCategory .COMPONENT_CATEGORY_EV_CHARGER
460471 )
@@ -478,7 +489,7 @@ async def test_ev_charger_data(self) -> None:
478489
479490 async def test_charge (self ) -> None :
480491 """Check if charge is able to charge component."""
481- async with _gprc_server (57899 ) as (servicer , microgrid ):
492+ async with _gprc_server () as (servicer , microgrid ):
482493 servicer .add_component (
483494 83 , components_pb .ComponentCategory .COMPONENT_CATEGORY_METER
484495 )
@@ -491,7 +502,7 @@ async def test_charge(self) -> None:
491502
492503 async def test_discharge (self ) -> None :
493504 """Check if discharge is able to discharge component."""
494- async with _gprc_server (57899 ) as (servicer , microgrid ):
505+ async with _gprc_server () as (servicer , microgrid ):
495506 servicer .add_component (
496507 73 , components_pb .ComponentCategory .COMPONENT_CATEGORY_METER
497508 )
@@ -504,7 +515,7 @@ async def test_discharge(self) -> None:
504515
505516 async def test_set_bounds (self ) -> None :
506517 """Check if set_bounds is able to set bounds for component."""
507- async with _gprc_server (57899 ) as (servicer , microgrid ):
518+ async with _gprc_server () as (servicer , microgrid ):
508519 servicer .add_component (
509520 38 , components_pb .ComponentCategory .COMPONENT_CATEGORY_INVERTER
510521 )
0 commit comments