aboutsummaryrefslogtreecommitdiff
path: root/avatar
diff options
context:
space:
mode:
authoruael <uael@google.com>2023-03-14 16:39:28 +0000
committeruael <uael@google.com>2023-03-14 16:39:28 +0000
commit1b80d71363bc96484f467012ad9b29cffeb22bdf (patch)
tree3384a9539266584e87962883598b7b77e5fa2579 /avatar
parent0c7ceeaacd8c691d9c6f33912e3201f63777325b (diff)
downloadavatar-1b80d71363bc96484f467012ad9b29cffeb22bdf.tar.gz
client: raise an error when mixing sync/async gRPC channels
Change-Id: I70c9a64a2ab8409e586ff5b652cfe9f295ef0d8c
Diffstat (limited to 'avatar')
-rw-r--r--avatar/bumble_server/security.py2
-rw-r--r--avatar/pandora_client.py16
2 files changed, 14 insertions, 4 deletions
diff --git a/avatar/bumble_server/security.py b/avatar/bumble_server/security.py
index 6f15d89..fb5a121 100644
--- a/avatar/bumble_server/security.py
+++ b/avatar/bumble_server/security.py
@@ -219,7 +219,7 @@ class SecurityService(SecurityServicer):
if connection.transport == BT_LE_TRANSPORT and connection.role == BT_PERIPHERAL_ROLE:
wait_for_security: asyncio.Future[bool] = asyncio.get_running_loop().create_future()
- connection.on("pairing", lambda *_: wait_for_security.set_result(True)) # type:ignore
+ connection.on("pairing", lambda *_: wait_for_security.set_result(True)) # type: ignore
connection.on("pairing_failure", wait_for_security.set_exception)
connection.request_pairing()
diff --git a/avatar/pandora_client.py b/avatar/pandora_client.py
index 2a42054..070ce31 100644
--- a/avatar/pandora_client.py
+++ b/avatar/pandora_client.py
@@ -15,6 +15,7 @@
"""Pandora client interface for Avatar tests."""
+import asyncio
import avatar.aio
import bumble
import bumble.device
@@ -55,9 +56,9 @@ class PandoraClient:
# public fields
grpc_target: str # Server address for the gRPC channel.
log: 'PandoraClientLoggerAdapter' # Logger adapter.
- channel: grpc.Channel # Synchronous gRPC channel.
# private fields
+ _channel: grpc.Channel # Synchronous gRPC channel.
_address: Address # Bluetooth device address
_aio: Optional['PandoraClient.Aio'] # Asynchronous gRPC channel.
@@ -71,13 +72,13 @@ class PandoraClient:
"""
self.grpc_target = grpc_target
self.log = PandoraClientLoggerAdapter(logging.getLogger(), {'client': self, 'client_name': name})
- self.channel = grpc.insecure_channel(grpc_target) # type: ignore
+ self._channel = grpc.insecure_channel(grpc_target) # type: ignore
self._address = Address(b'\x00\x00\x00\x00\x00\x00')
self._aio = None
def close(self) -> None:
"""Closes the gRPC channels."""
- self.channel.close()
+ self._channel.close()
if self._aio:
avatar.aio.run_until_complete(self._aio.channel.close())
@@ -103,6 +104,15 @@ class PandoraClient:
# This call might fail if the server is unavailable.
self._address = Address((await self.aio.host.ReadLocalAddress(wait_for_ready=True)).address)
+ @property
+ def channel(self) -> grpc.Channel:
+ """Returns the synchronous gRPC channel."""
+ try:
+ _ = asyncio.get_running_loop()
+ except:
+ return self._channel
+ raise RuntimeError('Trying to use the synchronous gRPC channel from asynchronous code.')
+
# Pandora interfaces
@property