From e429b7d421209ef086406398abdb80e8c2a70b74 Mon Sep 17 00:00:00 2001 From: Alex McAusland Date: Mon, 22 Sep 2025 20:18:16 +0100 Subject: [PATCH] AuditCaseRemoteOperationIT was flakey because it checked for three WireMock requests without waiting for all three to have been delivered --- .../lau/AuditCaseRemoteOperationIT.java | 40 +++++++++---------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/src/test/java/uk/gov/hmcts/ccd/domain/service/lau/AuditCaseRemoteOperationIT.java b/src/test/java/uk/gov/hmcts/ccd/domain/service/lau/AuditCaseRemoteOperationIT.java index 75e0c77cba..66e0133728 100644 --- a/src/test/java/uk/gov/hmcts/ccd/domain/service/lau/AuditCaseRemoteOperationIT.java +++ b/src/test/java/uk/gov/hmcts/ccd/domain/service/lau/AuditCaseRemoteOperationIT.java @@ -3,7 +3,6 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.github.tomakehurst.wiremock.client.WireMock; -import com.github.tomakehurst.wiremock.stubbing.ServeEvent; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -37,7 +36,6 @@ import java.time.LocalDateTime; import java.time.ZoneOffset; import java.time.ZonedDateTime; -import java.util.List; import java.util.concurrent.TimeUnit; import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; @@ -159,7 +157,7 @@ public void shouldMakeAuditRequestWhenPerformingCaseSearch() throws JsonProcessi .build(); auditService.audit(auditContext); - waitForPossibleAuditResponse(SEARCH_AUDIT_ENDPOINT); + waitForPossibleAuditResponse(SEARCH_AUDIT_ENDPOINT, 1); Mockito.verify(auditCaseRemoteOperation).postCaseSearch(captor.capture(), ArgumentMatchers.any()); assertThat(captor.getValue().getOperationType(), is(equalTo(AuditOperationType.SEARCH_CASE.getLabel()))); @@ -200,7 +198,7 @@ public void shouldMakeAuditRequestWhenPerformingCaseAction() throws JsonProcessi ArgumentCaptor captor = ArgumentCaptor.forClass(AuditEntry.class); auditService.audit(auditContext); - waitForPossibleAuditResponse(ACTION_AUDIT_ENDPOINT); + waitForPossibleAuditResponse(ACTION_AUDIT_ENDPOINT, 1); Mockito.verify(auditCaseRemoteOperation).postCaseAction(captor.capture(), ArgumentMatchers.any()); assertThat(captor.getValue().getOperationType(), is(equalTo(AuditOperationType.CASE_ACCESSED.getLabel()))); @@ -238,7 +236,7 @@ public void shouldNotThrowExceptionInAuditServiceIfLauIsDownAndRetry() .willReturn(aResponse().withStatus(AUDIT_UNAUTHORISED_HTTP_STATUS))); auditService.audit(auditContext); - waitForPossibleAuditResponse(ACTION_AUDIT_ENDPOINT); + waitForPossibleAuditResponse(ACTION_AUDIT_ENDPOINT, 3); verifyWireMock(3, postRequestedFor(urlEqualTo(ACTION_AUDIT_ENDPOINT)) .withRequestBody(equalToJson(EXPECTED_CASE_ACTION_LOG_JSON))); @@ -269,28 +267,30 @@ public void shouldNotThrowExceptionInAuditServiceIfLauSearchIsDownAndRetry() .build(); auditService.audit(auditContext); - waitForPossibleAuditResponse(SEARCH_AUDIT_ENDPOINT); + waitForPossibleAuditResponse(SEARCH_AUDIT_ENDPOINT, 3); verifyWireMock(3, postRequestedFor(urlEqualTo(SEARCH_AUDIT_ENDPOINT)) .withRequestBody(equalToJson(EXPECTED_CASE_SEARCH_LOG_JSON))); } private void waitForPossibleAuditResponse(String pathPrefix) throws InterruptedException { - List allServeEvents; - boolean found = false; - long finishTime = ZonedDateTime.now().toInstant().toEpochMilli() + ASYNC_DELAY_TIMEOUT_MILLISECONDS; - - while (ZonedDateTime.now().toInstant().toEpochMilli() < finishTime && !found) { - allServeEvents = getAllServeEvents(); - for (ServeEvent serveEvent : allServeEvents) { - if (serveEvent.getRequest().getUrl().startsWith(pathPrefix)) { - found = true; - } - } - if (!found) { - TimeUnit.MILLISECONDS.sleep(ASYNC_DELAY_INTERVAL_MILLISECONDS); - } + waitForPossibleAuditResponse(pathPrefix, 1); + } + + private void waitForPossibleAuditResponse(String pathPrefix, int expectedCount) throws InterruptedException { + long finishTime = System.currentTimeMillis() + ASYNC_DELAY_TIMEOUT_MILLISECONDS; + long currentCount = countServeEvents(pathPrefix); + + while (System.currentTimeMillis() < finishTime && currentCount < expectedCount) { + TimeUnit.MILLISECONDS.sleep(ASYNC_DELAY_INTERVAL_MILLISECONDS); + currentCount = countServeEvents(pathPrefix); } } + private long countServeEvents(String pathPrefix) { + return getAllServeEvents().stream() + .filter(serveEvent -> serveEvent.getRequest().getUrl().startsWith(pathPrefix)) + .count(); + } + }