|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | +package com.azure.spring.cloud.stream.binder.servicebus.implementation; |
| 4 | + |
| 5 | +import com.azure.spring.cloud.service.servicebus.properties.ServiceBusEntityType; |
| 6 | +import com.azure.spring.cloud.stream.binder.servicebus.core.implementation.provisioning.ServiceBusChannelProvisioner; |
| 7 | +import com.azure.spring.cloud.stream.binder.servicebus.core.properties.ServiceBusBindingProperties; |
| 8 | +import com.azure.spring.cloud.stream.binder.servicebus.core.properties.ServiceBusConsumerProperties; |
| 9 | +import com.azure.spring.cloud.stream.binder.servicebus.core.properties.ServiceBusExtendedBindingProperties; |
| 10 | +import com.azure.spring.integration.servicebus.inbound.ServiceBusInboundChannelAdapter; |
| 11 | +import org.junit.jupiter.api.AfterEach; |
| 12 | +import org.junit.jupiter.api.BeforeEach; |
| 13 | +import org.junit.jupiter.api.Test; |
| 14 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 15 | +import org.mockito.Mock; |
| 16 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 17 | +import org.springframework.cloud.stream.binder.BinderHeaders; |
| 18 | +import org.springframework.cloud.stream.binder.ExtendedConsumerProperties; |
| 19 | +import org.springframework.cloud.stream.binder.HeaderMode; |
| 20 | +import org.springframework.cloud.stream.provisioning.ConsumerDestination; |
| 21 | +import org.springframework.context.support.GenericApplicationContext; |
| 22 | +import org.springframework.integration.core.MessageProducer; |
| 23 | +import org.springframework.retry.backoff.ExponentialBackOffPolicy; |
| 24 | +import org.springframework.retry.support.RetryTemplate; |
| 25 | + |
| 26 | +import java.time.Duration; |
| 27 | +import java.util.HashMap; |
| 28 | +import java.util.Map; |
| 29 | +import java.util.concurrent.atomic.AtomicInteger; |
| 30 | + |
| 31 | +import static org.assertj.core.api.Assertions.assertThat; |
| 32 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 33 | +import static org.mockito.Mockito.when; |
| 34 | + |
| 35 | +/** |
| 36 | + * Tests for retry functionality in ServiceBusMessageChannelBinder. |
| 37 | + */ |
| 38 | +@ExtendWith(MockitoExtension.class) |
| 39 | +class ServiceBusRetryTest { |
| 40 | + |
| 41 | + @Mock |
| 42 | + private ConsumerDestination consumerDestination; |
| 43 | + |
| 44 | + private final ServiceBusExtendedBindingProperties extendedBindingProperties = |
| 45 | + new ServiceBusExtendedBindingProperties(); |
| 46 | + |
| 47 | + private ExtendedConsumerProperties<ServiceBusConsumerProperties> consumerProperties; |
| 48 | + |
| 49 | + private final ServiceBusConsumerProperties serviceBusConsumerProperties = new ServiceBusConsumerProperties(); |
| 50 | + |
| 51 | + private ServiceBusMessageChannelTestBinder binder; |
| 52 | + |
| 53 | + private GenericApplicationContext applicationContext; |
| 54 | + |
| 55 | + private static final String ENTITY_NAME = "test-entity"; |
| 56 | + private static final String GROUP = "test"; |
| 57 | + private static final String NAMESPACE_NAME = "test-namespace"; |
| 58 | + |
| 59 | + @BeforeEach |
| 60 | + void init() { |
| 61 | + binder = new ServiceBusMessageChannelTestBinder( |
| 62 | + BinderHeaders.STANDARD_HEADERS, new ServiceBusChannelProvisioner()); |
| 63 | + applicationContext = new GenericApplicationContext(); |
| 64 | + applicationContext.refresh(); |
| 65 | + binder.setApplicationContext(applicationContext); |
| 66 | + } |
| 67 | + |
| 68 | + @AfterEach |
| 69 | + void tearDown() { |
| 70 | + if (applicationContext != null) { |
| 71 | + applicationContext.close(); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + void testRetryTemplateConfiguredWhenMaxAttemptsGreaterThanOne() { |
| 77 | + // Arrange |
| 78 | + prepareConsumerProperties(); |
| 79 | + consumerProperties.setMaxAttempts(3); |
| 80 | + consumerProperties.setBackOffInitialInterval(1000); |
| 81 | + consumerProperties.setBackOffMultiplier(2.0); |
| 82 | + consumerProperties.setBackOffMaxInterval(5000); |
| 83 | + when(consumerDestination.getName()).thenReturn(ENTITY_NAME); |
| 84 | + |
| 85 | + // Act |
| 86 | + MessageProducer producer = binder.createConsumerEndpoint(consumerDestination, GROUP, consumerProperties); |
| 87 | + |
| 88 | + // Assert |
| 89 | + assertThat(producer).isInstanceOf(ServiceBusInboundChannelAdapter.class); |
| 90 | + ServiceBusInboundChannelAdapter adapter = (ServiceBusInboundChannelAdapter) producer; |
| 91 | + RetryTemplate retryTemplate = adapter.getRetryTemplate(); |
| 92 | + assertThat(retryTemplate).isNotNull(); |
| 93 | + |
| 94 | + // Verify maxAttempts=3 by executing the template and counting actual attempts |
| 95 | + AtomicInteger callCount = new AtomicInteger(0); |
| 96 | + assertThatThrownBy(() -> retryTemplate.execute(ctx -> { |
| 97 | + callCount.incrementAndGet(); |
| 98 | + throw new RuntimeException("test"); |
| 99 | + })).isInstanceOf(RuntimeException.class); |
| 100 | + assertThat(callCount.get()).isEqualTo(3); |
| 101 | + |
| 102 | + // Verify backoff policy configuration via the binder's factory method (no reflection needed) |
| 103 | + ExponentialBackOffPolicy backOffPolicy = binder.createExponentialBackOffPolicy(consumerProperties); |
| 104 | + assertThat(backOffPolicy.getInitialInterval()).isEqualTo(1000L); |
| 105 | + assertThat(backOffPolicy.getMultiplier()).isEqualTo(2.0); |
| 106 | + assertThat(backOffPolicy.getMaxInterval()).isEqualTo(5000L); |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + void testRetryTemplateNotConfiguredWhenMaxAttemptsIsOne() { |
| 111 | + // Arrange |
| 112 | + prepareConsumerProperties(); |
| 113 | + consumerProperties.setMaxAttempts(1); |
| 114 | + when(consumerDestination.getName()).thenReturn(ENTITY_NAME); |
| 115 | + |
| 116 | + // Act |
| 117 | + MessageProducer producer = binder.createConsumerEndpoint(consumerDestination, GROUP, consumerProperties); |
| 118 | + |
| 119 | + // Assert |
| 120 | + assertThat(producer).isInstanceOf(ServiceBusInboundChannelAdapter.class); |
| 121 | + assertThat(((ServiceBusInboundChannelAdapter) producer).getRetryTemplate()).isNull(); |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + void testRetryTemplateConfiguredWithDefaultSettings() { |
| 126 | + // Arrange |
| 127 | + prepareConsumerProperties(); |
| 128 | + // Spring Cloud Stream default maxAttempts is 3 (> 1), so a RetryTemplate should be created. |
| 129 | + when(consumerDestination.getName()).thenReturn(ENTITY_NAME); |
| 130 | + |
| 131 | + // Act |
| 132 | + MessageProducer producer = binder.createConsumerEndpoint(consumerDestination, GROUP, consumerProperties); |
| 133 | + |
| 134 | + // Assert |
| 135 | + assertThat(producer).isInstanceOf(ServiceBusInboundChannelAdapter.class); |
| 136 | + ServiceBusInboundChannelAdapter adapter = (ServiceBusInboundChannelAdapter) producer; |
| 137 | + RetryTemplate retryTemplate = adapter.getRetryTemplate(); |
| 138 | + assertThat(retryTemplate).isNotNull(); |
| 139 | + |
| 140 | + // Verify maxAttempts matches Spring Cloud Stream's default via observable behavior |
| 141 | + int expectedMaxAttempts = new ExtendedConsumerProperties<>(new ServiceBusConsumerProperties()).getMaxAttempts(); |
| 142 | + AtomicInteger callCount = new AtomicInteger(0); |
| 143 | + assertThatThrownBy(() -> retryTemplate.execute(ctx -> { |
| 144 | + callCount.incrementAndGet(); |
| 145 | + throw new RuntimeException("test"); |
| 146 | + })).isInstanceOf(RuntimeException.class); |
| 147 | + assertThat(callCount.get()).isEqualTo(expectedMaxAttempts); |
| 148 | + } |
| 149 | + |
| 150 | + @Test |
| 151 | + void testCustomRetryTemplateIsUsed() { |
| 152 | + // Arrange |
| 153 | + prepareConsumerProperties(); |
| 154 | + consumerProperties.setMaxAttempts(3); |
| 155 | + when(consumerDestination.getName()).thenReturn(ENTITY_NAME); |
| 156 | + |
| 157 | + // Create a custom RetryTemplate |
| 158 | + RetryTemplate customRetryTemplate = new RetryTemplate(); |
| 159 | + binder.setRetryTemplate(customRetryTemplate); |
| 160 | + |
| 161 | + // Act |
| 162 | + MessageProducer producer = binder.createConsumerEndpoint(consumerDestination, GROUP, consumerProperties); |
| 163 | + |
| 164 | + // Assert |
| 165 | + assertThat(producer).isInstanceOf(ServiceBusInboundChannelAdapter.class); |
| 166 | + ServiceBusInboundChannelAdapter adapter = (ServiceBusInboundChannelAdapter) producer; |
| 167 | + assertThat(adapter.getRetryTemplate()).isNotNull(); |
| 168 | + assertThat(adapter.getRetryTemplate()).isSameAs(customRetryTemplate); |
| 169 | + } |
| 170 | + |
| 171 | + @Test |
| 172 | + void testCustomRetryTemplateNotAppliedWhenMaxAttemptsIsOne() { |
| 173 | + // Arrange: maxAttempts=1 disables retry even when a custom RetryTemplate bean is injected |
| 174 | + prepareConsumerProperties(); |
| 175 | + consumerProperties.setMaxAttempts(1); |
| 176 | + when(consumerDestination.getName()).thenReturn(ENTITY_NAME); |
| 177 | + |
| 178 | + RetryTemplate customRetryTemplate = new RetryTemplate(); |
| 179 | + binder.setRetryTemplate(customRetryTemplate); |
| 180 | + |
| 181 | + // Act |
| 182 | + MessageProducer producer = binder.createConsumerEndpoint(consumerDestination, GROUP, consumerProperties); |
| 183 | + |
| 184 | + // Assert |
| 185 | + assertThat(producer).isInstanceOf(ServiceBusInboundChannelAdapter.class); |
| 186 | + assertThat(((ServiceBusInboundChannelAdapter) producer).getRetryTemplate()).isNull(); |
| 187 | + } |
| 188 | + |
| 189 | + private void prepareConsumerProperties() { |
| 190 | + serviceBusConsumerProperties.setEntityName(ENTITY_NAME); |
| 191 | + serviceBusConsumerProperties.setSubscriptionName(GROUP); |
| 192 | + serviceBusConsumerProperties.setEntityType(ServiceBusEntityType.TOPIC); |
| 193 | + serviceBusConsumerProperties.setNamespace(NAMESPACE_NAME); |
| 194 | + serviceBusConsumerProperties.getRetry().setTryTimeout(Duration.ofMinutes(5)); |
| 195 | + serviceBusConsumerProperties.setAutoComplete(false); |
| 196 | + ServiceBusBindingProperties bindingProperties = new ServiceBusBindingProperties(); |
| 197 | + bindingProperties.setConsumer(serviceBusConsumerProperties); |
| 198 | + |
| 199 | + Map<String, ServiceBusBindingProperties> bindings = new HashMap<>(); |
| 200 | + bindings.put(ENTITY_NAME, bindingProperties); |
| 201 | + extendedBindingProperties.setBindings(bindings); |
| 202 | + binder.setBindingProperties(extendedBindingProperties); |
| 203 | + |
| 204 | + consumerProperties = new ExtendedConsumerProperties<>(serviceBusConsumerProperties); |
| 205 | + consumerProperties.setHeaderMode(HeaderMode.embeddedHeaders); |
| 206 | + } |
| 207 | +} |
0 commit comments