From 0d18fb122a2b5625849043a3ffb2ff7b6a8ba606 Mon Sep 17 00:00:00 2001 From: Arne Tarara Date: Thu, 5 Sep 2024 21:08:16 +0200 Subject: [PATCH] Page.content() is now with timeout --- energy-tests/nextcloud_create_doc_and_share.py | 10 ++++++++++ energy-tests/nextcloud_create_event.py | 13 +++++++++++-- energy-tests/nextcloud_create_user.py | 10 ++++++++++ energy-tests/nextcloud_install.py | 13 +++++++++++-- energy-tests/nextcloud_talk.py | 10 ++++++++++ 5 files changed, 52 insertions(+), 4 deletions(-) diff --git a/energy-tests/nextcloud_create_doc_and_share.py b/energy-tests/nextcloud_create_doc_and_share.py index e736df92..7fcdc476 100644 --- a/energy-tests/nextcloud_create_doc_and_share.py +++ b/energy-tests/nextcloud_create_doc_and_share.py @@ -1,9 +1,13 @@ import contextlib import sys from time import sleep, time_ns +import signal from playwright.sync_api import Playwright, sync_playwright, TimeoutError +def timeout_handler(signum, frame): + raise TimeoutError("Page.content() timed out") + def log_note(message: str) -> None: timestamp = str(time_ns())[:16] print(f"{timestamp} {message}") @@ -52,7 +56,13 @@ def run(playwright: Playwright, browser_name: str) -> None: except Exception as e: if hasattr(e, 'message'): # only Playwright error class has this member log_note(f"Exception occurred: {e.message}") + + # set a timeout. Since the call to page.content() is blocking we need to defer it to the OS + signal.signal(signal.SIGALRM, timeout_handler) + signal.alarm(5) log_note(f"Page content was: {page.content()}") + signal.alarm(0) # remove timeout signal + raise e diff --git a/energy-tests/nextcloud_create_event.py b/energy-tests/nextcloud_create_event.py index d15aef95..4b263d20 100644 --- a/energy-tests/nextcloud_create_event.py +++ b/energy-tests/nextcloud_create_event.py @@ -1,9 +1,13 @@ import contextlib import sys from time import time_ns, sleep +import signal from playwright.sync_api import Playwright, sync_playwright, expect, TimeoutError +def timeout_handler(signum, frame): + raise TimeoutError("Page.content() timed out") + def log_note(message: str) -> None: timestamp = str(time_ns())[:16] print(f"{timestamp} {message}") @@ -53,8 +57,13 @@ def run(playwright: Playwright, browser_name: str) -> None: except Exception as e: if hasattr(e, 'message'): # only Playwright error class has this member log_note(f"Exception occurred: {e.message}") - log_note("Page content was:") - log_note(page.content()) + + # set a timeout. Since the call to page.content() is blocking we need to defer it to the OS + signal.signal(signal.SIGALRM, timeout_handler) + signal.alarm(5) + log_note(f"Page content was: {page.content()}") + signal.alarm(0) # remove timeout signal + raise e # --------------------- diff --git a/energy-tests/nextcloud_create_user.py b/energy-tests/nextcloud_create_user.py index c58752ea..c8f2bdf8 100644 --- a/energy-tests/nextcloud_create_user.py +++ b/energy-tests/nextcloud_create_user.py @@ -1,9 +1,13 @@ import sys import contextlib from time import time_ns, sleep +import signal from playwright.sync_api import Playwright, sync_playwright, TimeoutError +def timeout_handler(signum, frame): + raise TimeoutError("Page.content() timed out") + def log_note(message: str) -> None: timestamp = str(time_ns())[:16] print(f"{timestamp} {message}") @@ -52,7 +56,13 @@ def create_user(playwright: Playwright, browser_name: str, username: str, passwo except Exception as e: if hasattr(e, 'message'): # only Playwright error class has this member log_note(f"Exception occurred: {e.message}") + + # set a timeout. Since the call to page.content() is blocking we need to defer it to the OS + signal.signal(signal.SIGALRM, timeout_handler) + signal.alarm(5) log_note(f"Page content was: {page.content()}") + signal.alarm(0) # remove timeout signal + raise e diff --git a/energy-tests/nextcloud_install.py b/energy-tests/nextcloud_install.py index 910ac914..7d93a4d1 100644 --- a/energy-tests/nextcloud_install.py +++ b/energy-tests/nextcloud_install.py @@ -1,7 +1,10 @@ import sys +import signal from time import time_ns +from playwright.sync_api import sync_playwright, TimeoutError -from playwright.sync_api import sync_playwright +def timeout_handler(signum, frame): + raise TimeoutError("Page.content() timed out") def log_note(message: str) -> None: timestamp = str(time_ns())[:16] @@ -19,8 +22,8 @@ def main(browser_name: str = "chromium"): context = browser.new_context() page = context.new_page() try: - page.goto('http://nc/') page.set_default_timeout(240_000) # 240 seconds (timeout is in milliseconds) + page.goto('http://nc/') # 1. Create User log_note("Create admin user") @@ -42,7 +45,13 @@ def main(browser_name: str = "chromium"): except Exception as e: if hasattr(e, 'message'): # only Playwright error class has this member log_note(f"Exception occurred: {e.message}") + + # set a timeout. Since the call to page.content() is blocking we need to defer it to the OS + signal.signal(signal.SIGALRM, timeout_handler) + signal.alarm(5) log_note(f"Page content was: {page.content()}") + signal.alarm(0) # remove timeout signal + raise e diff --git a/energy-tests/nextcloud_talk.py b/energy-tests/nextcloud_talk.py index e025e1a5..2f97cfed 100644 --- a/energy-tests/nextcloud_talk.py +++ b/energy-tests/nextcloud_talk.py @@ -2,10 +2,14 @@ import contextlib import random import string import sys +import signal from time import sleep, time_ns from playwright.sync_api import Playwright, sync_playwright, expect, TimeoutError +def timeout_handler(signum, frame): + raise TimeoutError("Page.content() timed out") + def log_note(message: str) -> None: timestamp = str(time_ns())[:16] print(f"{timestamp} {message}") @@ -84,7 +88,13 @@ def create_conversation(playwright: Playwright, browser_name: str) -> str: except Exception as e: if hasattr(e, 'message'): # only Playwright error class has this member log_note(f"Exception occurred: {e.message}") + + # set a timeout. Since the call to page.content() is blocking we need to defer it to the OS + signal.signal(signal.SIGALRM, timeout_handler) + signal.alarm(5) log_note(f"Page content was: {page.content()}") + signal.alarm(0) # remove timeout signal + raise e def talk(playwright: Playwright, url: str, browser_name: str) -> None: