MCPcopy
hub / github.com/reflex-dev/reflex / test_background_task

Function test_background_task

tests/integration/test_background_task.py:283–339  ·  view source on GitHub ↗

Test that background tasks work as expected. Args: background_task: harness for BackgroundTask app. driver: WebDriver instance. token: The token for the connected client.

(
    background_task: AppHarness,
    driver: WebDriver,
    token: str,
)

Source from the content-addressed store, hash-verified

281
282
283def test_background_task(
284 background_task: AppHarness,
285 driver: WebDriver,
286 token: str,
287):
288 """Test that background tasks work as expected.
289
290 Args:
291 background_task: harness for BackgroundTask app.
292 driver: WebDriver instance.
293 token: The token for the connected client.
294 """
295 assert background_task.app_instance is not None
296
297 # get a reference to all buttons
298 delayed_increment_button = driver.find_element(By.ID, "delayed-increment")
299 yield_increment_button = driver.find_element(By.ID, "yield-increment")
300 increment_button = driver.find_element(By.ID, "increment")
301 blocking_pause_button = driver.find_element(By.ID, "blocking-pause")
302 non_blocking_pause_button = driver.find_element(By.ID, "non-blocking-pause")
303 racy_increment_button = driver.find_element(By.ID, "racy-increment")
304 driver.find_element(By.ID, "reset")
305
306 # get a reference to the counter
307 counter = driver.find_element(By.ID, "counter")
308 counter_async_cv = driver.find_element(By.ID, "counter-async-cv")
309
310 # get a reference to the iterations input
311 iterations_input = driver.find_element(By.ID, "iterations")
312
313 # kick off background tasks
314 iterations_input.clear()
315 iterations_input.send_keys("50")
316 delayed_increment_button.click()
317 blocking_pause_button.click()
318 delayed_increment_button.click()
319 for _ in range(10):
320 increment_button.click()
321 blocking_pause_button.click()
322 delayed_increment_button.click()
323 delayed_increment_button.click()
324 yield_increment_button.click()
325 racy_increment_button.click()
326 non_blocking_pause_button.click()
327 yield_increment_button.click()
328 blocking_pause_button.click()
329 yield_increment_button.click()
330 for _ in range(10):
331 increment_button.click()
332 yield_increment_button.click()
333 blocking_pause_button.click()
334 AppHarness.expect(lambda: counter.text == "620", timeout=40)
335 AppHarness.expect(lambda: counter_async_cv.text == "620", timeout=40)
336 # all tasks should have exited and cleaned up
337 AppHarness.expect(
338 lambda: not background_task.app_instance.event_processor._tasks # pyright: ignore [reportOptionalMemberAccess]
339 )
340

Callers

nothing calls this directly

Calls 2

clearMethod · 0.80
expectMethod · 0.80

Tested by

no test coverage detected