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)
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)
def test_main_loop_sleep_spawn_called(self): wait_time = 0.2 sleep_time = rand_int() now_values = [parse('2019-12-23 22:19:03'), parse('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('gevent.spawn', spawn): with patch('zato.common.scheduler.Job.get_sleep_time', get_sleep_time): for now in now_values: self.now = now with patch('zato.common.scheduler.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()) sleep(0.2) 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(1, len(callback)) callback = callback[0] 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[:]
def test_main_loop_sleep_spawn_called(self): wait_time = 0.2 sleep_time = rand_int() now_values = [ parse('2019-12-23 22:19:03'), parse('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('gevent.spawn', spawn): with patch('zato.common.scheduler.Job.get_sleep_time', get_sleep_time): for now in now_values: self.now = now with patch('zato.common.scheduler.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()) sleep(0.2) 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(1, len(callback)) callback = callback[0] 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[:]