def test_run_3_workers(self, mock_get_logger, mock_start, mock_close_all): """Test for RunCronTasks - 3 CronTask objects started""" now = timezone.now() # Pending Tasks: cron_task_c1 = CronTask.objects.run_now( "Sleep", now=now - datetime.timedelta(minutes=6))[0] cron_task_c2 = CronTask.objects.run_now( "ParamsLockedSleep", now=now - datetime.timedelta(minutes=1))[0] cron_task_c3 = CronTask.objects.run_now( "ClassLockedSleep", now=now - datetime.timedelta(minutes=1))[0] cron_task_c3.mark_as_queued() # Future Task: CronTask.objects.run_now( "ClassLockedSleep", params="42", now=now + datetime.timedelta(minutes=6), ) # Finished Task: CronTask.objects.run_now("IgnoreLockErrorsSleep")[0].mark_as_finished() self.assertTrue(call_worker("RunCronTasks").ok) mock_get_logger.return_value.info.assert_has_calls( [mock.call("Started 3 CronTask(s).")]) mock_start.assert_has_calls([ mock.call("Sleep:task_id={}".format(cron_task_c1.pk)), mock.call("ParamsLockedSleep:task_id={}".format(cron_task_c2.pk)), mock.call("ClassLockedSleep:task_id={}".format(cron_task_c3.pk)), ]) mock_close_all.assert_called_once()
def test_run_no_workers(self, mock_get_logger, mock_start, mock_close_all): """Test for RunCronTasks - no CronTask objects started""" self.assertTrue(call_worker("RunCronTasks").ok) mock_get_logger.return_value.info.assert_has_calls( [mock.call("No CronTasks started.")]) mock_start.assert_not_called() mock_close_all.assert_called_once()
def test_run_2_started_1_dead(self, mock_get_logger): """Test for CleanCronTasks - 2 CronTask objects started, 1 marked as failed. """ now = timezone.now() # Started Task - running: pid_s1 = 1001 cron_task_s1 = CronTask.objects.run_now( "Sleep", now=now - datetime.timedelta(minutes=6))[0] cron_task_s1.mark_as_started(pid_s1) # Started Task - dead: cron_task_s2 = CronTask.objects.run_now( "ParamsLockedSleep", now=now - datetime.timedelta(minutes=1))[0] pid_s2 = 1002 cron_task_s2.mark_as_started(pid_s2) # Queued Task: cron_task_q1 = CronTask.objects.run_now( "ClassLockedSleep", now=now - datetime.timedelta(minutes=1))[0] cron_task_q1.mark_as_queued() # Waiting Task: cron_task_w1 = CronTask.objects.run_now( "ClassLockedSleep", params="42", now=now + datetime.timedelta(minutes=6), )[0] # Finished Task: cron_task_f1 = CronTask.objects.run_now("IgnoreLockErrorsSleep")[0] cron_task_f1.mark_as_finished() create_pid_file(cron_task_s1.job_spec(), pid_s1) with patch_ps(active_pids=[pid_s1]): with patch_kill(active_pids=[pid_s1]): self.assertTrue(call_worker("CleanCronTasks").ok) mock_get_logger.return_value.info.assert_has_calls( [mock.call("1 CronTask(s) marked as failed.")]) # Started Task - running - no status change: cron_task_s1.refresh_from_db() self.assertEqual(cron_task_s1.status, CronTaskStatus.STARTED) # Started Task - dead - marked as failed: cron_task_s2.refresh_from_db() self.assertEqual(cron_task_s2.status, CronTaskStatus.FAILED) # Other statuses - no status change: cron_task_q1.refresh_from_db() self.assertEqual(cron_task_q1.status, CronTaskStatus.QUEUED) cron_task_w1.refresh_from_db() self.assertEqual(cron_task_w1.status, CronTaskStatus.WAITING) cron_task_f1.refresh_from_db() self.assertEqual(cron_task_f1.status, CronTaskStatus.FINISHED)
def test_run_worker_process_with_complex_params(self): """Test for running worker with various types of parameters""" job_spec = ( "Sleep:seconds=1,path={0}," 'param_1=AAA,param_2=3.14,param_3="a b c"'.format(TEMP_FILE)) self.assertTrue(call_worker(job_spec).ok) # Check if worker process sent a message to output file: self.assertMessageInTempFile("param_1=AAA") self.assertMessageInTempFile("param_2=3.14") self.assertMessageInTempFile("param_3=a b c") # quotes removed
def test_run_worker_params_lock_enabled_success(self): """Test for attempt to run worker while PARAMS-based lock is enabled - success. """ locked_job_spec = "ParamsLockedSleep:seconds=10" job_spec = "ParamsLockedSleep:seconds=1" # same class, diff. params pid = 1001 create_pid_file(locked_job_spec, pid) with patch_ps(active_pids=[pid]): with patch_kill(active_pids=[pid]): self.assertTrue(call_worker(job_spec).ok)
def test_run_worker_no_lock_enabled_success(self): """Test for running worker while other worker is on, but no lock is acquired - success. """ running_job_spec = "Sleep:seconds=10" job_spec = "Sleep:seconds=1" pid = 1001 create_pid_file(running_job_spec, pid) with patch_ps(active_pids=[pid]): with patch_kill(active_pids=[pid]): self.assertTrue(call_worker(job_spec).ok)
def test_run_worker_params_lock_enabled_failure(self): """Test for attempt to run worker while PARAMS-based lock is enabled - failure. """ locked_job_spec = "ParamsLockedSleep:seconds=10" job_spec = "ParamsLockedSleep:seconds=10" # same class, same params pid = 1001 create_pid_file(locked_job_spec, pid) with patch_ps(active_pids=[pid]): with patch_kill(active_pids=[pid]): result = call_worker(job_spec) self.assertEqual(result.exc_class_name, "CronWorkerLocked") self.assertEqual( result.exc_message, 'Unable to start "ParamsLockedSleep:seconds=10", ' "because similar process is already running (PID file exists).\n", )
def test_run_no_workers(self, mock_get_logger): """Test for CleanCronTasks - no CronTask object marked as failed.""" self.assertTrue(call_worker("CleanCronTasks").ok) mock_get_logger.return_value.info.assert_has_calls( [mock.call("No CronTasks marked as failed.")])
def test_run_worker_process_failure(self): """Test for running worker - failure""" # Error: "NaN" can't be converted to an integer: job_spec = "Sleep:seconds=NaN,path={0}".format(TEMP_FILE) self.assertFalse(call_worker(job_spec).ok)
def test_run_worker_process_success(self): """Test for running worker - success""" job_spec = "Sleep:seconds=1,path={0}".format(TEMP_FILE) self.assertTrue(call_worker(job_spec).ok) # Check if worker process sent a message to output file: self.assertMessageInTempFile("Slept for 1 second(s).")