Skip to content

Commit

Permalink
Merge pull request OpenMDAO#3224 from hschilling/I3223-move-get_free_…
Browse files Browse the repository at this point in the history
…port

Moved get_free_port function to utils directory file
  • Loading branch information
swryan authored May 6, 2024
2 parents d8ef4e9 + 3d83b1f commit e280ee4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
19 changes: 2 additions & 17 deletions openmdao/docs/openmdao_book/tests/jupyter_gui_test.py
Original file line number Diff line number Diff line change
@@ -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")
Expand Down
17 changes: 17 additions & 0 deletions openmdao/utils/gui_testing_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Define utils for use in testing GUIs with Playwright."""

import contextlib
import socket
import unittest


Expand Down Expand Up @@ -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

0 comments on commit e280ee4

Please sign in to comment.