Skip to content

Commit ffc9552

Browse files
committed
feat: add tests for SQS batch configuration properties and behavior
1 parent 874a571 commit ffc9552

File tree

1 file changed

+132
-0
lines changed

1 file changed

+132
-0
lines changed

spring-cloud-aws-autoconfigure/src/test/java/io/awspring/cloud/autoconfigure/sqs/SqsAutoConfigurationTest.java

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import io.awspring.cloud.sqs.listener.QueueNotFoundStrategy;
3636
import io.awspring.cloud.sqs.listener.errorhandler.AsyncErrorHandler;
3737
import io.awspring.cloud.sqs.listener.interceptor.AsyncMessageInterceptor;
38+
import io.awspring.cloud.sqs.operations.BatchingSqsClientAdapter;
3839
import io.awspring.cloud.sqs.operations.SqsTemplate;
3940
import io.awspring.cloud.sqs.support.converter.MessagingMessageConverter;
4041
import io.awspring.cloud.sqs.support.converter.SqsMessagingMessageConverter;
@@ -329,6 +330,137 @@ void configuresMessageConverter() {
329330
});
330331
}
331332

333+
@Test
334+
void sqsBatchAutoConfigurationIsDisabledByDefault() {
335+
this.contextRunner.run(context -> {
336+
assertThat(context).hasSingleBean(SqsAsyncClient.class);
337+
SqsAsyncClient client = context.getBean(SqsAsyncClient.class);
338+
assertThat(client).isNotInstanceOf(BatchingSqsClientAdapter.class);
339+
});
340+
}
341+
342+
@Test
343+
void sqsBatchAutoConfigurationIsEnabled() {
344+
this.contextRunner.withPropertyValues("spring.cloud.aws.sqs.batch.enabled:true").run(context -> {
345+
assertThat(context.getBeansOfType(SqsAsyncClient.class)).hasSize(2);
346+
347+
SqsAsyncClient primary = context.getBean(SqsAsyncClient.class);
348+
assertThat(primary).isInstanceOf(BatchingSqsClientAdapter.class);
349+
350+
assertThat(context).hasBean("sqsAsyncClient");
351+
assertThat(context).hasBean("batchSqsAsyncClient");
352+
});
353+
}
354+
355+
@Test
356+
void sqsBatchConfigurationProperties() {
357+
this.contextRunner.withPropertyValues(
358+
"spring.cloud.aws.sqs.batch.enabled:true",
359+
"spring.cloud.aws.sqs.batch.max-number-of-messages:5",
360+
"spring.cloud.aws.sqs.batch.send-batch-frequency:PT0.5S")
361+
.run(context -> {
362+
SqsAsyncClient client = context.getBean(SqsAsyncClient.class);
363+
assertThat(client).isInstanceOf(BatchingSqsClientAdapter.class);
364+
});
365+
}
366+
367+
@Test
368+
void sqsBatchConfigurationPropertiesWithAllSettings() {
369+
this.contextRunner.withPropertyValues(
370+
"spring.cloud.aws.sqs.batch.enabled:true",
371+
"spring.cloud.aws.sqs.batch.max-number-of-messages:8",
372+
"spring.cloud.aws.sqs.batch.send-batch-frequency:PT1S",
373+
"spring.cloud.aws.sqs.batch.visibility-timeout:PT30S",
374+
"spring.cloud.aws.sqs.batch.wait-time-seconds:PT5S",
375+
"spring.cloud.aws.sqs.batch.system-attribute-names:SentTimestamp,ApproximateReceiveCount",
376+
"spring.cloud.aws.sqs.batch.attribute-names:attr1,attr2")
377+
.run(context -> {
378+
assertThat(context).hasSingleBean(SqsProperties.class);
379+
SqsProperties sqsProperties = context.getBean(SqsProperties.class);
380+
SqsProperties.Batch batchConfig = sqsProperties.getBatch();
381+
382+
assertThat(batchConfig.isEnabled()).isTrue();
383+
assertThat(batchConfig.getMaxNumberOfMessages()).isEqualTo(8);
384+
assertThat(batchConfig.getSendBatchFrequency()).isEqualTo(Duration.ofSeconds(1));
385+
assertThat(batchConfig.getVisibilityTimeout()).isEqualTo(Duration.ofSeconds(30));
386+
assertThat(batchConfig.getWaitTimeSeconds()).isEqualTo(Duration.ofSeconds(5));
387+
assertThat(batchConfig.getSystemAttributeNames()).containsExactly(
388+
software.amazon.awssdk.services.sqs.model.MessageSystemAttributeName.SENT_TIMESTAMP,
389+
software.amazon.awssdk.services.sqs.model.MessageSystemAttributeName.APPROXIMATE_RECEIVE_COUNT);
390+
assertThat(batchConfig.getAttributeNames()).containsExactly("attr1", "attr2");
391+
392+
SqsAsyncClient client = context.getBean(SqsAsyncClient.class);
393+
assertThat(client).isInstanceOf(BatchingSqsClientAdapter.class);
394+
});
395+
}
396+
397+
@Test
398+
void sqsBatchConfigurationPropertiesWithDefaults() {
399+
this.contextRunner.withPropertyValues("spring.cloud.aws.sqs.batch.enabled:false")
400+
.run(context -> {
401+
assertThat(context).hasSingleBean(SqsProperties.class);
402+
SqsProperties sqsProperties = context.getBean(SqsProperties.class);
403+
SqsProperties.Batch batchConfig = sqsProperties.getBatch();
404+
405+
assertThat(batchConfig.isEnabled()).isFalse();
406+
assertThat(batchConfig.getMaxNumberOfMessages()).isNull();
407+
assertThat(batchConfig.getSendBatchFrequency()).isNull();
408+
assertThat(batchConfig.getVisibilityTimeout()).isNull();
409+
assertThat(batchConfig.getWaitTimeSeconds()).isNull();
410+
assertThat(batchConfig.getSystemAttributeNames()).isNull();
411+
assertThat(batchConfig.getAttributeNames()).isNull();
412+
413+
assertThat(context).hasSingleBean(SqsAsyncClient.class);
414+
SqsAsyncClient client = context.getBean(SqsAsyncClient.class);
415+
assertThat(client).isNotInstanceOf(BatchingSqsClientAdapter.class);
416+
});
417+
}
418+
419+
@Test
420+
void sqsBatchConfigurationWithVisibilityTimeout() {
421+
this.contextRunner.withPropertyValues(
422+
"spring.cloud.aws.sqs.batch.enabled:true",
423+
"spring.cloud.aws.sqs.batch.visibility-timeout:PT60S")
424+
.run(context -> {
425+
assertThat(context).hasSingleBean(SqsProperties.class);
426+
SqsProperties sqsProperties = context.getBean(SqsProperties.class);
427+
SqsProperties.Batch batchConfig = sqsProperties.getBatch();
428+
429+
assertThat(batchConfig.isEnabled()).isTrue();
430+
assertThat(batchConfig.getVisibilityTimeout()).isEqualTo(Duration.ofSeconds(60));
431+
});
432+
}
433+
434+
@Test
435+
void sqsBatchConfigurationWithWaitTimeSeconds() {
436+
this.contextRunner.withPropertyValues(
437+
"spring.cloud.aws.sqs.batch.enabled:true",
438+
"spring.cloud.aws.sqs.batch.wait-time-seconds:PT20S")
439+
.run(context -> {
440+
assertThat(context).hasSingleBean(SqsProperties.class);
441+
SqsProperties sqsProperties = context.getBean(SqsProperties.class);
442+
SqsProperties.Batch batchConfig = sqsProperties.getBatch();
443+
444+
assertThat(batchConfig.isEnabled()).isTrue();
445+
assertThat(batchConfig.getWaitTimeSeconds()).isEqualTo(Duration.ofSeconds(20));
446+
});
447+
}
448+
449+
@Test
450+
void sqsBatchConfigurationWithAttributeNames() {
451+
this.contextRunner.withPropertyValues(
452+
"spring.cloud.aws.sqs.batch.enabled:true",
453+
"spring.cloud.aws.sqs.batch.attribute-names:MessageGroupId,MessageDeduplicationId")
454+
.run(context -> {
455+
assertThat(context).hasSingleBean(SqsProperties.class);
456+
SqsProperties sqsProperties = context.getBean(SqsProperties.class);
457+
SqsProperties.Batch batchConfig = sqsProperties.getBatch();
458+
459+
assertThat(batchConfig.isEnabled()).isTrue();
460+
assertThat(batchConfig.getAttributeNames()).containsExactly("MessageGroupId", "MessageDeduplicationId");
461+
});
462+
}
463+
332464
@Configuration(proxyBeanMethods = false)
333465
static class CustomComponentsConfiguration {
334466

0 commit comments

Comments
 (0)