2020import static org .hamcrest .Matchers .instanceOf ;
2121import static org .junit .Assert .assertFalse ;
2222import static org .junit .Assert .assertThat ;
23+ import static org .junit .Assert .assertTrue ;
2324import static org .junit .Assert .fail ;
2425
26+ import java .util .concurrent .CountDownLatch ;
27+ import java .util .concurrent .TimeUnit ;
28+
2529import org .junit .After ;
2630import org .junit .Rule ;
2731import org .junit .Test ;
3034import org .springframework .amqp .core .Queue ;
3135import org .springframework .amqp .rabbit .connection .CachingConnectionFactory ;
3236import org .springframework .amqp .rabbit .connection .ConnectionFactory ;
37+ import org .springframework .amqp .rabbit .connection .RabbitUtils ;
38+ import org .springframework .amqp .rabbit .connection .ShutDownChannelListener ;
3339import org .springframework .amqp .rabbit .core .RabbitAdmin ;
3440import org .springframework .amqp .rabbit .junit .BrokerRunning ;
3541import org .springframework .amqp .rabbit .listener .adapter .MessageListenerAdapter ;
3642import org .springframework .amqp .rabbit .listener .exception .FatalListenerStartupException ;
43+ import org .springframework .context .ApplicationContext ;
3744import org .springframework .context .ApplicationContextException ;
3845import org .springframework .context .annotation .AnnotationConfigApplicationContext ;
3946import 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