From 46754434f00eb5a05ff694507f442f32a2ec26a5 Mon Sep 17 00:00:00 2001
From: Carolina Lopes <116589288+ccruzagralopes@users.noreply.github.com>
Date: Mon, 13 Nov 2023 05:37:53 -0300
Subject: [PATCH] Remove start and stop from run (#30396)

* Remove start and stop from run method

* Add property to check if runner is connected

* Add auto_start_stop config variable

* Fix typo
---
 .../py_matter_yamltests/matter_yamltests/runner.py   | 12 +++++++++---
 .../matter_yamltests/websocket_runner.py             |  7 +++++++
 2 files changed, 16 insertions(+), 3 deletions(-)

diff --git a/scripts/py_matter_yamltests/matter_yamltests/runner.py b/scripts/py_matter_yamltests/matter_yamltests/runner.py
index a25183feb6a9a6..541a261a43b4b2 100644
--- a/scripts/py_matter_yamltests/matter_yamltests/runner.py
+++ b/scripts/py_matter_yamltests/matter_yamltests/runner.py
@@ -70,11 +70,15 @@ class TestRunnerConfig:
     hooks: A configurable set of hooks to be called at various steps while
            running. It may may allow the callers to gain insights about the
            current running state.
+
+    auto_start_stop: Indicates whether the run method should start and stop
+            the runner of if that will be handled outside of that method.
     """
     adapter: TestAdapter = None
     pseudo_clusters: PseudoClusters = PseudoClusters([])
     options: TestRunnerOptions = field(default_factory=TestRunnerOptions)
     hooks: TestRunnerHooks = TestRunnerHooks()
+    auto_start_stop: bool = True
 
 
 class TestRunnerBase(ABC):
@@ -109,7 +113,7 @@ async def execute(self, request):
         pass
 
     @abstractmethod
-    def run(self, config: TestRunnerConfig) -> bool:
+    def run(self, parser_builder_config: TestParserBuilderConfig, runner_config: TestRunnerConfig) -> bool:
         """
         This method runs a test suite.
 
@@ -156,12 +160,14 @@ async def run(self, parser_builder_config: TestParserBuilderConfig, runner_confi
     async def _run_with_timeout(self, parser: TestParser, config: TestRunnerConfig):
         status = True
         try:
-            await self.start()
+            if config.auto_start_stop:
+                await self.start()
             status = await asyncio.wait_for(self._run(parser, config), parser.timeout)
         except (Exception, CancelledError) as exception:
             status = exception
         finally:
-            await self.stop()
+            if config.auto_start_stop:
+                await self.stop()
             return status
 
     async def _run(self, parser: TestParser, config: TestRunnerConfig):
diff --git a/scripts/py_matter_yamltests/matter_yamltests/websocket_runner.py b/scripts/py_matter_yamltests/matter_yamltests/websocket_runner.py
index c90c50d15a177f..c63554ecf064b0 100644
--- a/scripts/py_matter_yamltests/matter_yamltests/websocket_runner.py
+++ b/scripts/py_matter_yamltests/matter_yamltests/websocket_runner.py
@@ -46,6 +46,13 @@ def __init__(self, config: WebSocketRunnerConfig):
         self._server_startup_command = self._make_server_startup_command(
             config.server_path, config.server_arguments, config.server_port)
 
+    @property
+    def is_connected(self) -> bool:
+        if self._client is None:
+            return False
+
+        return self._client.state == websockets.protocol.State.OPEN
+
     async def start(self):
         self._server = await self._start_server(self._server_startup_command)
         self._client = await self._start_client(self._server_connection_url)