Files
slsDetectorPackage/python/tests/test_free.py
T
8a87c83615
Build on RHEL9 docker image / build (push) Successful in 5m3s
Build on RHEL8 docker image / build (push) Successful in 5m43s
Build and Deploy on local RHEL9 / build (push) Successful in 2m18s
Build and Deploy on local RHEL8 / build (push) Successful in 5m5s
Run Simulator Tests on local RHEL9 / build (push) Successful in 20m8s
Run Simulator Tests on local RHEL8 / build (push) Successful in 23m45s
dev: zmq hwm rebind (#1480)
* move hwm into the zmq constructor and reconstruct the socket instead of rebind. seems to work better than rebind (most connections cant bind so fast?)

* fix after merge

* added tests to reconnect zmq sockets when setting rx zmqport and rx zmqhwm, changed the tests scripts a bit to make the receiver starting tcp port configurable

* tests: slsreceiver also starting up  with 2000 as default tcp port, using latest cli args for receiver and multi receiver

* releasr notes

---------

Co-authored-by: AliceMazzoleni99 <alice.mazzoleni@psi.ch>
2026-07-21 15:45:56 +02:00

143 lines
5.5 KiB
Python

# SPDX-License-Identifier: LGPL-3.0-or-other
# Copyright (C) 2021 Contributors to the SLS Detector Package
'''
This file is used to start up simulators and test for freeing shm and accessing it from python.
Run this using: pytest -s test_free.py
'''
import pytest, sys
from pathlib import Path
current_dir = Path(__file__).resolve().parents[2]
scripts_dir = current_dir / "tests" / "scripts"
sys.path.append(str(scripts_dir))
from slsdet import Detector, Ctb, freeSharedMemory
from utils_for_test import (
Log,
LogLevel,
DET_START_TCP_PORTNO
)
from conftest import session_simulator
@pytest.mark.detectorintegration
@pytest.mark.parametrize("session_simulator",[("ctb", 1, 1)],indirect=True)
def test_exptime_after_free_should_raise(session_simulator):
Log(LogLevel.INFOBLUE, f'\nRunning test_exptime_after_free_should_raise')
d = Ctb() # creates multi shm (assuming no shm exists)
d.hostname = f"localhost:{DET_START_TCP_PORTNO}" # hostname command creates mod shm, d maps to it
d.free() # frees the shm, d should not map to it anymore
# accessing invalid shm should throw
with pytest.raises(Exception) as exc_info:
_ = d.exptime
Log(LogLevel.INFOGREEN, f"✅ Test passed, exception was: {exc_info.value}")
assert str(exc_info.value) == "Shared memory is invalid or freed. Close resources before access."
def free_and_create_shm():
k = Ctb() # opens existing shm if it exists
k.hostname = f"localhost:{DET_START_TCP_PORTNO}" # free and recreate shm, maps to local shm struct
@pytest.mark.detectorintegration
@pytest.mark.parametrize("session_simulator",[("ctb", 1, 1)],indirect=True)
def test_exptime_after_not_passing_var_should_raise(session_simulator):
Log(LogLevel.INFOBLUE, f'\nRunning test_exptime_after_not_passing_var_should_raise')
d = Ctb() # creates multi shm (assuming no shm exists)
d.hostname = f"localhost:{DET_START_TCP_PORTNO}" # hostname command creates mod shm, d maps to it
free_and_create_shm() # ctb() opens multi shm, hostname command frees and recreates mod shm but shm struct is local. d still maps to old shm struct
# accessing invalid shm should throw
with pytest.raises(Exception) as exc_info:
_ = d.exptime
Log(LogLevel.INFOGREEN, f"✅ Test passed, exception was: {exc_info.value}")
assert str(exc_info.value) == "Shared memory is invalid or freed. Close resources before access."
def free_and_create_shm_passing_ctb_var(k):
k = Ctb() # opens existing shm if it exists (disregards k as its new Ctb only local to this function)
k.hostname = f"localhost:{DET_START_TCP_PORTNO}" # free and recreate shm, maps to local shm struct
@pytest.mark.detectorintegration
@pytest.mark.parametrize("session_simulator",[("ctb", 1, 1)],indirect=True)
def test_exptime_after_passing_ctb_var_should_raise(session_simulator):
Log(LogLevel.INFOBLUE, f'\nRunning test_exptime_after_passing_ctb_var_should_raise')
d = Ctb() # creates multi shm (assuming no shm exists)
d.hostname = f"localhost:{DET_START_TCP_PORTNO}" # hostname command creates mod shm, d maps to it
free_and_create_shm_passing_ctb_var(d) # ctb() opens multi shm, hostname command frees and recreates mod shm but shm struct is local. d still maps to old shm struct
# accessing invalid shm should throw
with pytest.raises(Exception) as exc_info:
_ = d.exptime
Log(LogLevel.INFOGREEN, f"✅ Test passed, exception was: {exc_info.value}")
assert str(exc_info.value) == "Shared memory is invalid or freed. Close resources before access."
def free_and_create_shm_returning_ctb():
k = Ctb() # opens existing shm if it exists (disregards k as its new Ctb only local to this function)
k.hostname = f"localhost:{DET_START_TCP_PORTNO}" # free and recreate shm, maps to local shm struct
return k
@pytest.mark.detectorintegration
@pytest.mark.parametrize("session_simulator",[("ctb", 1, 1)],indirect=True)
def test_exptime_after_returning_ctb_should_raise(session_simulator):
Log(LogLevel.INFOBLUE, f'\nRunning test_exptime_after_returning_ctb_should_raise')
d = Ctb() # creates multi shm (assuming no shm exists)
d = free_and_create_shm_returning_ctb() # ctb() opens multi shm, hostname command frees and recreates mod shm but shm struct is local but returned. d now maps to the new sturct
# this should not throw
exptime_val = d.exptime
Log(LogLevel.INFOGREEN, f"✅ Test passed, exptime was: {exptime_val}")
assert isinstance(exptime_val, float)
free_and_create_shm_returning_ctb() # this time d is not updated, it maps to the old shm struct
# accessing invalid shm should throw
with pytest.raises(Exception) as exc_info:
_ = d.exptime
Log(LogLevel.INFOGREEN, f"✅ Test passed, exception was: {exc_info.value}")
assert str(exc_info.value) == "Shared memory is invalid or freed. Close resources before access."
@pytest.mark.detectorintegration
@pytest.mark.parametrize("session_simulator",[("ctb", 1, 1)],indirect=True)
def test_hostname_twice_acess_old_should_raise(session_simulator):
Log(LogLevel.INFOBLUE, f'\nRunning test_hostname_twice_acess_old_should_raise')
d = Ctb() # creates multi shm (assuming no shm exists)
d.hostname = f"localhost:{DET_START_TCP_PORTNO}" # hostname command creates mod shm, d maps to it
d.hostname = f"localhost:{DET_START_TCP_PORTNO}" # Freeing and recreating shm while mapping d to it (old shm is out of scope)
# this should not throw
exptime_val = d.exptime
Log(LogLevel.INFOGREEN, f"✅ Test passed, exptime was: {exptime_val}")
assert isinstance(exptime_val, float)