From 3d83b1f2bd02e4ed68b002c16faa7c63809bf1fe Mon Sep 17 00:00:00 2001 From: Herb Schilling Date: Mon, 6 May 2024 15:09:16 -0400 Subject: [PATCH] Moved get_free_port function to utils directory to make it available other OpenMDAO needs and also OpenMDAO related projects like Aviary --- .../openmdao_book/tests/jupyter_gui_test.py | 19 ++----------------- openmdao/utils/gui_testing_utils.py | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/openmdao/docs/openmdao_book/tests/jupyter_gui_test.py b/openmdao/docs/openmdao_book/tests/jupyter_gui_test.py index ebe1831807..2c4a0b5cd3 100644 --- a/openmdao/docs/openmdao_book/tests/jupyter_gui_test.py +++ b/openmdao/docs/openmdao_book/tests/jupyter_gui_test.py @@ -1,39 +1,24 @@ """Test Jupyter doc GUI mods specific to OpenMDAO using Playwright.""" import asyncio from aiounittest import async_test -import contextlib import http.server import os import pathlib -import socket import sys import threading import unittest + from playwright.async_api import async_playwright if 'win32' in sys.platform: # Windows specific event-loop policy & cmd asyncio.set_event_loop_policy(asyncio.WindowsProactorEventLoopPolicy()) -from openmdao.utils.gui_testing_utils import _GuiTestCase +from openmdao.utils.gui_testing_utils import _GuiTestCase, get_free_port HEADLESS = True # Set to False if you want to see the browser -def get_free_port(): - """ - Get a free port. - - Returns - ------- - port : int - a free port - """ - with contextlib.closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as _socket: - _socket.bind(('', 0)) - _, port = _socket.getsockname() - return port - # Only can test if the docs have been built @unittest.skipUnless(pathlib.Path(__file__).parent.parent.joinpath("_build").exists(), "Cannot test without docs being built") diff --git a/openmdao/utils/gui_testing_utils.py b/openmdao/utils/gui_testing_utils.py index f8000b672a..982e72fc62 100644 --- a/openmdao/utils/gui_testing_utils.py +++ b/openmdao/utils/gui_testing_utils.py @@ -1,5 +1,7 @@ """Define utils for use in testing GUIs with Playwright.""" +import contextlib +import socket import unittest @@ -104,3 +106,18 @@ async def get_handle(self, selector): selector + "' in the N2 diagram.") return handle + + +def get_free_port(): + """ + Get a free port. + + Returns + ------- + int + A free port. + """ + with contextlib.closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as _socket: + _socket.bind(('', 0)) + _, port = _socket.getsockname() + return port