|
47 | 47 | from matter_testing_support import MatterBaseTest, TestStep, async_test_body, default_matter_test_main, type_matches
|
48 | 48 | from mobly import asserts
|
49 | 49 |
|
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 |
| - |
76 | 50 | # TODO: Make this class more generic. Issue #35348
|
77 | 51 |
|
78 | 52 |
|
@@ -174,16 +148,32 @@ def stop(self):
|
174 | 148 |
|
175 | 149 | class AppServer:
|
176 | 150 |
|
| 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 | + |
177 | 161 | 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 |
178 | 164 |
|
179 | 165 | args = [app]
|
180 | 166 | args.extend(["--KVS", tempfile.mkstemp(dir=storage_dir, prefix="kvs-app-")[1]])
|
181 | 167 | args.extend(['--secured-device-port', str(port)])
|
182 | 168 | args.extend(["--discriminator", str(discriminator)])
|
183 | 169 | 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" |
185 | 172 | self.app.start()
|
186 | 173 |
|
| 174 | + # Wait for the server-app to be ready. |
| 175 | + self.wait_for_text() |
| 176 | + |
187 | 177 | def stop(self):
|
188 | 178 | self.app.stop()
|
189 | 179 |
|
@@ -265,12 +255,6 @@ def setup_class(self):
|
265 | 255 | discriminator=self.th_server_discriminator,
|
266 | 256 | passcode=self.th_server_passcode)
|
267 | 257 |
|
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 |
| - |
274 | 258 | def teardown_class(self):
|
275 | 259 | if self.th_fsa_controller is not None:
|
276 | 260 | self.th_fsa_controller.stop()
|
|
0 commit comments