add in 'wait for port'
import pathlib
import subprocess
import sys
+import time
+import socket
from contextlib import contextmanager
@@ -37,6 +39,7 @@ def process(*args, **kwargs):
-------
subprocess.Popen
The process being run
+
"""
proc = subprocess.Popen(*args, **kwargs)
try:
@@ -47,6 +50,38 @@ def process(*args, **kwargs):
proc.kill()
+def wait_for_port(port, host="localhost", timeout=5.0):
+ """Wait until a port starts accepting TCP connections.
+
+ Args
+ ----
+ port : int
+ Port number.
+ host : str
+ Host address on which the port should exist.
+ timeout : float
+ In seconds. How long to wait before raising errors.
+
+ Raises
+ ------
+ TimeoutError
+ The port isn't accepting connection after time specified in `timeout`.
+
+ """
+ start_time = time.perf_counter()
+ while True:
+ try:
+ with socket.create_connection((host, port), timeout=timeout):
+ break
+ except OSError as ex:
+ time.sleep(0.01)
+ if time.perf_counter() - start_time >= timeout:
+ raise TimeoutError(
+ "Waited too long for the port {} on host {} to start accepting "
+ "connections.".format(port, host)
+ ) from ex
+
+
def main():
if sys.platform == "win32":
prepend = ""
@@ -60,6 +95,8 @@ def main():
exe = built_executables[0]
with process(f"{prepend}{exe}", cwd=BUILD_DIST, shell=True):
+ wait_for_port(8501, timeout=300)
+
subprocess.check_call("yarn", cwd=CYPRESS, shell=True)
subprocess.check_call("yarn cypress run", cwd=CYPRESS, shell=True)
