-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #360 from lona-web-org/fscherf/fix-python311-async…
…io-wait fix python311 asyncio wait
- Loading branch information
Showing
5 changed files
with
171 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
from datetime import datetime | ||
|
||
from lona.html import NumberInput, Button, Table, HTML, Tr, Th, Td, H2 | ||
from lona import View | ||
|
||
|
||
class SleepTestView(View): | ||
def handle_request(self, request): | ||
|
||
# setup global state | ||
if 'id' in request.GET: | ||
self.view_id = request.GET['id'] | ||
|
||
else: | ||
self.view_id = 'sleep-test-view' | ||
|
||
self.server.state[self.view_id] = {} | ||
|
||
# setup html | ||
seconds = NumberInput(value=1, id='seconds') | ||
button = Button('Sleep', id='sleep') | ||
|
||
self.html = HTML( | ||
H2('View.sleep()'), | ||
|
||
Table( | ||
Tr( | ||
Th('State:'), | ||
Td(_id='state'), | ||
), | ||
Tr( | ||
Th('Started:'), | ||
Td(_id='started'), | ||
), | ||
Tr( | ||
Th('Stopped:'), | ||
Td(_id='stopped'), | ||
), | ||
), | ||
seconds, | ||
button, | ||
) | ||
|
||
self.set_state(sleeping=False, initial=True) | ||
|
||
# main loop | ||
while True: | ||
self.show(self.html) | ||
self.await_click(button) | ||
|
||
self.set_state(sleeping=True) | ||
self.show(self.html) | ||
|
||
self.sleep(seconds.value) | ||
self.set_state(sleeping=False) | ||
|
||
def set_state(self, sleeping, initial=False): | ||
with self.html.lock: | ||
state = self.html.query_selector('#state') | ||
started = self.html.query_selector('#started') | ||
stopped = self.html.query_selector('#stopped') | ||
|
||
# html | ||
if sleeping: | ||
state.set_text('Sleeping') | ||
started.set_text(str(datetime.now())) | ||
stopped.set_text('-') | ||
|
||
else: | ||
state.set_text('Waiting for start') | ||
|
||
if initial: | ||
started.set_text('-') | ||
stopped.set_text('-') | ||
|
||
else: | ||
stopped.set_text(str(datetime.now())) | ||
|
||
# server | ||
if not self.view_id: | ||
return | ||
|
||
self.server.state[self.view_id]['state'] = state.get_text() | ||
self.server.state[self.view_id]['started'] = started.get_text() | ||
self.server.state[self.view_id]['stopped'] = stopped.get_text() | ||
|
||
def on_cleanup(self) -> None: | ||
if self.view_id: | ||
self.server.state.pop(self.view_id) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
async def test_view_sleep(lona_project_context): | ||
import os | ||
|
||
from playwright.async_api import async_playwright | ||
|
||
from lona.pytest import eventually | ||
|
||
TEST_PROJECT_PATH = os.path.join(__file__, '../../test_project') | ||
|
||
context = await lona_project_context( | ||
project_root=TEST_PROJECT_PATH, | ||
settings=['settings.py'], | ||
) | ||
|
||
# FIXME: this is only necessary because get parameters seem not to work | ||
# with the current playwright setup | ||
test_id = 'sleep-test-view' | ||
|
||
async with async_playwright() as playwright: | ||
browser = await playwright.chromium.launch() | ||
browser_context = await browser.new_context() | ||
|
||
page = await browser_context.new_page() | ||
|
||
# short sleep ######################################################### | ||
await page.goto(context.make_url('/view-api/sleep')) | ||
await page.wait_for_selector('#lona h2:has-text("View.sleep()")') | ||
|
||
assert context.server.state[test_id]['state'] == 'Waiting for start' | ||
assert context.server.state[test_id]['started'] == '-' | ||
assert context.server.state[test_id]['stopped'] == '-' | ||
|
||
await page.fill('#seconds', '1') | ||
await page.click('#sleep') | ||
|
||
# wait for start | ||
for attempt in eventually(): | ||
async with attempt: | ||
assert context.server.state[test_id]['started'] != '-' | ||
assert context.server.state[test_id]['stopped'] == '-' | ||
|
||
# wait for stop | ||
for attempt in eventually(): | ||
async with attempt: | ||
assert context.server.state[test_id]['started'] != '-' | ||
assert context.server.state[test_id]['stopped'] != '-' | ||
|
||
# long, interrupted sleep ############################################# | ||
await page.goto(context.make_url('/view-api/sleep')) | ||
await page.wait_for_selector('#lona h2:has-text("View.sleep()")') | ||
|
||
assert context.server.state[test_id]['state'] == 'Waiting for start' | ||
assert context.server.state[test_id]['started'] == '-' | ||
assert context.server.state[test_id]['stopped'] == '-' | ||
|
||
await page.fill('#seconds', '300') | ||
await page.click('#sleep') | ||
|
||
# wait for start | ||
for attempt in eventually(): | ||
async with attempt: | ||
assert context.server.state[test_id]['started'] != '-' | ||
assert context.server.state[test_id]['stopped'] == '-' | ||
|
||
# close tab | ||
await page.close() | ||
|
||
for attempt in eventually(): | ||
async with attempt: | ||
assert test_id not in context.server.state |