Skip to content

Commit 9458992

Browse files
committed
Fix failing reconnection unit test
1 parent 6f8946b commit 9458992

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

tests/test_application.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,17 @@ async def znp_server(mocker):
137137
config = config_for_port_path(device)
138138

139139
server_znp = ServerZNP(config)
140-
server_znp._uart = ZnpMtProtocol(server_znp)
140+
server_znp._uart = None
141+
142+
server_znp_proto = ZnpMtProtocol(server_znp)
141143

142144
def passthrough_serial_conn(loop, protocol_factory, url, *args, **kwargs):
143145
fut = loop.create_future()
144146
assert url == device
145147

148+
if server_znp._uart is None:
149+
server_znp._uart = server_znp_proto
150+
146151
client_protocol = protocol_factory()
147152

148153
# Client writes go to the server
@@ -754,35 +759,40 @@ async def test_probe_multiple(pingable_serial_port): # noqa: F811
754759
@pytest_mark_asyncio_timeout(seconds=5)
755760
async def test_reconnect(event_loop, application):
756761
app, znp_server = application()
762+
763+
# Make auto-reconnection happen really fast
757764
app._config[conf.CONF_ZNP_CONFIG][conf.CONF_AUTO_RECONNECT_RETRY_DELAY] = 0.01
758765

766+
# Don't clean up our server's listeners when it gets disconnected
767+
znp_server.close = lambda self: None
768+
769+
# Start up the server
759770
await app.startup(auto_form=False)
771+
assert app._znp is not None
760772

761-
# Don't reply to the ping request this time
773+
# Don't reply to the ping request after this
762774
old_ping_replier = znp_server.ping_replier
763775
znp_server.ping_replier = lambda request: None
764776

765777
# Now that we're connected, close the connection due to an error
766778
SREQ_TIMEOUT = 0.2
767779
app._config[conf.CONF_ZNP_CONFIG][conf.CONF_SREQ_TIMEOUT] = SREQ_TIMEOUT
768780
app._znp._uart.connection_lost(RuntimeError("Uh oh"))
769-
app.connection_lost(RuntimeError("Uh oh"))
770781

782+
# ZNP should be closed
771783
assert app._znp is None
772784

773-
# Wait for the SREQ_TIMEOUT to pass, we should fail to reconnect
785+
# Wait for the SREQ_TIMEOUT to pass, we should still fail to reconnect
774786
await asyncio.sleep(SREQ_TIMEOUT + 0.1)
775787
assert app._znp is None
776788

777-
# Respond to the ping appropriately
789+
# Start responding to pings after this
778790
znp_server.ping_replier = old_ping_replier
779791

780-
# Our reconnect task should complete after we send the ping reply
781-
reconnect_fut = event_loop.create_future()
782-
app._reconnect_task.add_done_callback(lambda _: reconnect_fut.set_result(None))
792+
# Our reconnect task should complete a moment after we send the ping reply
793+
while app._znp is None:
794+
await asyncio.sleep(0.01)
783795

784-
# We should be reconnected soon and the app should have been restarted
785-
await reconnect_fut
786796
assert app._znp is not None
787797
assert app._znp._uart is not None
788798

0 commit comments

Comments
 (0)