From 1cf5a7da33dd49c21d7630017526ce05dac21f65 Mon Sep 17 00:00:00 2001 From: German <28149841+germa89@users.noreply.github.com> Date: Tue, 9 Jul 2024 14:30:34 +0200 Subject: [PATCH 1/5] test: moving tests to a class and adding delete method. --- tests/test_pool.py | 1332 ++++++++++++++++++++++---------------------- 1 file changed, 669 insertions(+), 663 deletions(-) diff --git a/tests/test_pool.py b/tests/test_pool.py index 2174c91985..1b713d2fca 100644 --- a/tests/test_pool.py +++ b/tests/test_pool.py @@ -66,704 +66,710 @@ NPROC = 1 -@pytest.fixture(scope="module") -def pool(tmpdir_factory): - run_path = str(tmpdir_factory.mktemp("ansys_pool")) +class TestMapdlPool: + + def __del__(self): + # making sure we are deleting everything + for each_mapdl in self.pool._instances: + each_mapdl.exit(force=True) + + @pytest.fixture(scope="class") + def pool(self, tmpdir_factory): + run_path = str(tmpdir_factory.mktemp("ansys_pool")) + + port = os.environ.get("PYMAPDL_PORT", 50056) + + if ON_LOCAL: + + mapdl_pool = MapdlPool( + 2, + license_server_check=False, + run_location=run_path, + port=port, + start_timeout=30, + exec_file=EXEC_FILE, + additional_switches=QUICK_LAUNCH_SWITCHES, + nproc=NPROC, + wait=True, # make sure that the pool is ready before testing + ) + else: + port2 = os.environ.get("PYMAPDL_PORT2", 50057) + + mapdl_pool = MapdlPool( + 2, + license_server_check=False, + start_instance=False, + port=[port, port2], + wait=True, + ) + + self.pool = mapdl_pool + yield mapdl_pool + + ########################################################################## + # test exit + mapdl_pool.exit() + + timeout = time.time() + TWAIT + + while len(mapdl_pool) != 0: + time.sleep(0.1) + if time.time() > timeout: + raise TimeoutError(f"Failed to kill instance in {TWAIT} seconds") + + assert len(mapdl_pool) == 0 + + # check it's been cleaned up + if mapdl_pool[0] is not None: + pth = mapdl_pool[0].directory + if mapdl_pool._spawn_kwargs["remove_temp_files"]: + assert not list(Path(pth).rglob("*.page*")) + + @skip_requires_194 + def test_invalid_exec(self): + with pytest.raises(VersionError): + MapdlPool( + 4, + nproc=NPROC, + exec_file="/usr/ansys_inc/v194/ansys/bin/mapdl", + additional_switches=QUICK_LAUNCH_SWITCHES, + ) + + # @pytest.mark.xfail(strict=False, reason="Flaky test. See #2435") + def test_heal(self, pool): + pool_sz = len(pool) + pool_names = pool._names # copy pool names + + # Killing one instance + pool[0].exit() + + time.sleep(1) # wait for shutdown + timeout = time.time() + TWAIT + while len(pool) < pool_sz: + time.sleep(0.1) + if time.time() > timeout: + raise TimeoutError(f"Failed to restart instance in {TWAIT} seconds") + + assert pool._names == pool_names + assert len(pool) == pool_sz + pool._verify_unique_ports() + + @skip_if_ignore_pool + def test_simple_map(self, pool): + pool_sz = len(pool) + _ = pool.map(lambda mapdl: mapdl.prep7()) + assert len(pool) == pool_sz + + @skip_if_ignore_pool + @requires("local") + def test_map_timeout(self, pool): + pool_sz = len(pool) + + def func(mapdl, tsleep): + mapdl.clear() + mapdl.prep7() + time.sleep(tsleep) + mapdl.post1() + return tsleep + + timeout = 2 + times = np.array([0, 1, 3, 4]) + output = pool.map(func, times, timeout=timeout, wait=True) + + assert len(output) == (times < timeout).sum() + + # the timeout option kills the MAPDL instance when we reach the timeout. + # Let's wait for the pool to heal before continuing + timeout = time.time() + TWAIT + while len(pool) < pool_sz: + time.sleep(0.1) + if time.time() > timeout: + raise TimeoutError(f"Failed to restart instance in {TWAIT} seconds") + + assert len(pool) == pool_sz + + @skip_if_ignore_pool + def test_simple(self, pool): + pool_sz = len(pool) + + def func(mapdl): + mapdl.clear() + + outs = pool.map(func) + assert len(outs) == len(pool) + assert len(pool) == pool_sz + + # fails intermittently + @skip_if_ignore_pool + def test_batch(self, pool): + input_files = [examples.vmfiles["vm%d" % i] for i in range(1, len(pool) + 3)] + outputs = pool.run_batch(input_files) + assert len(outputs) == len(input_files) + + # fails intermittently + @skip_if_ignore_pool + def test_map(self, pool): + completed_indices = [] + + def func(mapdl, input_file, index): + # input_file, index = args + print(len(pool)) + mapdl.clear() + output = mapdl.input(input_file) + completed_indices.append(index) + return mapdl.parameters.routine + + inputs = [(examples.vmfiles["vm%d" % i], i) for i in range(1, len(pool) + 1)] + outputs = pool.map(func, inputs, wait=True) + + assert len(outputs) == len(inputs) + + @skip_if_ignore_pool + @pytest.mark.skipif( + not START_INSTANCE, reason="This test requires the pool to be local" + ) + def test_abort(self, pool, tmpdir): + pool_sz = len(pool) # initial pool size - port = os.environ.get("PYMAPDL_PORT", 50056) + old_paths = [mapdl.directory for mapdl in pool] - if ON_LOCAL: + tmp_file = str(tmpdir.join("woa.inp")) + with open(tmp_file, "w") as f: + f.write("EXIT") - mapdl_pool = MapdlPool( - 2, - license_server_check=False, - run_location=run_path, - port=port, - start_timeout=30, - exec_file=EXEC_FILE, - additional_switches=QUICK_LAUNCH_SWITCHES, - nproc=NPROC, - wait=True, # make sure that the pool is ready before testing - ) - else: - port2 = os.environ.get("PYMAPDL_PORT2", 50057) + input_files = [examples.vmfiles["vm%d" % i] for i in range(1, 11)] + input_files += [tmp_file] - mapdl_pool = MapdlPool( - 2, - license_server_check=False, - start_instance=False, - port=[port, port2], - wait=True, - ) - - yield mapdl_pool + outputs = pool.run_batch(input_files) + assert len(outputs) == len(input_files) - ########################################################################## - # test exit - mapdl_pool.exit() + # ensure failed instance restarts + timeout = time.time() + TWAIT + while len(pool) < pool_sz: + time.sleep(0.1) + if time.time() > timeout: + raise TimeoutError(f"Failed to restart instance in {TWAIT} seconds") - timeout = time.time() + TWAIT + assert len(pool) == pool_sz - while len(mapdl_pool) != 0: - time.sleep(0.1) - if time.time() > timeout: - raise TimeoutError(f"Failed to kill instance in {TWAIT} seconds") + # verify the temporary directory has been cleaned up for one of the instances + for path in old_paths: + path_deleted = os.path.isdir(path) + if path_deleted: + break - assert len(mapdl_pool) == 0 + assert path_deleted - # check it's been cleaned up - if mapdl_pool[0] is not None: - pth = mapdl_pool[0].directory - if mapdl_pool._spawn_kwargs["remove_temp_files"]: - assert not list(Path(pth).rglob("*.page*")) + @skip_if_ignore_pool + def test_directory_names_default(self, pool): + dirs_path_pool = os.listdir(pool._root_dir) + for i, _ in enumerate(pool._instances): + assert pool._names(i) in dirs_path_pool + assert f"Instance_{i}" in dirs_path_pool - -@skip_requires_194 -def test_invalid_exec(): - with pytest.raises(VersionError): - MapdlPool( - 4, + @requires("local") + @skip_if_ignore_pool + def test_directory_names_custom_string(self, tmpdir): + pool = MapdlPool( + 2, + exec_file=EXEC_FILE, + run_location=tmpdir, nproc=NPROC, - exec_file="/usr/ansys_inc/v194/ansys/bin/mapdl", + names="my_instance", + port=50056, additional_switches=QUICK_LAUNCH_SWITCHES, ) + dirs_path_pool = os.listdir(pool._root_dir) + assert "my_instance_0" in dirs_path_pool + assert "my_instance_1" in dirs_path_pool -# @pytest.mark.xfail(strict=False, reason="Flaky test. See #2435") -def test_heal(pool): - pool_sz = len(pool) - pool_names = pool._names # copy pool names - - # Killing one instance - pool[0].exit() - - time.sleep(1) # wait for shutdown - timeout = time.time() + TWAIT - while len(pool) < pool_sz: - time.sleep(0.1) - if time.time() > timeout: - raise TimeoutError(f"Failed to restart instance in {TWAIT} seconds") - - assert pool._names == pool_names - assert len(pool) == pool_sz - pool._verify_unique_ports() - - -@skip_if_ignore_pool -def test_simple_map(pool): - pool_sz = len(pool) - _ = pool.map(lambda mapdl: mapdl.prep7()) - assert len(pool) == pool_sz - - -@skip_if_ignore_pool -@requires("local") -def test_map_timeout(pool): - pool_sz = len(pool) - - def func(mapdl, tsleep): - mapdl.clear() - mapdl.prep7() - time.sleep(tsleep) - mapdl.post1() - return tsleep - - timeout = 2 - times = np.array([0, 1, 3, 4]) - output = pool.map(func, times, timeout=timeout, wait=True) - - assert len(output) == (times < timeout).sum() - - # the timeout option kills the MAPDL instance when we reach the timeout. - # Let's wait for the pool to heal before continuing - timeout = time.time() + TWAIT - while len(pool) < pool_sz: - time.sleep(0.1) - if time.time() > timeout: - raise TimeoutError(f"Failed to restart instance in {TWAIT} seconds") - - assert len(pool) == pool_sz + pool.exit(block=True) + @requires("local") + @skip_if_ignore_pool + def test_directory_names_function(self, tmpdir): + def myfun(i): + if i == 0: + return "instance_zero" + elif i == 1: + return "instance_one" + else: + return "Other_instance" -@skip_if_ignore_pool -def test_simple(pool): - pool_sz = len(pool) - - def func(mapdl): - mapdl.clear() - - outs = pool.map(func) - assert len(outs) == len(pool) - assert len(pool) == pool_sz - - -# fails intermittently -@skip_if_ignore_pool -def test_batch(pool): - input_files = [examples.vmfiles["vm%d" % i] for i in range(1, len(pool) + 3)] - outputs = pool.run_batch(input_files) - assert len(outputs) == len(input_files) - - -# fails intermittently -@skip_if_ignore_pool -def test_map(pool): - completed_indices = [] - - def func(mapdl, input_file, index): - # input_file, index = args - print(len(pool)) - mapdl.clear() - output = mapdl.input(input_file) - completed_indices.append(index) - return mapdl.parameters.routine - - inputs = [(examples.vmfiles["vm%d" % i], i) for i in range(1, len(pool) + 1)] - outputs = pool.map(func, inputs, wait=True) - - assert len(outputs) == len(inputs) - - -@skip_if_ignore_pool -@pytest.mark.skipif( - not START_INSTANCE, reason="This test requires the pool to be local" -) -def test_abort(pool, tmpdir): - pool_sz = len(pool) # initial pool size - - old_paths = [mapdl.directory for mapdl in pool] - - tmp_file = str(tmpdir.join("woa.inp")) - with open(tmp_file, "w") as f: - f.write("EXIT") - - input_files = [examples.vmfiles["vm%d" % i] for i in range(1, 11)] - input_files += [tmp_file] - - outputs = pool.run_batch(input_files) - assert len(outputs) == len(input_files) - - # ensure failed instance restarts - timeout = time.time() + TWAIT - while len(pool) < pool_sz: - time.sleep(0.1) - if time.time() > timeout: - raise TimeoutError(f"Failed to restart instance in {TWAIT} seconds") - - assert len(pool) == pool_sz - - # verify the temporary directory has been cleaned up for one of the instances - for path in old_paths: - path_deleted = os.path.isdir(path) - if path_deleted: - break - - assert path_deleted - - -@skip_if_ignore_pool -def test_directory_names_default(pool): - dirs_path_pool = os.listdir(pool._root_dir) - for i, _ in enumerate(pool._instances): - assert pool._names(i) in dirs_path_pool - assert f"Instance_{i}" in dirs_path_pool - - -@requires("local") -@skip_if_ignore_pool -def test_directory_names_custom_string(tmpdir): - pool = MapdlPool( - 2, - exec_file=EXEC_FILE, - run_location=tmpdir, - nproc=NPROC, - names="my_instance", - port=50056, - additional_switches=QUICK_LAUNCH_SWITCHES, - ) - - dirs_path_pool = os.listdir(pool._root_dir) - assert "my_instance_0" in dirs_path_pool - assert "my_instance_1" in dirs_path_pool - - pool.exit(block=True) - - -@requires("local") -@skip_if_ignore_pool -def test_directory_names_function(tmpdir): - def myfun(i): - if i == 0: - return "instance_zero" - elif i == 1: - return "instance_one" - else: - return "Other_instance" - - pool = MapdlPool( - 3, - exec_file=EXEC_FILE, - nproc=NPROC, - names=myfun, - run_location=tmpdir, - additional_switches=QUICK_LAUNCH_SWITCHES, - ) + pool = MapdlPool( + 3, + exec_file=EXEC_FILE, + nproc=NPROC, + names=myfun, + run_location=tmpdir, + additional_switches=QUICK_LAUNCH_SWITCHES, + ) - dirs_path_pool = os.listdir(pool._root_dir) - assert "instance_zero" in dirs_path_pool - assert "instance_one" in dirs_path_pool - assert "Other_instance" in dirs_path_pool + dirs_path_pool = os.listdir(pool._root_dir) + assert "instance_zero" in dirs_path_pool + assert "instance_one" in dirs_path_pool + assert "Other_instance" in dirs_path_pool - pool.exit(block=True) + pool.exit(block=True) + def test_num_instances(self): + with pytest.raises(ValueError, match="least 1 instance"): + pool = MapdlPool( + 0, + exec_file=EXEC_FILE, + nproc=NPROC, + additional_switches=QUICK_LAUNCH_SWITCHES, + ) -def test_num_instances(): - with pytest.raises(ValueError, match="least 1 instance"): + @skip_if_ignore_pool + def test_only_one_instance(self): pool = MapdlPool( - 0, + 1, exec_file=EXEC_FILE, nproc=NPROC, additional_switches=QUICK_LAUNCH_SWITCHES, ) - - -@skip_if_ignore_pool -def test_only_one_instance(): - pool = MapdlPool( - 1, - exec_file=EXEC_FILE, - nproc=NPROC, - additional_switches=QUICK_LAUNCH_SWITCHES, - ) - pool_sz = len(pool) - _ = pool.map(lambda mapdl: mapdl.prep7()) - assert len(pool) == pool_sz - pool.exit() - - -def test_ip(monkeypatch): - monkeypatch.delenv("PYMAPDL_START_INSTANCE", raising=False) - monkeypatch.delenv("PYMAPDL_IP", raising=False) - - ips = ["127.0.0.1", "127.0.0.2", "127.0.0.3"] - ports = [50083, 50100, 50898] - pool_ = MapdlPool( - 3, - ip=ips, - port=ports, - exec_file=EXEC_FILE, - nproc=NPROC, - additional_switches=QUICK_LAUNCH_SWITCHES, - _debug_no_launch=True, - ) - args = pool_._debug_no_launch - - assert not args["start_instance"] # Because of ip - assert args["ips"] == ips - assert args["ports"] == ports - - -def test_next(pool): - # Check the instances are free - for each_instance in pool: - assert not each_instance.locked - assert not each_instance._busy - - with pool.next() as mapdl: - assert isinstance(mapdl, Mapdl) - assert mapdl.locked - assert mapdl._busy - mapdl.prep7() - - for each_instance in pool: - assert not each_instance.locked - assert not each_instance._busy - - -def test_next_with_returns_index(pool): - # Check the instances are free - for each_instance in pool: - assert not each_instance.locked - assert not each_instance._busy - - with pool.next(return_index=True) as (mapdl, index): - assert isinstance(mapdl, Mapdl) - assert isinstance(index, int) - - assert mapdl.locked - assert mapdl._busy - mapdl.prep7() - - assert mapdl == pool[index] - - for each_instance in pool: - assert not each_instance.locked - assert not each_instance._busy - - -def test_multiple_ips(): - ips = [ - "123.45.67.01", - "123.45.67.02", - "123.45.67.03", - "123.45.67.04", - "123.45.67.05", - ] - - conf = MapdlPool(ip=ips, _debug_no_launch=True)._debug_no_launch - - ips = [socket.gethostbyname(each) for each in ips] - - assert conf["ips"] == ips - assert conf["ports"] == [50052 for i in range(len(ips))] - assert conf["start_instance"] is False - assert conf["exec_file"] is None - assert conf["n_instances"] == len(ips) - - -@pytest.mark.parametrize( - "n_instances,ip,port,exp_n_instances,exp_ip,exp_port,context", - [ - ## n_instances not set - pytest.param( - None, - None, - None, - None, - None, - None, - pytest.raises( - ValueError, match="The number of instances could not be inferred " + pool_sz = len(pool) + _ = pool.map(lambda mapdl: mapdl.prep7()) + assert len(pool) == pool_sz + pool.exit() + + def test_ip(self, monkeypatch): + monkeypatch.delenv("PYMAPDL_START_INSTANCE", raising=False) + monkeypatch.delenv("PYMAPDL_IP", raising=False) + + ips = ["127.0.0.1", "127.0.0.2", "127.0.0.3"] + ports = [50083, 50100, 50898] + pool_ = MapdlPool( + 3, + ip=ips, + port=ports, + exec_file=EXEC_FILE, + nproc=NPROC, + additional_switches=QUICK_LAUNCH_SWITCHES, + _debug_no_launch=True, + ) + args = pool_._debug_no_launch + + assert not args["start_instance"] # Because of ip + assert args["ips"] == ips + assert args["ports"] == ports + + def test_next(self, pool): + # Check the instances are free + for each_instance in pool: + assert not each_instance.locked + assert not each_instance._busy + + with pool.next() as mapdl: + assert isinstance(mapdl, Mapdl) + assert mapdl.locked + assert mapdl._busy + mapdl.prep7() + + for each_instance in pool: + assert not each_instance.locked + assert not each_instance._busy + + def test_next_with_returns_index(self, pool): + # Check the instances are free + for each_instance in pool: + assert not each_instance.locked + assert not each_instance._busy + + with pool.next(return_index=True) as (mapdl, index): + assert isinstance(mapdl, Mapdl) + assert isinstance(index, int) + + assert mapdl.locked + assert mapdl._busy + mapdl.prep7() + + assert mapdl == pool[index] + + for each_instance in pool: + assert not each_instance.locked + assert not each_instance._busy + + def test_multiple_ips(self): + ips = [ + "123.45.67.01", + "123.45.67.02", + "123.45.67.03", + "123.45.67.04", + "123.45.67.05", + ] + + conf = MapdlPool(ip=ips, _debug_no_launch=True)._debug_no_launch + + ips = [socket.gethostbyname(each) for each in ips] + + assert conf["ips"] == ips + assert conf["ports"] == [50052 for i in range(len(ips))] + assert conf["start_instance"] is False + assert conf["exec_file"] is None + assert conf["n_instances"] == len(ips) + + @pytest.mark.parametrize( + "n_instances,ip,port,exp_n_instances,exp_ip,exp_port,context", + [ + ## n_instances not set + pytest.param( + None, + None, + None, + None, + None, + None, + pytest.raises( + ValueError, match="The number of instances could not be inferred " + ), ), - ), - pytest.param( - None, - [], - None, - None, - None, - None, - pytest.raises( - ValueError, match="The number of instances could not be inferred " + pytest.param( + None, + [], + None, + None, + None, + None, + pytest.raises( + ValueError, match="The number of instances could not be inferred " + ), ), - ), - pytest.param( - None, - [], - [], - None, - None, - None, - pytest.raises( - ValueError, match="The number of instances could not be inferred " + pytest.param( + None, + [], + [], + None, + None, + None, + pytest.raises( + ValueError, match="The number of instances could not be inferred " + ), ), - ), - pytest.param(None, [], 50052, 1, [LOCALHOST], [50052], NullContext()), - pytest.param( - None, - None, - [50052, 50053], - 2, - [LOCALHOST, LOCALHOST], - [50052, 50053], - NullContext(), - ), - pytest.param( - None, - None, - set(), - None, - None, - None, - pytest.raises(TypeError, match="Argument 'port' does not support"), - ), - pytest.param( - None, - "123.0.0.1", - [50052, 50053, 50055], - 3, - ["123.0.0.1", "123.0.0.1", "123.0.0.1"], - [50052, 50053, 50055], - NullContext(), - ), - pytest.param( - None, - "123.0.0.1", - [], - None, - None, - None, - pytest.raises( - ValueError, match="The number of ports should be higher than" + pytest.param(None, [], 50052, 1, [LOCALHOST], [50052], NullContext()), + pytest.param( + None, + None, + [50052, 50053], + 2, + [LOCALHOST, LOCALHOST], + [50052, 50053], + NullContext(), ), - ), - pytest.param( - None, - ["123.0.0.1", "123.0.0.2", "123.0.0.3"], - None, - 3, - ["123.0.0.1", "123.0.0.2", "123.0.0.3"], - [50052, 50052, 50052], - NullContext(), - ), - pytest.param( - None, - ["123.0.0.1", "123.0.0.2", "123.0.0.3"], - 50053, - 3, - ["123.0.0.1", "123.0.0.2", "123.0.0.3"], - [50053, 50053, 50053], - NullContext(), - ), - pytest.param( - None, - ["123.0.0.1", "123.0.0.2", "123.0.0.3"], - [50052, 50053], - None, - None, - None, - pytest.raises(ValueError, match="should be the same as the number of IPs"), - ), - pytest.param( - None, - ["123.0.0.1", "123.0.0.2", "123.0.0.3"], - [50052, 50053, 50053], - 3, - ["123.0.0.1", "123.0.0.2", "123.0.0.3"], - [50052, 50053, 50053], - NullContext(), - ), - pytest.param( - None, - set(), - None, - None, - None, - None, - pytest.raises(TypeError, match="Argument 'ip' does not support"), - ), - ## n_instances set - # ip is none - pytest.param( - {}, - None, - None, - None, - None, - None, - pytest.raises( - TypeError, match="Only integers are allowed for 'n_instances'" + pytest.param( + None, + None, + set(), + None, + None, + None, + pytest.raises(TypeError, match="Argument 'port' does not support"), ), - ), - pytest.param( - 0, - None, - None, - None, - None, - None, - pytest.raises( - ValueError, match="Must request at least 1 instance to create" + pytest.param( + None, + "123.0.0.1", + [50052, 50053, 50055], + 3, + ["123.0.0.1", "123.0.0.1", "123.0.0.1"], + [50052, 50053, 50055], + NullContext(), ), - ), - pytest.param( - 2, - None, - None, - 2, - [LOCALHOST, LOCALHOST], - [MAPDL_DEFAULT_PORT, MAPDL_DEFAULT_PORT + 1], - NullContext(), - ), - pytest.param( - 3, - None, - None, - 3, - [LOCALHOST, LOCALHOST, LOCALHOST], - [MAPDL_DEFAULT_PORT, MAPDL_DEFAULT_PORT + 1, MAPDL_DEFAULT_PORT + 2], - NullContext(), - ), - pytest.param( - 3, - None, - 50053, - 3, - [LOCALHOST, LOCALHOST, LOCALHOST], - [50053, 50053 + 1, 50053 + 2], - NullContext(), - ), - pytest.param( - 3, - None, - [50052, 50053], - None, - None, - None, - pytest.raises( - ValueError, - match="If using 'n_instances' and 'port' without multiple 'ip'", + pytest.param( + None, + "123.0.0.1", + [], + None, + None, + None, + pytest.raises( + ValueError, match="The number of ports should be higher than" + ), ), - ), - pytest.param( - 3, - None, - [50052, 50053, 50054], - 3, - [LOCALHOST, LOCALHOST, LOCALHOST], - [50052, 50053, 50054], - NullContext(), - ), - pytest.param( - 3, - None, - set(), - None, - None, - None, - pytest.raises( - TypeError, - match="Argument 'port' does not support this type of argument", + pytest.param( + None, + ["123.0.0.1", "123.0.0.2", "123.0.0.3"], + None, + 3, + ["123.0.0.1", "123.0.0.2", "123.0.0.3"], + [50052, 50052, 50052], + NullContext(), ), - ), - # ip is string - pytest.param( - 3, - "123.0.0.1", - None, - None, - None, - None, - pytest.raises(ValueError, match="If using 'n_instances' and only one 'ip'"), - ), - pytest.param( - 3, - "123.0.0.1", - 50053, - None, - None, - None, - pytest.raises(ValueError, match="If using 'n_instances' and only one 'ip'"), - ), - pytest.param( - 3, - "123.0.0.1", - [50053, 50052], - None, - None, - None, - pytest.raises(ValueError, match="If using 'n_instances' and only one 'ip'"), - ), - pytest.param( - 3, - "123.0.0.1", - [50053, 50052, 50054], - 3, - ["123.0.0.1", "123.0.0.1", "123.0.0.1"], - [50053, 50052, 50054], - NullContext(), - ), - # ip is list - pytest.param( - 3, - ["123.0.0.1", "123.0.0.2", "123.0.0.3", "123.0.0.4"], - None, - None, - None, - None, - pytest.raises( - ValueError, match="should be the same as the number of instances" + pytest.param( + None, + ["123.0.0.1", "123.0.0.2", "123.0.0.3"], + 50053, + 3, + ["123.0.0.1", "123.0.0.2", "123.0.0.3"], + [50053, 50053, 50053], + NullContext(), ), - ), - pytest.param( - 4, - ["123.0.0.1", "123.0.0.2", "123.0.0.3", "123.0.0.4"], - None, - 4, - ["123.0.0.1", "123.0.0.2", "123.0.0.3", "123.0.0.4"], - [ - MAPDL_DEFAULT_PORT, - MAPDL_DEFAULT_PORT, - MAPDL_DEFAULT_PORT, - MAPDL_DEFAULT_PORT, - ], - NullContext(), - ), - pytest.param( - 4, - ["123.0.0.1", "123.0.0.2", "123.0.0.3", "123.0.0.4"], - 50053, - 4, - ["123.0.0.1", "123.0.0.2", "123.0.0.3", "123.0.0.4"], - [50053, 50053, 50053, 50053], - NullContext(), - ), - pytest.param( - 4, - ["123.0.0.1", "123.0.0.2", "123.0.0.3", "123.0.0.4"], - [50053, 50054], - None, - None, - None, - pytest.raises( - ValueError, - match="you should provide as many ports as number of instances", + pytest.param( + None, + ["123.0.0.1", "123.0.0.2", "123.0.0.3"], + [50052, 50053], + None, + None, + None, + pytest.raises( + ValueError, match="should be the same as the number of IPs" + ), ), - ), - pytest.param( - 4, - ["123.0.0.1", "123.0.0.2", "123.0.0.3", "123.0.0.4"], - [50055] * 4, - 4, - ["123.0.0.1", "123.0.0.2", "123.0.0.3", "123.0.0.4"], - [50055] * 4, - NullContext(), - ), - pytest.param( - 4, - ["123.0.0.1", "123.0.0.2", "123.0.0.3", "123.0.0.4"], - set(), - None, - None, - None, - pytest.raises( - TypeError, match="Argument 'port' does not support this type of" + pytest.param( + None, + ["123.0.0.1", "123.0.0.2", "123.0.0.3"], + [50052, 50053, 50053], + 3, + ["123.0.0.1", "123.0.0.2", "123.0.0.3"], + [50052, 50053, 50053], + NullContext(), ), - ), - # ip type is not allowed - pytest.param( - 4, - set(), - None, - None, - None, - None, - pytest.raises( - TypeError, match="Argument 'ip' does not support this type of" + pytest.param( + None, + set(), + None, + None, + None, + None, + pytest.raises(TypeError, match="Argument 'ip' does not support"), ), - ), - ], -) -def test_ip_port_n_instance( - monkeypatch, n_instances, ip, port, exp_n_instances, exp_ip, exp_port, context -): - monkeypatch.delenv("PYMAPDL_START_INSTANCE", raising=False) - monkeypatch.delenv("PYMAPDL_IP", raising=False) - monkeypatch.setenv( - "PYMAPDL_MAPDL_EXEC", "/ansys_inc/v222/ansys/bin/ansys222" - ) # to avoid trying to find it. - - with context: - conf = MapdlPool( - n_instances=n_instances, ip=ip, port=port, _debug_no_launch=True - )._debug_no_launch - - if exp_ip: - exp_ip = [socket.gethostbyname(each) for each in exp_ip] - - assert conf["n_instances"] == exp_n_instances - assert len(conf["ips"]) == exp_n_instances - assert len(conf["ports"]) == exp_n_instances - assert conf["ips"] == exp_ip - assert conf["ports"] == exp_port - assert conf["exec_file"] == "/ansys_inc/v222/ansys/bin/ansys222" + ## n_instances set + # ip is none + pytest.param( + {}, + None, + None, + None, + None, + None, + pytest.raises( + TypeError, match="Only integers are allowed for 'n_instances'" + ), + ), + pytest.param( + 0, + None, + None, + None, + None, + None, + pytest.raises( + ValueError, match="Must request at least 1 instance to create" + ), + ), + pytest.param( + 2, + None, + None, + 2, + [LOCALHOST, LOCALHOST], + [MAPDL_DEFAULT_PORT, MAPDL_DEFAULT_PORT + 1], + NullContext(), + ), + pytest.param( + 3, + None, + None, + 3, + [LOCALHOST, LOCALHOST, LOCALHOST], + [MAPDL_DEFAULT_PORT, MAPDL_DEFAULT_PORT + 1, MAPDL_DEFAULT_PORT + 2], + NullContext(), + ), + pytest.param( + 3, + None, + 50053, + 3, + [LOCALHOST, LOCALHOST, LOCALHOST], + [50053, 50053 + 1, 50053 + 2], + NullContext(), + ), + pytest.param( + 3, + None, + [50052, 50053], + None, + None, + None, + pytest.raises( + ValueError, + match="If using 'n_instances' and 'port' without multiple 'ip'", + ), + ), + pytest.param( + 3, + None, + [50052, 50053, 50054], + 3, + [LOCALHOST, LOCALHOST, LOCALHOST], + [50052, 50053, 50054], + NullContext(), + ), + pytest.param( + 3, + None, + set(), + None, + None, + None, + pytest.raises( + TypeError, + match="Argument 'port' does not support this type of argument", + ), + ), + # ip is string + pytest.param( + 3, + "123.0.0.1", + None, + None, + None, + None, + pytest.raises( + ValueError, match="If using 'n_instances' and only one 'ip'" + ), + ), + pytest.param( + 3, + "123.0.0.1", + 50053, + None, + None, + None, + pytest.raises( + ValueError, match="If using 'n_instances' and only one 'ip'" + ), + ), + pytest.param( + 3, + "123.0.0.1", + [50053, 50052], + None, + None, + None, + pytest.raises( + ValueError, match="If using 'n_instances' and only one 'ip'" + ), + ), + pytest.param( + 3, + "123.0.0.1", + [50053, 50052, 50054], + 3, + ["123.0.0.1", "123.0.0.1", "123.0.0.1"], + [50053, 50052, 50054], + NullContext(), + ), + # ip is list + pytest.param( + 3, + ["123.0.0.1", "123.0.0.2", "123.0.0.3", "123.0.0.4"], + None, + None, + None, + None, + pytest.raises( + ValueError, match="should be the same as the number of instances" + ), + ), + pytest.param( + 4, + ["123.0.0.1", "123.0.0.2", "123.0.0.3", "123.0.0.4"], + None, + 4, + ["123.0.0.1", "123.0.0.2", "123.0.0.3", "123.0.0.4"], + [ + MAPDL_DEFAULT_PORT, + MAPDL_DEFAULT_PORT, + MAPDL_DEFAULT_PORT, + MAPDL_DEFAULT_PORT, + ], + NullContext(), + ), + pytest.param( + 4, + ["123.0.0.1", "123.0.0.2", "123.0.0.3", "123.0.0.4"], + 50053, + 4, + ["123.0.0.1", "123.0.0.2", "123.0.0.3", "123.0.0.4"], + [50053, 50053, 50053, 50053], + NullContext(), + ), + pytest.param( + 4, + ["123.0.0.1", "123.0.0.2", "123.0.0.3", "123.0.0.4"], + [50053, 50054], + None, + None, + None, + pytest.raises( + ValueError, + match="you should provide as many ports as number of instances", + ), + ), + pytest.param( + 4, + ["123.0.0.1", "123.0.0.2", "123.0.0.3", "123.0.0.4"], + [50055] * 4, + 4, + ["123.0.0.1", "123.0.0.2", "123.0.0.3", "123.0.0.4"], + [50055] * 4, + NullContext(), + ), + pytest.param( + 4, + ["123.0.0.1", "123.0.0.2", "123.0.0.3", "123.0.0.4"], + set(), + None, + None, + None, + pytest.raises( + TypeError, match="Argument 'port' does not support this type of" + ), + ), + # ip type is not allowed + pytest.param( + 4, + set(), + None, + None, + None, + None, + pytest.raises( + TypeError, match="Argument 'ip' does not support this type of" + ), + ), + ], + ) + def test_ip_port_n_instance( + self, + monkeypatch, + n_instances, + ip, + port, + exp_n_instances, + exp_ip, + exp_port, + context, + ): + monkeypatch.delenv("PYMAPDL_START_INSTANCE", raising=False) + monkeypatch.delenv("PYMAPDL_IP", raising=False) + monkeypatch.setenv( + "PYMAPDL_MAPDL_EXEC", "/ansys_inc/v222/ansys/bin/ansys222" + ) # to avoid trying to find it. + + with context: + conf = MapdlPool( + n_instances=n_instances, ip=ip, port=port, _debug_no_launch=True + )._debug_no_launch + + if exp_ip: + exp_ip = [socket.gethostbyname(each) for each in exp_ip] + + assert conf["n_instances"] == exp_n_instances + assert len(conf["ips"]) == exp_n_instances + assert len(conf["ports"]) == exp_n_instances + assert conf["ips"] == exp_ip + assert conf["ports"] == exp_port + assert conf["exec_file"] == "/ansys_inc/v222/ansys/bin/ansys222" From 45860089ac4be50bbcbd7e9251b073b00a2e79bb Mon Sep 17 00:00:00 2001 From: pyansys-ci-bot <92810346+pyansys-ci-bot@users.noreply.github.com> Date: Tue, 9 Jul 2024 12:35:38 +0000 Subject: [PATCH 2/5] chore: adding changelog file 3258.added.md --- doc/changelog.d/3258.added.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 doc/changelog.d/3258.added.md diff --git a/doc/changelog.d/3258.added.md b/doc/changelog.d/3258.added.md new file mode 100644 index 0000000000..63c8b1b946 --- /dev/null +++ b/doc/changelog.d/3258.added.md @@ -0,0 +1 @@ +refactor: moving tests to a class and adding delete method. \ No newline at end of file From 54613f5ec225bf9104980b4f5a1b9beb634475c8 Mon Sep 17 00:00:00 2001 From: German <28149841+germa89@users.noreply.github.com> Date: Mon, 15 Jul 2024 13:19:59 +0200 Subject: [PATCH 3/5] fix: missing self --- tests/test_pool.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/test_pool.py b/tests/test_pool.py index 529cf3ef8a..9fa950dfd9 100644 --- a/tests/test_pool.py +++ b/tests/test_pool.py @@ -263,7 +263,8 @@ def test_directory_names_default(self, pool): assert f"Instance_{i}" in dirs_path_pool @skip_if_ignore_pool - def test_directory_names_default_with_restart(pool): + def test_directory_names_default_with_restart(self, pool): + pool[1].exit() pool.wait_for_ready() From 8aeaca990acfbbd1077916c4569765779ac56363 Mon Sep 17 00:00:00 2001 From: German <28149841+germa89@users.noreply.github.com> Date: Mon, 15 Jul 2024 14:44:06 +0200 Subject: [PATCH 4/5] fix: duplicated test --- tests/test_pool.py | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/tests/test_pool.py b/tests/test_pool.py index 9fa950dfd9..1a8b267433 100644 --- a/tests/test_pool.py +++ b/tests/test_pool.py @@ -345,16 +345,6 @@ def test_only_one_instance(self): assert len(pool) == pool_sz pool.exit() - @skip_if_ignore_pool - def test_only_one_instance(self, mapdl): - pool = MapdlPool( - 1, - port=mapdl.port + 1, - exec_file=EXEC_FILE, - nproc=NPROC, - additional_switches=QUICK_LAUNCH_SWITCHES, - ) - def test_ip(self, monkeypatch): monkeypatch.delenv("PYMAPDL_START_INSTANCE", raising=False) monkeypatch.delenv("PYMAPDL_IP", raising=False) From 8739dfb7000009d0bffaa4b8412c2a8f17b39f3a Mon Sep 17 00:00:00 2001 From: German <28149841+germa89@users.noreply.github.com> Date: Mon, 15 Jul 2024 17:23:40 +0200 Subject: [PATCH 5/5] fix: skipping test if not on local --- tests/test_pool.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_pool.py b/tests/test_pool.py index 1a8b267433..69cab9d8b0 100644 --- a/tests/test_pool.py +++ b/tests/test_pool.py @@ -333,6 +333,7 @@ def test_num_instances(self): ) @skip_if_ignore_pool + @requires("local") def test_only_one_instance(self): pool = MapdlPool( 1,