forked from taurus-org/taurus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'fix-1178' into 'develop'
Refactor TaurusPollingTimer to avoid deadlock Closes taurus-org#1178 See merge request taurus-org/taurus!1181
- Loading branch information
Showing
3 changed files
with
105 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
|
||
from taurus.core import TaurusPollingTimer | ||
import taurus | ||
import sys | ||
import pytest | ||
import signal | ||
import time | ||
|
||
|
||
class _Timeout: | ||
"""Timeout context. Inspired by https://stackoverflow.com/a/59997890 """ | ||
def __init__(self, seconds=1, msg='Timeout'): | ||
self.seconds = seconds | ||
self.msg = msg | ||
|
||
def handle_timeout(self, signum, frame): | ||
pytest.fail(self.msg) | ||
|
||
def __enter__(self): | ||
signal.signal(signal.SIGALRM, self.handle_timeout) | ||
signal.alarm(self.seconds) | ||
|
||
def __exit__(self, type, value, traceback): | ||
signal.alarm(0) | ||
|
||
|
||
def test_polling(): | ||
period = 50 | ||
a = taurus.Attribute("eval:'test_polling'") | ||
|
||
class _Listener: | ||
def __init__(self): | ||
self.count = 0 | ||
|
||
def cb(self, *_): | ||
self.count += 1 | ||
|
||
listener = _Listener() | ||
a.addListener(listener.cb) | ||
pt = TaurusPollingTimer(period=period) | ||
pt.addAttribute(a) | ||
time.sleep(period * 4 / 1000.) | ||
assert listener.count > 0 | ||
assert listener.count < 7 | ||
|
||
|
||
def test_bug_1178(): | ||
""" | ||
Test if we are affected by https://github.com/taurus-org/taurus/issues/1178 | ||
""" | ||
a = taurus.Attribute("eval:1") | ||
pt = TaurusPollingTimer(period=500) # poll every 500ms | ||
with _Timeout(seconds=3, msg="Deadlock in TaurusPollingTimer (see #1178)"): | ||
for i in range(100): # this should finish in << 1s | ||
pt.addAttribute(a) | ||
print(i, end=" ") | ||
sys.stdout.flush() | ||
pt.removeAttribute(a) | ||
assert i == 99 | ||
|
||
|