Skip to content

Commit

Permalink
Executor test
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-janssen committed Jul 16, 2023
1 parent d5d5c3a commit 9856953
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pysqa/executor/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def __init__(self, cwd=None, queue_adapter=None, queue_adapter_kwargs=None):
"python -m pysqa.executor --cores "
+ str(queue_adapter_kwargs["cores"])
+ " --path "
+ str(self._cache_directory),
+ str(self._cache_directory)
)
self._queue_id = self._queue_adapter.submit_job(
working_directory=self._cache_directory,
Expand Down
56 changes: 55 additions & 1 deletion tests/test_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@
import unittest

from pysqa.executor.backend import execute_files_from_list
from pysqa.executor.executor import Executor
from pysqa.executor.helper import (
read_from_file,
deserialize,
serialize_funct,
write_to_file,
serialize_result,
set_future,
reload_previous_futures
reload_previous_futures,
get_file_name,
)
from pysqa.queueadapter import QueueAdapter


def funct_add(a, b):
Expand Down Expand Up @@ -91,3 +94,54 @@ def test_reload_previous_future(self):
self.assertEqual(len(future_dict_two), 1)
self.assertEqual(key, file_name_in.split(".in.pl")[0])
self.assertEqual(future_dict_two[key].result(), 3)


class TestExecutor(unittest.TestCase):
def setUp(self):
self.test_dir = os.path.join(os.path.abspath(os.path.dirname(__file__)), "executor_cache")
os.makedirs(self.test_dir, exist_ok=True)

def tearDown(self):
for f in os.listdir(self.test_dir):
os.remove(os.path.join(self.test_dir, f))
os.removedirs(self.test_dir)

def test_executor(self):
def execute_command(
commands,
working_directory=None,
split_output=True,
shell=False,
error_filename="pysqa.err",
):
return str(1)

queue_adapter = QueueAdapter(
directory=os.path.join(self.test_dir, "../config/slurm"),
execute_command=execute_command
)
with Executor(
cwd=self.test_dir,
queue_adapter=queue_adapter,
queue_adapter_kwargs={
"queue": "slurm",
"job_name": "test",
"cores": 1
}
) as exe:
fs = exe.submit(fn=funct_add, a=1, b=2)
funct_dict = serialize_funct(fn=funct_add, a=1, b=2)
file_name_in = get_file_name(name=list(funct_dict.keys())[0], state="in")
funct_dict = read_from_file(
file_name=os.path.join(self.test_dir, file_name_in)
)
apply_dict = deserialize(funct_dict=funct_dict)
result_dict = {
k: v["fn"].__call__(*v["args"], **v["kwargs"]) for k, v in apply_dict.items()
}
_ = write_to_file(
funct_dict=serialize_result(result_dict=result_dict),
state="out",
cache_directory=self.test_dir,
)[0]
self.assertEqual(fs.result(), 3)

0 comments on commit 9856953

Please sign in to comment.