Skip to content

Commit 3281872

Browse files
authored
Fix implementation of no_context (#2215)
* fix no context * fixes * fix * fix test * fix * deactivate test on docker * fix * remove test * Apply suggestions from code review
1 parent 011bde6 commit 3281872

File tree

4 files changed

+52
-44
lines changed

4 files changed

+52
-44
lines changed

src/ansys/dpf/core/server.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ def start_local_server(
160160
config=None,
161161
use_pypim_by_default=True,
162162
context=None,
163-
):
163+
) -> BaseServer:
164164
"""Start a new local DPF server at a given port and IP address.
165165
166166
This method requires Windows and ANSYS 2021 R1 or later. If ``as_global=True``, which is

src/ansys/dpf/core/server_context.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
class LicensingContextType(Enum):
4646
"""Enum representing different types of licensing contexts."""
4747

48-
none = 5
4948
premium = 1
5049
"""Checks if at least one license increment exists
5150
and allows operators to block an increment."""
@@ -81,10 +80,6 @@ def same_licensing_context(first, second):
8180
bool
8281
True if the licensing contexts are compatible, False otherwise.
8382
"""
84-
if (first == LicensingContextType.none and second != LicensingContextType.none) or (
85-
first != LicensingContextType.none and second == LicensingContextType.none
86-
):
87-
return False
8883
if int(first) == int(LicensingContextType.entry) and int(second) != int(
8984
LicensingContextType.entry
9085
):
@@ -295,7 +290,8 @@ def __ne__(self, other):
295290
class AvailableServerContexts:
296291
"""Defines available server contexts."""
297292

298-
no_context = ServerContext(LicensingContextType.none, "")
293+
"""Applies an empty plugins xml: no plugins are loaded."""
294+
no_context = ServerContext(2, "") # 2 == ContextType.userDefined. This is not exposed publicly.
299295
pre_defined_environment = ServerContext(0)
300296
"""DataProcessingCore.xml that is next to DataProcessingCore.dll/libDataProcessingCore.so will
301297
be taken"""

src/ansys/dpf/core/server_types.py

Lines changed: 44 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
from ansys.dpf.core import __version__, errors, server_context, server_factory
5151
from ansys.dpf.core._version import min_server_version, server_to_ansys_version
5252
from ansys.dpf.core.check_version import server_meet_version
53+
from ansys.dpf.core.server_context import AvailableServerContexts, ServerContext
5354
from ansys.dpf.gate import data_processing_grpcapi, load_api
5455

5556
if TYPE_CHECKING: # pragma: no cover
@@ -114,7 +115,11 @@ def _verify_ansys_path_is_valid(ansys_path, executable, path_in_install=None):
114115

115116

