def test_multirun(self):
        """
        Tests that the HardwareThread can be launched several times and that
        if one instance launches the thread, and another tries to do the same it
        will wait until it is done.
        This test can take over 8 seconds (2s per thread launch, 4 launches)
        """
        # These thread last 2 seconds
        hw_thread_instance_one = HardwareThread(
            lamp=(0, 1), room_light=(0, 1), coffee_time=0, total_time=2)
        hw_thread_instance_two = HardwareThread()

        # Mocking the hardware threads, they will finish as soon as they are
        # launched
        def mock_hw(cls):
            pass
        HardwareThread._launch_lamp = \
            types.MethodType(mock_hw, HardwareThread)
        HardwareThread._launch_room_light = \
            types.MethodType(mock_hw, HardwareThread)
        HardwareThread._launch_coffee = \
            types.MethodType(mock_hw, HardwareThread)

        # Launch the hardware thread, ensure it lasts 2 seconds
        start_time = time.time()
        hw_thread_instance_one.start()
        while hw_thread_instance_one.isAlive():
            pass
        end_time = time.time()
        self.assertAlmostEqual(2, end_time - start_time, delta=0.1)

        # Ensure the hardware thread can be launched multiple times
        start_time = time.time()
        hw_thread_instance_one.start()
        while hw_thread_instance_one.isAlive():
            pass
        end_time = time.time()
        self.assertAlmostEqual(2, end_time - start_time, delta=0.1)

        # Ensure the hardware thread can only be launched once at a time
        original_numb_threads = threading.activeCount()
        start_time = time.time()

        hw_thread_instance_one.start()
        time.sleep(0.2)  # Enough time for child threads launch and end
        hw_thread_count = threading.activeCount()
        self.assertEqual(original_numb_threads + 1, hw_thread_count)

        hw_thread_instance_two.start()
        self.assertEqual(hw_thread_count, threading.activeCount())
        while hw_thread_instance_two.isAlive():
            pass

        end_time = time.time()
        self.assertAlmostEqual(2*2, end_time - start_time, delta=0.1*2)
Beispiel #2
0
def main():
    minutes = lambda x: x * 60

    hardware_alert = HardwareThread(
        lamp=(0, minutes(3)), room_light=(minutes(2), minutes(13)), coffee_time=minutes(10), total_time=minutes(15)
    )

    hardware_alert.start()
    while hardware_alert.isAlive():
        sleep(1)
    print("Main LightUpHardware finished!")
Beispiel #3
0
def main():
    minutes = lambda x: x * 60

    hardware_alert = HardwareThread(lamp=(0, minutes(3)),
                                    room_light=(minutes(2), minutes(13)),
                                    coffee_time=minutes(10),
                                    total_time=minutes(15))

    hardware_alert.start()
    while hardware_alert.isAlive():
        sleep(1)
    print('Main LightUpHardware finished!')
    def test_run(self):
        """
        Because this unit test is designed to not require the hardware running,
        the methods that launch the threads to control the hw will be mocked.
        This also allows to check if the methods are called at the requested
        intervals.
        This test will take over 5 seconds.
        """
        if HardwareThread._HardwareThread__singleton is not None:
            HardwareThread._drop()

        lamp_start = 0
        lamp_duration = 2
        room_start = 1
        room_duration = 2
        coffee_time = 3
        total_time = 5
        start_time = 0

        # Mocking the _launch_lamp method
        def mock_launch_lamp(cls):
            self.launch_lamp_counter += 1
            now = time.time()
            self.assertAlmostEqual(now, start_time + lamp_start, delta=0.2)
        self.launch_lamp_counter = 0
        HardwareThread._launch_lamp = \
            types.MethodType(mock_launch_lamp, HardwareThread)

        # Mocking the _launch_room_light method
        def mock_launch_room_light(cls):
            self.launch_room_light_counter += 1
            now = time.time()
            self.assertAlmostEqual(now, start_time + room_start, delta=0.2)
        self.launch_room_light_counter = 0
        HardwareThread._launch_room_light = \
            types.MethodType(mock_launch_room_light, HardwareThread)

        # Mocking the _launch_coffee method
        def mock_launch_coffee(cls):
            self.launch_coffee_counter += 1
            now = time.time()
            self.assertAlmostEqual(now, start_time + coffee_time, delta=0.2)
        self.launch_coffee_counter = 0
        HardwareThread._launch_coffee = \
            types.MethodType(mock_launch_coffee, HardwareThread)

        def assert_thread_not_running():
            start_time = time.time()
            hw_thread_instance.start()
            while hw_thread_instance.isAlive():
                pass
            self.assertEqual(self.launch_lamp_counter, 0)
            self.assertEqual(self.launch_room_light_counter, 0)
            self.assertEqual(self.launch_coffee_counter, 0)

        # Test that thread will not run if variables are not set, stderr output
        hw_thread_instance = HardwareThread()
        with mock.patch('sys.stderr', new=io.StringIO()) as test_srderr:
            assert_thread_not_running()

            HardwareThread.lamp_time = lamp_start
            assert_thread_not_running()
            self.assert_stderr(test_srderr)

            HardwareThread.lamp_duration = lamp_duration
            assert_thread_not_running()
            self.assert_stderr(test_srderr)

            HardwareThread.room_light_time = room_start
            assert_thread_not_running()
            self.assert_stderr(test_srderr)

            HardwareThread.room_light_duration = room_duration
            assert_thread_not_running()
            self.assert_stderr(test_srderr)

            HardwareThread.coffee_time = coffee_time
            assert_thread_not_running()
            self.assert_stderr(test_srderr)

        HardwareThread.total_time = total_time

        # Now all variables set, it should run correctly
        start_time = time.time()
        hw_thread_instance.start()
        while hw_thread_instance.isAlive():
            pass
        end_time = time.time()
        self.assertAlmostEqual(total_time, end_time - start_time, delta=0.1)

        self.assertEqual(self.launch_lamp_counter, 1)
        self.assertEqual(self.launch_room_light_counter, 1)
        self.assertEqual(self.launch_coffee_counter, 1)