test fast yielding in background task

This commit is contained in:
Benedikt Bartscher 2024-11-22 09:57:18 +01:00
parent c29c6b657a
commit 9ec9ab7929
No known key found for this signature in database

View File

@ -42,6 +42,11 @@ def BackgroundTask():
yield State.increment() # type: ignore
await asyncio.sleep(0.005)
@rx.event(background=True)
async def fast_yielding(self):
for _ in range(100):
yield State.increment()
@rx.event
def increment(self):
self.counter += 1
@ -375,3 +380,28 @@ def test_yield_in_async_with_self(
yield_in_async_with_self_button.click()
assert background_task._poll_for(lambda: counter.text == "2", timeout=5)
def test_fast_yielding(
background_task: AppHarness,
driver: WebDriver,
token: str,
) -> None:
"""Test that fast yielding works as expected.
Args:
background_task: harness for BackgroundTask app.
driver: WebDriver instance.
token: The token for the connected client.
"""
assert background_task.app_instance is not None
# get a reference to all buttons
fast_yielding_button = driver.find_element(By.ID, "yield-increment")
# get a reference to the counter
counter = driver.find_element(By.ID, "counter")
assert background_task._poll_for(lambda: counter.text == "0", timeout=5)
fast_yielding_button.click()
assert background_task._poll_for(lambda: counter.text == "100", timeout=5)