do silly things

This commit is contained in:
Khaleel Al-Adhami 2025-01-17 15:51:11 -08:00
parent fa6c12e8b3
commit aadd8b56bf

View File

@ -17,9 +17,16 @@ def LifespanApp():
import reflex as rx import reflex as rx
def create_tasks():
lifespan_task_global = 0 lifespan_task_global = 0
lifespan_context_global = 0 lifespan_context_global = 0
def lifespan_context_global_getter():
return lifespan_context_global
def lifespan_task_global_getter():
return lifespan_task_global
@asynccontextmanager @asynccontextmanager
async def lifespan_context(app, inc: int = 1): async def lifespan_context(app, inc: int = 1):
nonlocal lifespan_context_global nonlocal lifespan_context_global
@ -57,6 +64,22 @@ def LifespanApp():
def tick(self, date): def tick(self, date):
pass pass
return (
lifespan_task,
lifespan_context,
LifespanState,
lifespan_task_global_getter,
lifespan_context_global_getter,
)
(
lifespan_task,
lifespan_context,
LifespanState,
lifespan_task_global_getter,
lifespan_context_global_getter,
) = create_tasks()
def index(): def index():
return rx.vstack( return rx.vstack(
rx.text(LifespanState.task_global, id="task_global"), rx.text(LifespanState.task_global, id="task_global"),
@ -113,13 +136,16 @@ async def test_lifespan(lifespan_app: AppHarness):
task_global = driver.find_element(By.ID, "task_global") task_global = driver.find_element(By.ID, "task_global")
assert context_global.text == "2" assert context_global.text == "2"
assert lifespan_app.app_module.lifespan_context_global == 2 # type: ignore assert lifespan_app.app_module.lifespan_context_global_getter() == 2 # type: ignore
original_task_global_text = task_global.text original_task_global_text = task_global.text
original_task_global_value = int(original_task_global_text) original_task_global_value = int(original_task_global_text)
lifespan_app.poll_for_content(task_global, exp_not_equal=original_task_global_text) lifespan_app.poll_for_content(task_global, exp_not_equal=original_task_global_text)
driver.find_element(By.ID, "toggle-tick").click() # avoid teardown errors driver.find_element(By.ID, "toggle-tick").click() # avoid teardown errors
assert lifespan_app.app_module.lifespan_task_global > original_task_global_value # type: ignore assert (
lifespan_app.app_module.lifespan_task_global_getter()
> original_task_global_value
) # type: ignore
assert int(task_global.text) > original_task_global_value assert int(task_global.text) > original_task_global_value
# Kill the backend # Kill the backend
@ -129,5 +155,5 @@ async def test_lifespan(lifespan_app: AppHarness):
lifespan_app.backend_thread.join() lifespan_app.backend_thread.join()
# Check that the lifespan tasks have been cancelled # Check that the lifespan tasks have been cancelled
assert lifespan_app.app_module.lifespan_task_global == 0 assert lifespan_app.app_module.lifespan_task_global_getter() == 0
assert lifespan_app.app_module.lifespan_context_global == 4 assert lifespan_app.app_module.lifespan_context_global_getter() == 4