116117
def _run_launch_server_process(
117-
ip, port, ansys_path=None, docker_config=server_factory.RunningDockerConfig()
118+
ip,
119+
port,
120+
ansys_path=None,
121+
docker_config=server_factory.RunningDockerConfig(),
122+
context: ServerContext = None,
118123
):
119124
bShell = False
120125
if docker_config.use_docker:
@@ -127,13 +132,25 @@ def _run_launch_server_process(
127132
if os.name == "nt":
128133
executable = "Ans.Dpf.Grpc.bat"
129134
run_cmd = f"{executable} --address {ip} --port {port}"
135+
if context not in (
136+
None,
137+
AvailableServerContexts.entry,
138+
AvailableServerContexts.premium,
139+
):
140+
run_cmd += f" --context {int(context.licensing_context_type)}"
130141
else:
131142
executable = "./Ans.Dpf.Grpc.sh" # pragma: no cover
132143
run_cmd = [
133144
executable,
134145
f"--address {ip}",
135146
f"--port {port}",
136147
] # pragma: no cover
148+
if context not in (
149+
None,
150+
AvailableServerContexts.entry,
151+
AvailableServerContexts.premium,
152+
):
153+
run_cmd.append(f"--context {int(context.licensing_context_type)}")
137154
path_in_install = load_api._get_path_in_install(internal_folder="bin")
138155
dpf_run_dir = _verify_ansys_path_is_valid(ansys_path, executable, path_in_install)
139156

@@ -204,7 +221,9 @@ def read_stdout():
204221
raise RuntimeError(errstr)
205222

206223

207-
def launch_dpf(ansys_path, ip=LOCALHOST, port=DPF_DEFAULT_PORT, timeout=10):
224+
def launch_dpf(
225+
ansys_path, ip=LOCALHOST, port=DPF_DEFAULT_PORT, timeout=10, context: ServerContext = None
226+
):
208227
"""Launch Ansys DPF.
209228
210229
Parameters
@@ -222,9 +241,10 @@ def launch_dpf(ansys_path, ip=LOCALHOST, port=DPF_DEFAULT_PORT, timeout=10):
222241
Maximum number of seconds for the initialization attempt.
223242
The default is ``10``. Once the specified number of seconds
224243
passes, the connection fails.
225-
244+
context : , optional
245+
Context to apply to DPF server when launching it.
226246
"""
227-
process = _run_launch_server_process(ip, port, ansys_path)
247+
process = _run_launch_server_process(ip, port, ansys_path, context=context)
228248
lines = []
229249
current_errors = []
230250
_wait_and_check_server_connection(
@@ -766,7 +786,7 @@ def __init__(
766786
launch_server: bool = True,
767787
docker_config: DockerConfig = RUNNING_DOCKER,
768788
use_pypim: bool = True,
769-
context: server_context.AvailableServerContexts = server_context.SERVER_CONTEXT,
789+
context: server_context.ServerContext = server_context.SERVER_CONTEXT,
770790
):
771791
# Load DPFClientAPI
772792
from ansys.dpf.core.misc import is_pypim_configured
@@ -808,7 +828,7 @@ def __init__(
808828
timeout=timeout,
809829
)
810830
else:
811-
launch_dpf(ansys_path, ip, port, timeout=timeout)
831+
launch_dpf(ansys_path, ip, port, timeout=timeout, context=context)
812832
self._local_server = True
813833

814834
# store port and ip for later reference
@@ -820,15 +840,11 @@ def __init__(
820840
self._create_shutdown_funcs()
821841
self._check_first_call(timeout=timeout - (time.time() - start_time)) # Pass remaining time
822842
if context:
823-
if context == core.AvailableServerContexts.no_context:
824-
self._base_service.initialize()
843+
try:
844+
self._base_service.initialize_with_context(context)
825845
self._context = context
826-
else:
827-
try:
828-
self._base_service.initialize_with_context(context)
829-
self._context = context
830-
except errors.DpfVersionNotSupported:
831-
pass
846+
except errors.DpfVersionNotSupported:
847+
pass
832848
self.set_as_global(as_global=as_global)
833849

834850
def _check_first_call(self, timeout: float):
@@ -1039,18 +1055,14 @@ def __init__(
10391055
)
10401056
raise e
10411057
if context:
1042-
if context == core.AvailableServerContexts.no_context:
1043-
self._base_service.initialize()
1044-
self._context = context
1045-
else:
1046-
try:
1047-
self.apply_context(context)
1048-
except errors.DpfVersionNotSupported:
1049-
self._base_service.initialize_with_context(
1050-
server_context.AvailableServerContexts.premium
1051-
)
1052-
self._context = server_context.AvailableServerContexts.premium
1053-
pass
1058+
try:
1059+
self.apply_context(context)
1060+
except errors.DpfVersionNotSupported:
1061+
self._base_service.initialize_with_context(
1062+
server_context.AvailableServerContexts.premium
1063+
)
1064+
self._context = server_context.AvailableServerContexts.premium
1065+
pass
10541066
self.set_as_global(as_global=as_global)
10551067
# Update the python os.environment
10561068
if not os.name == "posix":
@@ -1201,7 +1213,7 @@ def __init__(
12011213
launch_server: bool = True,
12021214
docker_config: DockerConfig = RUNNING_DOCKER,
12031215
use_pypim: bool = True,
1204-
context: server_context.AvailableServerContexts = server_context.SERVER_CONTEXT,
1216+
context: server_context.ServerContext = server_context.SERVER_CONTEXT,
12051217
):
12061218
"""Start the DPF server."""
12071219
# Use ansys.grpc.dpf
@@ -1247,7 +1259,7 @@ def __init__(
12471259
timeout=timeout,
12481260
)
12491261
else:
1250-
launch_dpf(ansys_path, ip, port, timeout=timeout)
1262+
launch_dpf(ansys_path, ip, port, timeout=timeout, context=context)
12511263
self._local_server = True
12521264
from ansys.dpf.core import misc, settings
12531265

@@ -1267,14 +1279,11 @@ def __init__(
12671279

12681280
check_ansys_grpc_dpf_version(self, timeout)
12691281
if context:
1270-
if context == core.AvailableServerContexts.no_context:
1282+
try:
1283+
self._base_service.initialize_with_context(context)
12711284
self._context = context
1272-
else:
1273-
try:
1274-
self._base_service.initialize_with_context(context)
1275-
self._context = context
1276-
except errors.DpfVersionNotSupported:
1277-
pass
1285+
except errors.DpfVersionNotSupported:
1286+
pass
12781287
self.set_as_global(as_global=as_global)
12791288

12801289
def _create_shutdown_funcs(self):

tests/test_service.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
import pytest
3131

3232
from ansys import dpf
33-
from ansys.dpf.core import examples, path_utilities
33+
from ansys.dpf.core import check_version, examples, path_utilities
3434
import conftest
3535
from conftest import running_docker
3636

@@ -499,7 +499,8 @@ def test_context_environment_variable(reset_context_environment_variable):
499499

500500

501501
@pytest.mark.skipif(
502-
not conftest.SERVERS_VERSION_GREATER_THAN_OR_EQUAL_TO_6_0, reason="Failures on Windows 231"
502+
not conftest.SERVERS_VERSION_GREATER_THAN_OR_EQUAL_TO_8_0 or running_docker,
503+
reason="Failures on Windows 231",
503504
)
504505
def test_server_without_context(remote_config_server_type):
505506
"""Tests starting a server without a no_context given."""
@@ -510,6 +511,8 @@ def test_server_without_context(remote_config_server_type):
510511
)
511512
none_type = dpf.core.AvailableServerContexts.no_context.licensing_context_type
512513
assert server.context.licensing_context_type == none_type
514+
if check_version.server_meet_version("10.0", server): # Before, there was a bug
515+
assert len(dpf.core.available_operator_names(server=server)) < 20
513516

514517

515518
@pytest.mark.order("last")

0 commit comments

Comments
 (0)