From b4669705fa8e581c17bbe0ca0c7cf4fadbd3deb8 Mon Sep 17 00:00:00 2001 From: Lars-Dominik Braun Date: Tue, 18 Jun 2019 13:41:53 +0200 Subject: Fix idle state tracking race condition MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Closes #16. Expose SiteLoader’s page idle changes through events and move state tracking into controller event handler. Relies on tracking time instead of asyncio event, which is more reliable. --- crocoite/test_controller.py | 40 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-) (limited to 'crocoite/test_controller.py') diff --git a/crocoite/test_controller.py b/crocoite/test_controller.py index 6f92e23..fa478a1 100644 --- a/crocoite/test_controller.py +++ b/crocoite/test_controller.py @@ -26,7 +26,9 @@ from aiohttp import web import pytest from .logger import Logger -from .controller import ControllerSettings, SinglePageController, SetEntry +from .controller import ControllerSettings, SinglePageController, SetEntry, \ + IdleStateTracker +from .browser import PageIdle from .devtools import Process from .test_browser import loader @@ -63,10 +65,13 @@ window.setInterval (function () { fetch('/').then (function (e) { console.log (e # hard-coded asyncio.sleep calls in there right now. # XXX fix this before = loop.time () - await asyncio.wait_for (controller.run (), settings.timeout*2) + await asyncio.wait_for (controller.run (), timeout=settings.timeout*2) after = loop.time () - assert after-before >= settings.timeout + assert after-before >= settings.timeout, (settings.timeout*2, after-before) finally: + # give the browser some time to close before interrupting the + # connection by destroying the HTTP server + await asyncio.sleep (1) await runner.cleanup () @pytest.mark.asyncio @@ -117,3 +122,32 @@ def test_set_entry (): assert a != c assert hash (a) != hash (c) +@pytest.mark.asyncio +async def test_idle_state_tracker (): + # default is idle + loop = asyncio.get_event_loop () + idle = IdleStateTracker (loop) + assert idle._idle + + # idle change + idle.push (PageIdle (False)) + assert not idle._idle + + # nothing happens for other objects + idle.push ({}) + assert not idle._idle + + # no state change -> wait does not return + with pytest.raises (asyncio.TimeoutError): + await asyncio.wait_for (idle.wait (0.1), timeout=1) + + # wait at least timeout + delta = 0.2 + timeout = 1 + idle.push (PageIdle (True)) + assert idle._idle + start = loop.time () + await idle.wait (timeout) + end = loop.time () + assert (timeout-delta) < (end-start) < (timeout+delta) + -- cgit v1.2.3