Skip to content

Commit

Permalink
Fix server handling in tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
lohedges committed Jan 20, 2025
1 parent 518565e commit 82a007d
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 29 deletions.
44 changes: 23 additions & 21 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
import pytest
import psutil
import subprocess


@pytest.fixture(autouse=True)
def wrapper():
"""
A wrapper function to stop the EMLE server after each test.
def start_server(cwd, env=None):
"""
Start the EMLE server using the environment variables.
yield
Parameters
----------
# Kill the EMLE server.
kill_server()
cwd : str
The current working directory.
env : dict
The environment variables.
def kill_server():
"""
Helper function to kill the EMLE server.
Returns
-------
process : subprocess.Popen
The EMLE server process object.
"""

# Kill the EMLE server. We do this manually rather than using emle-stop
# because there is sometimes a delay in the termination of the server,
# which causes the next test to fail. This only seems to happen when
# testing during CI.
for conn in psutil.net_connections(kind="inet"):
if conn.laddr.port == 10000:
process = psutil.Process(conn.pid)
process.terminate()
break
process = subprocess.Popen(
["emle-server"],
cwd=cwd,
env=env,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)

return process
22 changes: 14 additions & 8 deletions tests/test_delta_learning.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,18 @@
import shutil
import subprocess
import tempfile
import time
import yaml

from conftest import start_server

def test_delta_learning(backend="torchani,xtb"):

def test_delta_learning():
"""
Make sure that the server can run using two backends for the in vacuo
vacuo calculation. The first is the "reference" backend, the second
applies delta learning corrections.
"""

from conftest import kill_server

with tempfile.TemporaryDirectory() as tmpdir:
# Copy files to temporary directory.
shutil.copyfile("tests/input/adp.parm7", tmpdir + "/adp.parm7")
Expand All @@ -30,6 +29,9 @@ def test_delta_learning(backend="torchani,xtb"):
env["EMLE_BACKEND"] = "torchani,xtb"
env["EMLE_ENERGY_FREQUENCY"] = "1"

# Start the server.
server = start_server(tmpdir, env=env)

# Create the sander command.
command = "sander -O -i emle_sp.in -p adp.parm7 -c adp.rst7 -o emle.out"

Expand Down Expand Up @@ -60,10 +62,8 @@ def test_delta_learning(backend="torchani,xtb"):
+ float(lines[-1].strip())
)

# Kill the server. (Try twice, since there is sometimes a delay.)
kill_server()
time.sleep(1)
kill_server()
# Stop the server.
server.terminate()

# Now swap the order of the backends.

Expand All @@ -80,6 +80,9 @@ def test_delta_learning(backend="torchani,xtb"):
env["EMLE_BACKEND"] = "xtb,torchani"
env["EMLE_ENERGY_FREQUENCY"] = "1"

# Start the server.
server = start_server(tmpdir, env=env)

# Create the sander command.
command = "sander -O -i emle_sp.in -p adp.parm7 -c adp.rst7 -o emle.out"

Expand Down Expand Up @@ -109,5 +112,8 @@ def test_delta_learning(backend="torchani,xtb"):
+ float(lines[-1].strip())
)

# Stop the server.
server.terminate()

# Make sure that the results are the same.
assert math.isclose(result_ab, result_ba, rel_tol=1e-6)
14 changes: 14 additions & 0 deletions tests/test_external.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import subprocess
import tempfile

from conftest import start_server


def test_external_local_directory():
"""
Expand All @@ -25,6 +27,9 @@ def test_external_local_directory():
env["EMLE_EXTERNAL_BACKEND"] = "external.run_external"
env["EMLE_ENERGY_FREQUENCY"] = "1"

# Start the server.
server = start_server(tmpdir, env=env)

# Create the sander command.
command = "sander -O -i emle_sp.in -p adp.parm7 -c adp.rst7 -o emle.out"

Expand All @@ -42,6 +47,9 @@ def test_external_local_directory():
# Make sure that an energy file is written.
assert os.path.isfile(tmpdir + "/emle_energy.txt")

# Stop the server.
server.terminate()


def test_external_plugin_directory():
"""
Expand All @@ -63,6 +71,9 @@ def test_external_plugin_directory():
env["EMLE_PLUGIN_PATH"] = os.getcwd() + "/tests/input"
env["EMLE_ENERGY_FREQUENCY"] = "1"

