def build_watchdog_list(self, client, watchdog_queue): watchdogs_config = self._configuration.get_keys(['watchdogs']) watchdog_list = WatchdogList() for watchdog_config in watchdogs_config: topic, strategy, timeout, text = self._configuration.get_keys( ['topic', 'strategy', 'timeout', 'text'], watchdog_config ) strategy = getattr(library.watchdog.stragegy, strategy) watchdog = Watchdog( topic=topic, strategy=strategy(text), client=client, timeout=timeout ) watchdog.register_observer(watchdog_queue) watchdog_list.add(watchdog) try: enabler_config = self._configuration.get_keys(['enabler'], watchdog_config) topic, strategy = self._configuration.get_keys(['topic', 'strategy'], enabler_config) strategy = getattr(library.watchdog.stragegy, strategy) WatchdogEnabler( client=client, watchdog=watchdog, topic=topic, strategy=strategy('') ) except ConfigurationException: pass # there is no enabler configured return watchdog_list
def setUp(self): strategy = BinaryHighStrategy("The value is binary high") self.watch_dog = Watchdog(topic="/sensors/sensor0", strategy=strategy) self.notifier = Mock() self.message = Mock() self.message.topic = "/sensors/sensor0" self.message.payload = 0
class WatchDogTest(TestCase): def setUp(self): strategy = BinaryHighStrategy("The value is binary high") self.watch_dog = Watchdog(topic="/sensors/sensor0", strategy=strategy) self.notifier = Mock() self.message = Mock() self.message.topic = "/sensors/sensor0" self.message.payload = 0 def test_alarm(self): assert not self.watch_dog.is_alarm self.message.payload = 1 self.watch_dog.notify(notifier=self.notifier, message=self.message) assert not self.watch_dog.is_alarm self.message.payload = 0 self.watch_dog.notify(notifier=self.notifier, message=self.message) assert self.watch_dog.is_alarm def test_disable_enable(self): assert not self.watch_dog.is_alarm self.watch_dog.disable() self.watch_dog.notify(notifier=self.notifier, message=self.message) assert not self.watch_dog.is_alarm self.watch_dog.enable() assert self.watch_dog.is_alarm def test_notify_observers(self): observer = Mock(spec=Observer) self.watch_dog.register_observer(observer) self.watch_dog.notify(notifier=self.notifier, message=self.message) self.watch_dog.notify(notifier=self.notifier, message=self.message) observer.notify.assert_called_once_with(notifier=self.watch_dog) observer.notify.reset_mock() self.message.payload = 1 self.watch_dog.notify(notifier=self.notifier, message=self.message) observer.notify.assert_called_once_with(notifier=self.watch_dog) observer.notify.reset_mock() self.message.payload = 0 self.watch_dog.notify(notifier=self.notifier, message=self.message) observer.notify.assert_called_once_with(notifier=self.watch_dog)