|
| 1 | +/* |
| 2 | + * Copyright 2013-2026 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.awspring.cloud.autoconfigure.sqs; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | + |
| 20 | +import com.amazon.sqs.javamessaging.AmazonSQSExtendedAsyncClient; |
| 21 | +import io.awspring.cloud.autoconfigure.core.AwsAutoConfiguration; |
| 22 | +import io.awspring.cloud.autoconfigure.core.CredentialsProviderAutoConfiguration; |
| 23 | +import io.awspring.cloud.autoconfigure.core.RegionProviderAutoConfiguration; |
| 24 | +import io.awspring.cloud.sqs.annotation.SqsListener; |
| 25 | +import io.awspring.cloud.sqs.config.SqsBeanNames; |
| 26 | +import io.awspring.cloud.sqs.listener.DefaultListenerContainerRegistry; |
| 27 | +import java.util.concurrent.atomic.AtomicInteger; |
| 28 | +import org.junit.jupiter.api.Test; |
| 29 | +import org.springframework.boot.autoconfigure.AutoConfigurations; |
| 30 | +import org.springframework.boot.test.context.FilteredClassLoader; |
| 31 | +import org.springframework.boot.test.context.SpringBootTest; |
| 32 | +import org.springframework.boot.test.context.runner.ApplicationContextRunner; |
| 33 | +import org.springframework.context.ApplicationContextException; |
| 34 | +import org.springframework.context.annotation.Bean; |
| 35 | +import org.springframework.context.annotation.Configuration; |
| 36 | +import org.testcontainers.junit.jupiter.Container; |
| 37 | +import org.testcontainers.junit.jupiter.Testcontainers; |
| 38 | +import org.testcontainers.localstack.LocalStackContainer; |
| 39 | +import org.testcontainers.shaded.org.bouncycastle.util.Arrays; |
| 40 | +import org.testcontainers.utility.DockerImageName; |
| 41 | +import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; |
| 42 | +import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; |
| 43 | +import software.amazon.awssdk.regions.Region; |
| 44 | +import software.amazon.awssdk.services.sqs.SqsAsyncClient; |
| 45 | +import software.amazon.awssdk.services.sqs.model.QueueDoesNotExistException; |
| 46 | + |
| 47 | +/** |
| 48 | + * Integration tests for SQS listener startup. |
| 49 | + * |
| 50 | + * @author Bruno Garcia |
| 51 | + */ |
| 52 | +@Testcontainers |
| 53 | +@SpringBootTest |
| 54 | +class SqsListenerContainerStartupIntegrationTest { |
| 55 | + |
| 56 | + private static final String EXISTING_QUEUE_NAME = "messaging-greetings-notifications"; |
| 57 | + |
| 58 | + private static final String MISSING_QUEUE_NAME = "not-existing-queue"; |
| 59 | + |
| 60 | + @Container |
| 61 | + static LocalStackContainer localstack = new LocalStackContainer( |
| 62 | + DockerImageName.parse("localstack/localstack:4.4.0")); |
| 63 | + |
| 64 | + static { |
| 65 | + localstack.start(); |
| 66 | + } |
| 67 | + |
| 68 | + private static final String[] BASE_PARAMS = { "spring.cloud.aws.sqs.region=eu-west-1", |
| 69 | + "spring.cloud.aws.sqs.endpoint=" + localstack.getEndpoint(), "spring.cloud.aws.credentials.access-key=noop", |
| 70 | + "spring.cloud.aws.credentials.secret-key=noop", "spring.cloud.aws.region.static=eu-west-1" }; |
| 71 | + |
| 72 | + private static final AutoConfigurations BASE_CONFIGURATIONS = AutoConfigurations.of( |
| 73 | + RegionProviderAutoConfiguration.class, CredentialsProviderAutoConfiguration.class, |
| 74 | + SqsAutoConfiguration.class, AwsAutoConfiguration.class, ExistingAndMissingQueueListenerConfiguration.class); |
| 75 | + |
| 76 | + private final ApplicationContextRunner applicationContextRunnerWithFailStrategy = new ApplicationContextRunner() |
| 77 | + .withClassLoader(new FilteredClassLoader(AmazonSQSExtendedAsyncClient.class)) |
| 78 | + .withPropertyValues(Arrays.append(BASE_PARAMS, "spring.cloud.aws.sqs.queue-not-found-strategy=fail")) |
| 79 | + .withConfiguration(BASE_CONFIGURATIONS); |
| 80 | + |
| 81 | + @Test |
| 82 | + void stopsRegistryWhenOneSqsListenerFailsToResolveQueueOnStartup() { |
| 83 | + createQueue(EXISTING_QUEUE_NAME); |
| 84 | + TrackingListenerContainerRegistry registry = new TrackingListenerContainerRegistry(); |
| 85 | + |
| 86 | + applicationContextRunnerWithFailStrategy.withBean(SqsBeanNames.ENDPOINT_REGISTRY_BEAN_NAME, |
| 87 | + TrackingListenerContainerRegistry.class, () -> registry).run(context -> { |
| 88 | + assertThat(context.getStartupFailure()).isInstanceOf(ApplicationContextException.class) |
| 89 | + .hasRootCauseInstanceOf(QueueDoesNotExistException.class); |
| 90 | + |
| 91 | + assertThat(registry.stopInvocations).hasValue(1); |
| 92 | + assertThat(registry.getListenerContainers()).hasSize(2) |
| 93 | + .allSatisfy(container -> assertThat(container.isRunning()) |
| 94 | + .as("Container %s should be stopped", container.getId()).isFalse()); |
| 95 | + assertThat(registry.isRunning()).isFalse(); |
| 96 | + }); |
| 97 | + } |
| 98 | + |
| 99 | + private static void createQueue(String queueName) { |
| 100 | + try (SqsAsyncClient client = SqsAsyncClient.builder() |
| 101 | + .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("noop", "noop"))) |
| 102 | + .region(Region.EU_WEST_1).endpointOverride(localstack.getEndpoint()).build()) { |
| 103 | + client.createQueue(request -> request.queueName(queueName)).join(); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + static class ExistingAndMissingQueueListener { |
| 108 | + |
| 109 | + @SqsListener(EXISTING_QUEUE_NAME) |
| 110 | + void listenToExistingQueue(String message) { |
| 111 | + } |
| 112 | + |
| 113 | + @SqsListener(MISSING_QUEUE_NAME) |
| 114 | + void listenToMissingQueue(String message) { |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + @Configuration |
| 119 | + static class ExistingAndMissingQueueListenerConfiguration { |
| 120 | + |
| 121 | + @Bean |
| 122 | + ExistingAndMissingQueueListener existingAndMissingQueueListener() { |
| 123 | + return new ExistingAndMissingQueueListener(); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + static class TrackingListenerContainerRegistry extends DefaultListenerContainerRegistry { |
| 128 | + |
| 129 | + private final AtomicInteger stopInvocations = new AtomicInteger(); |
| 130 | + |
| 131 | + @Override |
| 132 | + public void stop() { |
| 133 | + this.stopInvocations.incrementAndGet(); |
| 134 | + super.stop(); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | +} |
0 commit comments