Use asyncio.Event when events should be re-awaitable

This commit is contained in:
Salad Dais
2023-12-18 18:34:14 +00:00
parent 631fe91049
commit f278a4bfcf
2 changed files with 14 additions and 13 deletions

View File

@@ -46,8 +46,8 @@ class VoiceClient:
self._host = host
self._port = port
self.logged_in = Event()
self.ready = Event()
self.logged_in = asyncio.Event()
self.ready = asyncio.Event()
self.session_added = Event()
self.channel_info_updated = Event()
self.participant_added = Event()
@@ -138,7 +138,7 @@ class VoiceClient:
client = cls(host, port)
await client.create_vivox_connection()
await client.ready
await client.ready.wait()
return client
async def create_vivox_connection(self):
@@ -174,7 +174,7 @@ class VoiceClient:
})
self._connector_handle = connector_resp['Results']['ConnectorHandle']
self.ready.notify(None)
self.ready.set()
async def login(self, username: Union[uuid.UUID, str], password: str):
# UUID, convert to Vivox format
@@ -202,7 +202,7 @@ class VoiceClient:
raise Exception(resp)
self._display_name = urllib.parse.unquote(resp["Results"]["DisplayName"])
await self.logged_in
await self.logged_in.wait()
return resp
@@ -215,6 +215,7 @@ class VoiceClient:
"AccountHandle": self._account_handle,
})
self._account_handle = None
self.logged_in.clear()
async def join_session(self, uri: str, region_handle: Optional[int] = None):
if self._session_handle:
@@ -371,7 +372,7 @@ class VoiceClient:
elif event_type == "AccountLoginStateChangeEvent":
if dict_msg.get('StatusString') == "OK" and dict_msg['State'] == '1':
self._account_handle = dict_msg['AccountHandle']
self.logged_in.notify(self._account_handle)
self.logged_in.set()
elif event_type == "SessionAddedEvent":
self._session_handle = dict_msg["SessionHandle"]
self._session_group_handle = dict_msg["SessionGroupHandle"]

View File

@@ -205,7 +205,7 @@ class TestVoiceClient(unittest.IsolatedAsyncioTestCase):
"ConnectorHandle": 2,
}
})
await self.client.ready
await self.client.ready.wait()
async def test_create_connector(self):
await self._serve_connector_setup()
@@ -216,6 +216,12 @@ class TestVoiceClient(unittest.IsolatedAsyncioTestCase):
async def _handle_login():
msg = await self._expect_message("Account.Login.1")
self.assertEqual("foo", msg.data["AccountName"])
await self.server_connection.send_event("AccountLoginStateChangeEvent", {
"AccountHandle": 2,
"StatusCode": 200,
"StatusString": "OK",
"State": 1,
})
await self.server_connection.send_response(msg.request_id, msg.name, {
"ReturnCode": 0,
"Results": {
@@ -225,12 +231,6 @@ class TestVoiceClient(unittest.IsolatedAsyncioTestCase):
"DisplayName": "foo",
}
})
await self.server_connection.send_event("AccountLoginStateChangeEvent", {
"AccountHandle": 2,
"StatusCode": 200,
"StatusString": "OK",
"State": 1,
})
login_task = asyncio.get_event_loop().create_task(_handle_login())