This is an automated email from the ASF dual-hosted git repository.

acassis pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/nuttx-ntfc.git

commit 7076fdd3a3cb887c84cc974c40963974c7323aa4
Author: Marco Casaroli <[email protected]>
AuthorDate: Mon Aug 31 09:54:12 2026 +0200

    ntfc: Set the line-buffered send delay where a reopen goes through.
    
    The per-send delay was turned off by DeviceSim._start_impl() after
    host_open() returned.  A device that crashes comes back through
    _dev_reopen(), which calls host_open() directly and never reaches the
    device's own start, so the setting was lost for the rest of the run.
    
    Setting it in host_open() keeps it across a reopen, and covers QEMU as well
    as the simulator, since both are host-based devices.  It changes nothing for
    either unless line_buffered is set, which is off by default.
    
    The test moves with the behaviour: it now opens a device, reopens it and
    checks the delay is still off.
    
    Signed-off-by: Marco Casaroli <[email protected]>
---
 src/ntfc/device/host.py   | 11 ++++++++++-
 src/ntfc/device/sim.py    |  4 +---
 tests/device/test_host.py | 20 ++++++++++++++++++++
 tests/device/test_sim.py  | 18 ------------------
 4 files changed, 31 insertions(+), 22 deletions(-)

diff --git a/src/ntfc/device/host.py b/src/ntfc/device/host.py
index fb11750..6ccba99 100644
--- a/src/ntfc/device/host.py
+++ b/src/ntfc/device/host.py
@@ -133,10 +133,19 @@ class DeviceHost(DeviceCommon):
         self._cmd = cmd
 
         logger.info(f"spawn cmd: {''.join(cmd)}")
-        self._child = pexpect.spawn(
+        child = pexpect.spawn(
             "".join(cmd), timeout=10, maxread=20000, cwd=self._cwd
         )
 
+        if self._conf.line_buffered:
+            # A line-buffered transport writes a whole command at once, so the
+            # per-send delay pexpect inserts between characters buys nothing.
+            # This is set here rather than by the caller so that it survives
+            # _dev_reopen(), which comes back through this function.
+            child.delaybeforesend = 0
+
+        self._child = child
+
         time.sleep(uptime)
 
         ret = self._wait_for_boot(self._conf.boot_timeout)
diff --git a/src/ntfc/device/sim.py b/src/ntfc/device/sim.py
index 6a19eaa..57cb226 100644
--- a/src/ntfc/device/sim.py
+++ b/src/ntfc/device/sim.py
@@ -49,9 +49,7 @@ class DeviceSim(DeviceHost):
         uptime = self._conf.uptime
 
         # open host-based emulation
-        child = self.host_open(cmd, uptime)
-        if self._conf.line_buffered:
-            child.delaybeforesend = 0
+        self.host_open(cmd, uptime)
 
     @property
     def name(self) -> str:
diff --git a/tests/device/test_host.py b/tests/device/test_host.py
index 710334a..1d1f57c 100644
--- a/tests/device/test_host.py
+++ b/tests/device/test_host.py
@@ -48,6 +48,26 @@ class DeviceHost2(DeviceHost):
             self._child.send(b"\n\n")
 
 
+def test_device_host_line_buffered_survives_reopen(envconfig_dummy):
+    """A line-buffered device keeps its send delay off across a reopen."""
+
+    conf = envconfig_dummy.product[0].cfg_core(0)
+    conf._config["line_buffered"] = True
+
+    path = "./tests/resources/nuttx/sim/nuttx"
+    dev = DeviceHost2(conf)
+
+    child = dev.host_open([path])
+    assert child.delaybeforesend == 0
+
+    # a crash brings the device back through host_open() rather than through
+    # the device's own start, so the setting has to be made there
+    child = dev._dev_reopen()
+    assert child.delaybeforesend == 0
+
+    dev.stop()
+
+
 def test_device_host_open(envconfig_dummy, monkeypatch):
 
     conf = envconfig_dummy.product[0].cfg_core(0)
diff --git a/tests/device/test_sim.py b/tests/device/test_sim.py
index b7c5293..7cbfc2c 100644
--- a/tests/device/test_sim.py
+++ b/tests/device/test_sim.py
@@ -65,24 +65,6 @@ def test_device_sim_start_opens_host():
         assert called["uptime"] == 3
 
 
-def test_device_sim_line_buffered_start_disables_send_delay():
-    with patch("ntfc.coreconfig.CoreConfig") as mockdevice:
-        config = mockdevice.return_value
-        config.elf_path = "/tmp/nuttx-sim"
-        config.line_buffered = True
-        config.uptime = 0
-        sim = DeviceSim(config)
-
-        class FakeChild:
-            delaybeforesend = 0.05
-
-        child = FakeChild()
-        sim.host_open = lambda *_args: child
-
-        sim.start()
-        assert child.delaybeforesend == 0
-
-
 def test_device_sim_write_adds_newline():
     with patch("ntfc.coreconfig.CoreConfig") as mockdevice:
         config = mockdevice.return_value

Reply via email to