Skip to content

Commit

Permalink
#29498 Adjusting tests to the recent changes
Browse files Browse the repository at this point in the history
  • Loading branch information
jgambarios committed Nov 9, 2024
1 parent 4e3ee0d commit 718daad
Show file tree
Hide file tree
Showing 5 changed files with 202 additions and 143 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import com.dotcms.jobs.business.job.JobPaginatedResult;
import com.dotcms.jobs.business.processor.JobProcessor;
import com.dotcms.jobs.business.queue.JobQueue;
import com.dotcms.jobs.business.queue.error.JobQueueDataException;
import com.dotmarketing.exception.DotDataException;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -94,10 +93,10 @@ String createJob(String queueName, Map<String, Object> parameters)
* @param page The page number
* @param pageSize The number of jobs per page
* @return A result object containing the list of active jobs and pagination information.
* @throws JobQueueDataException if there's an error fetching the jobs
* @throws DotDataException if there's an error fetching the jobs
*/
JobPaginatedResult getActiveJobs(String queueName, int page, int pageSize)
throws JobQueueDataException;
throws DotDataException;

/**
* Retrieves a list of jobs.
Expand All @@ -115,39 +114,39 @@ JobPaginatedResult getActiveJobs(String queueName, int page, int pageSize)
* @param page The page number
* @param pageSize The number of jobs per page
* @return A result object containing the list of active jobs and pagination information.
* @throws JobQueueDataException if there's an error fetching the jobs
* @throws DotDataException if there's an error fetching the jobs
*/
JobPaginatedResult getActiveJobs(int page, int pageSize) throws JobQueueDataException;
JobPaginatedResult getActiveJobs(int page, int pageSize) throws DotDataException;

/**
* Retrieves a list of completed jobs
*
* @param page The page number
* @param pageSize The number of jobs per page
* @return A result object containing the list of completed jobs and pagination information.
* @throws JobQueueDataException if there's an error fetching the jobs
* @throws DotDataException if there's an error fetching the jobs
*/
JobPaginatedResult getCompletedJobs(int page, int pageSize) throws JobQueueDataException;
JobPaginatedResult getCompletedJobs(int page, int pageSize) throws DotDataException;

/**
* Retrieves a list of canceled jobs
*
* @param page The page number
* @param pageSize The number of jobs per page
* @return A result object containing the list of canceled jobs and pagination information.
* @throws JobQueueDataException if there's an error fetching the jobs
* @throws DotDataException if there's an error fetching the jobs
*/
JobPaginatedResult getCanceledJobs(int page, int pageSize) throws JobQueueDataException;
JobPaginatedResult getCanceledJobs(int page, int pageSize) throws DotDataException;

/**
* Retrieves a list of failed jobs
*
* @param page The page number
* @param pageSize The number of jobs per page
* @return A result object containing the list of failed jobs and pagination information.
* @throws JobQueueDataException if there's an error fetching the jobs
* @throws DotDataException if there's an error fetching the jobs
*/
JobPaginatedResult getFailedJobs(int page, int pageSize) throws JobQueueDataException;
JobPaginatedResult getFailedJobs(int page, int pageSize) throws DotDataException;

