Skip to content

Commit fdc7e4d

Browse files
committed
Fix race condition in the RabbitTemplatePublisherCallbacksIntegration3Tests
1 parent d1ce0dc commit fdc7e4d

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018-2024 the original author or authors.
2+
* Copyright 2018-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -46,9 +46,9 @@
4646
* @since 2.1
4747
*
4848
*/
49-
@RabbitAvailable(queues = { RabbitTemplatePublisherCallbacksIntegration3Tests.QUEUE1,
49+
@RabbitAvailable(queues = {RabbitTemplatePublisherCallbacksIntegration3Tests.QUEUE1,
5050
RabbitTemplatePublisherCallbacksIntegration3Tests.QUEUE2,
51-
RabbitTemplatePublisherCallbacksIntegration3Tests.QUEUE3 })
51+
RabbitTemplatePublisherCallbacksIntegration3Tests.QUEUE3})
5252
public class RabbitTemplatePublisherCallbacksIntegration3Tests {
5353

5454
public static final String QUEUE1 = "synthetic.nack";
@@ -117,9 +117,11 @@ public void testDeferredChannelCacheNack() throws Exception {
117117

118118
@Test
119119
public void testDeferredChannelCacheAck() throws Exception {
120-
final CachingConnectionFactory cf = new CachingConnectionFactory(
121-
RabbitAvailableCondition.getBrokerRunning().getConnectionFactory());
120+
CachingConnectionFactory cf =
121+
new CachingConnectionFactory(RabbitAvailableCondition.getBrokerRunning().getConnectionFactory());
122122
cf.setPublisherConfirmType(ConfirmType.CORRELATED);
123+
ApplicationContext mockApplicationContext = mock();
124+
cf.setApplicationContext(mockApplicationContext);
123125
final RabbitTemplate template = new RabbitTemplate(cf);
124126
final CountDownLatch confirmLatch = new CountDownLatch(1);
125127
final AtomicInteger cacheCount = new AtomicInteger();
@@ -138,6 +140,7 @@ public void testDeferredChannelCacheAck() throws Exception {
138140
template.convertAndSend("", QUEUE2, "foo", new CorrelationData("foo"));
139141
assertThat(confirmLatch.await(10, TimeUnit.SECONDS)).isTrue();
140142
assertThat(cacheCount.get()).isEqualTo(1);
143+
cf.onApplicationEvent(new ContextClosedEvent(mockApplicationContext));
141144
cf.destroy();
142145
}
143146

@@ -146,6 +149,8 @@ public void testTwoSendsAndReceivesDRTMLC() throws Exception {
146149
CachingConnectionFactory cf = new CachingConnectionFactory(
147150
RabbitAvailableCondition.getBrokerRunning().getConnectionFactory());
148151
cf.setPublisherConfirmType(ConfirmType.CORRELATED);
152+
ApplicationContext mockApplicationContext = mock();
153+
cf.setApplicationContext(mockApplicationContext);
149154
RabbitTemplate template = new RabbitTemplate(cf);
150155
template.setReplyTimeout(0);
151156
final CountDownLatch confirmLatch = new CountDownLatch(2);
@@ -157,6 +162,8 @@ public void testTwoSendsAndReceivesDRTMLC() throws Exception {
157162
assertThat(confirmLatch.await(10, TimeUnit.SECONDS)).isTrue();
158163
assertThat(template.receive(QUEUE3, 10_000)).isNotNull();
159164
assertThat(template.receive(QUEUE3, 10_000)).isNotNull();
165+
cf.onApplicationEvent(new ContextClosedEvent(mockApplicationContext));
166+
cf.destroy();
160167
}
161168

162169
}

0 commit comments

Comments
 (0)