def test_cron_run_task(app): """The `run_task` method inserts a new BadpennyJob row""" cmd = cron.BadpennyCron() with app.app_context(): badpenny.periodic_task(seconds=10)(fake_task_func('ten')) cmd.sync_tasks() task = badpenny.Task.get('test.ten') today = dt(2014, 9, 6, 16, 10, 45) with mock.patch('relengapi.lib.time.now') as now: now.return_value = today cmd.run_task(task) job = tables.BadpennyJob.query.first() eq_(job.task_id, task.task_id) eq_(job.created_at, today) eq_(job.started_at, None) eq_(job.completed_at, None)
def test_cron_sync_tasks(app): """The `sync_tasks` method inserts new rows into the DB for any new registered tasks, and sets task.task_id""" cmd = cron.BadpennyCron() with app.app_context(): with empty_registry(): badpenny.periodic_task(seconds=10)(fake_task_func('foo')) badpenny.periodic_task(seconds=10)(fake_task_func('bar')) cmd.sync_tasks() dbtasks = tables.BadpennyTask.query.all() eq_(sorted([t.name for t in dbtasks]), sorted(['test.foo', 'test.bar'])) badpenny.periodic_task(seconds=30)(fake_task_func('bing')) cmd.sync_tasks() dbtasks = tables.BadpennyTask.query.all() eq_(sorted([t.name for t in dbtasks]), sorted(['test.foo', 'test.bar', 'test.bing'])) eq_(sorted([t.task_id for t in badpenny.Task.list()]), [1, 2, 3])