Commit b5edbdeb authored by Julien Prigent's avatar Julien Prigent Committed by oroulet

[HaClient] Fix test race condition

According to the logs of the recent test failure, there's a small window when we test the connection is established where we have a socket connected but don't have the session created yet, and this is throwing exception when trying to disconnect because the renew loop is not yet created.

This is caused by the HaManager (in charge of connecting/reconnecting the clients) running in its own task, thus we're not in the usual await connect(); await disconnect() scenario.
This can also arguably be fixed at the client level by checking if
the renew loop exist on disconnect.
parent 68b50bbf
...@@ -11,6 +11,7 @@ from contextlib import closing ...@@ -11,6 +11,7 @@ from contextlib import closing
from asyncua import Client from asyncua import Client
from asyncua import Server, ua from asyncua import Server, ua
from asyncua.client.ua_client import UASocketProtocol
from asyncua.client.ha.ha_client import HaClient, HaConfig, HaMode from asyncua.client.ha.ha_client import HaClient, HaConfig, HaMode
from asyncua.server.history import HistoryDict from asyncua.server.history import HistoryDict
from asyncua.server.history_sql import HistorySQLite from asyncua.server.history_sql import HistorySQLite
...@@ -390,7 +391,13 @@ async def wait_clients_socket(ha_client, state): ...@@ -390,7 +391,13 @@ async def wait_clients_socket(ha_client, state):
for client in ha_client.get_clients(): for client in ha_client.get_clients():
for _ in range(RETRY): for _ in range(RETRY):
if client.uaclient.protocol and client.uaclient.protocol.state == state: if client.uaclient.protocol and client.uaclient.protocol.state == state:
break # for connection OPEN, also wait for the session to be established
# otherwise we can encounter failure on disconnect
if state == UASocketProtocol.OPEN:
if client._renew_channel_task:
break
else:
break
await sleep(SLEEP) await sleep(SLEEP)
assert client.uaclient.protocol.state == state assert client.uaclient.protocol.state == state
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment