Example #1
0
    def test_set_alarm_thread(self):
        """
        Test that the __set_alarm_thread private method will only launch an
        Alarm Thread if there is an active alarm.
        Ensure the thread has been launched successfully.
        This test accesses private methods.
        """
        alarm = AlarmItem(self.hour,
                          34,
                          days=(True, True, True, True, True, True, True),
                          enabled=False,
                          label='test',
                          alarm_id=96)
        alarm_mgr = AlarmManager()
        alarm_mgr.delete_all_alarms()
        numb_threads = threading.activeCount()

        # Test setting inactive alarm
        launch_success = alarm_mgr._AlarmManager__set_alarm_thread(alarm)
        self.assertFalse(launch_success)
        self.assertEqual(threading.activeCount(), numb_threads)

        # Test enabled alarm with no repeats
        alarm.repeat = (False, False, False, False, False, False, False)
        alarm.enabled = True
        launch_success = alarm_mgr._AlarmManager__set_alarm_thread(alarm)
        self.assertFalse(launch_success)
        self.assertEqual(threading.activeCount(), numb_threads)

        # Test fully active alarm
        alarm.wednesday = True
        launch_success = alarm_mgr._AlarmManager__set_alarm_thread(alarm)
        self.assertTrue(launch_success)
        self.assertGreater(threading.activeCount(), numb_threads)
Example #2
0
 def test_is_alarm_running(self):
     """ Adds a couple of alarms and tests the is_alarm_running() method. """
     alarm_active = AlarmItem(self.hour,
                              20,
                              enabled=True,
                              alarm_id=96,
                              days=(True, True, True, True, True, True,
                                    True))
     alarm_inactive = AlarmItem(self.hour,
                                20,
                                enabled=False,
                                alarm_id=86,
                                days=(False, False, False, False, False,
                                      False, False))
     alarm_mgr = AlarmManager()
     alarm_mgr.delete_all_alarms()
     launch_success = alarm_mgr._AlarmManager__set_alarm_thread(
         alarm_active)
     self.assertTrue(launch_success)
     launch_success = alarm_mgr._AlarmManager__set_alarm_thread(
         alarm_inactive)
     self.assertFalse(launch_success)
     # Now everything is set up, test the is_alarm_running method
     self.assertTrue(alarm_mgr.is_alarm_running(alarm_active.id_))
     self.assertFalse(alarm_mgr.is_alarm_running(alarm_inactive.id_))
Example #3
0
 def test_stop_all_alarm_threads(self):
     """
     Launches 2 alarms threads, checks they are running and them stops them
     all and checks again.
     """
     alarm_one = AlarmItem(self.hour,
                           34,
                           enabled=True,
                           alarm_id=31,
                           days=(False, False, False, False, False, False,
                                 True))
     alarm_two = AlarmItem(self.hour,
                           34,
                           enabled=True,
                           alarm_id=32,
                           days=(False, False, False, False, False, False,
                                 True))
     alarm_mgr = AlarmManager()
     # There is a bit of circular dependency here as delete all will execute
     # __stop_all_alarm_threads
     alarm_mgr.delete_all_alarms()
     launch_success = alarm_mgr._AlarmManager__set_alarm_thread(alarm_one)
     self.assertTrue(launch_success)
     launch_success = alarm_mgr._AlarmManager__set_alarm_thread(alarm_two)
     self.assertTrue(launch_success)
     numb_threads = threading.activeCount()
     self.assertGreaterEqual(numb_threads, 2)
     delete_success = alarm_mgr._AlarmManager__stop_all_alarm_threads()
     self.assertTrue(delete_success)
     self.assertEqual(threading.activeCount(), numb_threads - 2)
Example #4
0
    def test_set_alarm_thread_edit(self):
        """
        Tests the __set_alarm_thread when an existing alarm thread is inputted.
        No need to check for input with wrong ID as that gets dealt with in the
        AlarmThread class and unit test.
        """
        first_alarm = AlarmItem(self.hour,
                                34,
                                label='test',
                                alarm_id=96,
                                days=(True, True, True, True, True, True,
                                      True),
                                enabled=True)
        new_alarm = AlarmItem(self.hour,
                              34,
                              enabled=False,
                              alarm_id=96,
                              label='test replace',
                              days=(False, False, False, False, False, False,
                                    False))
        alarm_mgr = AlarmManager()
        alarm_mgr.delete_all_alarms()
        numb_threads = threading.activeCount()
        launch_success = alarm_mgr._AlarmManager__set_alarm_thread(first_alarm)
        self.assertTrue(launch_success)
        self.assertGreater(threading.activeCount(), numb_threads)

        # Editing to the new alarm data should stop the thread as it is inactive
        launch_success = alarm_mgr._AlarmManager__set_alarm_thread(new_alarm)
        self.assertFalse(launch_success)
        self.assertEqual(threading.activeCount(), numb_threads)
    def test_set_alarm_thread_edit(self):
        """
        Tests the __set_alarm_thread when an existing alarm thread is inputted.
        No need to check for input with wrong ID as that gets dealt with in the
        AlarmThread class and unit test.
        """
        first_alarm = AlarmItem(
            self.hour, 34, label="test", alarm_id=96, days=(True, True, True, True, True, True, True), enabled=True
        )
        new_alarm = AlarmItem(
            self.hour,
            34,
            enabled=False,
            alarm_id=96,
            label="test replace",
            days=(False, False, False, False, False, False, False),
        )
        alarm_mgr = AlarmManager()
        alarm_mgr.delete_all_alarms()
        numb_threads = threading.activeCount()
        launch_success = alarm_mgr._AlarmManager__set_alarm_thread(first_alarm)
        self.assertTrue(launch_success)
        self.assertGreater(threading.activeCount(), numb_threads)

        # Editing to the new alarm data should stop the thread as it is inactive
        launch_success = alarm_mgr._AlarmManager__set_alarm_thread(new_alarm)
        self.assertFalse(launch_success)
        self.assertEqual(threading.activeCount(), numb_threads)
    def test_set_alarm_thread(self):
        """
        Test that the __set_alarm_thread private method will only launch an
        Alarm Thread if there is an active alarm.
        Ensure the thread has been launched successfully.
        This test accesses private methods.
        """
        alarm = AlarmItem(
            self.hour, 34, days=(True, True, True, True, True, True, True), enabled=False, label="test", alarm_id=96
        )
        alarm_mgr = AlarmManager()
        alarm_mgr.delete_all_alarms()
        numb_threads = threading.activeCount()

        # Test setting inactive alarm
        launch_success = alarm_mgr._AlarmManager__set_alarm_thread(alarm)
        self.assertFalse(launch_success)
        self.assertEqual(threading.activeCount(), numb_threads)

        # Test enabled alarm with no repeats
        alarm.repeat = (False, False, False, False, False, False, False)
        alarm.enabled = True
        launch_success = alarm_mgr._AlarmManager__set_alarm_thread(alarm)
        self.assertFalse(launch_success)
        self.assertEqual(threading.activeCount(), numb_threads)

        # Test fully active alarm
        alarm.wednesday = True
        launch_success = alarm_mgr._AlarmManager__set_alarm_thread(alarm)
        self.assertTrue(launch_success)
        self.assertGreater(threading.activeCount(), numb_threads)
Example #7
0
 def test_alarm_trigger_callback(self):
     """
     Creates and alarm to trigger within a minute and check it has done so
     correctly.
     This test uses the static variable AlarmManagerTestCase.thread_alert to
     be be change it in the callback function, and exit an infinite loop.
     This test can take over 10 seconds in its worse case scenario.
     """
     # Set alarm for current minute, but only if there is at least 10 seconds
     # left for this minute. Better than setting it for the next minute.
     time_now = time.localtime(time.time())
     while time_now.tm_sec > 50:
         time.sleep(1)
         time_now = time.localtime(time.time())
     alarm = AlarmItem(time_now.tm_hour,
                       time_now.tm_min,
                       days=(True, True, True, True, True, True, True),
                       enabled=True,
                       alarm_id=96)
     alarm_mgr = AlarmManager(alert_callback=AlarmManagerTestCase.c)
     alarm_mgr.delete_all_alarms()
     launch_success = alarm_mgr._AlarmManager__set_alarm_thread(alarm)
     self.assertTrue(launch_success)
     while AlarmManagerTestCase.thread_alert is False:
         # This loop will never finish if the thread does not callback
         time.sleep(0.001)
     self.assertTrue(AlarmManagerTestCase.thread_alert)
 def test_alarm_trigger_callback(self):
     """
     Creates and alarm to trigger within a minute and check it has done so
     correctly.
     This test uses the static variable AlarmManagerTestCase.thread_alert to
     be be change it in the callback function, and exit an infinite loop.
     This test can take over 10 seconds in its worse case scenario.
     """
     # Set alarm for current minute, but only if there is at least 10 seconds
     # left for this minute. Better than setting it for the next minute.
     time_now = time.localtime(time.time())
     while time_now.tm_sec > 50:
         time.sleep(1)
         time_now = time.localtime(time.time())
     alarm = AlarmItem(
         time_now.tm_hour,
         time_now.tm_min,
         days=(True, True, True, True, True, True, True),
         enabled=True,
         alarm_id=96,
     )
     alarm_mgr = AlarmManager(alert_callback=AlarmManagerTestCase.c)
     alarm_mgr.delete_all_alarms()
     launch_success = alarm_mgr._AlarmManager__set_alarm_thread(alarm)
     self.assertTrue(launch_success)
     while AlarmManagerTestCase.thread_alert is False:
         # This loop will never finish if the thread does not callback
         time.sleep(0.001)
     self.assertTrue(AlarmManagerTestCase.thread_alert)
 def test_is_alarm_running(self):
     """ Adds a couple of alarms and tests the is_alarm_running() method. """
     alarm_active = AlarmItem(
         self.hour, 20, enabled=True, alarm_id=96, days=(True, True, True, True, True, True, True)
     )
     alarm_inactive = AlarmItem(
         self.hour, 20, enabled=False, alarm_id=86, days=(False, False, False, False, False, False, False)
     )
     alarm_mgr = AlarmManager()
     alarm_mgr.delete_all_alarms()
     launch_success = alarm_mgr._AlarmManager__set_alarm_thread(alarm_active)
     self.assertTrue(launch_success)
     launch_success = alarm_mgr._AlarmManager__set_alarm_thread(alarm_inactive)
     self.assertFalse(launch_success)
     # Now everything is set up, test the is_alarm_running method
     self.assertTrue(alarm_mgr.is_alarm_running(alarm_active.id_))
     self.assertFalse(alarm_mgr.is_alarm_running(alarm_inactive.id_))
