Skip to content

Commit

Permalink
kiss tests: Test TCP connection failure handling
Browse files Browse the repository at this point in the history
  • Loading branch information
sjlongland committed May 8, 2024
1 parent 00e584a commit a85d19e
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions tests/test_kiss/test_tcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,69 @@ async def _create_connection(proto_factory, **kwargs):
finally:
# Restore mock
loop.create_connection = orig_create_connection


@asynctest
async def test_open_connection_fail():
# This will receive the arguments passed to create_connection
connection_args = {}

loop = get_event_loop()

# Stub the create_connection method
orig_create_connection = loop.create_connection

async def _create_connection(proto_factory, **kwargs):
# proto_factory should give us a KISSProtocol object
protocol = proto_factory()
assert isinstance(protocol, kiss.KISSProtocol)

connection_args.update(kwargs)
raise IOError("Connection failed")

loop.create_connection = _create_connection

try:
device = kiss.TCPKISSDevice(
host="localhost",
port=5432,
loop=loop,
log=logging.getLogger(__name__),
)

failures = []

def _on_fail(**kwargs):
failures.append(kwargs)

device.failed.connect(_on_fail)

await device._open_connection()

# Expect a connection attempt to have been made
assert connection_args == dict(
host="localhost",
port=5432,
ssl=None,
family=0,
proto=0,
flags=0,
sock=None,
local_addr=None,
server_hostname=None,
)

# Connection should be in the failed state
assert device.state == kiss.KISSDeviceState.FAILED

# Failure should have been reported
assert failures
failure = failures.pop(0)

assert failure.pop("action") == "open"
(ex_c, ex_v, _) = failure.pop("exc_info")
assert ex_c is IOError
assert str(ex_v) == "Connection failed"
finally:
# Restore mock
loop.create_connection = orig_create_connection

0 comments on commit a85d19e

Please sign in to comment.