Skip to content

Commit

Permalink
fixed possible caching errors in AlarmProvider and AudioPauseProvider (
Browse files Browse the repository at this point in the history
…#321)

<!--- Provide a general summary of your changes in the title above -->
<!--- Link the corresponding issues after you created the pull request
-->

## Types of changes
<!--- What types of changes does your code introduce? Put an `x` in all
the boxes that apply: -->
- [x] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)

## Checklist:
<!--- Go over all the following points, and put an `x` in all the boxes
that apply. -->
<!--- If you're unsure about any of these, don't hesitate to ask. We're
here to help! -->
- [x] I have updated the [changelog](../CHANGELOG.md) accordingly.
- [ ] I have added tests to cover my changes.
  • Loading branch information
deichmab-draeger authored Jan 26, 2024
1 parent 9d4fdf9 commit 2d21baa
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 19 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- fixed a bug where comparing `ExtensionLocalValue` would fail when using the `!=` operator [#305](https://github.com/Draegerwerk/sdc11073/issues/305)
- fixed wrong typing info in LocalizedText
- added a safety sleep in consumer when starting http server
- fixed possible caching errors in AlarmProvider and AudioPauseProvider

### Changed
- new interface for transactions: split transaction into different kinds, e.g. descriptor_transaction, metric_state_transaction, etc.
Expand Down
16 changes: 7 additions & 9 deletions src/sdc11073/roles/alarmprovider.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,15 +389,14 @@ def _run_worker_job(self):

def _update_alert_system_state_current_alerts(self):
"""Update AlertSystemState present alarms list."""
states_needing_update = self._get_alert_system_states_needing_update()
if len(states_needing_update) > 0:
try:
with self._mdib.alert_state_transaction() as mgr:
try:
with self._mdib.alert_state_transaction() as mgr:
states_needing_update = self._get_alert_system_states_needing_update()
if len(states_needing_update) > 0:
tr_states = [mgr.get_state(s.DescriptorHandle) for s in states_needing_update]
self._update_alert_system_states(self._mdib, mgr, tr_states)
except Exception:
exc = traceback.format_exc()
self._logger.error('_checkAlertStates: %s', exc)
except Exception:
self._logger.error('_update_alert_system_state_current_alerts: %s', traceback.format_exc())

def _get_alert_system_states_needing_update(self) -> list[AbstractStateProtocol]:
""":return: all AlertSystemStateContainers of those last"""
Expand All @@ -416,6 +415,5 @@ def _get_alert_system_states_needing_update(self) -> list[AbstractStateProtocol]
if time.time() - last_self_check >= self_check_period - self.self_check_safety_margin:
states_needing_update.append(alert_system_state)
except Exception:
exc = traceback.format_exc()
self._logger.error('_get_alert_system_states_needing_update: %r', exc)
self._logger.error('_get_alert_system_states_needing_update: %r', traceback.format_exc())
return states_needing_update
15 changes: 10 additions & 5 deletions src/sdc11073/roles/audiopauseprovider.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,12 @@ def _set_global_audio_pause(self, params: ExecuteParameters) -> ExecuteResult:
"""
pm_types = self._mdib.data_model.pm_types
pm_names = self._mdib.data_model.pm_names
alert_system_descriptors = self._mdib.descriptions.NODETYPE.get(pm_names.AlertSystemDescriptor)
if alert_system_descriptors is None:
self._logger.error('SDC_SetAudioPauseOperation called, but no AlertSystemDescriptor in mdib found')
return ExecuteResult(params.operation_instance.operation_target_handle, InvocationState.FAILED)
with self._mdib.alert_state_transaction() as mgr:
alert_system_descriptors = self._mdib.descriptions.NODETYPE.get(pm_names.AlertSystemDescriptor)
if alert_system_descriptors is None:
self._logger.warning('SDC_SetAudioPauseOperation called, but no AlertSystemDescriptor in mdib found')
return ExecuteResult(params.operation_instance.operation_target_handle, InvocationState.FAILED)

for alert_system_descriptor in alert_system_descriptors:
_alert_system_state = mgr.get_state(alert_system_descriptor.Handle)
alert_system_state = cast(AlertSystemStateContainer, _alert_system_state)
Expand Down Expand Up @@ -129,8 +130,12 @@ def _cancel_global_audio_pause(self, params: ExecuteParameters) -> ExecuteResult
"""
pm_types = self._mdib.data_model.pm_types
pm_names = self._mdib.data_model.pm_names
alert_system_descriptors = self._mdib.descriptions.NODETYPE.get(pm_names.AlertSystemDescriptor)
with self._mdib.alert_state_transaction() as mgr:
alert_system_descriptors = self._mdib.descriptions.NODETYPE.get(pm_names.AlertSystemDescriptor)
if alert_system_descriptors is None:
self._logger.warning('SDC_SetAudioPauseOperation called, but no AlertSystemDescriptor in mdib found')
return ExecuteResult(params.operation_instance.operation_target_handle, InvocationState.FAILED)

for alert_system_descriptor in alert_system_descriptors:
_alert_system_state = mgr.get_state(alert_system_descriptor.Handle)
alert_system_state = cast(AlertSystemStateContainer, _alert_system_state)
Expand Down
27 changes: 22 additions & 5 deletions tests/test_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,11 @@ def test_audio_pause(self):
client_mdib = ConsumerMdib(self.sdc_client)
client_mdib.init_mdib()
coding = pm_types.Coding(NomenclatureCodes.MDC_OP_SET_ALL_ALARMS_AUDIO_PAUSE)
operation = self.sdc_device.mdib.descriptions.coding.get_one(coding)
future = set_service.activate(operation_handle=operation.Handle, arguments=None)
operation_pause = self.sdc_device.mdib.descriptions.coding.get_one(coding)
coding = pm_types.Coding(NomenclatureCodes.MDC_OP_SET_CANCEL_ALARMS_AUDIO_PAUSE)
operation_cancel = self.sdc_device.mdib.descriptions.coding.get_one(coding)

future = set_service.activate(operation_handle=operation_pause.Handle, arguments=None)
result = future.result(timeout=SET_TIMEOUT)
state = result.InvocationInfo.InvocationState
self.assertEqual(state, msg_types.InvocationState.FINISHED)
Expand All @@ -298,9 +301,7 @@ def test_audio_pause(self):
if alert_system_descriptor.Handle != alert_system_off:
self.assertEqual(state.SystemSignalActivation[0].State, pm_types.AlertActivation.PAUSED)

coding = pm_types.Coding(NomenclatureCodes.MDC_OP_SET_CANCEL_ALARMS_AUDIO_PAUSE)
operation = self.sdc_device.mdib.descriptions.coding.get_one(coding)
future = set_service.activate(operation_handle=operation.Handle, arguments=None)
future = set_service.activate(operation_handle=operation_cancel.Handle, arguments=None)
result = future.result(timeout=SET_TIMEOUT)
state = result.InvocationInfo.InvocationState
self.assertEqual(state, msg_types.InvocationState.FINISHED)
Expand All @@ -313,6 +314,22 @@ def test_audio_pause(self):
state = self.sdc_client.mdib.states.descriptor_handle.get_one(alert_system_descriptor.Handle)
self.assertEqual(state.SystemSignalActivation[0].State, pm_types.AlertActivation.ON)

# now remove all alert systems from provider mdib and verify that operation now fails
with self.sdc_device.mdib.descriptor_transaction() as mgr:
for descr in alert_system_descriptors:
mgr.remove_descriptor(descr.Handle)
future = set_service.activate(operation_handle=operation_pause.Handle, arguments=None)
result = future.result(timeout=SET_TIMEOUT)
state = result.InvocationInfo.InvocationState
self.assertEqual(state, msg_types.InvocationState.FAILED)

future = set_service.activate(operation_handle=operation_cancel.Handle, arguments=None)
result = future.result(timeout=SET_TIMEOUT)
state = result.InvocationInfo.InvocationState
self.assertEqual(state, msg_types.InvocationState.FAILED)



def test_audio_pause_two_clients(self):
alert_system_descriptors = self.sdc_device.mdib.descriptions.NODETYPE.get(pm.AlertSystemDescriptor)
self.assertTrue(alert_system_descriptors is not None)
Expand Down

0 comments on commit 2d21baa

Please sign in to comment.