Skip to content

Commit bd63c35

Browse files
committed
DefaultMessageListenerContainer tests for stop-and-restart
See gh-30612
1 parent c052a02 commit bd63c35

File tree

1 file changed

+49
-16
lines changed

1 file changed

+49
-16
lines changed

spring-jms/src/test/java/org/springframework/jms/listener/DefaultMessageListenerContainerTests.java

+49-16
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
import org.springframework.util.backoff.BackOff;
3131
import org.springframework.util.backoff.BackOffExecution;
32+
import org.springframework.util.backoff.FixedBackOff;
3233

3334
import static org.assertj.core.api.Assertions.assertThat;
3435
import static org.mockito.BDDMockito.given;
@@ -38,6 +39,7 @@
3839

3940
/**
4041
* @author Stephane Nicoll
42+
* @author Juergen Hoeller
4143
*/
4244
public class DefaultMessageListenerContainerTests {
4345

@@ -58,6 +60,8 @@ public void applyBackOff() {
5860
assertThat(container.isRunning()).isFalse();
5961
verify(backOff).start();
6062
verify(execution).nextBackOff();
63+
64+
container.destroy();
6165
}
6266

6367
@Test
@@ -75,6 +79,8 @@ public void applyBackOffRetry() {
7579
assertThat(container.isRunning()).isFalse();
7680
verify(backOff).start();
7781
verify(execution, times(2)).nextBackOff();
82+
83+
container.destroy();
7884
}
7985

8086
@Test
@@ -92,27 +98,65 @@ public void recoverResetBackOff() {
9298
assertThat(container.isRunning()).isTrue();
9399
verify(backOff).start();
94100
verify(execution, times(1)).nextBackOff(); // only on attempt as the second one lead to a recovery
101+
102+
container.destroy();
103+
}
104+
105+
@Test
106+
public void stopAndRestart() {
107+
DefaultMessageListenerContainer container = createRunningContainer();
108+
container.stop();
109+
110+
container.start();
111+
container.destroy();
95112
}
96113

97114
@Test
98-
public void runnableIsInvokedEvenIfContainerIsNotRunning() throws InterruptedException {
115+
public void stopWithCallbackAndRestart() throws InterruptedException {
116+
DefaultMessageListenerContainer container = createRunningContainer();
117+
118+
TestRunnable stopCallback = new TestRunnable();
119+
container.stop(stopCallback);
120+
stopCallback.waitForCompletion();
121+
122+
container.start();
123+
container.destroy();
124+
}
125+
126+
@Test
127+
public void stopCallbackIsInvokedEvenIfContainerIsNotRunning() throws InterruptedException {
99128
DefaultMessageListenerContainer container = createRunningContainer();
100129
container.stop();
101130

102131
// container is stopped but should nevertheless invoke the runnable argument
103-
TestRunnable runnable2 = new TestRunnable();
104-
container.stop(runnable2);
105-
runnable2.waitForCompletion();
132+
TestRunnable stopCallback = new TestRunnable();
133+
container.stop(stopCallback);
134+
stopCallback.waitForCompletion();
135+
136+
container.destroy();
106137
}
107138

108139

109140
private DefaultMessageListenerContainer createRunningContainer() {
110141
DefaultMessageListenerContainer container = createContainer(createSuccessfulConnectionFactory());
142+
container.setCacheLevel(DefaultMessageListenerContainer.CACHE_CONNECTION);
143+
container.setBackOff(new FixedBackOff(100, 1));
111144
container.afterPropertiesSet();
112145
container.start();
113146
return container;
114147
}
115148

149+
private ConnectionFactory createSuccessfulConnectionFactory() {
150+
try {
151+
ConnectionFactory connectionFactory = mock();
152+
given(connectionFactory.createConnection()).willReturn(mock());
153+
return connectionFactory;
154+
}
155+
catch (JMSException ex) {
156+
throw new IllegalStateException(ex); // never happen
157+
}
158+
}
159+
116160
private DefaultMessageListenerContainer createContainer(ConnectionFactory connectionFactory) {
117161
Destination destination = new Destination() {};
118162

@@ -159,17 +203,6 @@ public Object answer(InvocationOnMock invocation) throws Throwable {
159203
}
160204
}
161205

162-
private ConnectionFactory createSuccessfulConnectionFactory() {
163-
try {
164-
ConnectionFactory connectionFactory = mock();
165-
given(connectionFactory.createConnection()).willReturn(mock());
166-
return connectionFactory;
167-
}
168-
catch (JMSException ex) {
169-
throw new IllegalStateException(ex); // never happen
170-
}
171-
}
172-
173206

174207
private static class TestRunnable implements Runnable {
175208

@@ -181,7 +214,7 @@ public void run() {
181214
}
182215

183216
public void waitForCompletion() throws InterruptedException {
184-
this.countDownLatch.await(2, TimeUnit.SECONDS);
217+
this.countDownLatch.await(1, TimeUnit.SECONDS);
185218
assertThat(this.countDownLatch.getCount()).as("callback was not invoked").isEqualTo(0);
186219
}
187220
}

0 commit comments

Comments
 (0)