def test_base_case(self): cron = Cron() cron.schedule( Tab('two_sec', verbose=False).every(seconds=2).run(time_logger, 'two_sec'), Tab('three_sec', verbose=False).every(seconds=3).run(time_logger, 'three_sec')) with PrintCatcher(stream='stdout') as stdout_catcher: cron.go(max_seconds=6) base_lookup = { 'three_sec': 3, 'two_sec': 2, } lines = list(stdout_catcher.text.split('\n')) # make sure times fall int right slots for line in lines: if line: words = line.split() name = words[0] time = parse('T'.join(words[1:])) self.assertEqual(time.second % base_lookup[name], 0) # make sure the tasks were run the proper number of times counter = Counter() for line in lines: if line: counter.update({line.split()[0]: 1}) self.assertEqual(counter['two_sec'], 3) self.assertEqual(counter['three_sec'], 2)
def test_non_robust_case(self): then = datetime.datetime.now() def timed_error(): now = datetime.datetime.now() if then + datetime.timedelta( seconds=3) < now < then + datetime.timedelta(seconds=6): print('timed_error_failure') raise ExpectedException( 'This exception is expected in tests. Don\'t worry about it.' ) else: print('timed_error_success') cron = Cron() cron.schedule( Tab('one_sec', verbose=False).every(seconds=1).run(time_logger, 'running_time_logger'), Tab('two_sec', verbose=False, robust=False).every(seconds=1).run(timed_error)) with PrintCatcher(stream='stdout') as catcher: cron.go(max_seconds=10) success_count = catcher.text.count('timed_error_success') failure_count = catcher.text.count('timed_error_failure') time_logger_count = catcher.text.count('running_time_logger') self.assertEqual(success_count, 3) self.assertEqual(failure_count, 1) self.assertEqual(time_logger_count, 10)
def test_non_robust_case(self): then = datetime.datetime.now() cron = Cron() cron.schedule( Tab('one_sec', verbose=False).every(seconds=1).run(time_logger, 'running_time_logger'), Tab('two_sec', verbose=False, robust=False).every(seconds=1).run( functools.partial(timed_error, then))) with PrintCatcher(stream='stdout') as catcher: cron.go(max_seconds=10) success_count = catcher.text.count('timed_error_success') failure_count = catcher.text.count('timed_error_failure') time_logger_count = catcher.text.count('running_time_logger') self.assertEqual(success_count, 3) self.assertEqual(failure_count, 1) self.assertEqual(time_logger_count, 10)
def test_anchored_case(self): cron = Cron() starting = datetime.datetime.now() cron.schedule( Tab('three_sec', verbose=False).starting(starting).every(seconds=3).run( time_logger, 'three_sec'), Tab('three_sec_str', verbose=False).starting(starting.isoformat()).every( seconds=3).run(time_logger, 'three_sec_str'), ) with PrintCatcher(stream='stdout') as stdout_catcher: cron.go(max_seconds=3.5) # make sure times fall int right slots lines = list(stdout_catcher.text.split('\n')) for line in lines: if line: words = line.split() time = parse('T'.join(words[1:])) elapsed = (time - starting).total_seconds() self.assertTrue(elapsed < 3)
def test_excluding(self): # Test base case cron = Cron() cron.schedule( Tab('base_case', verbose=True).every(seconds=1).run(time_logger, 'base_case'), Tab('d+').every(seconds=1).during(return_true).run( time_logger, 'd+'), Tab('d-').every(seconds=1).during(return_false).run( time_logger, 'd-'), Tab('e+').every(seconds=1).excluding(return_true).run( time_logger, 'e+'), Tab('e-').every(seconds=1).excluding(return_false).run( time_logger, 'e-'), ) with PrintCatcher(stream='stdout') as stdout_catcher: cron.go(max_seconds=2) self.assertTrue('d+' in stdout_catcher.text) self.assertFalse('d-' in stdout_catcher.text) self.assertFalse('e+' in stdout_catcher.text) self.assertTrue('e-' in stdout_catcher.text)