Skip to content

Commit 7734069

Browse files
[Fabric-Sync] Improve the stability of test TC_MCORE_FS_1_4 (project-chip#35416)
* Improve the stability of test TC_MCORE_FS_1_4 * Address the review comments * Make wait_for_server_initialization global * Restyled by autopep8 --------- Co-authored-by: Restyled.io <commits@restyled.io>
1 parent 7fdfc88 commit 7734069

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

src/python_testing/TC_MCORE_FS_1_4.py

+35-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,34 @@
4848
from mobly import asserts
4949

5050

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+
5176
# TODO: Make this class more generic. Issue #35348
77+
78+
5279
class Subprocess(threading.Thread):
5380

5481
def __init__(self, args: list = [], stdout_cb=None, tag="", **kw):
@@ -228,7 +255,7 @@ def setup_class(self):
228255

229256
self.th_server_port = 5544
230257
self.th_server_discriminator = self.th_fsa_bridge_discriminator + 1
231-
self.th_server_passcode = 20202021
258+
self.th_server_passcode = 20202022
232259

233260
# Start the TH_SERVER_NO_UID app.
234261
self.th_server = AppServer(
@@ -238,6 +265,12 @@ def setup_class(self):
238265
discriminator=self.th_server_discriminator,
239266
passcode=self.th_server_passcode)
240267

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+
241274
def teardown_class(self):
242275
if self.th_fsa_controller is not None:
243276
self.th_fsa_controller.stop()
@@ -271,6 +304,7 @@ async def test_TC_MCORE_FS_1_4(self):
271304
self.step(1)
272305

273306
th_server_th_node_id = 1
307+
274308
await self.default_controller.CommissionOnNetwork(
275309
nodeId=th_server_th_node_id,
276310
setupPinCode=self.th_server_passcode,

0 commit comments

Comments
 (0)