Skip to content

Commit 5bbe4c0

Browse files
committed
BAU: Make integration tests rerunnable after failures
Refactor SqsQueueExtension to handle container recreation: - Always create fresh queue to handle container restarts - Improve queue creation logic with fallback handling - Add error handling for queue purge operations - Remove unused Optional import
1 parent 05361d5 commit 5bbe4c0

File tree

3 files changed

+24
-16
lines changed

3 files changed

+24
-16
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ spotless {
200200

201201
dockerCompose {
202202
buildBeforeUp = true
203-
forceRecreate = false
203+
forceRecreate = true
204204

205205
if (System.getProperty("os.arch") == "aarch64") {
206206
useComposeFiles = [

docker-compose.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ services:
2020
- di-authentication-api-net
2121
ports:
2222
- 45678:45678
23+
healthcheck:
24+
test: curl -f http://localhost:45678/_localstack/health || exit 1
25+
interval: 5s
26+
timeout: 10s
27+
retries: 5
2328

2429
redis:
2530
image: redis:6.2.19-alpine@sha256:7fe72c486b910f6b1a9769c937dad5d63648ddee82e056f47417542dd40825bb

shared-test/src/main/java/uk/gov/di/authentication/sharedtest/extensions/SqsQueueExtension.java

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import java.net.URI;
2020
import java.util.List;
21-
import java.util.Optional;
2221
import java.util.stream.Collectors;
2322

2423
import static java.text.MessageFormat.format;
@@ -91,26 +90,30 @@ public void beforeAll(ExtensionContext context) {
9190
context.getTestClass().map(Class::getSimpleName).orElse("unknown"),
9291
queueNameSuffix);
9392
var truncatedQueueName = queueName.substring(0, Math.min(80, queueName.length()));
94-
queueUrl =
95-
getQueueUrlFor(truncatedQueueName).orElseGet(() -> createQueue(truncatedQueueName));
96-
sqsClient.purgeQueue(PurgeQueueRequest.builder().queueUrl(queueUrl).build());
97-
}
9893

99-
private Optional<String> getQueueUrlFor(String queueName) {
94+
// Always create a fresh queue to handle container recreation
95+
queueUrl = createQueue(truncatedQueueName);
96+
97+
// Clear any existing messages
10098
try {
101-
return Optional.of(
102-
sqsClient
103-
.getQueueUrl(GetQueueUrlRequest.builder().queueName(queueName).build())
104-
.toString());
105-
} catch (QueueDoesNotExistException ignored) {
106-
return Optional.empty();
99+
sqsClient.purgeQueue(PurgeQueueRequest.builder().queueUrl(queueUrl).build());
100+
} catch (Exception e) {
101+
// Ignore purge errors as queue might be newly created
107102
}
108103
}
109104

110105
private String createQueue(String queueName) {
111-
return sqsClient
112-
.createQueue(CreateQueueRequest.builder().queueName(queueName).build())
113-
.queueUrl();
106+
try {
107+
// Try to get existing queue first
108+
return sqsClient
109+
.getQueueUrl(GetQueueUrlRequest.builder().queueName(queueName).build())
110+
.queueUrl();
111+
} catch (QueueDoesNotExistException e) {
112+
// Create new queue if it doesn't exist
113+
return sqsClient
114+
.createQueue(CreateQueueRequest.builder().queueName(queueName).build())
115+
.queueUrl();
116+
}
114117
}
115118

116119
private List<Message> getMessages(int numberOfMessages) {

0 commit comments

Comments
 (0)