Skip to content

Commit

Permalink
Generalized regular expression parsing out server port.
Browse files Browse the repository at this point in the history
This should fix issue #72, where the server would not start when
Comsol was installed with the interface language set to Chinese.

Parsing is now handled by a separate function so that it can be
unit-tested. Currently, English (the default), German (issue #24),
and Chinese (issue #72) output from the server process is being
run against the regular expression. It is expected to work for
all languages, effectively just looking for a four- or five-digit
number in the line that begins with "COMSOL".
  • Loading branch information
john-hen committed Feb 28, 2022
1 parent ea20d30 commit 2038bd1
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 7 deletions.
20 changes: 17 additions & 3 deletions mph/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
########################################
# Server #
########################################

class Server:
"""
Manages a Comsol server process.
Expand Down Expand Up @@ -110,9 +111,8 @@ def __init__(self, cores=None, version=None, port=None,
line = process.stdout.readline().strip()
if line:
lines.append(line)
match = regex(r'(?i)^Comsol.+?server.+?(\d+)$', line.strip())
if match:
port = int(match.group(1))
port = parse_port(line)
if port:
break
if now() - t0 > timeout:
error = 'Sever failed to start within time-out period.'
Expand Down Expand Up @@ -167,3 +167,17 @@ def stop(self, timeout=10):
log.warning('Server did not shut down within time-out period.')
log.info('Trying to forcefully terminate server process.')
self.process.kill()


########################################
# Parsing #
########################################

def parse_port(line):
"""Parses out the port number from a line of server output."""
match = regex(r'^COMSOL.* \(.*\) .*?(\d{4,5}).*$', line)
if match:
port = int(match.group(1))
return port
else:
return None
22 changes: 22 additions & 0 deletions tests/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,27 @@ def test_stop():
server.stop()


def test_parse_port():
english = ('COMSOL Multiphysics server 5.6 (Build: 401) started '
'listening on port 2036')
german = ('COMSOL Multiphysics server 5.4 (Build-Version: 388) '
'startete Abhören an Port 2036')
chinese = ('COMSOL Multiphysics server 5.6 (开发版本: 341) '
'开始在端口 2036 上监听')
assert mph.server.parse_port(english) == 2036
assert mph.server.parse_port(german) == 2036
assert mph.server.parse_port(chinese) == 2036
english = ('COMSOL Multiphysics server 5.6 (Build: 401) started '
'listening on port 12345')
german = ('COMSOL Multiphysics server 5.4 (Build-Version: 388) '
'startete Abhören an Port 12345')
chinese = ('COMSOL Multiphysics server 5.6 (开发版本: 341) '
'开始在端口 12345 上监听')
assert mph.server.parse_port(english) == 12345
assert mph.server.parse_port(german) == 12345
assert mph.server.parse_port(chinese) == 12345


########################################
# Main #
########################################
Expand All @@ -55,5 +76,6 @@ def test_stop():
test_repr()
test_running()
test_stop()
test_parse_port()
finally:
teardown_module()
7 changes: 3 additions & 4 deletions tests/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@
########################################

def test_start():
with logging_disabled():
with raises(ValueError):
mph.option('session', 'invalid')
mph.start()
with logging_disabled(), raises(ValueError):
mph.option('session', 'invalid')
mph.start()
mph.option('session', 'client-server')
client = mph.start(cores=1)
assert client.java is not None
Expand Down

0 comments on commit 2038bd1

Please sign in to comment.