Пример #1
0
    def test_clone(self):

        interval = Interval(seconds=5)
        start_time = datetime.utcnow()

        def callback():
            pass

        def on_max_repeats_reached_cb():
            pass

        job = Job(rand_int(), 'a', SCHEDULER.JOB_TYPE.INTERVAL_BASED, interval, start_time)
        job.callback = callback
        job.on_max_repeats_reached_cb = on_max_repeats_reached_cb

        clone = job.clone()
        sleep(0.1)

        for name in 'name', 'interval', 'cb_kwargs', 'max_repeats', 'is_active':
            expected = getattr(job, name)
            given = getattr(clone, name)
            self.assertEquals(expected, given, '{} != {} ({})'.format(expected, given, name))

        self.assertEquals(job.start_time, clone.start_time)

        self.assertIs(job.callback, clone.callback)
        self.assertIs(job.on_max_repeats_reached_cb, clone.on_max_repeats_reached_cb)
Пример #2
0
    def test_edit(self):

        def callback():
            pass

        def on_max_repeats_reached_cb():
            pass

        start_time = datetime.utcnow()
        test_wait_time = 0.5
        job_interval1, job_interval2 = 2, 3
        job_sleep_time = 10
        job_max_repeats1, job_max_repeats2 = 20, 30

        scheduler = Scheduler(get_scheduler_config(), None)
        scheduler.lock = RLock()
        scheduler.iter_cb = iter_cb
        scheduler.iter_cb_args = (scheduler, datetime.utcnow() + timedelta(seconds=test_wait_time))

        def check(scheduler, job, label):
            self.assertIn(job.name, scheduler.job_greenlets)
            self.assertIn(job, scheduler.jobs)

            self.assertEquals(1, len(scheduler.job_greenlets))
            self.assertEquals(1, len(scheduler.jobs))

            self.assertIs(job.run.im_func, scheduler.job_greenlets.values()[0]._run.im_func)

            clone = list(scheduler.jobs)[0]

            for name in 'name', 'interval', 'cb_kwargs', 'max_repeats', 'is_active':
                expected = getattr(job, name)
                given = getattr(clone, name)
                self.assertEquals(expected, given, '{} != {} ({})'.format(expected, given, name))

            job_cb = job.callback
            clone_cb = clone.callback

            job_on_max_cb = job.on_max_repeats_reached_cb
            clone_on_max_cb = clone.on_max_repeats_reached_cb

            if label == 'first':
                self.assertEquals(job.start_time, clone.start_time)
                self.assertIs(job_cb.im_func, clone_cb.im_func)
                self.assertIs(job_on_max_cb.im_func, clone_on_max_cb.im_func)

            else:
                self.assertEquals(job.start_time, clone.start_time)
                self.assertIs(clone_cb.im_func, scheduler.on_job_executed.im_func)
                self.assertIs(clone_on_max_cb.im_func, scheduler.on_max_repeats_reached.im_func)

        job1 = Job(rand_int(), 'a', SCHEDULER.JOB_TYPE.INTERVAL_BASED, Interval(seconds=job_interval1), start_time, max_repeats=job_max_repeats1)
        job1.callback = callback
        job1.on_max_repeats_reached_cb = on_max_repeats_reached_cb
        job1.wait_sleep_time = job_sleep_time

        job2 = Job(rand_int(), 'a', SCHEDULER.JOB_TYPE.INTERVAL_BASED, Interval(seconds=job_interval2), start_time, max_repeats=job_max_repeats2)
        job2.callback = callback
        job2.on_max_repeats_reached_cb = on_max_repeats_reached_cb
        job2.wait_sleep_time = job_sleep_time

        scheduler.run()
        scheduler.create(job1)

        sleep(test_wait_time)

        # We have only job1 at this point
        check(scheduler, job1, 'first')

        # Removes job1 along the way ..
        scheduler.edit(job2)

        # .. so now job2 is the now removed job1.
        check(scheduler, job2, 'second')
Пример #3
0
    def test_main_loop_sleep_spawn_called(self):

        wait_time = 0.2
        sleep_time = rand_int()

        now_values = [parse_datetime('2019-12-23 22:19:03'), parse_datetime('2021-05-13 17:35:48')]

        sleep_history = []
        spawn_history = []
        sleep_time_history = []

        def sleep(value):
            if value != wait_time:
                sleep_history.append(value)

        def spawn(*args, **kwargs):
            spawn_history.append([args, kwargs])

        def get_sleep_time(*args, **kwargs):
            sleep_time_history.append(args[1])
            return sleep_time

        with patch('gevent.sleep', sleep):
            with patch('zato.scheduler.backend.Job._spawn', spawn):
                with patch('zato.scheduler.backend.Job.get_sleep_time', get_sleep_time):
                    for now in now_values:
                        self.now = now
                        with patch('zato.scheduler.backend.datetime', self._datetime):
                            for job_type in SCHEDULER.JOB_TYPE.CRON_STYLE, SCHEDULER.JOB_TYPE.INTERVAL_BASED:

                                max_repeats = choice(range(2, 5))

                                cb_kwargs = {
                                    rand_string():rand_string(),
                                    rand_string():rand_string()
                                }

                                interval = Interval(seconds=sleep_time) if job_type == SCHEDULER.JOB_TYPE.INTERVAL_BASED \
                                    else CronTab(DEFAULT_CRON_DEFINITION)

                                job = Job(rand_int(), rand_string(), job_type, interval, max_repeats=max_repeats)

                                if job.type == SCHEDULER.JOB_TYPE.CRON_STYLE:
                                    job.cron_definition = DEFAULT_CRON_DEFINITION

                                job.cb_kwargs = cb_kwargs
                                job.start_time = datetime.utcnow()
                                job.callback = dummy_callback

                                self.assertTrue(job.main_loop())
                                time.sleep(0.5)

                                self.assertEquals(max_repeats, len(sleep_history))
                                self.assertEquals(max_repeats, len(spawn_history))

                                for item in sleep_history:
                                    self.assertEquals(sleep_time, item)

                                for idx, (callback, ctx_dict) in enumerate(spawn_history, 1):
                                    self.assertEquals(2, len(callback))
                                    callback = callback[1]
                                    self.check_ctx(
                                        ctx_dict['ctx'], job, sleep_time, max_repeats, idx, cb_kwargs, len(spawn_history), job_type)
                                    self.assertIs(callback, dummy_callback)

                                del sleep_history[:]
                                del spawn_history[:]

                            for item in sleep_time_history:
                                self.assertEquals(item, now)

                            del sleep_time_history[:]