-
Notifications
You must be signed in to change notification settings - Fork 0
Handle any exception that is not handled by the client application #30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
...sts/src/test/java/uk/gov/hmcts/cp/taskmanager/integration/TaskInErrorIntegrationTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| package uk.gov.hmcts.cp.taskmanager.integration; | ||
|
|
||
| import static jakarta.json.Json.createObjectBuilder; | ||
| import static java.time.ZonedDateTime.now; | ||
| import static java.util.UUID.randomUUID; | ||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.awaitility.Awaitility.await; | ||
| import static uk.gov.hmcts.cp.taskmanager.domain.ExecutionStatus.INPROGRESS; | ||
|
|
||
| import uk.gov.hmcts.cp.taskmanager.domain.ExecutionInfo; | ||
| import uk.gov.hmcts.cp.taskmanager.domain.ExecutionStatus; | ||
| import uk.gov.hmcts.cp.taskmanager.integration.config.IntegrationTestConfiguration; | ||
| import uk.gov.hmcts.cp.taskmanager.integration.service.TaskStatus; | ||
| import uk.gov.hmcts.cp.taskmanager.integration.service.TaskStatusService; | ||
| import uk.gov.hmcts.cp.taskmanager.persistence.entity.Job; | ||
| import uk.gov.hmcts.cp.taskmanager.persistence.repository.JobsRepository; | ||
| import uk.gov.hmcts.cp.taskmanager.persistence.service.JobService; | ||
| import uk.gov.hmcts.cp.taskmanager.service.ExecutionService; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Optional; | ||
| import java.util.UUID; | ||
|
|
||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.jdbc.core.JdbcTemplate; | ||
| import org.springframework.test.annotation.DirtiesContext; | ||
|
|
||
| @IntegrationTestConfiguration | ||
| @DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD) | ||
|
|
||
| public class TaskInErrorIntegrationTest extends PostgresIntegrationTestBase { | ||
|
|
||
| @Autowired | ||
| private ExecutionService executionService; | ||
|
|
||
| @Autowired | ||
| private JobService jobService; | ||
|
|
||
| @Autowired | ||
| private JobsRepository jobsRepository; | ||
|
|
||
| @Autowired | ||
| private JdbcTemplate jdbcTemplate; | ||
|
|
||
| @Autowired | ||
| private TaskStatusService taskStatusService; | ||
|
|
||
| @AfterEach | ||
| void tearDown() { | ||
| jdbcTemplate.execute("delete from JOBS"); | ||
| final Integer jobs = jdbcTemplate.queryForObject("select count(*) from JOBS", Integer.class); | ||
| assertThat(jobs).isEqualTo(0); | ||
| } | ||
|
|
||
| @Test | ||
| void testErrorTaskWithNoRetryAttemptsShouldRunOnlyOnce() throws InterruptedException { | ||
| // Given - Create a job with error task | ||
| final UUID taskId = randomUUID(); | ||
| ExecutionInfo executionInfo = new ExecutionInfo( | ||
| createObjectBuilder() | ||
| .add("test", "data") | ||
| .add(ID_KEY, taskId.toString()) | ||
| .add(ERROR_KEY, "error") | ||
| .build(), | ||
| "TEST_ERROR_TASK", | ||
| now().minusSeconds(1), | ||
| ExecutionStatus.STARTED, | ||
| false | ||
| ); | ||
| executionService.executeWith(executionInfo); | ||
|
|
||
| assertTaskExecutedOnlyOnce(taskId, 1); | ||
| Thread.sleep(4000); | ||
| assertTaskExecutedOnlyOnce(taskId, 1); | ||
|
|
||
| // When - Wait for first execution | ||
| await().atMost(java.time.Duration.ofSeconds(5)).untilAsserted(() -> { | ||
| List<Job> jobs = jobsRepository.findAll(); | ||
| // Job should still exist (not deleted) because it's status is INPROGRESS | ||
| assertThat(jobs).isNotEmpty(); | ||
|
|
||
| Job job = jobs.get(0); | ||
| // Retry attempts should be set to 0 | ||
| assertThat(job.getRetryAttemptsRemaining()).isEqualTo(0); | ||
| }); | ||
| } | ||
|
|
||
| @Test | ||
| void testErrorRetryTaskWithRetryAttemptsShouldRunAllTheRetryAttempts() throws InterruptedException { | ||
| // Given - Create a job with error retry task | ||
| final UUID taskId = randomUUID(); | ||
| ExecutionInfo executionInfo = new ExecutionInfo( | ||
| createObjectBuilder() | ||
| .add("test", "data") | ||
| .add(ID_KEY, taskId.toString()) | ||
| .add(ERROR_KEY, "error") | ||
| .build(), | ||
| "TEST_ERROR_RETRY_TASK", | ||
| now().minusSeconds(1), | ||
| ExecutionStatus.STARTED, | ||
| true | ||
| ); | ||
| executionService.executeWith(executionInfo); | ||
|
|
||
| assertTaskExecutedOnlyOnce(taskId, 3); | ||
| Thread.sleep(4000); | ||
| assertTaskExecutedOnlyOnce(taskId, 3); | ||
|
|
||
| // When - Wait for first execution | ||
| await().atMost(java.time.Duration.ofSeconds(5)).untilAsserted(() -> { | ||
| List<Job> jobs = jobsRepository.findAll(); | ||
| // Job should still exist (not deleted) because it's status is INPROGRESS | ||
| assertThat(jobs).isNotEmpty(); | ||
|
|
||
| Job job = jobs.get(0); | ||
| // Retry attempts should be set to 0 | ||
| assertThat(job.getRetryAttemptsRemaining()).isEqualTo(0); | ||
| }); | ||
| } | ||
|
|
||
| private void assertTaskExecutedOnlyOnce(final UUID taskId, final int retryAttempts) { | ||
| await().atMost(java.time.Duration.ofSeconds(5)).untilAsserted(() -> { | ||
| final Optional<TaskStatus> task = taskStatusService.getById(taskId); | ||
| assertThat(task.isEmpty()).isFalse(); | ||
| assertThat(task.get().getStatus().equals(INPROGRESS.name())).isTrue(); | ||
| assertThat(task.get().getJobData().getInt(ATTEMPTS_KEY)).isEqualTo(retryAttempts); | ||
| }); | ||
| } | ||
| } |
65 changes: 65 additions & 0 deletions
65
...tests/src/test/java/uk/gov/hmcts/cp/taskmanager/integration/tasks/TestErrorRetryTask.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| package uk.gov.hmcts.cp.taskmanager.integration.tasks; | ||
|
|
||
| import static java.util.UUID.fromString; | ||
| import static java.util.UUID.randomUUID; | ||
| import static uk.gov.hmcts.cp.taskmanager.domain.ExecutionInfo.executionInfo; | ||
| import static uk.gov.hmcts.cp.taskmanager.domain.ExecutionStatus.COMPLETED; | ||
| import static uk.gov.hmcts.cp.taskmanager.domain.ExecutionStatus.INPROGRESS; | ||
| import static uk.gov.hmcts.cp.taskmanager.integration.PostgresIntegrationTestBase.ERROR_KEY; | ||
| import static uk.gov.hmcts.cp.taskmanager.integration.PostgresIntegrationTestBase.ID_KEY; | ||
|
|
||
| import uk.gov.hmcts.cp.taskmanager.domain.ExecutionInfo; | ||
| import uk.gov.hmcts.cp.taskmanager.integration.service.TaskStatusService; | ||
| import uk.gov.hmcts.cp.taskmanager.service.task.ExecutableTask; | ||
| import uk.gov.hmcts.cp.taskmanager.service.task.Task; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Optional; | ||
| import java.util.UUID; | ||
|
|
||
| import jakarta.json.JsonObject; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| /** | ||
| * Test task with retry attempts that fails to complete. | ||
| * Used for integration testing of tasks that throw unexpected exception during task execution. | ||
| */ | ||
| @Task("TEST_ERROR_RETRY_TASK") | ||
| @Component | ||
| public class TestErrorRetryTask implements ExecutableTask { | ||
|
|
||
| @Autowired | ||
| private TaskStatusService taskStatusService; | ||
|
|
||
| private static final Logger logger = LoggerFactory.getLogger(TestErrorRetryTask.class); | ||
|
|
||
| @Override | ||
| public ExecutionInfo execute(ExecutionInfo executionInfo) { | ||
| final JsonObject jobData = executionInfo.getJobData(); | ||
|
|
||
| logger.info("TestErrorTask executing for job: {}", jobData); | ||
|
|
||
| if (jobData.containsKey(ERROR_KEY)) { | ||
| final UUID id = jobData.containsKey(ID_KEY) ? fromString(jobData.getString(ID_KEY)) : randomUUID(); | ||
| taskStatusService.recordRetryAttempt(id, jobData); | ||
|
|
||
| throw new IllegalStateException("Task with retry attempts failed to complete due to unexpected errors!"); | ||
| } | ||
|
|
||
| return executionInfo().from(executionInfo) | ||
| .withJobData(jobData) | ||
| .withExecutionStatus(INPROGRESS) | ||
| .withShouldRetry(true) | ||
| .build(); | ||
| } | ||
|
|
||
| @Override | ||
| public Optional<List<Long>> getRetryDurationsInSecs() { | ||
| // Return 3 retry attempts with delays: 1s, 2s, 3s | ||
| return Optional.of(List.of(1L, 2L, 3L)); | ||
| } | ||
| } | ||
|
|
||
54 changes: 54 additions & 0 deletions
54
...tion-tests/src/test/java/uk/gov/hmcts/cp/taskmanager/integration/tasks/TestErrorTask.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| package uk.gov.hmcts.cp.taskmanager.integration.tasks; | ||
|
|
||
| import static java.util.UUID.fromString; | ||
| import static java.util.UUID.randomUUID; | ||
| import static uk.gov.hmcts.cp.taskmanager.domain.ExecutionInfo.executionInfo; | ||
| import static uk.gov.hmcts.cp.taskmanager.domain.ExecutionStatus.COMPLETED; | ||
| import static uk.gov.hmcts.cp.taskmanager.integration.PostgresIntegrationTestBase.ERROR_KEY; | ||
| import static uk.gov.hmcts.cp.taskmanager.integration.PostgresIntegrationTestBase.ID_KEY; | ||
|
|
||
| import uk.gov.hmcts.cp.taskmanager.domain.ExecutionInfo; | ||
| import uk.gov.hmcts.cp.taskmanager.integration.service.TaskStatusService; | ||
| import uk.gov.hmcts.cp.taskmanager.service.task.ExecutableTask; | ||
| import uk.gov.hmcts.cp.taskmanager.service.task.Task; | ||
|
|
||
| import java.util.UUID; | ||
|
|
||
| import jakarta.json.JsonObject; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| /** | ||
| * Test task that fails to complete immediately. | ||
| * Used for integration testing of tasks that throw unexpected exception during task execution. | ||
| */ | ||
| @Task("TEST_ERROR_TASK") | ||
| @Component | ||
| public class TestErrorTask implements ExecutableTask { | ||
|
|
||
| @Autowired | ||
| private TaskStatusService taskStatusService; | ||
|
|
||
| private static final Logger logger = LoggerFactory.getLogger(TestErrorTask.class); | ||
|
|
||
| @Override | ||
| public ExecutionInfo execute(ExecutionInfo executionInfo) { | ||
| final JsonObject jobData = executionInfo.getJobData(); | ||
|
|
||
| logger.info("TestErrorTask executing for job: {}", jobData); | ||
|
|
||
| if (jobData.containsKey(ERROR_KEY)) { | ||
|
||
| final UUID id = jobData.containsKey(ID_KEY) ? fromString(jobData.getString(ID_KEY)) : randomUUID(); | ||
| taskStatusService.recordRetryAttempt(id, jobData); | ||
|
|
||
| throw new IllegalStateException("Task failed to complete due to unexpected errors!"); | ||
| } | ||
|
|
||
| return executionInfo().from(executionInfo) | ||
| .withExecutionStatus(COMPLETED) | ||
| .build(); | ||
| } | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can simplify this and just throw an exception without the if and also not return..
just these three lines will do
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done