test_connection_banner.py 3.8 KB

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