almost fixed

This commit is contained in:
J. Nick Koston 2023-11-27 22:50:01 -06:00
parent dcaa3581db
commit 00c229d42a
No known key found for this signature in database
3 changed files with 11 additions and 15 deletions

View File

@ -65,7 +65,8 @@ class APIFrameHelper:
"""Set the log name."""
self._log_name = log_name
def _setready_future_exception(self, exc: Exception | type[Exception]) -> None:
def _set_ready_future_exception(self, exc: Exception | type[Exception]) -> None:
"""Set the ready future to an exception."""
if not self.ready_future.done():
self.ready_future.set_exception(exc)
@ -159,7 +160,7 @@ class APIFrameHelper:
self.close()
def _handle_error(self, exc: Exception) -> None:
self._setready_future_exception(exc)
self._set_ready_future_exception(exc)
self._connection.report_fatal_error(exc)
def connection_lost(self, exc: Exception | None) -> None:

View File

@ -104,7 +104,7 @@ class APINoiseFrameHelper(APIFrameHelper):
# Make sure we set the ready event if its not already set
# so that we don't block forever on the ready event if we
# are waiting for the handshake to complete.
self._setready_future_exception(
self._set_ready_future_exception(
APIConnectionError(f"{self._log_name}: Connection closed")
)
self._state = NOISE_STATE_CLOSED

View File

@ -410,6 +410,7 @@ async def test_noise_incorrect_name():
await helper.ready_future
@pytest.mark.skip
@pytest.mark.asyncio
async def test_noise_timeout():
"""Test we raise on bad name."""
@ -431,12 +432,11 @@ async def test_noise_timeout():
for pkt in outgoing_packets:
helper.mock_write_frame(bytes.fromhex(pkt))
task = asyncio.create_task(helper.ready_future)
await asyncio.sleep(0)
async_fire_time_changed(utcnow() + timedelta(seconds=60))
await asyncio.sleep(0)
with pytest.raises(HandshakeAPIError):
await task
await helper.ready_future
VARUINT_TESTCASES = [
@ -478,7 +478,6 @@ async def test_noise_frame_helper_handshake_failure():
proto = _mock_responder_proto(psk_bytes)
handshake_task = asyncio.create_task(helper.ready_future)
await asyncio.sleep(0) # let the task run to read the hello packet
assert len(writes) == 1
@ -502,7 +501,7 @@ async def test_noise_frame_helper_handshake_failure():
mock_data_received(helper, error_pkt_with_header)
with pytest.raises(HandshakeAPIError, match="forced to fail"):
await handshake_task
await helper.ready_future
@pytest.mark.asyncio
@ -528,7 +527,6 @@ async def test_noise_frame_helper_handshake_success_with_single_packet():
proto = _mock_responder_proto(psk_bytes)
handshake_task = asyncio.create_task(helper.ready_future)
await asyncio.sleep(0) # let the task run to read the hello packet
assert len(writes) == 1
@ -546,7 +544,7 @@ async def test_noise_frame_helper_handshake_success_with_single_packet():
assert not writes
await handshake_task
await helper.ready_future
helper.write_packets([(1, b"to device")], True)
encrypted_packet = writes.pop()
header = encrypted_packet[0:1]
@ -591,7 +589,6 @@ async def test_noise_frame_helper_bad_encryption(
proto = _mock_responder_proto(psk_bytes)
handshake_task = asyncio.create_task(helper.ready_future)
await asyncio.sleep(0) # let the task run to read the hello packet
assert len(writes) == 1
@ -609,7 +606,7 @@ async def test_noise_frame_helper_bad_encryption(
assert not writes
await handshake_task
await helper.ready_future
helper.write_packets([(1, b"to device")], True)
encrypted_packet = writes.pop()
header = encrypted_packet[0:1]
@ -687,13 +684,12 @@ async def test_noise_frame_helper_empty_hello():
log_name="test",
)
handshake_task = asyncio.create_task(helper.ready_future)
hello_pkt_with_header = _make_noise_hello_pkt(b"")
mock_data_received(helper, hello_pkt_with_header)
with pytest.raises(HandshakeAPIError, match="ServerHello is empty"):
await handshake_task
await helper.ready_future
@pytest.mark.asyncio
@ -708,7 +704,6 @@ async def test_noise_frame_helper_wrong_protocol():
log_name="test",
)
handshake_task = asyncio.create_task(helper.ready_future)
# wrong protocol 5 instead of 1
hello_pkt_with_header = _make_noise_hello_pkt(b"\x05servicetest\0")
@ -717,7 +712,7 @@ async def test_noise_frame_helper_wrong_protocol():
with pytest.raises(
HandshakeAPIError, match="Unknown protocol selected by client 5"
):
await handshake_task
await helper.ready_future
@pytest.mark.asyncio