Skip to content

Commit 22db90a

Browse files
authored
Switch to check the specific log output from stdout (project-chip#35547)
1 parent ac289ed commit 22db90a

File tree

1 file changed

+17
-33
lines changed

1 file changed

+17
-33
lines changed

src/python_testing/TC_MCORE_FS_1_4.py

+17-33
Original file line numberDiff line numberDiff line change
@@ -47,32 +47,6 @@
4747
from matter_testing_support import MatterBaseTest, TestStep, async_test_body, default_matter_test_main, type_matches
4848
from mobly import asserts
4949

50-
51-
async def wait_for_server_initialization(server_port, timeout=5):
52-
"""Wait until the server is ready by checking if it opens the expected port."""
53-
start_time = asyncio.get_event_loop().time()
54-
elapsed_time = 0
55-
retry_interval = 1
56-
57-
logging.info(f"Waiting for server to initialize on TCP port {server_port} for up to {timeout} seconds.")
58-
59-
while elapsed_time < timeout:
60-
try:
61-
# Try connecting to the server to check if it's ready
62-
reader, writer = await asyncio.open_connection('::1', server_port)
63-
writer.close()
64-
await writer.wait_closed()
65-
logging.info(f"TH_SERVER_NO_UID is initialized and ready on port {server_port}.")
66-
return
67-
except (ConnectionRefusedError, OSError) as e:
68-
logging.warning(f"Connection to port {server_port} failed: {e}. Retrying in {retry_interval} seconds...")
69-
70-
await asyncio.sleep(retry_interval)
71-
elapsed_time = asyncio.get_event_loop().time() - start_time
72-
73-
raise TimeoutError(f"Server on port {server_port} did not initialize within {timeout} seconds. "
74-
f"Total time waited: {elapsed_time} seconds.")
75-
7650
# TODO: Make this class more generic. Issue #35348
7751

7852

@@ -174,16 +148,32 @@ def stop(self):
174148

175149
class AppServer:
176150

151+
def _process_admin_output(self, line):
152+
if self.wait_for_text_text is not None and self.wait_for_text_text in line:
153+
self.wait_for_text_event.set()
154+
155+
def wait_for_text(self, timeout=30):
156+
if not self.wait_for_text_event.wait(timeout=timeout):
157+
raise Exception(f"Timeout waiting for text: {self.wait_for_text_text}")
158+
self.wait_for_text_event.clear()
159+
self.wait_for_text_text = None
160+
177161
def __init__(self, app, storage_dir, port=None, discriminator=None, passcode=None):
162+
self.wait_for_text_event = threading.Event()
163+
self.wait_for_text_text = None
178164

179165
args = [app]
180166
args.extend(["--KVS", tempfile.mkstemp(dir=storage_dir, prefix="kvs-app-")[1]])
181167
args.extend(['--secured-device-port', str(port)])
182168
args.extend(["--discriminator", str(discriminator)])
183169
args.extend(["--passcode", str(passcode)])
184-
self.app = Subprocess(args, tag="SERVER")
170+
self.app = Subprocess(args, stdout_cb=self._process_admin_output, tag="SERVER")
171+
self.wait_for_text_text = "Server initialization complete"
185172
self.app.start()
186173

174+
# Wait for the server-app to be ready.
175+
self.wait_for_text()
176+
187177
def stop(self):
188178
self.app.stop()
189179

@@ -265,12 +255,6 @@ def setup_class(self):
265255
discriminator=self.th_server_discriminator,
266256
passcode=self.th_server_passcode)
267257

268-
# Wait for TH_SERVER_NO_UID get initialized.
269-
try:
270-
asyncio.run(wait_for_server_initialization(self.th_server_port))
271-
except TimeoutError:
272-
asserts.fail(f"TH_SERVER_NO_UID server failed to open port {self.th_server_port}")
273-
274258
def teardown_class(self):
275259
if self.th_fsa_controller is not None:
276260
self.th_fsa_controller.stop()

0 commit comments

Comments
 (0)