Skip to content
Snippets Groups Projects
Commit 060ddf8d authored by Michi Hostettler's avatar Michi Hostettler :coffee:
Browse files

Merge branch 'feature/unsubscribe-specific' into 'master'

Selectors to start/stop subscriptions and ability to unsubscribe from a specific parameter

See merge request !7
parents fe76e04e 022c9f22
No related branches found
No related tags found
1 merge request!7Selectors to start/stop subscriptions and ability to unsubscribe from a specific parameter
......@@ -7,3 +7,14 @@ Full documentation and examples are available on the Scripting Tools wiki:
https://wikis.cern.ch/display/ST/PyJapc
Testing
=======
To run test, install required libraries:
pip install .[testing]
Then run tests
pytest .
......@@ -885,6 +885,12 @@ class PyJapc(object):
# Create a unique string key for this subscription
# --------------------------------------------------------------------
parameterKey = self._getDictKeyFromParameterName(parameterName)
try:
sel = kwargs['timingSelectorOverride']
except KeyError:
sel = None
parameterKey = self._transformSubscribeCacheKey(parameterKey=parameterKey,
selector=sel)
# --------------------------------------------------------------------
# Check if SubscriptionHandle exist already
......@@ -993,16 +999,25 @@ class PyJapc(object):
return sh
def stopSubscriptions(self, parameterName=None):
def stopSubscriptions(self, parameterName=None, selector=None):
"""Stop Monitoring on all previously subscribed parameters.
Args:
parameterName (Optional[str]): If not None, only the subscription
of this particular parameter will be stopped.
selector (Optional[str]): If not None, it augments the parameterName to stop
subscription for the particular selector only.
"""
if parameterName is not None:
sh = self._subscriptionHandleDict[parameterName]
sh.stopMonitoring()
if selector is not None:
key = self._transformSubscribeCacheKey(parameterKey=parameterName, selector=selector)
sh = self._subscriptionHandleDict[key]
sh.stopMonitoring()
else:
for name, sh in self._subscriptionHandleDict.items():
# undefined selector will stop subscriptions to all selectors of this parameter
if name == parameterName or name.startswith(parameterName + '@'):
sh.stopMonitoring()
else:
for pN, sh in self._subscriptionHandleDict.items():
sh.stopMonitoring()
......@@ -1016,16 +1031,25 @@ class PyJapc(object):
self.stopSubscriptions()
self._subscriptionHandleDict.clear()
def startSubscriptions(self, parameterName=None):
def startSubscriptions(self, parameterName=None, selector=None):
"""Start Monitoring on all previously Subscribed Parameters.
Args:
parameterName (Optional[str]): If not None, only the subscription
of this particular parameter will restarted.
selector (Optional[str]): If not None, it augments the parameterName to stop
subscription for the particular selector only.
"""
if parameterName is not None:
sh = self._subscriptionHandleDict[parameterName]
sh.startMonitoring()
if selector is not None:
key = self._transformSubscribeCacheKey(parameterKey=parameterName, selector=selector)
sh = self._subscriptionHandleDict[key]
sh.startMonitoring()
else:
for name, sh in self._subscriptionHandleDict.items():
# undefined selector will make subscriptions to all selectors of this parameter
if name == parameterName or name.startswith(parameterName + '@'):
sh.startMonitoring()
else:
for pN, sh in self._subscriptionHandleDict.items():
sh.startMonitoring()
......@@ -1427,4 +1451,7 @@ class PyJapc(object):
headerDict["selector"] = None
return headerDict
def _transformSubscribeCacheKey(self, parameterKey, selector):
return parameterKey + '@' + selector if selector is not None else parameterKey
# EOF
......@@ -117,6 +117,9 @@ setuptools.setup(
'six',
'pytz'
],
extras_require={
'testing': ['pytest'],
},
cmdclass={
'install': install,
'deploy_sphinx': deploy_sphinx
......
......@@ -125,3 +125,200 @@ def test_set_value():
japc.setSelector('LHC.USER.TEST')
japc.setParam('TEST/TestProperty', 1.0, checkDims=False)
verify(mock).setValue(sel('LHC.USER.TEST'), spv(1.0))
def japc_subscription_cleanup():
japc.stopSubscriptions()
japc._subscriptionHandleDict = {}
def test_multiple_selectors_same_no_param():
param1 = 'TEST/TestProperty1'
param2 = 'TEST/TestProperty2'
mocj1 = mockParameter(param1)
mocj2 = mockParameter(param2)
japc.subscribeParam(parameterName='TEST/TestProperty1', timingSelectorOverride='LHC.USER.TEST1')
japc.subscribeParam(parameterName='TEST/TestProperty1', timingSelectorOverride='LHC.USER.TEST2')
japc.subscribeParam(parameterName='TEST/TestProperty2', timingSelectorOverride='LHC.USER.TEST1')
assert 'TEST/TestProperty1@LHC.USER.TEST1' in japc._subscriptionHandleDict
assert 'TEST/TestProperty1@LHC.USER.TEST2' in japc._subscriptionHandleDict
assert 'TEST/TestProperty2@LHC.USER.TEST1' in japc._subscriptionHandleDict
assert not japc._subscriptionHandleDict['TEST/TestProperty1@LHC.USER.TEST1'].isMonitoring()
assert not japc._subscriptionHandleDict['TEST/TestProperty1@LHC.USER.TEST2'].isMonitoring()
assert not japc._subscriptionHandleDict['TEST/TestProperty2@LHC.USER.TEST1'].isMonitoring()
japc.startSubscriptions()
assert japc._subscriptionHandleDict['TEST/TestProperty1@LHC.USER.TEST1'].isMonitoring()
assert japc._subscriptionHandleDict['TEST/TestProperty1@LHC.USER.TEST2'].isMonitoring()
assert japc._subscriptionHandleDict['TEST/TestProperty2@LHC.USER.TEST1'].isMonitoring()
japc.stopSubscriptions()
assert not japc._subscriptionHandleDict['TEST/TestProperty1@LHC.USER.TEST1'].isMonitoring()
assert not japc._subscriptionHandleDict['TEST/TestProperty1@LHC.USER.TEST2'].isMonitoring()
assert not japc._subscriptionHandleDict['TEST/TestProperty2@LHC.USER.TEST1'].isMonitoring()
japc_subscription_cleanup()
def test_multiple_selectors_same_no_selector_on_subscribe():
param1 = 'TEST/TestProperty1'
param2 = 'TEST/TestProperty2'
mocj1 = mockParameter(param1)
mocj2 = mockParameter(param2)
japc.subscribeParam(parameterName='TEST/TestProperty1', timingSelectorOverride='LHC.USER.TEST1')
japc.subscribeParam(parameterName='TEST/TestProperty1', timingSelectorOverride='LHC.USER.TEST2')
japc.subscribeParam(parameterName='TEST/TestProperty2', timingSelectorOverride='LHC.USER.TEST1')
assert 'TEST/TestProperty1@LHC.USER.TEST1' in japc._subscriptionHandleDict
assert 'TEST/TestProperty1@LHC.USER.TEST2' in japc._subscriptionHandleDict
assert 'TEST/TestProperty2@LHC.USER.TEST1' in japc._subscriptionHandleDict
assert not japc._subscriptionHandleDict['TEST/TestProperty1@LHC.USER.TEST1'].isMonitoring()
assert not japc._subscriptionHandleDict['TEST/TestProperty1@LHC.USER.TEST2'].isMonitoring()
assert not japc._subscriptionHandleDict['TEST/TestProperty2@LHC.USER.TEST1'].isMonitoring()
japc.startSubscriptions('TEST/TestProperty1')
assert japc._subscriptionHandleDict['TEST/TestProperty1@LHC.USER.TEST1'].isMonitoring()
assert japc._subscriptionHandleDict['TEST/TestProperty1@LHC.USER.TEST2'].isMonitoring()
assert not japc._subscriptionHandleDict['TEST/TestProperty2@LHC.USER.TEST1'].isMonitoring()
japc_subscription_cleanup()
def test_multiple_selectors_same_no_selector_on_unsubscribe():
param1 = 'TEST/TestProperty1'
param2 = 'TEST/TestProperty2'
mocj1 = mockParameter(param1)
mocj2 = mockParameter(param2)
japc.subscribeParam(parameterName='TEST/TestProperty1', timingSelectorOverride='LHC.USER.TEST1')
japc.subscribeParam(parameterName='TEST/TestProperty1', timingSelectorOverride='LHC.USER.TEST2')
japc.subscribeParam(parameterName='TEST/TestProperty2', timingSelectorOverride='LHC.USER.TEST1')
assert 'TEST/TestProperty1@LHC.USER.TEST1' in japc._subscriptionHandleDict
assert 'TEST/TestProperty1@LHC.USER.TEST2' in japc._subscriptionHandleDict
assert 'TEST/TestProperty2@LHC.USER.TEST1' in japc._subscriptionHandleDict
assert not japc._subscriptionHandleDict['TEST/TestProperty1@LHC.USER.TEST1'].isMonitoring()
assert not japc._subscriptionHandleDict['TEST/TestProperty1@LHC.USER.TEST2'].isMonitoring()
assert not japc._subscriptionHandleDict['TEST/TestProperty2@LHC.USER.TEST1'].isMonitoring()
japc.startSubscriptions()
assert japc._subscriptionHandleDict['TEST/TestProperty1@LHC.USER.TEST1'].isMonitoring()
assert japc._subscriptionHandleDict['TEST/TestProperty1@LHC.USER.TEST2'].isMonitoring()
assert japc._subscriptionHandleDict['TEST/TestProperty2@LHC.USER.TEST1'].isMonitoring()
japc.stopSubscriptions('TEST/TestProperty1')
assert not japc._subscriptionHandleDict['TEST/TestProperty1@LHC.USER.TEST1'].isMonitoring()
assert not japc._subscriptionHandleDict['TEST/TestProperty1@LHC.USER.TEST2'].isMonitoring()
assert japc._subscriptionHandleDict['TEST/TestProperty2@LHC.USER.TEST1'].isMonitoring()
japc_subscription_cleanup()
def test_multiple_selectors_same_with_selector_on_subscribe():
param1 = 'TEST/TestProperty1'
param2 = 'TEST/TestProperty2'
mocj1 = mockParameter(param1)
mocj2 = mockParameter(param2)
japc.subscribeParam(parameterName='TEST/TestProperty1', timingSelectorOverride='LHC.USER.TEST1')
japc.subscribeParam(parameterName='TEST/TestProperty1', timingSelectorOverride='LHC.USER.TEST2')
japc.subscribeParam(parameterName='TEST/TestProperty2', timingSelectorOverride='LHC.USER.TEST1')
assert 'TEST/TestProperty1@LHC.USER.TEST1' in japc._subscriptionHandleDict
assert 'TEST/TestProperty1@LHC.USER.TEST2' in japc._subscriptionHandleDict
assert 'TEST/TestProperty2@LHC.USER.TEST1' in japc._subscriptionHandleDict
assert not japc._subscriptionHandleDict['TEST/TestProperty1@LHC.USER.TEST1'].isMonitoring()
assert not japc._subscriptionHandleDict['TEST/TestProperty1@LHC.USER.TEST2'].isMonitoring()
assert not japc._subscriptionHandleDict['TEST/TestProperty2@LHC.USER.TEST1'].isMonitoring()
japc.startSubscriptions(parameterName='TEST/TestProperty1', selector='LHC.USER.TEST1')
assert japc._subscriptionHandleDict['TEST/TestProperty1@LHC.USER.TEST1'].isMonitoring()
assert not japc._subscriptionHandleDict['TEST/TestProperty1@LHC.USER.TEST2'].isMonitoring()
assert not japc._subscriptionHandleDict['TEST/TestProperty2@LHC.USER.TEST1'].isMonitoring()
japc_subscription_cleanup()
def test_multiple_selectors_same_with_selector_on_unsubscribe():
param1 = 'TEST/TestProperty1'
param2 = 'TEST/TestProperty2'
mocj1 = mockParameter(param1)
mocj2 = mockParameter(param2)
japc.subscribeParam(parameterName='TEST/TestProperty1', timingSelectorOverride='LHC.USER.TEST1')
japc.subscribeParam(parameterName='TEST/TestProperty1', timingSelectorOverride='LHC.USER.TEST2')
japc.subscribeParam(parameterName='TEST/TestProperty2', timingSelectorOverride='LHC.USER.TEST1')
assert 'TEST/TestProperty1@LHC.USER.TEST1' in japc._subscriptionHandleDict
assert 'TEST/TestProperty1@LHC.USER.TEST2' in japc._subscriptionHandleDict
assert 'TEST/TestProperty2@LHC.USER.TEST1' in japc._subscriptionHandleDict
assert not japc._subscriptionHandleDict['TEST/TestProperty1@LHC.USER.TEST1'].isMonitoring()
assert not japc._subscriptionHandleDict['TEST/TestProperty1@LHC.USER.TEST2'].isMonitoring()
assert not japc._subscriptionHandleDict['TEST/TestProperty2@LHC.USER.TEST1'].isMonitoring()
japc.startSubscriptions()
assert japc._subscriptionHandleDict['TEST/TestProperty1@LHC.USER.TEST1'].isMonitoring()
assert japc._subscriptionHandleDict['TEST/TestProperty1@LHC.USER.TEST2'].isMonitoring()
assert japc._subscriptionHandleDict['TEST/TestProperty2@LHC.USER.TEST1'].isMonitoring()
japc.stopSubscriptions(parameterName='TEST/TestProperty1', selector='LHC.USER.TEST1')
assert not japc._subscriptionHandleDict['TEST/TestProperty1@LHC.USER.TEST1'].isMonitoring()
assert japc._subscriptionHandleDict['TEST/TestProperty1@LHC.USER.TEST2'].isMonitoring()
assert japc._subscriptionHandleDict['TEST/TestProperty2@LHC.USER.TEST1'].isMonitoring()
japc_subscription_cleanup()
def test_subscribe_specific_no_selectors():
param1 = 'TEST/TestProperty1'
param2 = 'TEST/TestProperty1'
mocj1 = mockParameter(param1)
mocj2 = mockParameter(param2)
japc.setSelector('LHC.USER.TEST')
japc.subscribeParam('TEST/TestProperty1')
japc.subscribeParam('TEST/TestProperty2')
assert 'TEST/TestProperty1' in japc._subscriptionHandleDict
assert 'TEST/TestProperty2' in japc._subscriptionHandleDict
assert not japc._subscriptionHandleDict['TEST/TestProperty1'].isMonitoring()
assert not japc._subscriptionHandleDict['TEST/TestProperty2'].isMonitoring()
japc.startSubscriptions('TEST/TestProperty1')
assert japc._subscriptionHandleDict['TEST/TestProperty1'].isMonitoring()
assert not japc._subscriptionHandleDict['TEST/TestProperty2'].isMonitoring()
japc_subscription_cleanup()
def test_subscribe_all_no_selectors():
param1 = 'TEST/TestProperty1'
param2 = 'TEST/TestProperty1'
mocj1 = mockParameter(param1)
mocj2 = mockParameter(param2)
japc.setSelector('LHC.USER.TEST')
japc.subscribeParam('TEST/TestProperty1')
japc.subscribeParam('TEST/TestProperty2')
assert 'TEST/TestProperty1' in japc._subscriptionHandleDict
assert 'TEST/TestProperty2' in japc._subscriptionHandleDict
assert not japc._subscriptionHandleDict['TEST/TestProperty1'].isMonitoring()
assert not japc._subscriptionHandleDict['TEST/TestProperty2'].isMonitoring()
japc.startSubscriptions()
assert japc._subscriptionHandleDict['TEST/TestProperty1'].isMonitoring()
assert japc._subscriptionHandleDict['TEST/TestProperty2'].isMonitoring()
japc_subscription_cleanup()
def test_unsubscribe_specific_no_selectors():
param1 = 'TEST/TestProperty1'
param2 = 'TEST/TestProperty1'
mocj1 = mockParameter(param1)
mocj2 = mockParameter(param2)
japc.setSelector('LHC.USER.TEST')
japc.subscribeParam('TEST/TestProperty1')
japc.subscribeParam('TEST/TestProperty2')
japc.startSubscriptions()
assert 'TEST/TestProperty1' in japc._subscriptionHandleDict
assert 'TEST/TestProperty2' in japc._subscriptionHandleDict
assert japc._subscriptionHandleDict['TEST/TestProperty1'].isMonitoring()
assert japc._subscriptionHandleDict['TEST/TestProperty2'].isMonitoring()
japc.stopSubscriptions('TEST/TestProperty1')
assert 'TEST/TestProperty1' in japc._subscriptionHandleDict
assert 'TEST/TestProperty2' in japc._subscriptionHandleDict
assert not japc._subscriptionHandleDict['TEST/TestProperty1'].isMonitoring()
assert japc._subscriptionHandleDict['TEST/TestProperty2'].isMonitoring()
japc_subscription_cleanup()
def test_unsubscribe_all_no_selectors():
param1 = 'TEST/TestProperty1'
param2 = 'TEST/TestProperty1'
mocj1 = mockParameter(param1)
mocj2 = mockParameter(param2)
japc.setSelector('LHC.USER.TEST')
japc.subscribeParam('TEST/TestProperty1')
japc.subscribeParam('TEST/TestProperty2')
japc.startSubscriptions()
assert 'TEST/TestProperty1' in japc._subscriptionHandleDict
assert 'TEST/TestProperty2' in japc._subscriptionHandleDict
assert japc._subscriptionHandleDict['TEST/TestProperty1'].isMonitoring()
assert japc._subscriptionHandleDict['TEST/TestProperty2'].isMonitoring()
japc.stopSubscriptions()
assert 'TEST/TestProperty1' in japc._subscriptionHandleDict
assert 'TEST/TestProperty2' in japc._subscriptionHandleDict
assert not japc._subscriptionHandleDict['TEST/TestProperty1'].isMonitoring()
assert not japc._subscriptionHandleDict['TEST/TestProperty2'].isMonitoring()
japc_subscription_cleanup()
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment