Skip to content

Commit 0db905c

Browse files
garyrussellartembilan
authored andcommitted
Make Mismatched Queue Tests More Robust
https://build.spring.io/browse/AMQP-MEIGHT-1066/test - Add latches to wait for channel close events - normal close after cancel - fatal close for mismatch - Add `ShutDownChannelListener` `@FunctionalInterface` Add .editorConfig
1 parent 5e33f52 commit 0db905c

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

.editorconfig

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
root=true
2+
3+
[*.java]
4+
indent_style = tab
5+
indent_size = 4
6+
continuation_indent_size = 8
7+
8+
[*.xml]
9+
indent_style = tab
10+
indent_size = 4
11+
continuation_indent_size = 8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.amqp.rabbit.connection;
18+
19+
import com.rabbitmq.client.Channel;
20+
import com.rabbitmq.client.ShutdownSignalException;
21+
22+
/**
23+
* Functional sub interface enabling a lambda for the onShutDown method.
24+
*
25+
* @author Gary Russell
26+
* @since 2.0
27+
*
28+
*/
29+
@FunctionalInterface
30+
public interface ShutDownChannelListener extends ChannelListener {
31+
32+
@Override
33+
default void onCreate(Channel channel, boolean transactional) {
34+
}
35+
36+
@Override
37+
void onShutDown(ShutdownSignalException signal);
38+
39+
}

spring-rabbit/src/test/java/org/springframework/amqp/rabbit/listener/ContainerInitializationTests.java

+28
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,12 @@
2020
import static org.hamcrest.Matchers.instanceOf;
2121
import static org.junit.Assert.assertFalse;
2222
import static org.junit.Assert.assertThat;
23+
import static org.junit.Assert.assertTrue;
2324
import static org.junit.Assert.fail;
2425

26+
import java.util.concurrent.CountDownLatch;
27+
import java.util.concurrent.TimeUnit;
28+
2529
import org.junit.After;
2630
import org.junit.Rule;
2731
import org.junit.Test;
@@ -30,10 +34,13 @@
3034
import org.springframework.amqp.core.Queue;
3135
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
3236
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
37+
import org.springframework.amqp.rabbit.connection.RabbitUtils;
38+
import org.springframework.amqp.rabbit.connection.ShutDownChannelListener;
3339
import org.springframework.amqp.rabbit.core.RabbitAdmin;
3440
import org.springframework.amqp.rabbit.junit.BrokerRunning;
3541
import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
3642
import org.springframework.amqp.rabbit.listener.exception.FatalListenerStartupException;
43+
import org.springframework.context.ApplicationContext;
3744
import org.springframework.context.ApplicationContextException;
3845
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
3946
import org.springframework.context.annotation.Bean;
@@ -87,9 +94,12 @@ public void testMismatchedQueue() {
8794
@Test
8895
public void testMismatchedQueueDuringRestart() throws Exception {
8996
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config2.class);
97+
CountDownLatch[] latches = setUpChannelLatches(context);
9098
RabbitAdmin admin = context.getBean(RabbitAdmin.class);
9199
admin.deleteQueue(TEST_MISMATCH);
100+
assertTrue(latches[0].await(20, TimeUnit.SECONDS));
92101
admin.declareQueue(new Queue(TEST_MISMATCH, false, false, true));
102+
assertTrue(latches[1].await(20, TimeUnit.SECONDS));
93103
SimpleMessageListenerContainer container = context.getBean(SimpleMessageListenerContainer.class);
94104
int n = 0;
95105
while (n++ < 200 && container.isRunning()) {
@@ -102,9 +112,12 @@ public void testMismatchedQueueDuringRestart() throws Exception {
102112
@Test
103113
public void testMismatchedQueueDuringRestartMultiQueue() throws Exception {
104114
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config3.class);
115+
CountDownLatch[] latches = setUpChannelLatches(context);
105116
RabbitAdmin admin = context.getBean(RabbitAdmin.class);
106117
admin.deleteQueue(TEST_MISMATCH);
118+
assertTrue(latches[0].await(20, TimeUnit.SECONDS));
107119
admin.declareQueue(new Queue(TEST_MISMATCH, false, false, true));
120+
assertTrue(latches[1].await(20, TimeUnit.SECONDS));
108121
SimpleMessageListenerContainer container = context.getBean(SimpleMessageListenerContainer.class);
109122
int n = 0;
110123
while (n++ < 200 && container.isRunning()) {
@@ -114,6 +127,21 @@ public void testMismatchedQueueDuringRestartMultiQueue() throws Exception {
114127
context.close();
115128
}
116129

130+
private CountDownLatch[] setUpChannelLatches(ApplicationContext context) {
131+
CachingConnectionFactory cf = context.getBean(CachingConnectionFactory.class);
132+
final CountDownLatch cancelLatch = new CountDownLatch(1);
133+
final CountDownLatch mismatchLatch = new CountDownLatch(1);
134+
cf.addChannelListener((ShutDownChannelListener) s -> {
135+
if (RabbitUtils.isNormalChannelClose(s)) {
136+
cancelLatch.countDown();
137+
}
138+
else if (RabbitUtils.isMismatchedQueueArgs(s)) {
139+
mismatchLatch.countDown();
140+
}
141+
});
142+
return new CountDownLatch[] { cancelLatch, mismatchLatch };
143+
}
144+
117145
@Configuration
118146
static class Config0 {
119147

0 commit comments

Comments
 (0)