Esempio n. 1
0
    def test_schedule_called_once(self, method):
        method.side_effect = self.target_method

        job = scheduler_base.SchedulerJob(
            run_after=1,
            func_name=TARGET_METHOD_PATH,
            func_args={'name': 'task', 'id': '321'}
        )

        self.scheduler.schedule(job)

        self._wait_target_method_start()

        # Check that the persistent job has been created and captured.
        scheduled_jobs = db_api.get_scheduled_jobs()

        self.assertEqual(1, len(scheduled_jobs))

        captured_at = scheduled_jobs[0].captured_at

        self.assertIsNotNone(captured_at)
        self.assertTrue(
            datetime.datetime.utcnow() - captured_at <
            datetime.timedelta(seconds=3)
        )

        self._unlock_target_method()
        self._wait_target_method_end()

        method.assert_called_once_with(name='task', id='321')

        # After the job is processed the persistent object must be deleted.
        self._await(lambda: not db_api.get_scheduled_jobs())
Esempio n. 2
0
    def test_pickup_from_job_store(self, method):
        method.side_effect = self.target_method

        self.override_config('pickup_job_after', 1, 'scheduler')

        # 1. Create a scheduled job in Job Store.
        execute_at = datetime.datetime.utcnow() + datetime.timedelta(seconds=1)

        db_api.create_scheduled_job({
            'run_after': 1,
            'func_name': TARGET_METHOD_PATH,
            'func_args': {'name': 'task', 'id': '321'},
            'execute_at': execute_at,
            'captured_at': None,
            'auth_ctx': {}
        })

        self.assertTrue(len(db_api.get_scheduled_jobs()) > 0)

        self._unlock_target_method()
        self._wait_target_method_end()

        # 2. Wait till Scheduler picks up the job and processes it.
        self._await(lambda: not db_api.get_scheduled_jobs())

        method.assert_called_once_with(name='task', id='321')
Esempio n. 3
0
    def test_schedule_called_once(self, method):
        # Delegate from the module function to the method of the test class.
        method.side_effect = self.target_method

        job = scheduler_base.SchedulerJob(run_after=1,
                                          func_name=TARGET_METHOD_PATH,
                                          func_args={
                                              'name': 'task',
                                              'id': '321'
                                          })

        self.scheduler.schedule(job)

        self._wait_target_method_start()

        # Check that the persistent job has been created and captured.
        scheduled_jobs = db_api.get_scheduled_jobs()

        self.assertEqual(1, len(scheduled_jobs))

        captured_at = scheduled_jobs[0].captured_at

        self.assertIsNotNone(captured_at)
        self.assertTrue(datetime.datetime.utcnow() -
                        captured_at < datetime.timedelta(seconds=3))

        self._unlock_target_method()
        self._wait_target_method_end()

        method.assert_called_once_with(name='task', id='321')

        # After the job is processed the persistent object must be deleted.
        self._await(lambda: not db_api.get_scheduled_jobs())
Esempio n. 4
0
    def test_pickup_from_job_store(self, method):
        # Delegate from the module function to the method of the test class.
        method.side_effect = self.target_method

        self.override_config('pickup_job_after', 1, 'scheduler')

        # 1. Create a scheduled job in Job Store.
        execute_at = datetime.datetime.utcnow() + datetime.timedelta(seconds=1)

        db_api.create_scheduled_job({
            'run_after': 1,
            'func_name': TARGET_METHOD_PATH,
            'func_args': {
                'name': 'task',
                'id': '321'
            },
            'execute_at': execute_at,
            'captured_at': None,
            'auth_ctx': {}
        })

        self.assertEqual(1, len(db_api.get_scheduled_jobs()))

        self._unlock_target_method()
        self._wait_target_method_end()

        # 2. Wait till Scheduler picks up the job and processes it.
        self._await(lambda: not db_api.get_scheduled_jobs())

        method.assert_called_once_with(name='task', id='321')
Esempio n. 5
0
    def test_recapture_job(self, method):
        # Delegate from the module function to the method of the test class.
        method.side_effect = self.target_method

        self.override_config('pickup_job_after', 1, 'scheduler')
        self.override_config('captured_job_timeout', 3, 'scheduler')

        # 1. Create a scheduled job in Job Store marked as captured in one
        #    second in the future. It can be captured again only after 3
        #    seconds after that according to the config option.
        captured_at = datetime.datetime.utcnow() + datetime.timedelta(
            seconds=1)

        before_ts = datetime.datetime.utcnow()

        db_api.create_scheduled_job({
            'run_after': 1,
            'func_name': TARGET_METHOD_PATH,
            'func_args': {
                'name': 'task',
                'id': '321'
            },
            'execute_at': datetime.datetime.utcnow(),
            'captured_at': captured_at,
            'auth_ctx': {}
        })

        self.assertEqual(1, len(db_api.get_scheduled_jobs()))

        self._unlock_target_method()
        self._wait_target_method_end()

        # 2. Wait till Scheduler picks up the job and processes it.
        self._await(lambda: not db_api.get_scheduled_jobs())

        method.assert_called_once_with(name='task', id='321')

        # At least 3 seconds should have passed.
        self.assertTrue(datetime.datetime.utcnow() -
                        before_ts >= datetime.timedelta(seconds=3))
Esempio n. 6
0
    def test_recapture_job(self, method):
        method.side_effect = self.target_method

        self.override_config('pickup_job_after', 1, 'scheduler')
        self.override_config('captured_job_timeout', 3, 'scheduler')

        # 1. Create a scheduled job in Job Store marked as captured in one
        #    second in the future. It can be captured again only after 3
        #    seconds after that according to the config option.
        captured_at = datetime.datetime.utcnow() + datetime.timedelta(
            seconds=1
        )

        before_ts = datetime.datetime.utcnow()

        db_api.create_scheduled_job({
            'run_after': 1,
            'func_name': TARGET_METHOD_PATH,
            'func_args': {'name': 'task', 'id': '321'},
            'execute_at': datetime.datetime.utcnow(),
            'captured_at': captured_at,
            'auth_ctx': {}
        })

        self.assertTrue(len(db_api.get_scheduled_jobs()) > 0)

        self._unlock_target_method()
        self._wait_target_method_end()

        # 2. Wait till Scheduler picks up the job and processes it.
        self._await(lambda: not db_api.get_scheduled_jobs())

        method.assert_called_once_with(name='task', id='321')

        # At least 3 seconds should have passed.
        self.assertTrue(
            datetime.datetime.utcnow() - before_ts >=
            datetime.timedelta(seconds=3)
        )