Skip to content

Commit 1ccade2

Browse files
committed
feat: add tests for SQS batch configuration properties and behavior
1 parent e445e92 commit 1ccade2

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;
@@ -332,6 +333,137 @@ void configuresMessageConverter() {
332333
});
333334
}
334335

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

0 commit comments

Comments
 (0)