def test_if_right_pid_is_read(self, mock_process, mock_setup_locations): args = self.parser.parse_args(['celery', 'stop']) pid = "123" # Calling stop_worker should delete the temporary pid file with self.assertRaises(FileNotFoundError): with NamedTemporaryFile("w+") as f: # Create pid file f.write(pid) f.flush() # Setup mock mock_setup_locations.return_value = (f.name, None, None, None) # Check if works as expected celery_command.stop_worker(args) mock_process.assert_called_once_with(int(pid)) mock_process.return_value.terminate.assert_called_once_with()
def test_same_pid_file_is_used_in_start_and_stop( self, mock_setup_locations, mock_celery_worker, mock_read_pid_from_pidfile): pid_file = "test_pid_file" mock_setup_locations.return_value = (pid_file, None, None, None) mock_read_pid_from_pidfile.return_value = None # Call worker worker_args = self.parser.parse_args( ['celery', 'worker', '--skip-serve-logs']) celery_command.worker(worker_args) assert mock_celery_worker.call_args assert 'pidfile' in mock_celery_worker.call_args.kwargs assert mock_celery_worker.call_args.kwargs['pidfile'] == pid_file # Call stop stop_args = self.parser.parse_args(['celery', 'stop']) celery_command.stop_worker(stop_args) mock_read_pid_from_pidfile.assert_called_once_with(pid_file)