/**
* Cancels a job.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,11 +316,11 @@ public Job getJob(final String jobId) throws DotDataException {
@CloseDBIfOpened
@Override
public JobPaginatedResult getActiveJobs(String queueName, int page, int pageSize)
throws JobQueueDataException {
throws DotDataException {
try {
return jobQueue.getActiveJobs(queueName, page, pageSize);
} catch (JobQueueDataException e) {
throw new JobQueueDataException("Error fetching active jobs", e);
throw new DotDataException("Error fetching active jobs", e);
}
}

Expand All @@ -336,45 +336,41 @@ public JobPaginatedResult getJobs(final int page, final int pageSize) throws Dot

@CloseDBIfOpened
@Override
public JobPaginatedResult getActiveJobs(int page, int pageSize)
throws JobQueueDataException {
public JobPaginatedResult getActiveJobs(int page, int pageSize) throws DotDataException {
try {
return jobQueue.getActiveJobs(page, pageSize);
} catch (JobQueueDataException e) {
throw new JobQueueDataException("Error fetching active jobs", e);
throw new DotDataException("Error fetching active jobs", e);
}
}

@CloseDBIfOpened
@Override
public JobPaginatedResult getCompletedJobs(int page, int pageSize)
throws JobQueueDataException {
public JobPaginatedResult getCompletedJobs(int page, int pageSize) throws DotDataException {
try {
return jobQueue.getCompletedJobs(page, pageSize);
} catch (JobQueueDataException e) {
throw new JobQueueDataException("Error fetching completed jobs", e);
throw new DotDataException("Error fetching completed jobs", e);
}
}

@CloseDBIfOpened
@Override
public JobPaginatedResult getCanceledJobs(int page, int pageSize)
throws JobQueueDataException {
public JobPaginatedResult getCanceledJobs(int page, int pageSize) throws DotDataException {
try {
return jobQueue.getCanceledJobs(page, pageSize);
} catch (JobQueueDataException e) {
throw new JobQueueDataException("Error fetching canceled jobs", e);
throw new DotDataException("Error fetching canceled jobs", e);
}
}

@CloseDBIfOpened
@Override
public JobPaginatedResult getFailedJobs(int page, int pageSize)
throws JobQueueDataException {
public JobPaginatedResult getFailedJobs(int page, int pageSize) throws DotDataException {
try {
return jobQueue.getFailedJobs(page, pageSize);
} catch (JobQueueDataException e) {
throw new JobQueueDataException("Error fetching failed jobs", e);
throw new DotDataException("Error fetching failed jobs", e);
}
}

Expand All @@ -401,8 +397,9 @@ public void cancelJob(final String jobId) throws DotDataException {
*
* @param event The event that triggers the job cancellation request.
*/
@VisibleForTesting
@WrapInTransaction
private void onCancelRequestJob(final JobCancelRequestEvent event) {
void onCancelRequestJob(final JobCancelRequestEvent event) {

try {

Expand Down Expand Up @@ -862,19 +859,24 @@ private void handleJobCompletion(final Job job, final JobProcessor processor)

final float progress = getJobProgress(job);

final var latestState = getJobState(job.id());
if (latestState == JobState.CANCELLING) {
Job canceledJob = job.markAsCanceled(jobResult).withProgress(progress);
updateJobStatus(canceledJob);
eventProducer.getEvent(JobCanceledEvent.class).fire(
new JobCanceledEvent(canceledJob, LocalDateTime.now())
);
} else {
final Job completedJob = job.markAsCompleted(jobResult).withProgress(progress);
updateJobStatus(completedJob);
eventProducer.getEvent(JobCompletedEvent.class).fire(
new JobCompletedEvent(completedJob, LocalDateTime.now())
);
try {
if (jobQueue.hasJobBeenInState(job.id(), JobState.CANCELLING)) {
Job canceledJob = job.markAsCanceled(jobResult).withProgress(progress);
updateJobStatus(canceledJob);
eventProducer.getEvent(JobCanceledEvent.class).fire(
new JobCanceledEvent(canceledJob, LocalDateTime.now())
);
} else {
final Job completedJob = job.markAsCompleted(jobResult).withProgress(progress);
updateJobStatus(completedJob);
eventProducer.getEvent(JobCompletedEvent.class).fire(
new JobCompletedEvent(completedJob, LocalDateTime.now())
);
}
} catch (JobQueueDataException e) {
final var errorMessage = "Error updating job status";
Logger.error(this, errorMessage, e);
throw new DotDataException(errorMessage, e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import com.dotcms.jobs.business.job.JobState;
import com.dotcms.jobs.business.processor.JobProcessor;
import com.dotcms.jobs.business.processor.Queue;
import com.dotcms.jobs.business.queue.error.JobQueueDataException;
import com.dotcms.rest.api.v1.temp.DotTempFile;
import com.dotcms.rest.api.v1.temp.TempFileAPI;
import com.dotmarketing.business.APILocator;
Expand Down Expand Up @@ -247,7 +246,7 @@ void watchJob(String jobId, Consumer<Job> watcher) {
JobPaginatedResult getActiveJobs(String queueName, int page, int pageSize) {
try {
return jobQueueManagerAPI.getActiveJobs(queueName, page, pageSize);
} catch (JobQueueDataException e) {
} catch (DotDataException e) {
Logger.error(this.getClass(), "Error fetching active jobs", e);
}
return JobPaginatedResult.builder().build();
Expand Down Expand Up @@ -279,7 +278,7 @@ JobPaginatedResult getJobs(int page, int pageSize) {
JobPaginatedResult getActiveJobs(int page, int pageSize) {
try {
return jobQueueManagerAPI.getActiveJobs(page, pageSize);
} catch (JobQueueDataException e) {
} catch (DotDataException e) {
Logger.error(this.getClass(), "Error fetching active jobs", e);
}
return JobPaginatedResult.builder().build();
Expand All @@ -295,7 +294,7 @@ JobPaginatedResult getActiveJobs(int page, int pageSize) {
JobPaginatedResult getCompletedJobs(int page, int pageSize) {
try {
return jobQueueManagerAPI.getCompletedJobs(page, pageSize);
} catch (JobQueueDataException e) {
} catch (DotDataException e) {
Logger.error(this.getClass(), "Error fetching completed jobs", e);
}
return JobPaginatedResult.builder().build();
Expand All @@ -311,7 +310,7 @@ JobPaginatedResult getCompletedJobs(int page, int pageSize) {
JobPaginatedResult getCanceledJobs(int page, int pageSize) {
try {
return jobQueueManagerAPI.getCanceledJobs(page, pageSize);
} catch (JobQueueDataException e) {
} catch (DotDataException e) {
Logger.error(this.getClass(), "Error fetching canceled jobs", e);
}
return JobPaginatedResult.builder().build();
Expand All @@ -327,7 +326,7 @@ JobPaginatedResult getCanceledJobs(int page, int pageSize) {
JobPaginatedResult getFailedJobs(int page, int pageSize) {
try {
return jobQueueManagerAPI.getFailedJobs(page, pageSize);
} catch (JobQueueDataException e) {
} catch (DotDataException e) {
Logger.error(this.getClass(), "Error fetching failed jobs", e);
}
return JobPaginatedResult.builder().build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import javax.inject.Inject;
import org.awaitility.Awaitility;
import org.jboss.weld.junit5.EnableWeld;
import org.jboss.weld.junit5.WeldJunit5Extension;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -38,7 +37,6 @@
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestInstance.Lifecycle;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.extension.ExtendWith;

/**
* Integration tests for the JobQueueManagerAPI.
Expand All @@ -50,6 +48,8 @@
@TestInstance(Lifecycle.PER_CLASS)
public class JobQueueManagerAPIIntegrationTest extends com.dotcms.Junit5WeldBaseTest {

private static int attempts = 0;

@Inject
JobQueueManagerAPI jobQueueManagerAPI;

Expand Down Expand Up @@ -85,6 +85,9 @@ void reset() {
if(null != jobQueueManagerAPI) {
jobQueueManagerAPI.getCircuitBreaker().reset();
}

// Reset retry attempts
attempts = 0;
}

/**
Expand Down Expand Up @@ -185,8 +188,6 @@ void test_JobRetry() throws Exception {
});
}



/**
* Method to test: Job failure handling in JobQueueManagerAPI
* Given Scenario: A job is created that is designed to fail
Expand Down Expand Up @@ -473,7 +474,6 @@ public Map<String, Object> getResultMetadata(Job job) {
static class RetryingJobProcessor implements JobProcessor {

public static final int MAX_RETRIES = 3;
private int attempts = 0;

public RetryingJobProcessor() {
// needed for instantiation purposes
Expand Down
Loading

0 comments on commit 718daad

Please sign in to comment.