Skip to content

Commit 7223503

Browse files
committed
Merge remote-tracking branch 'origin/main' into release/reflex-0.8.3
2 parents 0c2b4fc + 9114d21 commit 7223503

File tree

2 files changed

+39
-21
lines changed

2 files changed

+39
-21
lines changed

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,3 +273,6 @@ hooks = [{ id = "darglint", exclude = "^reflex/reflex.py" }]
273273
repo = "https://github.com/pre-commit/mirrors-prettier"
274274
rev = "f62a70a3a7114896b062de517d72829ea1c884b6"
275275
hooks = [{ id = "prettier", require_serial = true }]
276+
277+
[tool.uv]
278+
required-version = ">=0.7.0"

reflex/testing.py

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424
from pathlib import Path
2525
from typing import TYPE_CHECKING, Any, Literal, TypeVar
2626

27-
import psutil
28-
2927
import reflex
3028
import reflex.reflex
3129
import reflex.utils.exec
@@ -105,6 +103,24 @@ class ReflexProcessExitNonZeroError(RuntimeError):
105103
"""Exception raised when the reflex process exits with a non-zero status."""
106104

107105

106+
def _is_port_responsive(port: int) -> bool:
107+
"""Check if a port is responsive.
108+
109+
Args:
110+
port: the port to check
111+
112+
Returns:
113+
True if the port is responsive, False otherwise
114+
"""
115+
try:
116+
with contextlib.closing(
117+
socket.socket(socket.AF_INET, socket.SOCK_STREAM)
118+
) as sock:
119+
return sock.connect_ex(("127.0.0.1", port)) == 0
120+
except (OverflowError, PermissionError, OSError):
121+
return False
122+
123+
108124
@dataclasses.dataclass
109125
class AppHarness:
110126
"""AppHarness executes a reflex app in-process for testing."""
@@ -366,29 +382,28 @@ def _wait_for_servers(self, backend: bool, frontend: bool):
366382
msg = "Reflex process has no pid."
367383
raise RuntimeError(msg)
368384

385+
if backend and self.backend_port is None:
386+
msg = "Backend port is not set."
387+
raise RuntimeError(msg)
388+
389+
if frontend and self.frontend_port is None:
390+
msg = "Frontend port is not set."
391+
raise RuntimeError(msg)
392+
369393
frontend_ready = False
370394
backend_ready = False
371-
timeout = 30
372-
start_time = time.time()
373395

374-
process = psutil.Process(self.reflex_process.pid)
375396
while not ((not frontend or frontend_ready) and (not backend or backend_ready)):
376-
if time.time() - start_time > timeout:
377-
msg = f"Timeout waiting for servers. Frontend ready: {frontend_ready}, Backend ready: {backend_ready}"
378-
raise RuntimeError(msg)
379-
380-
for proc in process.children(recursive=True):
381-
with contextlib.suppress(psutil.NoSuchProcess, psutil.AccessDenied):
382-
if ncs := proc.net_connections():
383-
for net_conn in ncs:
384-
if net_conn.status == psutil.CONN_LISTEN:
385-
if net_conn.laddr.port == self.frontend_port:
386-
frontend_ready = True
387-
self.frontend_url = (
388-
f"http://localhost:{self.frontend_port}/"
389-
)
390-
elif net_conn.laddr.port == self.backend_port:
391-
backend_ready = True
397+
if backend and self.backend_port and _is_port_responsive(self.backend_port):
398+
backend_ready = True
399+
if (
400+
frontend
401+
and self.frontend_port
402+
and _is_port_responsive(self.frontend_port)
403+
):
404+
frontend_ready = True
405+
self.frontend_url = f"http://localhost:{self.frontend_port}/"
406+
time.sleep(POLL_INTERVAL)
392407

393408
async def _reset_backend_state_manager(self):
394409
"""Reset the StateManagerRedis event loop affinity.

0 commit comments

Comments
 (0)