Skip to content

Commit

Permalink
Merge pull request #27 from glutanimate/customizable-qtbot-timeouts
Browse files Browse the repository at this point in the history
Make qtbot timeouts customizable
  • Loading branch information
glutanimate authored Jul 31, 2022
2 parents b3396bf + 3503c94 commit 994085e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
7 changes: 5 additions & 2 deletions pytest_anki/_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,13 +289,14 @@ def run_in_thread_and_wait(
task: Callable,
task_args: Optional[Tuple[Any, ...]] = None,
task_kwargs: Optional[Dict[str, Any]] = None,
timeout: int = 5000,
):
thread_pool = QThreadPool.globalInstance()
worker = SignallingWorker(
task=task, task_args=task_args, task_kwargs=task_kwargs
)

with self._qtbot.wait_signal(worker.signals.finished):
with self._qtbot.wait_signal(worker.signals.finished, timeout=timeout):
thread_pool.start(worker)

if exception := worker.error:
Expand Down Expand Up @@ -341,12 +342,14 @@ def run_with_chrome_driver(
self,
test_function: Callable[[webdriver.Chrome], Optional[bool]],
target_web_view: Optional[Union[AnkiWebViewType, str]] = None,
timeout: int = 5000,
):
"""[summary]
Args:
test_function (Callable[[webdriver.Chrome], Optional[bool]]): [description]
target_web_view: Web view as identified by its title. Defaults to None.
timeout: Time to wait for task to complete until qtbot raises a TimeoutError
"""
if self._web_debugging_port is None:
raise AnkiSessionError("Web debugging interface is not active")
Expand Down Expand Up @@ -374,7 +377,7 @@ def test_wrapper() -> Optional[bool]:
return test_function(self._chrome_driver)

with self._allow_selenium_to_detect_anki():
return self.run_in_thread_and_wait(test_wrapper)
return self.run_in_thread_and_wait(test_wrapper, timeout=timeout)

def reset_chrome_driver(self):
if not self._chrome_driver:
Expand Down
28 changes: 28 additions & 0 deletions tests/test_web_debugging.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@
#
# Any modifications to this file must keep this entire header intact.

import time
from unittest.mock import Mock

import pytest
import requests
from pytestqt.qtbot import TimeoutError
from selenium import webdriver

from pytest_anki import AnkiSession, AnkiWebViewType
Expand All @@ -46,6 +49,31 @@ def test_run_in_thread(anki_session: AnkiSession):
mock_task.assert_called_once_with(*args, **kwargs)


def test_can_supply_timeout(anki_session: AnkiSession):
timeout_duration = 4
task_duration = 3

def mock_task():
time.sleep(task_duration)

start_time = time.time()
try:
anki_session.run_in_thread_and_wait(
task=mock_task, timeout=timeout_duration * 1000
)
except TimeoutError:
pytest.fail("Call unexpectedly timed out")

wait_time = time.time() - start_time

assert timeout_duration > wait_time >= task_duration

with pytest.raises(TimeoutError):
anki_session.run_in_thread_and_wait(
task=mock_task, timeout=(task_duration - 1) * 1000
)


def test_web_debugging_available_on_launch(anki_session: AnkiSession):
port = anki_session.web_debugging_port

Expand Down

0 comments on commit 994085e

Please sign in to comment.