From 750075c74a81b349969cecb8c1cd14a9f03be97a Mon Sep 17 00:00:00 2001 From: CRoberto1926 <3205022+CRoberto1926@users.noreply.github.com> Date: Mon, 28 Oct 2024 14:15:22 +0100 Subject: [PATCH] Closes #47 - Fix flakiness of some tests --- .../jobs/AbstractKadaiJobAccTest.java | 16 ++++++++----- ...rityWithWorkingDaysCalculationAccTest.java | 23 ++++++++++++++++++- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/lib/kadai-core-test/src/test/java/acceptance/jobs/AbstractKadaiJobAccTest.java b/lib/kadai-core-test/src/test/java/acceptance/jobs/AbstractKadaiJobAccTest.java index aa3dcd056..73130bc73 100644 --- a/lib/kadai-core-test/src/test/java/acceptance/jobs/AbstractKadaiJobAccTest.java +++ b/lib/kadai-core-test/src/test/java/acceptance/jobs/AbstractKadaiJobAccTest.java @@ -57,9 +57,8 @@ import org.junit.jupiter.api.function.ThrowingConsumer; /** - * Acceptance test for the AbstractKadaiJob class. - * This is not an abstract test class, but a concrete test implementation that verifies - * the functionality of the AbstractKadaiJob class. + * Acceptance test for the AbstractKadaiJob class. This is not an abstract test class, but a + * concrete test implementation that verifies the functionality of the AbstractKadaiJob class. */ @KadaiIntegrationTest class AbstractKadaiJobAccTest { @@ -114,6 +113,7 @@ Stream should_DeleteOldCleanupJobs_When_InitializingSchedule() { Pair.of("Delete Old Task Cleanup Jobs", TaskCleanupJob.class), Pair.of("Delete Old History Cleanup Jobs", HistoryCleanupJob.class), Pair.of("Delete Old Workbasket Cleanup Jobs", WorkbasketCleanupJob.class)); + ThrowingConsumer>> test = t -> { for (int i = 0; i < 10; i++) { @@ -126,18 +126,22 @@ Stream should_DeleteOldCleanupJobs_When_InitializingSchedule() { kadaiEngine.getJobService().createJob(job); } - List jobsToRun = jobMapper.findJobsToRun(Instant.now()); + final Instant now = Instant.now(); List cleanupJobs = - jobsToRun.stream() + jobMapper.findJobsToRun(now).stream() .filter(scheduledJob -> scheduledJob.getType().equals(t.getRight().getName())) .toList(); + assertThat(cleanupJobs).isNotEmpty(); + AbstractKadaiJob.initializeSchedule(kadaiEngine, t.getRight()); - jobsToRun = jobMapper.findJobsToRun(Instant.now()); + final List jobsToRun = jobMapper.findJobsToRun(now); + assertThat(jobsToRun).isNotEmpty(); assertThat(jobsToRun).doesNotContainAnyElementsOf(cleanupJobs); + cleanupJobs(); }; diff --git a/lib/kadai-core/src/test/java/acceptance/task/ServiceLevelPriorityWithWorkingDaysCalculationAccTest.java b/lib/kadai-core/src/test/java/acceptance/task/ServiceLevelPriorityWithWorkingDaysCalculationAccTest.java index d260b4fab..bce8657c4 100644 --- a/lib/kadai-core/src/test/java/acceptance/task/ServiceLevelPriorityWithWorkingDaysCalculationAccTest.java +++ b/lib/kadai-core/src/test/java/acceptance/task/ServiceLevelPriorityWithWorkingDaysCalculationAccTest.java @@ -38,6 +38,7 @@ import io.kadai.workbasket.api.exceptions.NotAuthorizedOnWorkbasketException; import java.time.Duration; import java.time.Instant; +import java.time.LocalDate; import java.time.temporal.ChronoUnit; import java.util.ArrayList; import java.util.List; @@ -48,7 +49,7 @@ import org.junit.jupiter.api.extension.ExtendWith; @ExtendWith(JaasExtension.class) -public class ServiceLevelPriorityWithWorkingDaysCalculationAccTest extends AbstractAccTest { +class ServiceLevelPriorityWithWorkingDaysCalculationAccTest extends AbstractAccTest { private static ClassificationService classificationService; @@ -184,6 +185,26 @@ void should_VerifyThatCreateAndPlannedAreClose() throws Exception { assertThat(createdTask.getPlanned()).isEqualTo(planned); assertThat(createdTask.getCreated()).isBefore(createdTask.getPlanned()); + LocalDate localDateNow = + LocalDate.ofInstant(now, kadaiConfiguration.getWorkingTimeScheduleTimeZone()); + LocalDate localDateInTwoHours = + LocalDate.ofInstant(inTwoHours, kadaiConfiguration.getWorkingTimeScheduleTimeZone()); + + if (!localDateNow.isEqual(localDateInTwoHours) + && (!workingTimeCalculator.isWorkingDay(now) + || !workingTimeCalculator.isWorkingDay(inTwoHours))) { + System.err.println( + """ + The next .isCloseTo assertion fails if now lies \ + between 22:00:00 and 23:59:59 of a working day that precedes a non working day, \ + or if now lies between 22:00:00 and 23:59:59 of a non working day. \ + If you are reading this error message, \ + now satisfies one of the two conditions just described. \ + To avoid stopping a CI pipeline due to this flakiness, \ + the .isCloseTo assertion will be skipped."""); + return; + } + assertThat(createdTask.getPlanned()) .isCloseTo( moveForwardToWorkingDay(createdTask.getCreated()),