Skip to content

Commit e429b7d

Browse files
author
Alex McAusland
committed
AuditCaseRemoteOperationIT was flakey because it checked for three WireMock requests without waiting for all three to have been delivered
1 parent 8d7d4a8 commit e429b7d

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
lines changed

src/test/java/uk/gov/hmcts/ccd/domain/service/lau/AuditCaseRemoteOperationIT.java

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import com.fasterxml.jackson.core.JsonProcessingException;
44
import com.fasterxml.jackson.databind.ObjectMapper;
55
import com.github.tomakehurst.wiremock.client.WireMock;
6-
import com.github.tomakehurst.wiremock.stubbing.ServeEvent;
76
import org.junit.After;
87
import org.junit.Before;
98
import org.junit.Test;
@@ -37,7 +36,6 @@
3736
import java.time.LocalDateTime;
3837
import java.time.ZoneOffset;
3938
import java.time.ZonedDateTime;
40-
import java.util.List;
4139
import java.util.concurrent.TimeUnit;
4240

4341
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
@@ -159,7 +157,7 @@ public void shouldMakeAuditRequestWhenPerformingCaseSearch() throws JsonProcessi
159157
.build();
160158

161159
auditService.audit(auditContext);
162-
waitForPossibleAuditResponse(SEARCH_AUDIT_ENDPOINT);
160+
waitForPossibleAuditResponse(SEARCH_AUDIT_ENDPOINT, 1);
163161

164162
Mockito.verify(auditCaseRemoteOperation).postCaseSearch(captor.capture(), ArgumentMatchers.any());
165163
assertThat(captor.getValue().getOperationType(), is(equalTo(AuditOperationType.SEARCH_CASE.getLabel())));
@@ -200,7 +198,7 @@ public void shouldMakeAuditRequestWhenPerformingCaseAction() throws JsonProcessi
200198
ArgumentCaptor<AuditEntry> captor = ArgumentCaptor.forClass(AuditEntry.class);
201199

202200
auditService.audit(auditContext);
203-
waitForPossibleAuditResponse(ACTION_AUDIT_ENDPOINT);
201+
waitForPossibleAuditResponse(ACTION_AUDIT_ENDPOINT, 1);
204202

205203
Mockito.verify(auditCaseRemoteOperation).postCaseAction(captor.capture(), ArgumentMatchers.any());
206204
assertThat(captor.getValue().getOperationType(), is(equalTo(AuditOperationType.CASE_ACCESSED.getLabel())));
@@ -238,7 +236,7 @@ public void shouldNotThrowExceptionInAuditServiceIfLauIsDownAndRetry()
238236
.willReturn(aResponse().withStatus(AUDIT_UNAUTHORISED_HTTP_STATUS)));
239237

240238
auditService.audit(auditContext);
241-
waitForPossibleAuditResponse(ACTION_AUDIT_ENDPOINT);
239+
waitForPossibleAuditResponse(ACTION_AUDIT_ENDPOINT, 3);
242240

243241
verifyWireMock(3, postRequestedFor(urlEqualTo(ACTION_AUDIT_ENDPOINT))
244242
.withRequestBody(equalToJson(EXPECTED_CASE_ACTION_LOG_JSON)));
@@ -269,28 +267,30 @@ public void shouldNotThrowExceptionInAuditServiceIfLauSearchIsDownAndRetry()
269267
.build();
270268

271269
auditService.audit(auditContext);
272-
waitForPossibleAuditResponse(SEARCH_AUDIT_ENDPOINT);
270+
waitForPossibleAuditResponse(SEARCH_AUDIT_ENDPOINT, 3);
273271

274272
verifyWireMock(3, postRequestedFor(urlEqualTo(SEARCH_AUDIT_ENDPOINT))
275273
.withRequestBody(equalToJson(EXPECTED_CASE_SEARCH_LOG_JSON)));
276274
}
277275

278276
private void waitForPossibleAuditResponse(String pathPrefix) throws InterruptedException {
279-
List<ServeEvent> allServeEvents;
280-
boolean found = false;
281-
long finishTime = ZonedDateTime.now().toInstant().toEpochMilli() + ASYNC_DELAY_TIMEOUT_MILLISECONDS;
282-
283-
while (ZonedDateTime.now().toInstant().toEpochMilli() < finishTime && !found) {
284-
allServeEvents = getAllServeEvents();
285-
for (ServeEvent serveEvent : allServeEvents) {
286-
if (serveEvent.getRequest().getUrl().startsWith(pathPrefix)) {
287-
found = true;
288-
}
289-
}
290-
if (!found) {
291-
TimeUnit.MILLISECONDS.sleep(ASYNC_DELAY_INTERVAL_MILLISECONDS);
292-
}
277+
waitForPossibleAuditResponse(pathPrefix, 1);
278+
}
279+
280+
private void waitForPossibleAuditResponse(String pathPrefix, int expectedCount) throws InterruptedException {
281+
long finishTime = System.currentTimeMillis() + ASYNC_DELAY_TIMEOUT_MILLISECONDS;
282+
long currentCount = countServeEvents(pathPrefix);
283+
284+
while (System.currentTimeMillis() < finishTime && currentCount < expectedCount) {
285+
TimeUnit.MILLISECONDS.sleep(ASYNC_DELAY_INTERVAL_MILLISECONDS);
286+
currentCount = countServeEvents(pathPrefix);
293287
}
294288
}
295289

290+
private long countServeEvents(String pathPrefix) {
291+
return getAllServeEvents().stream()
292+
.filter(serveEvent -> serveEvent.getRequest().getUrl().startsWith(pathPrefix))
293+
.count();
294+
}
295+
296296
}

0 commit comments

Comments
 (0)