Example #1
0
    def test_periodic_task_can_be_stopped(self):
        pt = PeriodicTask('Test', 0.5, self._collect_info_task)

        pt.start()
        sleep(0.01)
        pt.stop()
        sleep(1)
        assert pt.context['count'] == 1
Example #2
0
    def test_task_can_stop_itself(self):
        def auto_stop_task(pt):
            pt.stop()
            pt.context['count'] += 1

        pt = PeriodicTask('Test', 0.5, auto_stop_task)
        pt.context['count'] = 0

        with self.run_periodic_task_context(pt):
            sleep(1)
            assert pt.context['count'] == 1
Example #3
0
    def test_shared_context(self):
        def task(pt):
            # assert raised inside a task are ignored by pytest, but it'll
            # prevent the 2nd value to be set, so the test will fail anyway.
            assert pt.context['test #1'] == 9
            pt.context['test #2'] = 23

        pt = PeriodicTask('Test', 23, task)
        pt.context['test #1'] = 9
        with self.run_periodic_task_context(pt):
            sleep(0.01)
            assert pt.context['test #2'] == 23
Example #4
0
    def test_apply_now(self):
        def task(pt):
            pt.context['count'] += 1
            return pt.context['count']

        pt = PeriodicTask('test', 99, task)
        pt.context['count'] = 0

        with self.run_periodic_task_context(pt):
            sleep(0.01)
            promise = pt.apply_now()
            assert promise.result(0.01) is 2
Example #5
0
    def test_delay_can_be_changed(self):
        pt = PeriodicTask('Test', 0.5, self._collect_info_task)

        with self.run_periodic_task_context(pt):
            sleep(1.01)
            assert pt.context['count'] == 3
            pt.context['count'] = 0
            pt.delay = 0.1
            # Next execution is scheduled at 1.5s from pt.start() (or 500ms
            # from now). After that, the new delay will be used, so each 100ms
            # for 500ms.
            sleep(1)
            assert pt.context['count'] == 6
Example #6
0
    def test_apply_now_with_failure(self):
        class MyException(Exception):
            pass

        def task(pt):
            if pt.context['apply_now_mode']:
                raise MyException()

        pt = PeriodicTask('test', 99, task)
        pt.context['apply_now_mode'] = False
        with self.run_periodic_task_context(pt):
            sleep(0.01)
            pt.context['apply_now_mode'] = True
            promise = pt.apply_now()
            assert isinstance(promise.exception(), MyException)
Example #7
0
    def test_apply_now_when_task_is_running(self):
        def task(pt):
            pt.context['count'] += 1
            if pt.context['count'] == 1:
                # 2nd execution will start just after this one.
                pt.context['promise'] = pt.apply_now()

            return pt.context['count']

        pt = PeriodicTask('test', 99, task)
        pt.context['count'] = 0

        with self.run_periodic_task_context(pt):
            sleep(0.1)
            assert pt.context['count'] == 2
            assert pt.context['promise'].result(0.01) is 2