test_connection_banner.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. """Test case for displaying the connection banner when the websocket drops."""
  2. from typing import Generator
  3. import pytest
  4. from selenium.common.exceptions import NoSuchElementException
  5. from selenium.webdriver.common.by import By
  6. from reflex.testing import AppHarness, WebDriver
  7. from .utils import SessionStorage
  8. def ConnectionBanner():
  9. """App with a connection banner."""
  10. import asyncio
  11. import reflex as rx
  12. class State(rx.State):
  13. foo: int = 0
  14. async def delay(self):
  15. await asyncio.sleep(5)
  16. def index():
  17. return rx.vstack(
  18. rx.text("Hello World"),
  19. rx.input(value=State.foo, read_only=True, id="counter"),
  20. rx.button(
  21. "Increment",
  22. id="increment",
  23. on_click=State.set_foo(State.foo + 1), # type: ignore
  24. ),
  25. rx.button("Delay", id="delay", on_click=State.delay),
  26. )
  27. app = rx.App(state=rx.State)
  28. app.add_page(index)
  29. @pytest.fixture()
  30. def connection_banner(tmp_path) -> Generator[AppHarness, None, None]:
  31. """Start ConnectionBanner app at tmp_path via AppHarness.
  32. Args:
  33. tmp_path: pytest tmp_path fixture
  34. Yields:
  35. running AppHarness instance
  36. """
  37. with AppHarness.create(
  38. root=tmp_path,
  39. app_source=ConnectionBanner, # type: ignore
  40. ) as harness:
  41. yield harness
  42. CONNECTION_ERROR_XPATH = "//*[ contains(text(), 'Cannot connect to server') ]"
  43. def has_error_modal(driver: WebDriver) -> bool:
  44. """Check if the connection error modal is displayed.
  45. Args:
  46. driver: Selenium webdriver instance.
  47. Returns:
  48. True if the modal is displayed, False otherwise.
  49. """
  50. try:
  51. driver.find_element(By.XPATH, CONNECTION_ERROR_XPATH)
  52. return True
  53. except NoSuchElementException:
  54. return False
  55. @pytest.mark.asyncio
  56. async def test_connection_banner(connection_banner: AppHarness):
  57. """Test that the connection banner is displayed when the websocket drops.
  58. Args:
  59. connection_banner: AppHarness instance.
  60. """
  61. assert connection_banner.app_instance is not None
  62. assert connection_banner.backend is not None
  63. driver = connection_banner.frontend()
  64. ss = SessionStorage(driver)
  65. assert connection_banner._poll_for(
  66. lambda: ss.get("token") is not None
  67. ), "token not found"
  68. assert connection_banner._poll_for(lambda: not has_error_modal(driver))
  69. delay_button = driver.find_element(By.ID, "delay")
  70. increment_button = driver.find_element(By.ID, "increment")
  71. counter_element = driver.find_element(By.ID, "counter")
  72. # Increment the counter
  73. increment_button.click()
  74. assert connection_banner.poll_for_value(counter_element, exp_not_equal="0") == "1"
  75. # Start an long event before killing the backend, to mark event_processing=true
  76. delay_button.click()
  77. # Get the backend port
  78. backend_port = connection_banner._poll_for_servers().getsockname()[1]
  79. # Kill the backend
  80. connection_banner.backend.should_exit = True
  81. if connection_banner.backend_thread is not None:
  82. connection_banner.backend_thread.join()
  83. # Error modal should now be displayed
  84. assert connection_banner._poll_for(lambda: has_error_modal(driver))
  85. # Increment the counter with backend down
  86. increment_button.click()
  87. assert connection_banner.poll_for_value(counter_element, exp_not_equal="0") == "1"
  88. # Bring the backend back up
  89. connection_banner._start_backend(port=backend_port)
  90. # Create a new StateManager to avoid async loop affinity issues w/ redis
  91. await connection_banner._reset_backend_state_manager()
  92. # Banner should be gone now
  93. assert connection_banner._poll_for(lambda: not has_error_modal(driver))
  94. # Count should have incremented after coming back up
  95. assert connection_banner.poll_for_value(counter_element, exp_not_equal="1") == "2"