From bdcd26b8c6cff43bcdef42228f2b73e9899e71df Mon Sep 17 00:00:00 2001 From: sebr72 Date: Wed, 28 Aug 2024 18:05:20 +0200 Subject: [PATCH] Ensure Time window is initialised and tested --- .../print/metrics/ApplicationStatus.java | 17 ++-- .../print/metrics/ApplicationStatusTest.java | 84 +++++++++++++++++++ 2 files changed, 93 insertions(+), 8 deletions(-) create mode 100644 core/src/test/java/org/mapfish/print/metrics/ApplicationStatusTest.java diff --git a/core/src/main/java/org/mapfish/print/metrics/ApplicationStatus.java b/core/src/main/java/org/mapfish/print/metrics/ApplicationStatus.java index 84da854684..0d6b7cc18b 100644 --- a/core/src/main/java/org/mapfish/print/metrics/ApplicationStatus.java +++ b/core/src/main/java/org/mapfish/print/metrics/ApplicationStatus.java @@ -1,8 +1,8 @@ package org.mapfish.print.metrics; import com.codahale.metrics.health.HealthCheck; +import java.time.Duration; import java.time.Instant; -import java.time.temporal.TemporalAmount; import java.util.Date; import org.mapfish.print.servlet.job.JobQueue; import org.mapfish.print.servlet.job.impl.ThreadPoolJobManager; @@ -13,8 +13,6 @@ class ApplicationStatus extends HealthCheck { @Value("${expectedMaxTime.sinceLastPrint.InSeconds}") private int secondsInFloatingWindow; - private final TemporalAmount timeWindowSize = - java.time.Duration.ofSeconds(secondsInFloatingWindow); @Autowired private JobQueue jobQueue; @Autowired private ThreadPoolJobManager jobManager; @@ -32,10 +30,7 @@ protected Result check() throws Exception { if (jobManager.getLastExecutedJobTimestamp() == null) { return Result.unhealthy("No print job was ever processed by this server. " + health); - } else if (jobManager - .getLastExecutedJobTimestamp() - .toInstant() - .isAfter(getBeginningOfTimeWindow())) { + } else if (hasJobExecutedRecently()) { if (waitingJobsCount > previousNumberOfWaitingJobs) { previousNumberOfWaitingJobs = waitingJobsCount; return Result.unhealthy( @@ -54,7 +49,13 @@ protected Result check() throws Exception { } } + private boolean hasJobExecutedRecently() { + final Instant lastExecutedJobTime = jobManager.getLastExecutedJobTimestamp().toInstant(); + final Instant beginningOfTimeWindow = getBeginningOfTimeWindow(); + return lastExecutedJobTime.isAfter(beginningOfTimeWindow); + } + private Instant getBeginningOfTimeWindow() { - return new Date().toInstant().minus(timeWindowSize); + return new Date().toInstant().minus(Duration.ofSeconds(secondsInFloatingWindow)); } } diff --git a/core/src/test/java/org/mapfish/print/metrics/ApplicationStatusTest.java b/core/src/test/java/org/mapfish/print/metrics/ApplicationStatusTest.java new file mode 100644 index 0000000000..6a5f4f0274 --- /dev/null +++ b/core/src/test/java/org/mapfish/print/metrics/ApplicationStatusTest.java @@ -0,0 +1,84 @@ +package org.mapfish.print.metrics; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; +import static org.mockito.Mockito.when; + +import com.codahale.metrics.health.HealthCheck; +import java.util.Date; +import org.junit.Before; +import org.junit.Test; +import org.mapfish.print.AbstractMapfishSpringTest; +import org.mapfish.print.servlet.job.JobQueue; +import org.mapfish.print.servlet.job.impl.ThreadPoolJobManager; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.springframework.beans.factory.annotation.Autowired; + +public class ApplicationStatusTest extends AbstractMapfishSpringTest { + + @Mock private JobQueue jobQueue; + + @Mock private ThreadPoolJobManager jobManager; + + @Autowired @InjectMocks private ApplicationStatus applicationStatus; + + @Before + public void setUp() throws Exception { + // Initialize mocks created above + MockitoAnnotations.openMocks(this); + } + + @Test + public void testCheck_Success_NoPrintJobs() throws Exception { + when(jobQueue.getWaitingJobsCount()).thenReturn(0L); + + HealthCheck.Result result = applicationStatus.check(); + + assertTrue(result.isHealthy()); + assertEquals("No print job is waiting in the queue.", result.getMessage()); + } + + @Test + public void testCheck_Failed_NoPrintJobs() throws Exception { + when(jobQueue.getWaitingJobsCount()).thenReturn(1L); + when(jobManager.getLastExecutedJobTimestamp()).thenReturn(new Date(0L)); + + try { + applicationStatus.check(); + fail("Expected exception not thrown"); + } catch (RuntimeException e) { + assertEquals( + "No print job was processed by this server, in the last (seconds): 300", e.getMessage()); + } catch (Exception e) { + fail("Incorrect Exception thrown: " + e); + } + } + + @Test + public void testCheck_Success_PrintJobs() throws Exception { + when(jobQueue.getWaitingJobsCount()).thenReturn(10L, 9L); + when(jobManager.getLastExecutedJobTimestamp()).thenReturn(new Date()); + + applicationStatus.check(); + HealthCheck.Result result = applicationStatus.check(); + + assertTrue(result.isHealthy()); + assertTrue(result.getMessage().contains("Print jobs are being dequeued")); + } + + @Test + public void testCheck_Fail_PrintJobsButIncreasing() throws Exception { + when(jobQueue.getWaitingJobsCount()).thenReturn(0L, 1L); + when(jobManager.getLastExecutedJobTimestamp()).thenReturn(new Date()); + + applicationStatus.check(); + HealthCheck.Result result = applicationStatus.check(); + + assertFalse(result.isHealthy()); + assertTrue(result.getMessage().contains("Number of print jobs queued is increasing.")); + } +}