예제 #1
0
    def test_on_job_executed_cb(self):

        data = {'runs':[], 'ctx':[]}

        def get_context():
            ctx = {'name': rand_string(), 'type':SCHEDULER.JOB_TYPE.INTERVAL_BASED}
            data['ctx'].append(ctx)
            return ctx

        def on_job_executed_cb(ctx):
            data['runs'].append(ctx)

        test_wait_time = 0.5
        job_sleep_time = 0.1
        job_max_repeats = 10

        job = Job(rand_int(), 'a', SCHEDULER.JOB_TYPE.INTERVAL_BASED, Interval(seconds=0.1), max_repeats=job_max_repeats)
        job.wait_sleep_time = job_sleep_time
        job.get_context = get_context

        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))
        scheduler.on_job_executed_cb = on_job_executed_cb

        scheduler.create(job, spawn=False)
        scheduler.run()

        self.assertEquals(len(data['runs']), len(data['ctx']))

        for idx, item in enumerate(data['runs']):
            self.assertEquals(data['ctx'][idx], item)
예제 #2
0
    def test_get_context(self):

        id = rand_int()
        name = rand_string()
        start_time = rand_date_utc()
        interval_in_seconds = rand_int()
        max_repeats_reached = rand_bool()
        current_run, max_repeats = rand_int(count=2)
        cb_kwargs = {rand_string(): rand_string()}

        for job_type in SCHEDULER.JOB_TYPE.INTERVAL_BASED, SCHEDULER.JOB_TYPE.CRON_STYLE:

            interval=Interval(in_seconds=interval_in_seconds) if \
                job_type == SCHEDULER.JOB_TYPE.INTERVAL_BASED else CronTab(DEFAULT_CRON_DEFINITION)

            job = Job(id,
                      name,
                      job_type,
                      cb_kwargs=cb_kwargs,
                      interval=interval)
            job.start_time = start_time
            job.current_run = current_run
            job.max_repeats = max_repeats
            job.max_repeats_reached = max_repeats_reached

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

            ctx = job.get_context()
            cid = ctx.pop('cid')

            self.assertTrue(is_like_cid(cid))

            expected = {
                'current_run': current_run,
                'id': id,
                'name': name,
                'start_time': start_time.isoformat(),
                'max_repeats': max_repeats,
                'max_repeats_reached': max_repeats_reached,
                'cb_kwargs': cb_kwargs,
                'type': job_type,
            }

            if job_type == SCHEDULER.JOB_TYPE.CRON_STYLE:
                expected['cron_definition'] = job.cron_definition
            else:
                expected['interval_in_seconds'] = job.interval.in_seconds

            self.assertDictEqual(ctx, expected)