Skip to content
This repository has been archived by the owner on Dec 18, 2024. It is now read-only.

Commit

Permalink
Wait for databroker availability in integration tests
Browse files Browse the repository at this point in the history
Signed-off-by: Sebastian Schildt <[email protected]>
  • Loading branch information
SebastianSchildt committed May 11, 2023
1 parent 2b334b0 commit 3dfef21
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
23 changes: 23 additions & 0 deletions kuksa_databroker/integration_test/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ class Databroker:
Databroker wraps collector and broker APIs of the databroker.
"""

@classmethod
async def ConnectedDatabroker(cls, address: str):
self = Databroker(address)
await self.await_connectivity()
return self

def __init__(self, address: str) -> None:

self._address = address
Expand Down Expand Up @@ -87,6 +93,23 @@ async def __get_datapoints(self, datapoints: list):
GetDatapointsRequest(datapoints=datapoints)
)
return response

async def await_connectivity(self):
# We need to "manually" wait in a loop, as wait_for_state_change can not wait for a
# specific target state
tries = 0
while tries < 10:
state = self._channel.get_state(try_to_connect=True)
logger.info("GRPC State is %s", state)
if state == grpc.ChannelConnectivity.READY:
break
logger.info("Try %i: Waiting for GRPC connection to become READY...", tries)
tries = tries + 1
try:
await asyncio.wait_for(self._channel.wait_for_state_change(state), timeout=2)
except asyncio.exceptions.TimeoutError:
# We need to catch this, and will wait again until tries are used up
pass

async def get_metadata(self, names=[]):
"""Requests metadata from databroker, allows for optional list of names
Expand Down
4 changes: 2 additions & 2 deletions kuksa_databroker/integration_test/test_databroker.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@
@pytest.fixture
async def setup_helper() -> Databroker:
logger.info("Using DATABROKER_ADDRESS={}".format(DATABROKER_ADDRESS))
helper = Databroker(DATABROKER_ADDRESS)
helper = await Databroker.ConnectedDatabroker(DATABROKER_ADDRESS)
return helper


@pytest.mark.asyncio
async def test_databroker_connection() -> None:
logger.info("Connecting to VehicleDataBroker {}".format(DATABROKER_ADDRESS))
helper = Databroker(DATABROKER_ADDRESS)
helper = await Databroker.ConnectedDatabroker(DATABROKER_ADDRESS)
await helper.get_metadata()
logger.info("Databroker._address = {}".format(helper._address))
await helper.close()
Expand Down

0 comments on commit 3dfef21

Please sign in to comment.