# Start the server.
server = start_server(tmpdir, env=env)

# Create the sander command.
command = "sander -O -i emle_sp.in -p adp.parm7 -c adp.rst7 -o emle.out"

Expand All @@ -79,3 +90,6 @@ def test_external_plugin_directory():

# Make sure that an energy file is written.
assert os.path.isfile(tmpdir + "/emle_energy.txt")

# Stop the server.
server.terminate()
26 changes: 26 additions & 0 deletions tests/test_interpolate.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import subprocess
import tempfile

from conftest import start_server


def parse_mdinfo(mdinfo_file):
"""
Expand All @@ -30,6 +32,9 @@ def test_interpolate():
shutil.copyfile("tests/input/adp.rst7", tmpdir + "/adp.rst7")
shutil.copyfile("tests/input/mm_sp.in", tmpdir + "/mm_sp.in")

# Start the server.
server = start_server(tmpdir)

# Create the sander command.
command = "sander -O -i mm_sp.in -p adp.parm7 -c adp.rst7"

Expand All @@ -50,6 +55,9 @@ def test_interpolate():

assert math.isclose(nrg_ref, nrg_mm, rel_tol=1e-5)

# Stop the server.
server.terminate()

# Now perform and interpolated EMLE simulation at lambda=0.
with tempfile.TemporaryDirectory() as tmpdir:
# Copy files to temporary directory.
Expand All @@ -74,6 +82,9 @@ def test_interpolate():
env["EMLE_QM_INDICES"] = "adp_qm_indices.txt"
env["EMLE_ENERGY_FREQUENCY"] = "1"

# Start the server.
server = start_server(tmpdir, env=env)

# Create the sander command.
command = "sander -O -i emle_sp.in -p adp.parm7 -c adp.rst7 -o emle.out"

Expand All @@ -92,6 +103,9 @@ def test_interpolate():

assert math.isclose(nrg_ref, nrg_emle, rel_tol=1e-4)

# Stop the server.
server.terminate()


def test_interpolate_steps():
"""
Expand Down Expand Up @@ -123,6 +137,9 @@ def test_interpolate_steps():
env["EMLE_QM_INDICES"] = "adp_qm_indices.txt"
env["EMLE_ENERGY_FREQUENCY"] = "1"

# Start the server.
server = start_server(tmpdir, env=env)

# Create the sander command.
command = "sander -O -i emle_prod.in -p adp.parm7 -c adp.rst7 -o emle.out"

Expand All @@ -147,6 +164,9 @@ def test_interpolate_steps():
nrg_interp = lam * data[4] + (1 - lam) * data[3]
assert math.isclose(nrg_lambda, nrg_interp, rel_tol=1e-5)

# Stop the server.
server.terminate()


def test_interpolate_steps_config():
"""
Expand All @@ -168,6 +188,9 @@ def test_interpolate_steps_config():
# Set environment variables.
env["EMLE_CONFIG"] = "config.yaml"

# Start the server.
server = start_server(tmpdir, env=env)

# Create the sander command.
command = "sander -O -i emle_prod.in -p adp.parm7 -c adp.rst7 -o emle.out"

Expand All @@ -191,3 +214,6 @@ def test_interpolate_steps_config():
nrg_lambda = data[2]
nrg_interp = lam * data[4] + (1 - lam) * data[3]
assert math.isclose(nrg_lambda, nrg_interp, rel_tol=1e-5)

# Stop the server.
server.terminate()
8 changes: 8 additions & 0 deletions tests/test_qm_xyz.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import subprocess
import tempfile

from conftest import start_server


def test_qm_xyz():
"""
Expand All @@ -22,6 +24,9 @@ def test_qm_xyz():
# Set environment variables.
env["EMLE_QM_XYZ_FREQUENCY"] = "2"

# Start the server.
server = start_server(tmpdir, env=env)

# Create the sander command.
command = "sander -O -i emle_prod.in -p adp.parm7 -c adp.rst7 -o emle.out"

Expand All @@ -46,3 +51,6 @@ def test_qm_xyz():
if line.startswith("22"):
num_frames += 1
assert num_frames == 11

# Stop the server.
server.terminate()

0 comments on commit 82a007d

Please sign in to comment.