Skip to content

Commit

Permalink
Ensure Time window is initialised and tested
Browse files Browse the repository at this point in the history
  • Loading branch information
sebr72 committed Aug 28, 2024
1 parent a2f751e commit bdcd26b
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand All @@ -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(
Expand All @@ -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));
}
}
Original file line number Diff line number Diff line change
@@ -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."));
}
}

0 comments on commit bdcd26b

Please sign in to comment.