Skip to content

Commit

Permalink
fix read_callback only_once logic
Browse files Browse the repository at this point in the history
  • Loading branch information
realvitya committed Oct 15, 2024
1 parent 5f0907b commit 08b7208
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 8 deletions.
3 changes: 2 additions & 1 deletion scrapli/driver/generic/async_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
3 changes: 2 additions & 1 deletion scrapli/driver/generic/sync_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
27 changes: 24 additions & 3 deletions tests/unit/driver/generic/test_generic_async_driver.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
import pytest

from scrapli.driver.generic.base_driver import ReadCallback
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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
27 changes: 24 additions & 3 deletions tests/unit/driver/generic/test_generic_sync_driver.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
import pytest

from scrapli.driver.generic.base_driver import ReadCallback
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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

0 comments on commit 08b7208

Please sign in to comment.