def test_tasks_run_again(self): task = self._create_object_and_test_task('run_something_fast', 'key1') task.run() with LogCheck(self, _start_message(task)): Task.objects._do_schedule() self._wait_until('key1', "run_something_fast") time.sleep(0.5) self._reset('key1', "run_something_fast") self._assert_status("successful", task) task = self._create_object_and_test_task('run_something_fast', 'key1') task.run() output_check = LogCheck(self, fail_if_different=False) with output_check: Task.objects._do_schedule() self._wait_until('key1', "run_something_fast") time.sleep(0.5) import re pks = re.findall(r'(\d+)', output_check.log.getvalue()) new_task = Task.objects.get(pk=int(pks[0])) self.assertEquals(_start_message(new_task) # TODO: this should be logged, and is not... this is very strange # since the task is marked as successful in the DB #+ 'INFO: Task ' + str(new_task.pk) + ' finished with status "successful"\n' , output_check.log.getvalue()) self.assertTrue(new_task.pk != task.pk) self.assertEquals("successful", new_task.status) self.assertEquals(1, len(new_task.archives()))
def test_tasks_run_again(self): task = self._create_object_and_test_task('run_something_fast', 'key1') task.run() with LogCheck(self, _start_message(task)): Task.objects._do_schedule() self._wait_until('key1', "run_something_fast") time.sleep(0.5) self._reset('key1', "run_something_fast") self._assert_status("successful", task) task = self._create_object_and_test_task('run_something_fast', 'key1') task.run() output_check = LogCheck(self, fail_if_different=False) with output_check: Task.objects._do_schedule() self._wait_until('key1', "run_something_fast") time.sleep(0.5) import re pks = re.findall(r'(\d+)', output_check.log.getvalue()) new_task = Task.objects.get(pk=int(pks[0])) self.assertEquals( _start_message(new_task) # TODO: this should be logged, and is not... this is very strange # since the task is marked as successful in the DB #+ 'INFO: Task ' + str(new_task.pk) + ' finished with status "successful"\n' , output_check.log.getvalue()) self.assertTrue(new_task.pk != task.pk) self.assertEquals("successful", new_task.status) self.assertEquals(1, len(new_task.archives()))
def test_tasks_run_two_dependent_tasks_successful(self): key = 'key2' dependent_task = self._create_object_and_test_task( 'run_something_long', key) with_dependent_task = self._create_object_and_test_task( 'run_something_with_dependent', key) task = self._create_object_and_test_task( 'run_something_with_two_dependent', key) self.assertEquals("defined", dependent_task.status) task.run() self._assert_status("scheduled", task) self._assert_status("scheduled", dependent_task) self._check_running(key, dependent_task, None, 'run_something_long_2') self._check_running(key, with_dependent_task, dependent_task, 'run_something_with_dependent') self._check_running(key, task, with_dependent_task, 'run_something_with_two_dependent') task = Task.objects.get(pk=task.pk) complete_log, _ = DATETIME_REGEX.subn('', task.complete_log()) self.assertEquals( u'djangotasks.testmodel.run_something_long(pk = %s/key2) started on \n' % self.tempdir + u'running run_something_long_1\n' + u'running run_something_long_2\n' + u'\n' + u'djangotasks.testmodel.run_something_long(pk = %s/key2) finished successfully on \n' % self.tempdir + u'djangotasks.testmodel.run_something_with_dependent(pk = %s/key2) started on \n' % self.tempdir + u'running run_something_with_dependent\n' + u'\n' + u'djangotasks.testmodel.run_something_with_dependent(pk = %s/key2) finished successfully on \n' % self.tempdir + u'djangotasks.testmodel.run_something_with_two_dependent(pk = %s/key2) started on \n' % self.tempdir + u'running run_something_with_two_dependent\n' + u'\n' + u'djangotasks.testmodel.run_something_with_two_dependent(pk = %s/key2) finished successfully on ' % self.tempdir, complete_log) complete_log_direct, _ = DATETIME_REGEX.subn('', task.complete_log(True)) self.assertEquals( u'djangotasks.testmodel.run_something_long(pk = %s/key2) started on \n' % self.tempdir + u'running run_something_long_1\n' + u'running run_something_long_2\n' + u'\n' + u'djangotasks.testmodel.run_something_long(pk = %s/key2) finished successfully on \n' % self.tempdir + u'djangotasks.testmodel.run_something_with_dependent(pk = %s/key2) started on \n' % self.tempdir + u'running run_something_with_dependent\n' + u'\n' + u'djangotasks.testmodel.run_something_with_dependent(pk = %s/key2) finished successfully on \n' % self.tempdir + u'djangotasks.testmodel.run_something_with_two_dependent(pk = %s/key2) started on \n' % self.tempdir + u'running run_something_with_two_dependent\n' + u'\n' + u'djangotasks.testmodel.run_something_with_two_dependent(pk = %s/key2) finished successfully on ' % self.tempdir, complete_log_direct)
def test_tasks_run_check_database(self): task = self._create_object_and_test_task('check_database_settings', 'key1') task.run() from django.db import connection self._check_running( 'key1', task, None, 'check_database_settings', connection.settings_dict["NAME"] + u'\n' ) # May fail if your Django settings define a different test database for each run: in which case you should modify it, to ensure it's always the same.
def test_tasks_run_cancel_scheduled(self): task = self._create_object_and_test_task('run_something_long', 'key1') with LogCheck(self): Task.objects._do_schedule() task.run() task.cancel() with LogCheck(self, "INFO: Cancelling task " + str(task.pk) + "...\nINFO: Task " + str(task.pk) + " finished with status \"cancelled\"\nINFO: ...Task " + str(task.pk) + " cancelled.\n"): Task.objects._do_schedule() new_task = Task.objects.get(pk=task.pk) self.assertEquals("cancelled", new_task.status) self.assertEquals("", new_task.log)
def test_tasks_run_dependent_with_two_dependent_tasks_successful(self): key = 'key3' dependent_task = self._create_object_and_test_task('run_something_long', key) with_dependent_task = self._create_object_and_test_task('run_something_with_dependent', key) with_two_dependent_task = self._create_object_and_test_task('run_something_with_two_dependent', key) task = self._create_object_and_test_task('run_something_with_dependent_with_two_dependent', key) self.assertEquals("defined", dependent_task.status) task.run() self._assert_status("scheduled", task) self._assert_status("scheduled", with_dependent_task) self._assert_status("scheduled", with_two_dependent_task) self._assert_status("scheduled", dependent_task) self._check_running(key, dependent_task, None, 'run_something_long_2') self._check_running(key, with_dependent_task, dependent_task, "run_something_with_dependent") self._check_running(key, with_two_dependent_task, with_dependent_task, "run_something_with_two_dependent") self._check_running(key, task, with_two_dependent_task, "run_something_with_dependent_with_two_dependent") task = Task.objects.get(pk=task.pk) complete_log, _ = DATETIME_REGEX.subn('', task.complete_log()) self.assertEquals(u'djangotasks.testmodel.run_something_long(pk = %s/key3) started on \n' % self.tempdir + u'running run_something_long_1\n' + u'running run_something_long_2\n' + u'\n' + u'djangotasks.testmodel.run_something_long(pk = %s/key3) finished successfully on \n' % self.tempdir + u'djangotasks.testmodel.run_something_with_dependent(pk = %s/key3) started on \n' % self.tempdir + u'running run_something_with_dependent\n' + u'\n' + u'djangotasks.testmodel.run_something_with_dependent(pk = %s/key3) finished successfully on \n' % self.tempdir + u'djangotasks.testmodel.run_something_with_two_dependent(pk = %s/key3) started on \n' % self.tempdir + u'running run_something_with_two_dependent\n' + u'\n' + u'djangotasks.testmodel.run_something_with_two_dependent(pk = %s/key3) finished successfully on \n' % self.tempdir + u'djangotasks.testmodel.run_something_with_dependent_with_two_dependent(pk = %s/key3) started on \n' % self.tempdir + u'running run_something_with_dependent_with_two_dependent\n' + u'\n' + u'djangotasks.testmodel.run_something_with_dependent_with_two_dependent(pk = %s/key3) finished successfully on ' % self.tempdir, complete_log) complete_log_direct, _ = DATETIME_REGEX.subn('', task.complete_log(True)) self.assertEquals(u'djangotasks.testmodel.run_something_with_two_dependent(pk = %s/key3) started on \n' % self.tempdir + u'running run_something_with_two_dependent\n' + u'\n' + u'djangotasks.testmodel.run_something_with_two_dependent(pk = %s/key3) finished successfully on \n' % self.tempdir + u'djangotasks.testmodel.run_something_with_dependent_with_two_dependent(pk = %s/key3) started on \n' % self.tempdir + u'running run_something_with_dependent_with_two_dependent\n' + u'\n' + u'djangotasks.testmodel.run_something_with_dependent_with_two_dependent(pk = %s/key3) finished successfully on ' % self.tempdir, complete_log_direct)
def test_tasks_run_failing(self): task = self._create_object_and_test_task('run_something_failing', 'key1') task.run() with LogCheck(self, _start_message(task)): Task.objects._do_schedule() self._wait_until('key1', "run_something_failing") time.sleep(0.5) new_task = Task.objects.get(pk=task.pk) self.assertEquals("unsuccessful", new_task.status) self.assertTrue(u'running run_something_failing' in new_task.log) self.assertTrue(u'raise Exception("Failed !")' in new_task.log) self.assertTrue(u'Exception: Failed !' in new_task.log)
def test_tasks_exception_in_thread(self): task = self._create_object_and_test_task('run_something_long', 'key1') task.run() task = self._create_object_and_test_task('run_something_long', 'key1') task_delete = self._create_object_and_test_task('run_something_long', 'key1') task_delete.delete() try: Task.objects.get(pk=task.pk) self.fail("Should throw an exception") except Exception as e: self.assertEquals("Task matching query does not exist.", str(e)) with LogCheck(self, 'WARNING: Failed to change status from "scheduled" to "running" for task %d\n' % task.pk): task.do_run() time.sleep(0.5)
def test_tasks_run_cancel_scheduled(self): task = self._create_object_and_test_task('run_something_long', 'key1') with LogCheck(self): Task.objects._do_schedule() task.run() task.cancel() with LogCheck( self, "INFO: Cancelling task " + str(task.pk) + "...\nINFO: Task " + str(task.pk) + " finished with status \"cancelled\"\nINFO: ...Task " + str(task.pk) + " cancelled.\n"): Task.objects._do_schedule() new_task = Task.objects.get(pk=task.pk) self.assertEquals("cancelled", new_task.status) self.assertEquals("", new_task.log)
def test_run_function_task_with_dependent(self): task = _test_function_with_dependent() task = task.run() self.assertEquals("scheduled", task.status) if True: # with LogCheck(self): Task.objects._do_schedule() time.sleep(0.5) with LogCheck(self): Task.objects._do_schedule() time.sleep(0.5) with LogCheck(self): Task.objects._do_schedule() i = 0 while i < 100: # 20 seconds should be enough i += 1 time.sleep(0.2) task = Task.objects.get(pk=task.pk) if task.status == "successful": break self.assertEquals("successful", task.status) self.assertEquals("running _test_function_with_dependent\n", task.log) complete_log, _ = DATETIME_REGEX.subn('', task.complete_log(True)) self.assertEquals( u'djangotasks.functiontask.run_function_task(pk = djangotasks.tests._test_function) started on \n' + u'running _test_function\n\n' + u'djangotasks.functiontask.run_function_task(pk = djangotasks.tests._test_function) finished successfully on \n' + u'djangotasks.functiontask.run_function_task(pk = djangotasks.tests._test_function_with_dependent) started on \n' + u'running _test_function_with_dependent\n\n' + u'djangotasks.functiontask.run_function_task(pk = djangotasks.tests._test_function_with_dependent) finished successfully on ', complete_log)
def test_run_function_task_with_dependent(self): task = _test_function_with_dependent() task = task.run() self.assertEquals("scheduled", task.status) if True: # with LogCheck(self): Task.objects._do_schedule() time.sleep(0.5) with LogCheck(self): Task.objects._do_schedule() time.sleep(0.5) with LogCheck(self): Task.objects._do_schedule() i = 0 while i < 100: # 20 seconds should be enough i += 1 time.sleep(0.2) task = Task.objects.get(pk=task.pk) if task.status == "successful": break self.assertEquals("successful", task.status) self.assertEquals("running _test_function_with_dependent\n", task.log) complete_log, _ = DATETIME_REGEX.subn('', task.complete_log(True)) self.assertEquals(u'djangotasks.functiontask.run_function_task(pk = djangotasks.tests._test_function) started on \n' + u'running _test_function\n\n' + u'djangotasks.functiontask.run_function_task(pk = djangotasks.tests._test_function) finished successfully on \n' + u'djangotasks.functiontask.run_function_task(pk = djangotasks.tests._test_function_with_dependent) started on \n' + u'running _test_function_with_dependent\n\n' + u'djangotasks.functiontask.run_function_task(pk = djangotasks.tests._test_function_with_dependent) finished successfully on ', complete_log)
def test_tasks_run_dependent_task_failing(self): dependent_task = self._create_object_and_test_task('run_something_failing', 'key1') task = self._create_object_and_test_task('run_something_with_dependent_failing', 'key1') self.assertEquals("defined", dependent_task.status) task.run() self._assert_status("scheduled", task) self._assert_status("scheduled", dependent_task) with LogCheck(self, _start_message(dependent_task)): Task.objects._do_schedule() time.sleep(0.5) self._assert_status("scheduled", task) self._assert_status("running", dependent_task) self._wait_until('key1', 'run_something_failing') time.sleep(0.5) self._assert_status("scheduled", task) self._assert_status("unsuccessful", dependent_task) with LogCheck(self): Task.objects._do_schedule() time.sleep(0.5) self._assert_status("unsuccessful", task) task = Task.objects.get(pk=task.pk) complete_log, _ = DATETIME_REGEX.subn('', task.complete_log()) self.assertTrue(complete_log.startswith('djangotasks.testmodel.run_something_failing(pk = %s/key1) started on \n' % self.tempdir + 'running run_something_failing\n' + 'Traceback (most recent call last):\n')) self.assertTrue(complete_log.endswith(u', in run_something_failing\n' + u' raise Exception("Failed !")\n' + u'Exception: Failed !\n\n' + u'djangotasks.testmodel.run_something_failing(pk = %s/key1) failed on \n' % self.tempdir + u'djangotasks.testmodel.run_something_with_dependent_failing(pk = %s/key1) started\n' % self.tempdir + u'djangotasks.testmodel.run_something_with_dependent_failing(pk = %s/key1) failed' % self.tempdir)) complete_log_direct, _ = DATETIME_REGEX.subn('', task.complete_log(True)) self.assertTrue(complete_log_direct.startswith('djangotasks.testmodel.run_something_failing(pk = %s/key1) started on \n' % self.tempdir + u'running run_something_failing\n' + u'Traceback (most recent call last):\n')) self.assertTrue(complete_log_direct.endswith(u', in run_something_failing\n' + u' raise Exception("Failed !")\n' + u'Exception: Failed !\n\n' + u'djangotasks.testmodel.run_something_failing(pk = %s/key1) failed on \n' % self.tempdir + u'djangotasks.testmodel.run_something_with_dependent_failing(pk = %s/key1) started\n' % self.tempdir + u'djangotasks.testmodel.run_something_with_dependent_failing(pk = %s/key1) failed' % self.tempdir)) self.assertEquals("unsuccessful", task.status)
def test_tasks_exception_in_thread(self): task = self._create_object_and_test_task('run_something_long', 'key1') task.run() task = self._create_object_and_test_task('run_something_long', 'key1') task_delete = self._create_object_and_test_task( 'run_something_long', 'key1') task_delete.delete() try: Task.objects.get(pk=task.pk) self.fail("Should throw an exception") except Exception as e: self.assertEquals("Task matching query does not exist.", str(e)) with LogCheck( self, 'WARNING: Failed to change status from "scheduled" to "running" for task %d\n' % task.pk): task.do_run() time.sleep(0.5)
def test_send_signal_on_task_completed(self): from djangotasks.models import TaskManager from djangotasks.signals import task_completed def receiver(sender, **kwargs): task = kwargs['task'] self.assertEqual(TaskManager, sender.__class__) self.assertEqual(Task, task.__class__) self.assertEqual(TestModel, kwargs['object'].__class__) self.assertEqual('successful', task.status) # Ensure that this function was called by the Signal task.log = "Text added from the signal receiver" task.save() task_completed.connect(receiver) task = self._create_object_and_test_task('run_something_fast', 'key1') task.run() self._check_running('key1', task, None, 'run_something_fast', u'Text added from the signal receiver') task_completed.disconnect()
def test_tasks_run_cancel_running(self): task = self._create_object_and_test_task('run_something_long', 'key1') task.run() with LogCheck(self, _start_message(task)): Task.objects._do_schedule() self._wait_until('key1', "run_something_long_1") task.cancel() output_check = LogCheck(self, fail_if_different=False) with output_check: Task.objects._do_schedule() time.sleep(0.3) self.assertTrue(("Cancelling task " + str(task.pk) + "...") in output_check.log.getvalue()) self.assertTrue("cancelled.\n" in output_check.log.getvalue()) #self.assertTrue('INFO: failed to mark tasked as finished, from status "running" to "unsuccessful" for task 3. May have been finished in a different thread already.\n' # in output_check.log.getvalue()) new_task = Task.objects.get(pk=task.pk) self.assertEquals("cancelled", new_task.status) self.assertTrue(u'running run_something_long_1' in new_task.log) self.assertFalse(u'running run_something_long_2' in new_task.log) self.assertFalse('finished' in new_task.log)
def test_tasks_archive_task(self): task = Task.objects._task_for_object(TestModel, 'key1', 'run_something_fast', None) self.assertTrue(task.pk) task.status = 'successful' task.save() self.assertEquals(False, task.archived) new_task = task.run() self.assertTrue(new_task.pk) self.assertTrue(task.pk != new_task.pk) old_task = Task.objects.get(pk=task.pk) self.assertEquals(True, old_task.archived, "Task should have been archived once a new one has been created") self.assertEquals(1, len(task.archives()))
def test_run_function_task(self): task = _test_function() task = task.run() self.assertEquals("scheduled", task.status) with LogCheck(self, _start_message(task)): Task.objects._do_schedule() i = 0 while i < 100: # 20 seconds should be enough i += 1 time.sleep(0.2) task = Task.objects.get(pk=task.pk) if task.status == "successful": break self.assertEquals("successful", task.status) self.assertEquals("running _test_function\n", task.log)
def test_tasks_archive_task(self): task = Task.objects._task_for_object(TestModel, 'key1', 'run_something_fast', None) self.assertTrue(task.pk) task.status = 'successful' task.save() self.assertEquals(False, task.archived) new_task = task.run() self.assertTrue(new_task.pk) self.assertTrue(task.pk != new_task.pk) old_task = Task.objects.get(pk=task.pk) self.assertEquals( True, old_task.archived, "Task should have been archived once a new one has been created") self.assertEquals(1, len(task.archives()))
def test_tasks_run_dependent_task_failing(self): dependent_task = self._create_object_and_test_task( 'run_something_failing', 'key1') task = self._create_object_and_test_task( 'run_something_with_dependent_failing', 'key1') self.assertEquals("defined", dependent_task.status) task.run() self._assert_status("scheduled", task) self._assert_status("scheduled", dependent_task) with LogCheck(self, _start_message(dependent_task)): Task.objects._do_schedule() time.sleep(0.5) self._assert_status("scheduled", task) self._assert_status("running", dependent_task) self._wait_until('key1', 'run_something_failing') time.sleep(0.5) self._assert_status("scheduled", task) self._assert_status("unsuccessful", dependent_task) with LogCheck(self): Task.objects._do_schedule() time.sleep(0.5) self._assert_status("unsuccessful", task) task = Task.objects.get(pk=task.pk) complete_log, _ = DATETIME_REGEX.subn('', task.complete_log()) self.assertTrue( complete_log.startswith( 'djangotasks.testmodel.run_something_failing(pk = %s/key1) started on \n' % self.tempdir + 'running run_something_failing\n' + 'Traceback (most recent call last):\n')) self.assertTrue( complete_log.endswith( u', in run_something_failing\n' + u' raise Exception("Failed !")\n' + u'Exception: Failed !\n\n' + u'djangotasks.testmodel.run_something_failing(pk = %s/key1) failed on \n' % self.tempdir + u'djangotasks.testmodel.run_something_with_dependent_failing(pk = %s/key1) started\n' % self.tempdir + u'djangotasks.testmodel.run_something_with_dependent_failing(pk = %s/key1) failed' % self.tempdir)) complete_log_direct, _ = DATETIME_REGEX.subn('', task.complete_log(True)) self.assertTrue( complete_log_direct.startswith( 'djangotasks.testmodel.run_something_failing(pk = %s/key1) started on \n' % self.tempdir + u'running run_something_failing\n' + u'Traceback (most recent call last):\n')) self.assertTrue( complete_log_direct.endswith( u', in run_something_failing\n' + u' raise Exception("Failed !")\n' + u'Exception: Failed !\n\n' + u'djangotasks.testmodel.run_something_failing(pk = %s/key1) failed on \n' % self.tempdir + u'djangotasks.testmodel.run_something_with_dependent_failing(pk = %s/key1) started\n' % self.tempdir + u'djangotasks.testmodel.run_something_with_dependent_failing(pk = %s/key1) failed' % self.tempdir)) self.assertEquals("unsuccessful", task.status)
def test_tasks_run_with_space_fast(self): task = self._create_object_and_test_task('run_something_fast', 'key with space') task.run() self._check_running('key with space', task, None, 'run_something_fast', u'running run_something_fast\n')
def test_tasks_run_check_database(self): task = self._create_object_and_test_task('check_database_settings', 'key1') task.run() from django.db import connection self._check_running('key1', task, None, 'check_database_settings', connection.settings_dict["NAME"] + u'\n') # May fail if your Django settings define a different test database for each run: in which case you should modify it, to ensure it's always the same.
def test_tasks_run_successful(self): task = self._create_object_and_test_task('run_something_long', 'key1') task.run() self._check_running('key1', task, None, 'run_something_long_2', u'running run_something_long_1\nrunning run_something_long_2\n')
def test_tasks_run_successful(self): task = self._create_object_and_test_task('run_something_long', 'key1') task.run() self._check_running( 'key1', task, None, 'run_something_long_2', u'running run_something_long_1\nrunning run_something_long_2\n')