diff --git a/scrapli/driver/generic/async_driver.py b/scrapli/driver/generic/async_driver.py index c934fdfd..62cf00e0 100644 --- a/scrapli/driver/generic/async_driver.py +++ b/scrapli/driver/generic/async_driver.py @@ -594,7 +594,8 @@ def callback_two(cls: GenericDriver, read_output: str): _run_callback = callback.check(read_output=read_output) if ( - callback.only_once is True + _run_callback is True + and callback.only_once is True and callback._triggered is True # pylint: disable=W0212 ): self.logger.warning( diff --git a/scrapli/driver/generic/sync_driver.py b/scrapli/driver/generic/sync_driver.py index a6cd5d70..67a1a5f1 100644 --- a/scrapli/driver/generic/sync_driver.py +++ b/scrapli/driver/generic/sync_driver.py @@ -595,7 +595,8 @@ def callback_two(cls: GenericDriver, read_output: str): _run_callback = callback.check(read_output=read_output) if ( - callback.only_once is True + _run_callback is True + and callback.only_once is True and callback._triggered is True # pylint: disable=W0212 ): self.logger.warning( diff --git a/tests/unit/driver/generic/test_generic_async_driver.py b/tests/unit/driver/generic/test_generic_async_driver.py index 4e1affb4..9d0d3cb8 100644 --- a/tests/unit/driver/generic/test_generic_async_driver.py +++ b/tests/unit/driver/generic/test_generic_async_driver.py @@ -1,3 +1,4 @@ +import logging import pytest from scrapli.driver.generic.base_driver import ReadCallback @@ -111,9 +112,15 @@ async def test_send_interact_no_base_transport_args(async_generic_driver): await async_generic_driver.send_interactive(interact_events=[]) -async def test_readcallback_basic(monkeypatch, async_generic_driver): +async def test_readcallback_basic(caplog, monkeypatch, async_generic_driver): + outputs = [b"some output#", b"rtr1#", b"rtr1#", b"rtr1#", b"rtr1#"] + i = -1 + async def _read(cls): - return b"rtr1#" + nonlocal i, outputs + + i += 1 + return outputs[i] def _write(cls, channel_input, redacted=False): return @@ -134,7 +141,16 @@ async def callback_two(cls, read_output): callback_two_counter += 1 + async def callback_some(cls, read_output): + pass + callbacks = [ + ReadCallback( + contains="some", + callback=callback_some, + name="some", + only_once=True, + ), ReadCallback( contains="rtr1#", callback=callback_one, @@ -156,7 +172,12 @@ async def callback_two(cls, read_output): ), ] - await async_generic_driver.read_callback(callbacks=callbacks, initial_input="nada") + with caplog.at_level(logging.WARNING): + await async_generic_driver.read_callback(callbacks=callbacks, initial_input="nada") + assert "some matches but is set to 'only_once'" not in caplog.text + assert "call1 matches but is set to 'only_once'" in caplog.text + assert "call1.5 matches but is set to 'only_once'" in caplog.text + assert "call2 matches but is set to 'only_once'" not in caplog.text assert callback_one_counter == 2 assert callback_two_counter == 1 diff --git a/tests/unit/driver/generic/test_generic_sync_driver.py b/tests/unit/driver/generic/test_generic_sync_driver.py index 3e23863f..9d9e7c6d 100644 --- a/tests/unit/driver/generic/test_generic_sync_driver.py +++ b/tests/unit/driver/generic/test_generic_sync_driver.py @@ -1,3 +1,4 @@ +import logging import pytest from scrapli.driver.generic.base_driver import ReadCallback @@ -100,9 +101,15 @@ def test_send_interact_no_base_transport_args(sync_generic_driver): sync_generic_driver.send_interactive(interact_events=[]) -def test_readcallback_basic(monkeypatch, sync_generic_driver): +def test_readcallback_basic(caplog, monkeypatch, sync_generic_driver): + outputs = [b"some output#", b"rtr1#", b"rtr1#", b"rtr1#", b"rtr1#"] + i = -1 + def _read(cls): - return b"rtr1#" + nonlocal i, outputs + + i += 1 + return outputs[i] def _write(cls, channel_input, redacted=False): return @@ -123,7 +130,16 @@ def callback_two(cls, read_output): callback_two_counter += 1 + def callback_some(cls, read_output): + pass + callbacks = [ + ReadCallback( + contains="some", + callback=callback_some, + name="some", + only_once=True, + ), ReadCallback( contains="rtr1#", callback=callback_one, @@ -145,7 +161,12 @@ def callback_two(cls, read_output): ), ] - sync_generic_driver.read_callback(callbacks=callbacks, initial_input="nada") + with caplog.at_level(logging.WARNING): + sync_generic_driver.read_callback(callbacks=callbacks, initial_input="nada") + assert "some matches but is set to 'only_once'" not in caplog.text + assert "call1 matches but is set to 'only_once'" in caplog.text + assert "call1.5 matches but is set to 'only_once'" in caplog.text + assert "call2 matches but is set to 'only_once'" not in caplog.text assert callback_one_counter == 2 assert callback_two_counter == 1