testing.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
  1. """reflex.testing - tools for testing reflex apps."""
  2. from __future__ import annotations
  3. import asyncio
  4. import contextlib
  5. import dataclasses
  6. import functools
  7. import inspect
  8. import os
  9. import pathlib
  10. import platform
  11. import re
  12. import signal
  13. import socket
  14. import socketserver
  15. import subprocess
  16. import textwrap
  17. import threading
  18. import time
  19. import types
  20. from http.server import SimpleHTTPRequestHandler
  21. from typing import (
  22. TYPE_CHECKING,
  23. Any,
  24. AsyncIterator,
  25. Callable,
  26. Coroutine,
  27. Optional,
  28. Type,
  29. TypeVar,
  30. Union,
  31. )
  32. import psutil
  33. import uvicorn
  34. import reflex
  35. import reflex.reflex
  36. import reflex.utils.build
  37. import reflex.utils.exec
  38. import reflex.utils.prerequisites
  39. import reflex.utils.processes
  40. from reflex.state import (
  41. BaseState,
  42. State,
  43. StateManagerMemory,
  44. StateManagerRedis,
  45. reload_state_module,
  46. )
  47. try:
  48. from selenium import webdriver # pyright: ignore [reportMissingImports]
  49. from selenium.webdriver.remote.webdriver import ( # pyright: ignore [reportMissingImports]
  50. WebDriver,
  51. )
  52. if TYPE_CHECKING:
  53. from selenium.webdriver.common.options import (
  54. ArgOptions, # pyright: ignore [reportMissingImports]
  55. )
  56. from selenium.webdriver.remote.webelement import ( # pyright: ignore [reportMissingImports]
  57. WebElement,
  58. )
  59. has_selenium = True
  60. except ImportError:
  61. has_selenium = False
  62. # The timeout (minutes) to check for the port.
  63. DEFAULT_TIMEOUT = 15
  64. POLL_INTERVAL = 0.25
  65. FRONTEND_POPEN_ARGS = {}
  66. T = TypeVar("T")
  67. TimeoutType = Optional[Union[int, float]]
  68. if platform.system() == "Windows":
  69. FRONTEND_POPEN_ARGS["creationflags"] = subprocess.CREATE_NEW_PROCESS_GROUP # type: ignore
  70. FRONTEND_POPEN_ARGS["shell"] = True
  71. else:
  72. FRONTEND_POPEN_ARGS["start_new_session"] = True
  73. # borrowed from py3.11
  74. class chdir(contextlib.AbstractContextManager):
  75. """Non thread-safe context manager to change the current working directory."""
  76. def __init__(self, path):
  77. """Prepare contextmanager.
  78. Args:
  79. path: the path to change to
  80. """
  81. self.path = path
  82. self._old_cwd = []
  83. def __enter__(self):
  84. """Save current directory and perform chdir."""
  85. self._old_cwd.append(os.getcwd())
  86. os.chdir(self.path)
  87. def __exit__(self, *excinfo):
  88. """Change back to previous directory on stack.
  89. Args:
  90. excinfo: sys.exc_info captured in the context block
  91. """
  92. os.chdir(self._old_cwd.pop())
  93. @dataclasses.dataclass
  94. class AppHarness:
  95. """AppHarness executes a reflex app in-process for testing."""
  96. app_name: str
  97. app_source: Optional[types.FunctionType | types.ModuleType] | str
  98. app_path: pathlib.Path
  99. app_module_path: pathlib.Path
  100. app_module: Optional[types.ModuleType] = None
  101. app_instance: Optional[reflex.App] = None
  102. frontend_process: Optional[subprocess.Popen] = None
  103. frontend_url: Optional[str] = None
  104. frontend_output_thread: Optional[threading.Thread] = None
  105. backend_thread: Optional[threading.Thread] = None
  106. backend: Optional[uvicorn.Server] = None
  107. state_manager: Optional[StateManagerMemory | StateManagerRedis] = None
  108. _frontends: list["WebDriver"] = dataclasses.field(default_factory=list)
  109. _decorated_pages: list = dataclasses.field(default_factory=list)
  110. @classmethod
  111. def create(
  112. cls,
  113. root: pathlib.Path,
  114. app_source: Optional[types.FunctionType | types.ModuleType | str] = None,
  115. app_name: Optional[str] = None,
  116. ) -> "AppHarness":
  117. """Create an AppHarness instance at root.
  118. Args:
  119. root: the directory that will contain the app under test.
  120. app_source: if specified, the source code from this function or module is used
  121. as the main module for the app. It may also be the raw source code text, as a str.
  122. If unspecified, then root must already contain a working reflex app and will be used directly.
  123. app_name: provide the name of the app, otherwise will be derived from app_source or root.
  124. Raises:
  125. ValueError: when app_source is a string and app_name is not provided.
  126. Returns:
  127. AppHarness instance
  128. """
  129. if app_name is None:
  130. if app_source is None:
  131. app_name = root.name
  132. elif isinstance(app_source, functools.partial):
  133. keywords = app_source.keywords
  134. slug_suffix = "_".join([str(v) for v in keywords.values()])
  135. func_name = app_source.func.__name__
  136. app_name = f"{func_name}_{slug_suffix}"
  137. app_name = re.sub(r"[^a-zA-Z0-9_]", "_", app_name)
  138. elif isinstance(app_source, str):
  139. raise ValueError(
  140. "app_name must be provided when app_source is a string."
  141. )
  142. else:
  143. app_name = app_source.__name__
  144. app_name = app_name.lower()
  145. return cls(
  146. app_name=app_name,
  147. app_source=app_source,
  148. app_path=root,
  149. app_module_path=root / app_name / f"{app_name}.py",
  150. )
  151. def _get_globals_from_signature(self, func: Any) -> dict[str, Any]:
  152. """Get the globals from a function or module object.
  153. Args:
  154. func: function or module object
  155. Returns:
  156. dict of globals
  157. """
  158. overrides = {}
  159. glbs = {}
  160. if not callable(func):
  161. return glbs
  162. if isinstance(func, functools.partial):
  163. overrides = func.keywords
  164. func = func.func
  165. for param in inspect.signature(func).parameters.values():
  166. if param.default is not inspect.Parameter.empty:
  167. glbs[param.name] = param.default
  168. glbs.update(overrides)
  169. return glbs
  170. def _get_source_from_app_source(self, app_source: Any) -> str:
  171. """Get the source from app_source.
  172. Args:
  173. app_source: function or module or str
  174. Returns:
  175. source code
  176. """
  177. if isinstance(app_source, str):
  178. return app_source
  179. source = inspect.getsource(app_source)
  180. source = re.sub(
  181. r"^\s*def\s+\w+\s*\(.*?\)(\s+->\s+\w+)?:", "", source, flags=re.DOTALL
  182. )
  183. return textwrap.dedent(source)
  184. def _initialize_app(self):
  185. os.environ["TELEMETRY_ENABLED"] = "" # disable telemetry reporting for tests
  186. self.app_path.mkdir(parents=True, exist_ok=True)
  187. if self.app_source is not None:
  188. app_globals = self._get_globals_from_signature(self.app_source)
  189. if isinstance(self.app_source, functools.partial):
  190. self.app_source = self.app_source.func # type: ignore
  191. # get the source from a function or module object
  192. source_code = "\n".join(
  193. [
  194. "\n".join(
  195. self.get_app_global_source(k, v) for k, v in app_globals.items()
  196. ),
  197. self._get_source_from_app_source(self.app_source),
  198. ]
  199. )
  200. with chdir(self.app_path):
  201. reflex.reflex._init(
  202. name=self.app_name,
  203. template=reflex.constants.Templates.DEFAULT,
  204. loglevel=reflex.constants.LogLevel.INFO,
  205. )
  206. self.app_module_path.write_text(source_code)
  207. with chdir(self.app_path):
  208. # ensure config and app are reloaded when testing different app
  209. reflex.config.get_config(reload=True)
  210. # Save decorated pages before importing the test app module
  211. before_decorated_pages = reflex.app.DECORATED_PAGES.copy()
  212. # Ensure the AppHarness test does not skip State assignment due to running via pytest
  213. os.environ.pop(reflex.constants.PYTEST_CURRENT_TEST, None)
  214. self.app_module = reflex.utils.prerequisites.get_compiled_app(reload=True)
  215. # Save the pages that were added during testing
  216. self._decorated_pages = [
  217. p for p in reflex.app.DECORATED_PAGES if p not in before_decorated_pages
  218. ]
  219. self.app_instance = self.app_module.app
  220. if isinstance(self.app_instance._state_manager, StateManagerRedis):
  221. # Create our own redis connection for testing.
  222. self.state_manager = StateManagerRedis.create(self.app_instance.state)
  223. else:
  224. self.state_manager = self.app_instance._state_manager
  225. def _reload_state_module(self):
  226. """Reload the rx.State module to avoid conflict when reloading."""
  227. reload_state_module(module=f"{self.app_name}.{self.app_name}")
  228. def _get_backend_shutdown_handler(self):
  229. if self.backend is None:
  230. raise RuntimeError("Backend was not initialized.")
  231. original_shutdown = self.backend.shutdown
  232. async def _shutdown_redis(*args, **kwargs) -> None:
  233. # ensure redis is closed before event loop
  234. try:
  235. if self.app_instance is not None and isinstance(
  236. self.app_instance.state_manager, StateManagerRedis
  237. ):
  238. await self.app_instance.state_manager.close()
  239. except ValueError:
  240. pass
  241. await original_shutdown(*args, **kwargs)
  242. return _shutdown_redis
  243. def _start_backend(self, port=0):
  244. if self.app_instance is None:
  245. raise RuntimeError("App was not initialized.")
  246. self.backend = uvicorn.Server(
  247. uvicorn.Config(
  248. app=self.app_instance.api,
  249. host="127.0.0.1",
  250. port=port,
  251. )
  252. )
  253. self.backend.shutdown = self._get_backend_shutdown_handler()
  254. self.backend_thread = threading.Thread(target=self.backend.run)
  255. self.backend_thread.start()
  256. def _start_frontend(self):
  257. # Set up the frontend.
  258. with chdir(self.app_path):
  259. config = reflex.config.get_config()
  260. config.api_url = "http://{0}:{1}".format(
  261. *self._poll_for_servers().getsockname(),
  262. )
  263. reflex.utils.build.setup_frontend(self.app_path)
  264. # Start the frontend.
  265. self.frontend_process = reflex.utils.processes.new_process(
  266. [reflex.utils.prerequisites.get_package_manager(), "run", "dev"],
  267. cwd=self.app_path / reflex.constants.Dirs.WEB,
  268. env={"PORT": "0"},
  269. **FRONTEND_POPEN_ARGS,
  270. )
  271. def _wait_frontend(self):
  272. while self.frontend_url is None:
  273. line = (
  274. self.frontend_process.stdout.readline() # pyright: ignore [reportOptionalMemberAccess]
  275. )
  276. if not line:
  277. break
  278. print(line) # for pytest diagnosis
  279. m = re.search(reflex.constants.Next.FRONTEND_LISTENING_REGEX, line)
  280. if m is not None:
  281. self.frontend_url = m.group(1)
  282. break
  283. if self.frontend_url is None:
  284. raise RuntimeError("Frontend did not start")
  285. def consume_frontend_output():
  286. while True:
  287. line = (
  288. self.frontend_process.stdout.readline() # pyright: ignore [reportOptionalMemberAccess]
  289. )
  290. if not line:
  291. break
  292. print(line)
  293. self.frontend_output_thread = threading.Thread(target=consume_frontend_output)
  294. self.frontend_output_thread.start()
  295. def start(self) -> "AppHarness":
  296. """Start the backend in a new thread and dev frontend as a separate process.
  297. Returns:
  298. self
  299. """
  300. self._initialize_app()
  301. self._start_backend()
  302. self._start_frontend()
  303. self._wait_frontend()
  304. return self
  305. @staticmethod
  306. def get_app_global_source(key, value):
  307. """Get the source code of a global object.
  308. If value is a function or class we render the actual
  309. source of value otherwise we assign value to key.
  310. Args:
  311. key: variable name to assign value to.
  312. value: value of the global variable.
  313. Returns:
  314. The rendered app global code.
  315. """
  316. if not inspect.isclass(value) and not inspect.isfunction(value):
  317. return f"{key} = {value!r}"
  318. return inspect.getsource(value)
  319. def __enter__(self) -> "AppHarness":
  320. """Contextmanager protocol for `start()`.
  321. Returns:
  322. Instance of AppHarness after calling start()
  323. """
  324. return self.start()
  325. def stop(self) -> None:
  326. """Stop the frontend and backend servers."""
  327. self._reload_state_module()
  328. if self.backend is not None:
  329. self.backend.should_exit = True
  330. if self.frontend_process is not None:
  331. # https://stackoverflow.com/a/70565806
  332. frontend_children = psutil.Process(self.frontend_process.pid).children(
  333. recursive=True,
  334. )
  335. if platform.system() == "Windows":
  336. self.frontend_process.terminate()
  337. else:
  338. pgrp = os.getpgid(self.frontend_process.pid)
  339. os.killpg(pgrp, signal.SIGTERM)
  340. # kill any remaining child processes
  341. for child in frontend_children:
  342. # It's okay if the process is already gone.
  343. with contextlib.suppress(psutil.NoSuchProcess):
  344. child.terminate()
  345. _, still_alive = psutil.wait_procs(frontend_children, timeout=3)
  346. for child in still_alive:
  347. # It's okay if the process is already gone.
  348. with contextlib.suppress(psutil.NoSuchProcess):
  349. child.kill()
  350. # wait for main process to exit
  351. self.frontend_process.communicate()
  352. if self.backend_thread is not None:
  353. self.backend_thread.join()
  354. if self.frontend_output_thread is not None:
  355. self.frontend_output_thread.join()
  356. for driver in self._frontends:
  357. driver.quit()
  358. # Cleanup decorated pages added during testing
  359. for page in self._decorated_pages:
  360. reflex.app.DECORATED_PAGES.remove(page)
  361. def __exit__(self, *excinfo) -> None:
  362. """Contextmanager protocol for `stop()`.
  363. Args:
  364. excinfo: sys.exc_info captured in the context block
  365. """
  366. self.stop()
  367. @staticmethod
  368. def _poll_for(
  369. target: Callable[[], T],
  370. timeout: TimeoutType = None,
  371. step: TimeoutType = None,
  372. ) -> T | bool:
  373. """Generic polling logic.
  374. Args:
  375. target: callable that returns truthy if polling condition is met.
  376. timeout: max polling time
  377. step: interval between checking target()
  378. Returns:
  379. return value of target() if truthy within timeout
  380. False if timeout elapses
  381. """
  382. if timeout is None:
  383. timeout = DEFAULT_TIMEOUT
  384. if step is None:
  385. step = POLL_INTERVAL
  386. deadline = time.time() + timeout
  387. while time.time() < deadline:
  388. success = target()
  389. if success:
  390. return success
  391. time.sleep(step)
  392. return False
  393. @staticmethod
  394. async def _poll_for_async(
  395. target: Callable[[], Coroutine[None, None, T]],
  396. timeout: TimeoutType = None,
  397. step: TimeoutType = None,
  398. ) -> T | bool:
  399. """Generic polling logic for async functions.
  400. Args:
  401. target: callable that returns truthy if polling condition is met.
  402. timeout: max polling time
  403. step: interval between checking target()
  404. Returns:
  405. return value of target() if truthy within timeout
  406. False if timeout elapses
  407. """
  408. if timeout is None:
  409. timeout = DEFAULT_TIMEOUT
  410. if step is None:
  411. step = POLL_INTERVAL
  412. deadline = time.time() + timeout
  413. while time.time() < deadline:
  414. success = await target()
  415. if success:
  416. return success
  417. await asyncio.sleep(step)
  418. return False
  419. def _poll_for_servers(self, timeout: TimeoutType = None) -> socket.socket:
  420. """Poll backend server for listening sockets.
  421. Args:
  422. timeout: how long to wait for listening socket.
  423. Returns:
  424. first active listening socket on the backend
  425. Raises:
  426. RuntimeError: when the backend hasn't started running
  427. TimeoutError: when server or sockets are not ready
  428. """
  429. if self.backend is None:
  430. raise RuntimeError("Backend is not running.")
  431. backend = self.backend
  432. # check for servers to be initialized
  433. if not self._poll_for(
  434. target=lambda: getattr(backend, "servers", False),
  435. timeout=timeout,
  436. ):
  437. raise TimeoutError("Backend servers are not initialized.")
  438. # check for sockets to be listening
  439. if not self._poll_for(
  440. target=lambda: getattr(backend.servers[0], "sockets", False),
  441. timeout=timeout,
  442. ):
  443. raise TimeoutError("Backend is not listening.")
  444. return backend.servers[0].sockets[0]
  445. def frontend(self, driver_clz: Optional[Type["WebDriver"]] = None) -> "WebDriver":
  446. """Get a selenium webdriver instance pointed at the app.
  447. Args:
  448. driver_clz: webdriver.Chrome (default), webdriver.Firefox, webdriver.Safari,
  449. webdriver.Edge, etc
  450. Returns:
  451. Instance of the given webdriver navigated to the frontend url of the app.
  452. Raises:
  453. RuntimeError: when selenium is not importable or frontend is not running
  454. """
  455. if not has_selenium:
  456. raise RuntimeError(
  457. "Frontend functionality requires `selenium` to be installed, "
  458. "and it could not be imported."
  459. )
  460. if self.frontend_url is None:
  461. raise RuntimeError("Frontend is not running.")
  462. want_headless = False
  463. options: ArgOptions | None = None
  464. if os.environ.get("APP_HARNESS_HEADLESS"):
  465. want_headless = True
  466. if driver_clz is None:
  467. requested_driver = os.environ.get("APP_HARNESS_DRIVER", "Chrome")
  468. driver_clz = getattr(webdriver, requested_driver)
  469. options = webdriver.ChromeOptions()
  470. if driver_clz is webdriver.Chrome and want_headless:
  471. options = webdriver.ChromeOptions()
  472. options.add_argument("--headless=new")
  473. elif driver_clz is webdriver.Firefox and want_headless:
  474. options = webdriver.FirefoxOptions()
  475. options.add_argument("-headless")
  476. elif driver_clz is webdriver.Edge and want_headless:
  477. options = webdriver.EdgeOptions()
  478. options.add_argument("headless")
  479. if options and (args := os.environ.get("APP_HARNESS_DRIVER_ARGS")):
  480. for arg in args.split(","):
  481. options.add_argument(arg)
  482. driver = driver_clz(options=options) # type: ignore
  483. driver.get(self.frontend_url)
  484. self._frontends.append(driver)
  485. return driver
  486. async def get_state(self, token: str) -> BaseState:
  487. """Get the state associated with the given token.
  488. Args:
  489. token: The state token to look up.
  490. Returns:
  491. The state instance associated with the given token
  492. Raises:
  493. RuntimeError: when the app hasn't started running
  494. """
  495. if self.state_manager is None:
  496. raise RuntimeError("state_manager is not set.")
  497. try:
  498. return await self.state_manager.get_state(token)
  499. finally:
  500. if isinstance(self.state_manager, StateManagerRedis):
  501. await self.state_manager.close()
  502. async def set_state(self, token: str, **kwargs) -> None:
  503. """Set the state associated with the given token.
  504. Args:
  505. token: The state token to set.
  506. kwargs: Attributes to set on the state.
  507. Raises:
  508. RuntimeError: when the app hasn't started running
  509. """
  510. if self.state_manager is None:
  511. raise RuntimeError("state_manager is not set.")
  512. state = await self.get_state(token)
  513. for key, value in kwargs.items():
  514. setattr(state, key, value)
  515. try:
  516. await self.state_manager.set_state(token, state)
  517. finally:
  518. if isinstance(self.state_manager, StateManagerRedis):
  519. await self.state_manager.close()
  520. @contextlib.asynccontextmanager
  521. async def modify_state(self, token: str) -> AsyncIterator[State]:
  522. """Modify the state associated with the given token and send update to frontend.
  523. Args:
  524. token: The state token to modify
  525. Yields:
  526. The state instance associated with the given token
  527. Raises:
  528. RuntimeError: when the app hasn't started running
  529. """
  530. if self.state_manager is None:
  531. raise RuntimeError("state_manager is not set.")
  532. if self.app_instance is None:
  533. raise RuntimeError("App is not running.")
  534. app_state_manager = self.app_instance.state_manager
  535. if isinstance(self.state_manager, StateManagerRedis):
  536. # Temporarily replace the app's state manager with our own, since
  537. # the redis connection is on the backend_thread event loop
  538. self.app_instance._state_manager = self.state_manager
  539. try:
  540. async with self.app_instance.modify_state(token) as state:
  541. yield state
  542. finally:
  543. if isinstance(self.state_manager, StateManagerRedis):
  544. self.app_instance._state_manager = app_state_manager
  545. await self.state_manager.close()
  546. def poll_for_content(
  547. self,
  548. element: "WebElement",
  549. timeout: TimeoutType = None,
  550. exp_not_equal: str = "",
  551. ) -> str:
  552. """Poll element.text for change.
  553. Args:
  554. element: selenium webdriver element to check
  555. timeout: how long to poll element.text
  556. exp_not_equal: exit the polling loop when the element text does not match
  557. Returns:
  558. The element text when the polling loop exited
  559. Raises:
  560. TimeoutError: when the timeout expires before text changes
  561. """
  562. if not self._poll_for(
  563. target=lambda: element.text != exp_not_equal,
  564. timeout=timeout,
  565. ):
  566. raise TimeoutError(
  567. f"{element} content remains {exp_not_equal!r} while polling.",
  568. )
  569. return element.text
  570. def poll_for_value(
  571. self,
  572. element: "WebElement",
  573. timeout: TimeoutType = None,
  574. exp_not_equal: str = "",
  575. ) -> Optional[str]:
  576. """Poll element.get_attribute("value") for change.
  577. Args:
  578. element: selenium webdriver element to check
  579. timeout: how long to poll element value attribute
  580. exp_not_equal: exit the polling loop when the value does not match
  581. Returns:
  582. The element value when the polling loop exited
  583. Raises:
  584. TimeoutError: when the timeout expires before value changes
  585. """
  586. if not self._poll_for(
  587. target=lambda: element.get_attribute("value") != exp_not_equal,
  588. timeout=timeout,
  589. ):
  590. raise TimeoutError(
  591. f"{element} content remains {exp_not_equal!r} while polling.",
  592. )
  593. return element.get_attribute("value")
  594. def poll_for_clients(self, timeout: TimeoutType = None) -> dict[str, BaseState]:
  595. """Poll app state_manager for any connected clients.
  596. Args:
  597. timeout: how long to wait for client states
  598. Returns:
  599. active state instances when the polling loop exited
  600. Raises:
  601. RuntimeError: when the app hasn't started running
  602. TimeoutError: when the timeout expires before any states are seen
  603. """
  604. if self.app_instance is None:
  605. raise RuntimeError("App is not running.")
  606. state_manager = self.app_instance.state_manager
  607. assert isinstance(
  608. state_manager, StateManagerMemory
  609. ), "Only works with memory state manager"
  610. if not self._poll_for(
  611. target=lambda: state_manager.states,
  612. timeout=timeout,
  613. ):
  614. raise TimeoutError("No states were observed while polling.")
  615. return state_manager.states
  616. class SimpleHTTPRequestHandlerCustomErrors(SimpleHTTPRequestHandler):
  617. """SimpleHTTPRequestHandler with custom error page handling."""
  618. def __init__(self, *args, error_page_map: dict[int, pathlib.Path], **kwargs):
  619. """Initialize the handler.
  620. Args:
  621. error_page_map: map of error code to error page path
  622. *args: passed through to superclass
  623. **kwargs: passed through to superclass
  624. """
  625. self.error_page_map = error_page_map
  626. super().__init__(*args, **kwargs)
  627. def send_error(
  628. self, code: int, message: str | None = None, explain: str | None = None
  629. ) -> None:
  630. """Send the error page for the given error code.
  631. If the code matches a custom error page, then message and explain are
  632. ignored.
  633. Args:
  634. code: the error code
  635. message: the error message
  636. explain: the error explanation
  637. """
  638. error_page = self.error_page_map.get(code)
  639. if error_page:
  640. self.send_response(code, message)
  641. self.send_header("Connection", "close")
  642. body = error_page.read_bytes()
  643. self.send_header("Content-Type", self.error_content_type)
  644. self.send_header("Content-Length", str(len(body)))
  645. self.end_headers()
  646. self.wfile.write(body)
  647. else:
  648. super().send_error(code, message, explain)
  649. class Subdir404TCPServer(socketserver.TCPServer):
  650. """TCPServer for SimpleHTTPRequestHandlerCustomErrors that serves from a subdir."""
  651. def __init__(
  652. self,
  653. *args,
  654. root: pathlib.Path,
  655. error_page_map: dict[int, pathlib.Path] | None,
  656. **kwargs,
  657. ):
  658. """Initialize the server.
  659. Args:
  660. root: the root directory to serve from
  661. error_page_map: map of error code to error page path
  662. *args: passed through to superclass
  663. **kwargs: passed through to superclass
  664. """
  665. self.root = root
  666. self.error_page_map = error_page_map or {}
  667. super().__init__(*args, **kwargs)
  668. def finish_request(self, request: socket.socket, client_address: tuple[str, int]):
  669. """Finish one request by instantiating RequestHandlerClass.
  670. Args:
  671. request: the requesting socket
  672. client_address: (host, port) referring to the client’s address.
  673. """
  674. self.RequestHandlerClass(
  675. request,
  676. client_address,
  677. self,
  678. directory=str(self.root), # type: ignore
  679. error_page_map=self.error_page_map, # type: ignore
  680. )
  681. class AppHarnessProd(AppHarness):
  682. """AppHarnessProd executes a reflex app in-process for testing.
  683. In prod mode, instead of running `next dev` the app is exported as static
  684. files and served via the builtin python http.server with custom 404 redirect
  685. handling. Additionally, the backend runs in multi-worker mode.
  686. """
  687. frontend_thread: Optional[threading.Thread] = None
  688. frontend_server: Optional[Subdir404TCPServer] = None
  689. def _run_frontend(self):
  690. web_root = self.app_path / reflex.constants.Dirs.WEB_STATIC
  691. error_page_map = {
  692. 404: web_root / "404.html",
  693. }
  694. with Subdir404TCPServer(
  695. ("", 0),
  696. SimpleHTTPRequestHandlerCustomErrors,
  697. root=web_root,
  698. error_page_map=error_page_map,
  699. ) as self.frontend_server:
  700. self.frontend_url = "http://localhost:{1}".format(
  701. *self.frontend_server.socket.getsockname()
  702. )
  703. self.frontend_server.serve_forever()
  704. def _start_frontend(self):
  705. # Set up the frontend.
  706. with chdir(self.app_path):
  707. config = reflex.config.get_config()
  708. config.api_url = "http://{0}:{1}".format(
  709. *self._poll_for_servers().getsockname(),
  710. )
  711. reflex.reflex.export(
  712. zipping=False,
  713. frontend=True,
  714. backend=False,
  715. loglevel=reflex.constants.LogLevel.INFO,
  716. )
  717. self.frontend_thread = threading.Thread(target=self._run_frontend)
  718. self.frontend_thread.start()
  719. def _wait_frontend(self):
  720. self._poll_for(lambda: self.frontend_server is not None)
  721. if self.frontend_server is None or not self.frontend_server.socket.fileno():
  722. raise RuntimeError("Frontend did not start")
  723. def _start_backend(self):
  724. if self.app_instance is None:
  725. raise RuntimeError("App was not initialized.")
  726. os.environ[reflex.constants.SKIP_COMPILE_ENV_VAR] = "yes"
  727. self.backend = uvicorn.Server(
  728. uvicorn.Config(
  729. app=self.app_instance,
  730. host="127.0.0.1",
  731. port=0,
  732. workers=reflex.utils.processes.get_num_workers(),
  733. ),
  734. )
  735. self.backend.shutdown = self._get_backend_shutdown_handler()
  736. self.backend_thread = threading.Thread(target=self.backend.run)
  737. self.backend_thread.start()
  738. def _poll_for_servers(self, timeout: TimeoutType = None) -> socket.socket:
  739. try:
  740. return super()._poll_for_servers(timeout)
  741. finally:
  742. os.environ.pop(reflex.constants.SKIP_COMPILE_ENV_VAR, None)
  743. def stop(self):
  744. """Stop the frontend python webserver."""
  745. super().stop()
  746. if self.frontend_server is not None:
  747. self.frontend_server.shutdown()
  748. if self.frontend_thread is not None:
  749. self.frontend_thread.join()