1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
|
# SPDX-FileCopyrightText: All Contributors to the PyTango project
# SPDX-License-Identifier: LGPL-3.0-or-later
from tango import (
Util,
SerialModel,
AutoTangoAllowThreads,
EnsureOmniThread,
AutoTangoMonitor,
DevFailed,
)
from tango.server import Device, attribute
from tango.test_utils import DeviceTestContext
from threading import Thread, Event
from queue import Queue
import pytest
from time import sleep
pytestmark = pytest.mark.extra_src_test
def do_nothing():
sleep(1e-3)
class SimpleDS(Device):
def init_device(self):
u = Util.instance()
mode = SerialModel.BY_CLASS
u.set_serial_model(mode)
assert u.get_serial_model() == mode
with AutoTangoAllowThreads(self):
with AutoTangoAllowThreads(self):
do_nothing()
@attribute
def attr(self) -> int:
return 1234
def test_dont_crash_with_nested_tango_allow_threads():
with DeviceTestContext(SimpleDS) as proxy:
assert proxy.attr == 1234
class MonSameThread(Device):
serial_model = None
unlock = None
running = None
def init_device(self):
u = Util.instance()
u.set_serial_model(MonSameThread.serial_model)
assert u.get_serial_model() == MonSameThread.serial_model
self._thread = Thread(target=self.thread_func)
MonSameThread.running = True
self._thread.start()
def delete_device(self):
MonSameThread.running = False
self._thread.join()
def thread_func(self):
with EnsureOmniThread():
if MonDiffThread.serial_model == SerialModel.BY_CLASS:
obj = self.get_device_class()
else:
obj = self
with AutoTangoMonitor(obj):
if MonSameThread.unlock:
# unlock it again
with AutoTangoAllowThreads(self):
while MonSameThread.running:
do_nothing()
else:
while MonSameThread.running:
do_nothing()
@attribute
def attr(self) -> int:
return 1234
@pytest.mark.parametrize("unlock", [True, False])
def test_monitor_force_unlock_from_same_thread(
unlock, server_green_mode, server_serial_model
):
MonSameThread.serial_model = server_serial_model
MonSameThread.unlock = unlock
if unlock:
if server_serial_model == SerialModel.BY_CLASS:
pytest.xfail(
"Not implemented, see https://gitlab.com/tango-controls/pytango/-/issues/650"
)
elif server_serial_model == SerialModel.BY_PROCESS:
pytest.xfail(
"Not implemented, see https://gitlab.com/tango-controls/pytango/-/issues/650"
)
with DeviceTestContext(MonSameThread) as proxy:
if server_serial_model == SerialModel.NO_SYNC:
assert proxy.attr == 1234
elif unlock:
assert proxy.attr == 1234
else:
with pytest.raises(
DevFailed, match="not able to acquire serialization monitor"
):
assert proxy.attr == 1234
# required especially by SerialModel.BY_PROCESS so that the DS can be killed from the test context
MonSameThread.running = False
class MonDiffThread(Device):
serial_model = None
unlock = None
running = None
def init_device(self):
u = Util.instance()
u.set_serial_model(MonDiffThread.serial_model)
assert u.get_serial_model() == MonDiffThread.serial_model
MonDiffThread.running = True
self._lock_thread = Thread(target=self.lock_thread_func)
self._lock_thread.start()
self._unlock_thread = Thread(target=self.unlock_thread_func)
self._unlock_thread.start()
def delete_device(self):
MonDiffThread.running = False
self._lock_thread.join()
self._unlock_thread.join()
def lock_thread_func(self):
with EnsureOmniThread():
if MonDiffThread.serial_model == SerialModel.BY_CLASS:
obj = self.get_device_class()
else:
obj = self
with AutoTangoMonitor(obj):
while MonDiffThread.running:
do_nothing()
def unlock_thread_func(self):
with EnsureOmniThread():
if MonDiffThread.unlock:
# try to unlock it, but as this is from a different thread, nothing happens
with AutoTangoAllowThreads(self):
while MonDiffThread.running:
do_nothing()
else:
while MonDiffThread.running:
do_nothing()
@attribute
def attr(self) -> int:
return 1234
@pytest.mark.parametrize("unlock", [True, False])
def test_monitor_force_unlock_from_different_thread(
unlock, server_green_mode, server_serial_model
):
MonDiffThread.serial_model = server_serial_model
MonDiffThread.unlock = unlock
with DeviceTestContext(MonDiffThread) as proxy:
if server_serial_model == SerialModel.NO_SYNC:
assert proxy.attr == 1234
else:
with pytest.raises(
DevFailed, match="not able to acquire serialization monitor"
):
assert proxy.attr == 1234
# required especially by SerialModel.BY_PROCESS so that the DS can be killed from the test context
MonDiffThread.running = False
class DontDeadlock(Device):
def init_device(self) -> None:
self.queue = Queue[Event | None]()
self.thread = Thread(target=self._run)
self.thread.start()
def delete_device(self) -> None:
self.queue.put(None)
self.thread.join()
def _run(self) -> None:
with EnsureOmniThread():
while True:
ev = self.queue.get()
if ev is None:
break
with AutoTangoMonitor(self):
ev.set()
@attribute
def attr(self) -> int:
ev = Event()
self.queue.put(ev)
# Allow the thread to run and start waiting for the monitor
sleep(0.01)
with AutoTangoAllowThreads(self):
ev.wait()
return 1234
# This test will probably only fail if the system is under load
def test_dont_deadlock_when_releasing_monitor():
with DeviceTestContext(DontDeadlock, process=True) as proxy:
for _ in range(100):
assert proxy.attr == 1234
|