|
| 1 | +package io.awspring.cloud.sqs.integration; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 4 | +import io.awspring.cloud.sqs.MessageHeaderUtils; |
| 5 | +import io.awspring.cloud.sqs.annotation.SqsListener; |
| 6 | +import io.awspring.cloud.sqs.config.SqsBootstrapConfiguration; |
| 7 | +import io.awspring.cloud.sqs.config.SqsMessageListenerContainerFactory; |
| 8 | +import io.awspring.cloud.sqs.listener.SqsHeaders; |
| 9 | +import io.awspring.cloud.sqs.listener.errorhandler.ImmediateRetryAsyncErrorHandler; |
| 10 | +import io.awspring.cloud.sqs.operations.SqsTemplate; |
| 11 | +import org.junit.jupiter.api.BeforeAll; |
| 12 | +import org.junit.jupiter.api.Test; |
| 13 | +import org.slf4j.Logger; |
| 14 | +import org.slf4j.LoggerFactory; |
| 15 | +import org.springframework.beans.factory.annotation.Autowired; |
| 16 | +import org.springframework.boot.test.context.SpringBootTest; |
| 17 | +import org.springframework.context.annotation.Bean; |
| 18 | +import org.springframework.context.annotation.Configuration; |
| 19 | +import org.springframework.context.annotation.Import; |
| 20 | +import org.springframework.messaging.Message; |
| 21 | +import org.springframework.messaging.handler.annotation.Header; |
| 22 | +import org.springframework.messaging.support.MessageBuilder; |
| 23 | +import software.amazon.awssdk.services.sqs.SqsAsyncClient; |
| 24 | +import software.amazon.awssdk.services.sqs.model.QueueAttributeName; |
| 25 | + |
| 26 | +import java.time.Duration; |
| 27 | +import java.util.Collections; |
| 28 | +import java.util.List; |
| 29 | +import java.util.Map; |
| 30 | +import java.util.UUID; |
| 31 | +import java.util.concurrent.CompletableFuture; |
| 32 | +import java.util.concurrent.ConcurrentHashMap; |
| 33 | +import java.util.concurrent.CountDownLatch; |
| 34 | +import java.util.concurrent.TimeUnit; |
| 35 | +import java.util.stream.Collectors; |
| 36 | +import java.util.stream.IntStream; |
| 37 | + |
| 38 | +import static java.util.Collections.singletonMap; |
| 39 | +import static org.assertj.core.api.Assertions.assertThat; |
| 40 | + |
| 41 | +/** |
| 42 | + * Integration tests for SQS ErrorHandler integration. |
| 43 | + * |
| 44 | + * @author Bruno Garcia |
| 45 | + * @author Rafael Pavarini |
| 46 | + */ |
| 47 | + |
| 48 | +@SpringBootTest |
| 49 | +public class SqsErrorHandlerIntegrationTests extends BaseSqsIntegrationTest { |
| 50 | + |
| 51 | + private static final Logger logger = LoggerFactory.getLogger(SqsErrorHandlerIntegrationTests.class); |
| 52 | + |
| 53 | + static final String SUCCESS_VISIBILITY_TIMEOUT_TO_ZERO_QUEUE_NAME = "success_visibility_timeout_to_zero_test_queue"; |
| 54 | + |
| 55 | + static final String SUCCESS_VISIBILITY_TIMEOUT_TO_ZERO_BATCH_QUEUE_NAME = "success_visibility_batch_timeout_to_zero_test_queue"; |
| 56 | + |
| 57 | + static final String SUCCESS_VISIBILITY_TIMEOUT_TO_ZERO_FACTORY = "receivesMessageErrorFactory"; |
| 58 | + |
| 59 | + @BeforeAll |
| 60 | + static void beforeTests() { |
| 61 | + SqsAsyncClient client = createAsyncClient(); |
| 62 | + CompletableFuture.allOf( |
| 63 | + createQueue(client, SUCCESS_VISIBILITY_TIMEOUT_TO_ZERO_QUEUE_NAME, |
| 64 | + singletonMap(QueueAttributeName.VISIBILITY_TIMEOUT, "500")), |
| 65 | + createQueue(client, SUCCESS_VISIBILITY_TIMEOUT_TO_ZERO_BATCH_QUEUE_NAME, |
| 66 | + singletonMap(QueueAttributeName.VISIBILITY_TIMEOUT, "500"))) |
| 67 | + .join(); |
| 68 | + } |
| 69 | + |
| 70 | + |
| 71 | + @Autowired |
| 72 | + LatchContainer latchContainer; |
| 73 | + |
| 74 | + @Autowired |
| 75 | + SqsTemplate sqsTemplate; |
| 76 | + |
| 77 | + @Autowired |
| 78 | + ObjectMapper objectMapper; |
| 79 | + |
| 80 | + @Test |
| 81 | + void receivesMessageVisibilityTimeout() throws Exception { |
| 82 | + String messageBody = UUID.randomUUID().toString(); |
| 83 | + sqsTemplate.send(SUCCESS_VISIBILITY_TIMEOUT_TO_ZERO_QUEUE_NAME, messageBody); |
| 84 | + logger.debug("Sent message to queue {} with messageBody {}", SUCCESS_VISIBILITY_TIMEOUT_TO_ZERO_QUEUE_NAME, |
| 85 | + messageBody); |
| 86 | + |
| 87 | + assertThat(latchContainer.receivesRetryMessageQuicklyLatch.await(10, TimeUnit.SECONDS)).isTrue(); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + void receivesMessageVisibilityTimeoutBatch() throws Exception { |
| 92 | + List<Message<String>> messages = create10Messages("receivesMessageVisibilityTimeoutBatch"); |
| 93 | + |
| 94 | + sqsTemplate.sendManyAsync(SUCCESS_VISIBILITY_TIMEOUT_TO_ZERO_BATCH_QUEUE_NAME, messages); |
| 95 | + logger.debug("Sent message to queue {} with messageBody {}", |
| 96 | + SUCCESS_VISIBILITY_TIMEOUT_TO_ZERO_BATCH_QUEUE_NAME, messages); |
| 97 | + |
| 98 | + assertThat(latchContainer.receivesRetryBatchMessageQuicklyLatch.await(10, TimeUnit.SECONDS)).isTrue(); |
| 99 | + } |
| 100 | + |
| 101 | + static class ImmediateRetryAsyncErrorHandlerListener { |
| 102 | + |
| 103 | + @Autowired |
| 104 | + LatchContainer latchContainer; |
| 105 | + |
| 106 | + private static final Map<String, Long> previousReceivedMessageTimestamps = new ConcurrentHashMap<>(); |
| 107 | + |
| 108 | + private static final int MAX_EXPECTED_ELAPSED_TIME_BETWEEN_MSG_RECEIVES_IN_MS = 5000; |
| 109 | + |
| 110 | + @SqsListener(queueNames = SUCCESS_VISIBILITY_TIMEOUT_TO_ZERO_QUEUE_NAME, messageVisibilitySeconds = "500", factory = SUCCESS_VISIBILITY_TIMEOUT_TO_ZERO_FACTORY, id = "visibilityErrHandler") |
| 111 | + CompletableFuture<Void> listen(Message<String> message, |
| 112 | + @Header(SqsHeaders.SQS_QUEUE_NAME_HEADER) String queueName) { |
| 113 | + logger.info("Received message {} from queue {}", message, queueName); |
| 114 | + String msgId = MessageHeaderUtils.getHeader(message, "id", UUID.class).toString(); |
| 115 | + Long prevReceivedMessageTimestamp = previousReceivedMessageTimestamps.get(msgId); |
| 116 | + if (prevReceivedMessageTimestamp == null) { |
| 117 | + previousReceivedMessageTimestamps.put(msgId, System.currentTimeMillis()); |
| 118 | + return CompletableFuture |
| 119 | + .failedFuture(new RuntimeException("Expected exception from visibility-err-handler")); |
| 120 | + } |
| 121 | + |
| 122 | + long elapsedTimeBetweenMessageReceivesInMs = System.currentTimeMillis() - prevReceivedMessageTimestamp; |
| 123 | + if (elapsedTimeBetweenMessageReceivesInMs < MAX_EXPECTED_ELAPSED_TIME_BETWEEN_MSG_RECEIVES_IN_MS) { |
| 124 | + latchContainer.receivesRetryMessageQuicklyLatch.countDown(); |
| 125 | + } |
| 126 | + |
| 127 | + return CompletableFuture.completedFuture(null); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + static class ImmediateRetryAsyncBatchErrorHandlerListener { |
| 132 | + |
| 133 | + @Autowired |
| 134 | + LatchContainer latchContainer; |
| 135 | + |
| 136 | + private static final Map<String, Long> previousReceivedMessageTimestamps = new ConcurrentHashMap<>(); |
| 137 | + |
| 138 | + private static final int MAX_EXPECTED_ELAPSED_TIME_BETWEEN_BATCH_MSG_RECEIVES_IN_MS = 5000; |
| 139 | + |
| 140 | + @SqsListener(queueNames = SUCCESS_VISIBILITY_TIMEOUT_TO_ZERO_BATCH_QUEUE_NAME, messageVisibilitySeconds = "500", factory = SUCCESS_VISIBILITY_TIMEOUT_TO_ZERO_FACTORY, id = "visibilityBatchErrHandler") |
| 141 | + CompletableFuture<Void> listen(List<Message<String>> messages) { |
| 142 | + logger.info("Received messages {} from queue {}", MessageHeaderUtils.getId(messages), |
| 143 | + messages.get(0).getHeaders().get(SqsHeaders.SQS_QUEUE_NAME_HEADER)); |
| 144 | + |
| 145 | + for(Message<String> message : messages) { |
| 146 | + String msgId = MessageHeaderUtils.getHeader(message, "id", UUID.class).toString(); |
| 147 | + if (!previousReceivedMessageTimestamps.containsKey(msgId)) { |
| 148 | + previousReceivedMessageTimestamps.put(msgId, System.currentTimeMillis()); |
| 149 | + return CompletableFuture.failedFuture(new RuntimeException("Expected exception from visibility-err-handler")); |
| 150 | + } |
| 151 | + else { |
| 152 | + long timediff = System.currentTimeMillis() - previousReceivedMessageTimestamps.get(msgId); |
| 153 | + if (MAX_EXPECTED_ELAPSED_TIME_BETWEEN_BATCH_MSG_RECEIVES_IN_MS > timediff) { |
| 154 | + latchContainer.receivesRetryBatchMessageQuicklyLatch.countDown(); |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + return CompletableFuture.completedFuture(null); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + static class LatchContainer { |
| 164 | + final CountDownLatch receivesRetryMessageQuicklyLatch = new CountDownLatch(1); |
| 165 | + final CountDownLatch receivesRetryBatchMessageQuicklyLatch = new CountDownLatch(10); |
| 166 | + } |
| 167 | + |
| 168 | + @Import(SqsBootstrapConfiguration.class) |
| 169 | + @Configuration |
| 170 | + static class SQSConfiguration { |
| 171 | + |
| 172 | + // @formatter:off |
| 173 | + @Bean(name = SUCCESS_VISIBILITY_TIMEOUT_TO_ZERO_FACTORY) |
| 174 | + public SqsMessageListenerContainerFactory<Object> errorHandlerVisibility() { |
| 175 | + return SqsMessageListenerContainerFactory |
| 176 | + .builder() |
| 177 | + .configure(options -> options |
| 178 | + .maxConcurrentMessages(10) |
| 179 | + .pollTimeout(Duration.ofSeconds(10)) |
| 180 | + .maxMessagesPerPoll(10) |
| 181 | + .queueAttributeNames(Collections.singletonList(QueueAttributeName.QUEUE_ARN)) |
| 182 | + .maxDelayBetweenPolls(Duration.ofSeconds(10))) |
| 183 | + .errorHandler(new ImmediateRetryAsyncErrorHandler<>()) |
| 184 | + .sqsAsyncClientSupplier(BaseSqsIntegrationTest::createAsyncClient) |
| 185 | + .build(); |
| 186 | + } |
| 187 | + // @formatter:on |
| 188 | + |
| 189 | + @Bean |
| 190 | + ImmediateRetryAsyncErrorHandlerListener immediateRetryAsyncErrorHandlerListener() { |
| 191 | + return new ImmediateRetryAsyncErrorHandlerListener(); |
| 192 | + } |
| 193 | + |
| 194 | + @Bean |
| 195 | + ImmediateRetryAsyncBatchErrorHandlerListener immediateRetryAsyncBatchErrorHandlerListener() { |
| 196 | + return new ImmediateRetryAsyncBatchErrorHandlerListener(); |
| 197 | + } |
| 198 | + |
| 199 | + LatchContainer latchContainer = new LatchContainer(); |
| 200 | + |
| 201 | + @Bean |
| 202 | + LatchContainer latchContainer() { |
| 203 | + return this.latchContainer; |
| 204 | + } |
| 205 | + |
| 206 | + @Bean |
| 207 | + ObjectMapper objectMapper() { |
| 208 | + return new ObjectMapper(); |
| 209 | + } |
| 210 | + |
| 211 | + @Bean |
| 212 | + SqsTemplate sqsTemplate() { |
| 213 | + return SqsTemplate.builder().sqsAsyncClient(BaseSqsIntegrationTest.createAsyncClient()).build(); |
| 214 | + } |
| 215 | + } |
| 216 | + |
| 217 | + private List<Message<String>> create10Messages(String testName) { |
| 218 | + return IntStream.range(0, 10).mapToObj(index -> testName + "-payload-" + index) |
| 219 | + .map(payload -> MessageBuilder.withPayload(payload).build()).collect(Collectors.toList()); |
| 220 | + } |
| 221 | +} |
0 commit comments