summaryrefslogtreecommitdiff
path: root/crocoite/test_controller.py
diff options
context:
space:
mode:
authorLars-Dominik Braun <lars@6xq.net>2019-06-18 13:41:53 +0200
committerLars-Dominik Braun <lars@6xq.net>2019-06-18 13:41:53 +0200
commitb4669705fa8e581c17bbe0ca0c7cf4fadbd3deb8 (patch)
treeb64aa972023caed27ab5158e4e49aecb008a4bdf /crocoite/test_controller.py
parentc33431e6c5ccf5c0b274e2ed9c21ddf776759b67 (diff)
downloadcrocoite-b4669705fa8e581c17bbe0ca0c7cf4fadbd3deb8.tar.gz
crocoite-b4669705fa8e581c17bbe0ca0c7cf4fadbd3deb8.tar.bz2
crocoite-b4669705fa8e581c17bbe0ca0c7cf4fadbd3deb8.zip
Fix idle state tracking race condition
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.
Diffstat (limited to 'crocoite/test_controller.py')
-rw-r--r--crocoite/test_controller.py40
1 files changed, 37 insertions, 3 deletions
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)
+