Skip to content

Commit 75a05d7

Browse files
committed
Fix race condition in the RabbitTemplatePublisherCallbacksIntegration3Tests
The `CachingConnectionFactory` has an ability to wait for async channels close when it is destroyed. However, that happens only if an `ApplicationContext` is stopped. * Supply a mock `ApplicationContext` to the `CachingConnectionFactory` of the test and emit respective `ContextClosedEvent` in the test before calling `cf.destroy()`
1 parent db0c274 commit 75a05d7

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

spring-rabbit/src/test/java/org/springframework/amqp/rabbit/core/RabbitTemplatePublisherCallbacksIntegration3Tests.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.amqp.rabbit.core;
1818

1919
import static org.assertj.core.api.Assertions.assertThat;
20+
import static org.mockito.Mockito.mock;
2021

2122
import java.util.List;
2223
import java.util.concurrent.CountDownLatch;
@@ -33,11 +34,15 @@
3334
import org.springframework.amqp.rabbit.junit.RabbitAvailable;
3435
import org.springframework.amqp.rabbit.junit.RabbitAvailableCondition;
3536
import org.springframework.amqp.utils.test.TestUtils;
37+
import org.springframework.context.ApplicationContext;
38+
import org.springframework.context.event.ContextClosedEvent;
3639

3740
import com.rabbitmq.client.Channel;
3841

3942
/**
4043
* @author Gary Russell
44+
* @author Artem Bilan
45+
*
4146
* @since 2.1
4247
*
4348
*/
@@ -72,15 +77,17 @@ public void testRepublishOnNackThreadNoExchange() throws Exception {
7277

7378
@Test
7479
public void testDeferredChannelCacheNack() throws Exception {
75-
final CachingConnectionFactory cf = new CachingConnectionFactory(
80+
CachingConnectionFactory cf = new CachingConnectionFactory(
7681
RabbitAvailableCondition.getBrokerRunning().getConnectionFactory());
7782
cf.setPublisherReturns(true);
7883
cf.setPublisherConfirmType(ConfirmType.CORRELATED);
79-
final RabbitTemplate template = new RabbitTemplate(cf);
80-
final CountDownLatch returnLatch = new CountDownLatch(1);
81-
final CountDownLatch confirmLatch = new CountDownLatch(1);
82-
final AtomicInteger cacheCount = new AtomicInteger();
83-
final AtomicBoolean returnCalledFirst = new AtomicBoolean();
84+
ApplicationContext mockApplicationContext = mock();
85+
cf.setApplicationContext(mockApplicationContext);
86+
RabbitTemplate template = new RabbitTemplate(cf);
87+
CountDownLatch returnLatch = new CountDownLatch(1);
88+
CountDownLatch confirmLatch = new CountDownLatch(1);
89+
AtomicInteger cacheCount = new AtomicInteger();
90+
AtomicBoolean returnCalledFirst = new AtomicBoolean();
8491
template.setConfirmCallback((cd, a, c) -> {
8592
cacheCount.set(TestUtils.getPropertyValue(cf, "cachedChannelsNonTransactional", List.class).size());
8693
returnCalledFirst.set(returnLatch.getCount() == 0);
@@ -104,6 +111,7 @@ public void testDeferredChannelCacheNack() throws Exception {
104111
assertThat(cacheCount.get()).isEqualTo(1);
105112
assertThat(returnCalledFirst.get()).isTrue();
106113
assertThat(correlationData.getReturned()).isNotNull();
114+
cf.onApplicationEvent(new ContextClosedEvent(mockApplicationContext));
107115
cf.destroy();
108116
}
109117

0 commit comments

Comments
 (0)