async def test_channel_lock_context_manager_no_channel_lock( async_transport_no_abc): base_channel_args = BaseChannelArgs(channel_lock=False) async_channel = AsyncChannel(transport=async_transport_no_abc, base_channel_args=base_channel_args) async with async_channel._channel_lock(): assert True
async def test_channel_lock_context_manager(async_transport_no_abc): base_channel_args = BaseChannelArgs(channel_lock=True) async_channel = AsyncChannel(transport=async_transport_no_abc, base_channel_args=base_channel_args) assert async_channel.channel_lock.locked() is False async with async_channel._channel_lock(): assert async_channel.channel_lock.locked() is True assert async_channel.channel_lock.locked() is False
async def test_channel_read(fs, caplog, monkeypatch, async_transport_no_abc): # fs needed to mock filesystem for asserting log location _ = fs caplog.set_level(logging.DEBUG, logger="scrapli.channel") channel_read_called = False expected_read_output = b"read_data" base_channel_args = BaseChannelArgs(channel_log=True) async_channel = AsyncChannel(transport=async_transport_no_abc, base_channel_args=base_channel_args) async_channel.open() async def _read(cls): nonlocal channel_read_called channel_read_called = True return b"read_data\r" monkeypatch.setattr( "scrapli.transport.base.async_transport.AsyncTransport.read", _read) actual_read_output = await async_channel.read() async_channel.channel_log.close() assert channel_read_called is True assert actual_read_output == expected_read_output # assert the log output/level as expected; skip the first log message that will be about # channel_log being on log_record = caplog.records[1] assert "read: b'read_data'" == log_record.msg assert logging.DEBUG == log_record.levelno # assert channel log output as expected assert Path("/scrapli_channel.log").is_file() with open("/scrapli_channel.log", "r") as actual_channel_log: assert actual_channel_log.readline() == "read_data"
def test_channel_lock(async_transport_no_abc): base_channel_args = BaseChannelArgs(channel_lock=True) async_channel = AsyncChannel(transport=async_transport_no_abc, base_channel_args=base_channel_args) assert async_channel.channel_lock
def async_channel(async_transport_no_abc, base_channel_args): async_channel = AsyncChannel(transport=async_transport_no_abc, base_channel_args=base_channel_args) return async_channel