Skip to content

Commit

Permalink
Release3 (#234)
Browse files Browse the repository at this point in the history
* task/JM-7626 (#233)

* task/JM-7889 Fix intermittent failing test (#232)

* task/JM-7889

* Update SchedulerIT.java

* updating failing test in pipeline
  • Loading branch information
akikrahman1 authored Oct 24, 2024
1 parent 3c3359d commit 57a820d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package uk.gov.hmcts.juror.scheduler.controllers;

import com.jayway.jsonpath.JsonPath;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand Down Expand Up @@ -121,53 +121,54 @@ void getJobTasks() throws Exception {

@DisplayName("Disable and enable a scheduled job")
@Test
@Disabled("Fixed test stability")
void disableAndEnableScheduledJob() throws Exception {
String payload = updateJson(getTestDataAsStringFromFile(API_DUMMY_CRON_JOB_JSON),
"key",
uniqueJobKey);

mockMvcPerform(URL_JOBS_API, jwtAdmin, POST, payload).andExpect(status().isOk());
//Give job a few seconds to execute
TimeUnit.SECONDS.sleep(30);

String url = "/job/" + uniqueJobKey;
int tasksCounter;
int counter = 0; // this is required to ensure don't have an infinite loop
MvcResult resultBefore;
do {
resultBefore = mockMvcPerform(url + URL_TASKS, jwtAdmin, GET, "")
.andDo(print())
.andExpect(status().isOk())
.andExpect(jsonPath("$").isArray())
.andReturn();
tasksCounter = JsonPath.read(resultBefore.getResponse().getContentAsString(), "$.length()");
counter++;
} while (tasksCounter == 0 && counter < 100);

resultBefore = mockMvcPerform(url + URL_TASKS, jwtAdmin, GET, "")
.andDo(print())
.andExpect(status().isOk())
.andExpect(jsonPath("$").isArray())
.andReturn();
tasksCounter = JsonPath.read(resultBefore.getResponse().getContentAsString(), "$.length()");

// assert that tasks have been executed
Assertions.assertTrue(tasksCounter > 0);

mockMvcPerform(url + "/disable", jwtAdmin, PUT, "")
.andExpect(status().isAccepted());

MvcResult resultAfter = mockMvcPerform(url + URL_TASKS, jwtAdmin, GET,
MvcResult resultAfterDisable = mockMvcPerform(url + URL_TASKS, jwtAdmin, GET,
"")
.andExpect(status().isOk())
.andExpect(jsonPath("$").isArray())
.andReturn();

//Get count of tasks executed
Integer tasksExecutedBeforeDisable = JsonPath.read(resultAfter.getResponse().getContentAsString(),
Integer tasksExecutedBeforeReEnable = JsonPath.read(resultAfterDisable.getResponse().getContentAsString(),
"$.length()");

//Enable the job
mockMvcPerform(url + "/enable", jwtAdmin, PUT, "")
.andExpect(status().isAccepted());

//Give job a few seconds to execute
TimeUnit.SECONDS.sleep(2);
TimeUnit.SECONDS.sleep(30);

//If the job has successfully resumed, there should now be more tasks executed
mockMvcPerform(url + URL_TASKS, jwtAdmin, GET, "")
.andExpect(status().isOk())
.andExpect(jsonPath("$").isArray())
.andExpect(jsonPath("$", hasSize(greaterThan(tasksExecutedBeforeDisable))));
.andExpect(jsonPath("$", hasSize(greaterThan(tasksExecutedBeforeReEnable))));
}

@DisplayName("Update a scheduled job")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import uk.gov.hmcts.juror.standard.service.exceptions.InternalServerException;
import uk.gov.hmcts.juror.standard.service.exceptions.NotFoundException;

import java.util.TimeZone;

@Service
@Slf4j
@SuppressWarnings("PMD.TooManyMethods")
Expand Down Expand Up @@ -162,7 +164,8 @@ private Trigger cronTriggerBuilder(APIJobDetailsEntity jobDetails) {
return TriggerBuilder
.newTrigger()
.withIdentity(jobDetails.getKey())
.withSchedule(CronScheduleBuilder.cronSchedule(jobDetails.getCronExpression()))
.withSchedule(CronScheduleBuilder.cronSchedule(jobDetails.getCronExpression())
.inTimeZone(TimeZone.getTimeZone("Europe/London")))
.startNow()
.build();
}
Expand Down

0 comments on commit 57a820d

Please sign in to comment.