Skip to content

Commit ee790d6

Browse files
diningPhilosopher64Prabhakar Kumar
authored andcommitted
Bugfix: Stop the integration and error out when web server port is already...
1 parent 1b86a5d commit ee790d6

File tree

4 files changed

+62
-7
lines changed

4 files changed

+62
-7
lines changed

jupyter_matlab_proxy/app.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,8 @@ def main():
407407

408408
logger = mwi_logger.get(init=True)
409409

410+
app = create_app()
411+
410412
logger.info("Starting MATLAB proxy-app")
411413
logger.info(
412414
f" with base_url: {os.environ[mwi_env.get_env_name_base_url()]} and "
@@ -417,8 +419,6 @@ def main():
417419
f"\n The webdesktop can be accessed on http://localhost:{os.environ[mwi_env.get_env_name_app_port()]}{os.environ[mwi_env.get_env_name_base_url()]}/index.html"
418420
)
419421

420-
app = create_app()
421-
422422
loop = asyncio.get_event_loop()
423423

424424
# Override default loggers

jupyter_matlab_proxy/settings.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,9 @@ def get(dev=False):
120120
],
121121
"create_xvfb_cmd": create_xvfb_cmd,
122122
"base_url": os.environ[mwi_env.get_env_name_base_url()],
123-
"app_port": os.environ[mwi_env.get_env_name_app_port()],
123+
"app_port": mwi_validators.validate_app_port_is_free(
124+
os.environ[mwi_env.get_env_name_app_port()]
125+
),
124126
"host_interface": os.environ.get(mwi_env.get_env_name_app_host()),
125127
"mwapikey": str(uuid.uuid4()),
126128
"matlab_protocol": "https",

jupyter_matlab_proxy/util/mwi_validators.py

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010
1111
Exceptions are thrown to signal failure.
1212
"""
13+
from . import mwi_logger
14+
from jupyter_matlab_proxy import mwi_environment_variables as mwi_env
15+
import sys
16+
17+
logger = mwi_logger.get()
1318

1419

1520
def validate_mlm_license_file(nlm_conn_str):
@@ -20,11 +25,8 @@ def validate_mlm_license_file(nlm_conn_str):
2025
"""
2126
import re
2227
import os
23-
from . import mwi_logger
2428
from .mwi_exceptions import NetworkLicensingError
2529

26-
logger = mwi_logger.get()
27-
2830
if nlm_conn_str is None:
2931
return None
3032

@@ -59,3 +61,34 @@ def validate_mlm_license_file(nlm_conn_str):
5961

6062
# Validation passed
6163
return nlm_conn_str
64+
65+
66+
def validate_app_port_is_free(port):
67+
"""Validates and returns port if its free else will error out and exit.
68+
69+
Args:
70+
port (str|int): Port number either as a string or an integer.
71+
72+
Raises:
73+
e: socket.error
74+
75+
Returns:
76+
Boolean: True if provided port is occupied else False.
77+
"""
78+
import socket, errno
79+
80+
try:
81+
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
82+
s.bind(("", int(port)))
83+
s.close()
84+
85+
# Was able to allocate port. Validation passed.
86+
return port
87+
except socket.error as e:
88+
if e.errno == errno.EADDRINUSE:
89+
logger.error(
90+
f"The port {port} is not available. Please set another value for the environment variable {mwi_env.get_env_name_app_port()}"
91+
)
92+
sys.exit(1)
93+
else:
94+
raise e

tests/util/test_mwi_validators.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"""Tests for functions in jupyter_matlab_proxy/util/mwi_validators.py
33
"""
44

5-
import pytest, os, tempfile
5+
import pytest, os, tempfile, socket
66
from jupyter_matlab_proxy.util import mwi_validators
77
from jupyter_matlab_proxy import mwi_environment_variables as mwi_env
88
from jupyter_matlab_proxy.util.mwi_exceptions import NetworkLicensingError
@@ -50,3 +50,23 @@ def test_get_with_environment_variables(monkeypatch):
5050
assert conn_str == str(path)
5151
finally:
5252
os.remove(path)
53+
54+
55+
def test_validate_app_port_is_free_false():
56+
"""Test to validate if supplied app port is free"""
57+
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
58+
s.bind(("", 0))
59+
port = s.getsockname()[1]
60+
with pytest.raises(SystemExit) as e:
61+
mwi_validators.validate_app_port_is_free(port)
62+
assert e.value.code == 1
63+
s.close()
64+
65+
66+
def test_validate_app_port_is_free_true():
67+
"""Test to validate if supplied app port is free"""
68+
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
69+
s.bind(("", 0))
70+
port = s.getsockname()[1]
71+
s.close()
72+
assert mwi_validators.validate_app_port_is_free(port) == port

0 commit comments

Comments
 (0)