From 37e601e5b57286c2c67c25dc61ab61314cb52d1d Mon Sep 17 00:00:00 2001 From: Ivan Kravets Date: Wed, 24 Mar 2021 19:07:40 +0200 Subject: [PATCH] Ensure that a serial port is ready before running unit tests on a remote target // Resolve #3742 --- HISTORY.rst | 4 ++++ platformio/commands/test/embedded.py | 22 +++++++++++++++++----- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/HISTORY.rst b/HISTORY.rst index 3f602892..3088ddd9 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -27,6 +27,10 @@ PlatformIO Core 5 * `Cppcheck `__ v2.4.1 with new checks and MISRA improvements * `PVS-Studio `__ v7.12 with new diagnostics and extended capabilities for security and safety standards +* **Miscellaneous** + + - Ensure that a serial port is ready before running unit tests on a remote target (`issue #3742 `_) + 5.1.1 (2021-03-17) ~~~~~~~~~~~~~~~~~~ diff --git a/platformio/commands/test/embedded.py b/platformio/commands/test/embedded.py index 02bf675b..d0b53390 100644 --- a/platformio/commands/test/embedded.py +++ b/platformio/commands/test/embedded.py @@ -117,13 +117,10 @@ class EmbeddedTestProcessor(TestProcessorBase): port = item["port"] for hwid in board_hwids: hwid_str = ("%s:%s" % (hwid[0], hwid[1])).replace("0x", "") - if hwid_str in item["hwid"]: + if hwid_str in item["hwid"] and self.is_serial_port_ready(port): return port - # check if port is already configured - try: - serial.Serial(port, timeout=self.SERIAL_TIMEOUT).close() - except serial.SerialException: + if port and not self.is_serial_port_ready(port): port = None if not port: @@ -136,3 +133,18 @@ class EmbeddedTestProcessor(TestProcessorBase): "global `--test-port` option." ) return port + + @staticmethod + def is_serial_port_ready(port, timeout=3): + if not port: + return False + elapsed = 0 + while elapsed < timeout: + try: + serial.Serial(port, timeout=1).close() + return True + except: # pylint: disable=bare-except + pass + sleep(1) + elapsed += 1 + return False