mirror of
https://github.com/Textualize/textual.git
synced 2025-10-17 02:38:12 +03:00
Additional testing for devtools client/server
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
import asyncio
|
||||
import io
|
||||
import re
|
||||
from typing import Callable
|
||||
|
||||
import pytest
|
||||
from rich.console import Console, RenderableType
|
||||
|
||||
|
||||
re_link_ids = re.compile(r"id=[\d\.\-]*?;.*?\x1b")
|
||||
|
||||
|
||||
@@ -23,3 +25,32 @@ def render(renderable: RenderableType, no_wrap: bool = False) -> str:
|
||||
console.print(renderable, no_wrap=no_wrap, end="")
|
||||
output = replace_link_ids(capture.get())
|
||||
return output
|
||||
|
||||
|
||||
async def wait_for_predicate(
|
||||
predicate: Callable[[], bool],
|
||||
timeout_secs: float = 2,
|
||||
poll_delay_secs: float = 0.001,
|
||||
) -> None:
|
||||
"""Wait for the given predicate to become True by evaluating it every `poll_delay_secs`
|
||||
seconds. Fail the pytest test if the predicate does not become True after `timeout_secs`
|
||||
seconds.
|
||||
|
||||
Args:
|
||||
predicate (Callable[[], bool]): The predicate function which will be called repeatedly.
|
||||
timeout_secs (float): If the predicate doesn't evaluate to True after this number of
|
||||
seconds, the test will fail.
|
||||
poll_delay_secs (float): The number of seconds to wait between each call to the
|
||||
predicate function.
|
||||
"""
|
||||
time_taken = 0
|
||||
while True:
|
||||
result = predicate()
|
||||
if result:
|
||||
return
|
||||
await asyncio.sleep(poll_delay_secs)
|
||||
time_taken += poll_delay_secs
|
||||
if time_taken > timeout_secs:
|
||||
pytest.fail(
|
||||
f"Predicate {predicate} did not return True after {timeout_secs} seconds."
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user