Skip to content

Commit

Permalink
replace asserts with ValueErrors, add tests for empty ensemble or mis…
Browse files Browse the repository at this point in the history
…sing nworkers
  • Loading branch information
jlnav committed Jun 27, 2024
1 parent 1d51663 commit 96586d5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
8 changes: 4 additions & 4 deletions libensemble/ensemble.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,13 +303,13 @@ def __init__(
self._libE_specs = LibeSpecs(**self._libE_specs)
self._known_comms = self._libE_specs.comms

assert (
self._known_comms is not None
), "comms must be specified, either by setting parse_args=True or in libE_specs.comms"
if self._known_comms is None:
raise ValueError("comms must be specified, either by setting parse_args=True or in libE_specs.comms")

if self._known_comms == "local":
self.is_manager = True
assert self.nworkers, "nworkers must be specified if comms is 'local'"
if not self.nworkers:
raise ValueError("nworkers must be specified if comms is 'local'")

def _parse_args(self) -> (int, bool, LibeSpecs):
self.nworkers, self.is_manager, libE_specs_parsed, self.extra_args = parse_args_f()
Expand Down
25 changes: 25 additions & 0 deletions libensemble/tests/unit_tests/test_ensemble.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,30 @@ def test_ensemble_prevent_comms_overwrite():
warnings.resetwarnings()


def test_empty_ensemble():
"""Test that an empty ensemble can't be created, plus that nworkers must be specified"""
from libensemble.ensemble import Ensemble
from libensemble.specs import LibeSpecs

flag = 1
try:
Ensemble()
except ValueError:
flag = 0

assert not flag, "Empty ensemble, without comms, should not be created"

flag = 1
try:
Ensemble(
libE_specs=LibeSpecs(comms="local"),
)
except ValueError:
flag = 0

assert not flag, "'local' ensemble without nworkers should not be created"


if __name__ == "__main__":
test_ensemble_init()
test_ensemble_parse_args_false()
Expand All @@ -221,3 +245,4 @@ def test_ensemble_prevent_comms_overwrite():
test_flakey_workflow()
test_ensemble_specs_update_libE_specs()
test_ensemble_prevent_comms_overwrite()
test_empty_ensemble()

0 comments on commit 96586d5

Please sign in to comment.