Example #10
0
    def test_check_threads_state(self):
        """ Test almost all pathways of check_threads_state. """
        alarm_mgr = AlarmManager()
        self.create_alarms(alarm_mgr)
        check_result = alarm_mgr.check_threads_state()
        self.assertTrue(check_result)

        # Now that everything is working correctly, sneak around AlarmManager
        # and edit a running thread by editing an AlarmItem using the
        # AlarmThread internal reference to the AlarmItem instance (which does
        # not do the extra checks for tracking like AlarmManager does).
        # Also edit the database entry for that alarm bypassing AlarmManager.
        alarm_bypass = \
            alarm_mgr._AlarmManager__alarm_threads[0]._AlarmThread__alarm
        alarm_bypass.enabled = False
        alarm_id = alarm_bypass.id_
        AlarmDb().edit_alarm(alarm_id, enabled=False)
        check_result = alarm_mgr.check_threads_state()
        self.assertFalse(check_result)
        # Executing check_threads_state should have return false as there was
        # an issue, but if fixed correctly the second time should return true
        check_result = alarm_mgr.check_threads_state()
        self.assertTrue(check_result)

        # Now the thread for alarm 'alarm_bypass' with ID 'alarm_id' has been
        # stopped, we can bypass AlarmManager again to activate it and check
        # if check_threads_state recovers again.
        alarm_bypass.enabled = True
        AlarmDb().edit_alarm(alarm_bypass.id_, enabled=True)
        check_result = alarm_mgr.check_threads_state()
        self.assertFalse(check_result)
        check_result = alarm_mgr.check_threads_state()
        self.assertTrue(check_result)

        # Now that everything is working correctly once more, add extra thread
        alarm_test = AlarmItem(self.hour,
                               20,
                               enabled=True,
                               alarm_id=96,
                               days=(True, True, True, True, True, True, True))
        launch_success = alarm_mgr._AlarmManager__set_alarm_thread(alarm_test)
        self.assertTrue(launch_success)
        check_result = alarm_mgr.check_threads_state()
        self.assertFalse(check_result)
        check_result = alarm_mgr.check_threads_state()
        self.assertTrue(check_result)

        # Now we stop a running thread bypassing AlarmManager public methods
        alarm_mgr._AlarmManager__alarm_threads[0].stop()
        while alarm_mgr._AlarmManager__alarm_threads[0].isAlive():
            time.sleep(0.1)
        check_result = alarm_mgr.check_threads_state()
        self.assertFalse(check_result)
        check_result = alarm_mgr.check_threads_state()
        self.assertTrue(check_result)
    def test_check_threads_state(self):
        """ Test almost all pathways of check_threads_state. """
        alarm_mgr = AlarmManager()
        self.create_alarms(alarm_mgr)
        check_result = alarm_mgr.check_threads_state()
        self.assertTrue(check_result)

        # Now that everything is working correctly, sneak around AlarmManager
        # and edit a running thread by editing an AlarmItem using the
        # AlarmThread internal reference to the AlarmItem instance (which does
        # not do the extra checks for tracking like AlarmManager does).
        # Also edit the database entry for that alarm bypassing AlarmManager.
        alarm_bypass = alarm_mgr._AlarmManager__alarm_threads[0]._AlarmThread__alarm
        alarm_bypass.enabled = False
        alarm_id = alarm_bypass.id_
        AlarmDb().edit_alarm(alarm_id, enabled=False)
        check_result = alarm_mgr.check_threads_state()
        self.assertFalse(check_result)
        # Executing check_threads_state should have return false as there was
        # an issue, but if fixed correctly the second time should return true
        check_result = alarm_mgr.check_threads_state()
        self.assertTrue(check_result)

        # Now the thread for alarm 'alarm_bypass' with ID 'alarm_id' has been
        # stopped, we can bypass AlarmManager again to activate it and check
        # if check_threads_state recovers again.
        alarm_bypass.enabled = True
        AlarmDb().edit_alarm(alarm_bypass.id_, enabled=True)
        check_result = alarm_mgr.check_threads_state()
        self.assertFalse(check_result)
        check_result = alarm_mgr.check_threads_state()
        self.assertTrue(check_result)

        # Now that everything is working correctly once more, add extra thread
        alarm_test = AlarmItem(
            self.hour, 20, enabled=True, alarm_id=96, days=(True, True, True, True, True, True, True)
        )
        launch_success = alarm_mgr._AlarmManager__set_alarm_thread(alarm_test)
        self.assertTrue(launch_success)
        check_result = alarm_mgr.check_threads_state()
        self.assertFalse(check_result)
        check_result = alarm_mgr.check_threads_state()
        self.assertTrue(check_result)

        # Now we stop a running thread bypassing AlarmManager public methods
        alarm_mgr._AlarmManager__alarm_threads[0].stop()
        while alarm_mgr._AlarmManager__alarm_threads[0].isAlive():
            time.sleep(0.1)
        check_result = alarm_mgr.check_threads_state()
        self.assertFalse(check_result)
        check_result = alarm_mgr.check_threads_state()
        self.assertTrue(check_result)
 def test_stop_all_alarm_threads(self):
     """
     Launches 2 alarms threads, checks they are running and them stops them
     all and checks again.
     """
     alarm_one = AlarmItem(
         self.hour, 34, enabled=True, alarm_id=31, days=(False, False, False, False, False, False, True)
     )
     alarm_two = AlarmItem(
         self.hour, 34, enabled=True, alarm_id=32, days=(False, False, False, False, False, False, True)
     )
     alarm_mgr = AlarmManager()
     # There is a bit of circular dependency here as delete all will execute
     # __stop_all_alarm_threads
     alarm_mgr.delete_all_alarms()
     launch_success = alarm_mgr._AlarmManager__set_alarm_thread(alarm_one)
     self.assertTrue(launch_success)
     launch_success = alarm_mgr._AlarmManager__set_alarm_thread(alarm_two)
     self.assertTrue(launch_success)
     numb_threads = threading.activeCount()
     self.assertGreaterEqual(numb_threads, 2)
     delete_success = alarm_mgr._AlarmManager__stop_all_alarm_threads()
     self.assertTrue(delete_success)
     self.assertEqual(threading.activeCount(), numb_threads - 2)
 def test_stop_alarm_thread(self):
     """
     Test that the __stop_alarm_thread private method will stop a running
     Alarm Thread, which due to the nature of the thread code can take up to
     10 seconds.
     This test accesses private methods.
     """
     alarm = AlarmItem(
         self.hour, 34, enabled=True, label="t", alarm_id=96, days=(False, True, True, True, True, True, True)
     )
     alarm_mgr = AlarmManager()
     alarm_mgr.delete_all_alarms()
     numb_threads = threading.activeCount()
     launch_success = alarm_mgr._AlarmManager__set_alarm_thread(alarm)
     self.assertTrue(launch_success)
     self.assertGreater(threading.activeCount(), numb_threads)
     stop_success = alarm_mgr._AlarmManager__stop_alarm_thread(alarm.id_)
     self.assertTrue(stop_success)
     self.assertEqual(threading.activeCount(), numb_threads)
Example #14
0
 def test_stop_alarm_thread(self):
     """
     Test that the __stop_alarm_thread private method will stop a running
     Alarm Thread, which due to the nature of the thread code can take up to
     10 seconds.
     This test accesses private methods.
     """
     alarm = AlarmItem(self.hour,
                       34,
                       enabled=True,
                       label='t',
                       alarm_id=96,
                       days=(False, True, True, True, True, True, True))
     alarm_mgr = AlarmManager()
     alarm_mgr.delete_all_alarms()
     numb_threads = threading.activeCount()
     launch_success = alarm_mgr._AlarmManager__set_alarm_thread(alarm)
     self.assertTrue(launch_success)
     self.assertGreater(threading.activeCount(), numb_threads)
     stop_success = alarm_mgr._AlarmManager__stop_alarm_thread(alarm.id_)
     self.assertTrue(stop_success)
     self.assertEqual(threading.activeCount(), numb_threads)