Skip to content

Commit

Permalink
Maint/unit tests fixes (#130)
Browse files Browse the repository at this point in the history
  • Loading branch information
rfahlberg authored May 22, 2023
1 parent a86d245 commit 82fe3f6
Show file tree
Hide file tree
Showing 5 changed files with 205 additions and 198 deletions.
33 changes: 10 additions & 23 deletions src/ansys/optislang/core/tcp_osl_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from pathlib import Path
from queue import Queue
import re
import select
import signal
import socket
import struct
Expand Down Expand Up @@ -400,29 +399,17 @@ def _recv_response_length(self, timeout: Union[float, None]) -> int:

response_len = -1
bytes_to_receive = self.__class__._RESPONSE_SIZE_BYTES

# read from socket until response size (twice) has been received
while True:
try:
# Test if we will be able to read something from the connection.
readable_sockets, _, _ = select.select([self.__socket], [], [], 1)
if self.__socket in readable_sockets:
# Read and convert response size. Assume server sent response size twice.
# Sizes need to match.
response_len_1 = struct.unpack("!Q", self.__socket.recv(bytes_to_receive))[0]
response_len_2 = struct.unpack("!Q", self.__socket.recv(bytes_to_receive))[0]
if response_len_1 != response_len_2:
raise ResponseFormatError("The message size values do not match.")

response_len = response_len_1
if response_len >= 0:
break
except Exception as e:
self._logger.debug(e)
pass
now = time.time()
elapsed = now - start_time
if timeout is not None and elapsed > timeout:
raise TimeoutError("Time to receive message length has expired.")
response_len_1 = struct.unpack("!Q", self._receive_bytes(bytes_to_receive, timeout))[0]
response_len_2 = struct.unpack("!Q", self._receive_bytes(bytes_to_receive, timeout))[0]

if response_len_1 != response_len_2:
raise ResponseFormatError(
"Server response format unrecognized. Response sizes do not match."
)

response_len = response_len_1

return response_len

Expand Down
33 changes: 12 additions & 21 deletions tests/test_examples.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from contextlib import nullcontext as does_not_raise
import os
import pathlib
import time

import matplotlib.pyplot as plt
import pytest

pytestmark = pytest.mark.local_osl
Expand All @@ -26,7 +26,6 @@ def test_01_ten_bar_truss():
name = "01_ten_bar_truss"
file = list(filter(lambda path: name in path, run_python_script_example_files_paths))[0]
exec(open(file).read())
time.sleep(5)
assert dnr is None


Expand All @@ -36,7 +35,6 @@ def test_02_1_oscillator_robustness():
name = "02_1_oscillator_robustness"
file = list(filter(lambda path: name in path, run_python_script_example_files_paths))[0]
exec(open(file).read())
time.sleep(5)
assert dnr is None


Expand All @@ -46,7 +44,6 @@ def test_02_2_oscillator_python_system():
name = "02_2_oscillator_python_system"
file = list(filter(lambda path: name in path, run_python_script_example_files_paths))[0]
exec(open(file).read())
time.sleep(5)
assert dnr is None


Expand All @@ -56,7 +53,6 @@ def test_02_3_oscillator_optimization_on_EA():
name = "02_3_oscillator_optimization_on_EA"
file = list(filter(lambda path: name in path, run_python_script_example_files_paths))[0]
exec(open(file).read())
time.sleep(5)
assert dnr is None


Expand All @@ -66,7 +62,6 @@ def test_02_4_oscillator_MOP_sensitivity_and_optimization():
name = "02_4_oscillator_MOP_sensitivity_and_optimization"
file = list(filter(lambda path: name in path, run_python_script_example_files_paths))[0]
exec(open(file).read())
time.sleep(5)
assert dnr is None


Expand All @@ -76,7 +71,6 @@ def test_02_5_oscillator_calibration_systems():
name = "02_5_oscillator_calibration_systems"
file = list(filter(lambda path: name in path, run_python_script_example_files_paths))[0]
exec(open(file).read())
time.sleep(5)
assert dnr is None


Expand All @@ -86,18 +80,18 @@ def test_03_etk_abaqus():
name = "03_etk_abaqus"
file = list(filter(lambda path: name in path, run_python_script_example_files_paths))[0]
exec(open(file).read())
time.sleep(5)
assert dnr is None


def test_04_python_node_and_help():
"""Test 04_python_node_and_help.py."""
with does_not_raise() as dnr:
name = "04_python_node_and_help"
file = list(filter(lambda path: name in path, run_python_script_example_files_paths))[0]
exec(open(file).read())
time.sleep(5)
assert dnr is None
# The use of "help" function (intended for interactive use) in this example,
# leads to a straying osl process. Deactivate this test for now.
# def test_04_python_node_and_help():
# """Test 04_python_node_and_help.py."""
# with does_not_raise() as dnr:
# name = "04_python_node_and_help"
# file = list(filter(lambda path: name in path, run_python_script_example_files_paths))[0]
# exec(open(file).read())
# assert dnr is None


def test_05_optimizer_settings():
Expand All @@ -106,7 +100,6 @@ def test_05_optimizer_settings():
name = "05_optimizer_settings"
file = list(filter(lambda path: name in path, run_python_script_example_files_paths))[0]
exec(open(file).read())
time.sleep(5)
assert dnr is None


Expand All @@ -116,7 +109,6 @@ def test_06_sensitivity_settings():
name = "06_sensitivity_settings"
file = list(filter(lambda path: name in path, run_python_script_example_files_paths))[0]
exec(open(file).read())
time.sleep(5)
assert dnr is None


Expand All @@ -126,7 +118,6 @@ def test_07_simple_calculator():
name = "07_simple_calculator"
file = list(filter(lambda path: name in path, run_python_script_example_files_paths))[0]
exec(open(file).read())
time.sleep(5)
assert dnr is None


Expand All @@ -139,11 +130,11 @@ def test_07_simple_calculator():
evaluate_design_example_files_paths.append(os.path.join(evaluate_design_examples_dir, file))


def test_01_ten_bar_truss_evaluate_design():
def test_01_ten_bar_truss_evaluate_design(monkeypatch):
"""Test 01_ten_bar_truss_evaluate_design.py."""
with does_not_raise() as dnr:
name = "01_ten_bar_truss"
file = list(filter(lambda path: name in path, evaluate_design_example_files_paths))[0]
monkeypatch.setattr(plt, "show", lambda: None)
exec(open(file).read())
time.sleep(5)
assert dnr is None
Loading

0 comments on commit 82fe3f6

Please sign in to comment.