29
29
30
30
import org .springframework .util .backoff .BackOff ;
31
31
import org .springframework .util .backoff .BackOffExecution ;
32
+ import org .springframework .util .backoff .FixedBackOff ;
32
33
33
34
import static org .assertj .core .api .Assertions .assertThat ;
34
35
import static org .mockito .BDDMockito .given ;
38
39
39
40
/**
40
41
* @author Stephane Nicoll
42
+ * @author Juergen Hoeller
41
43
*/
42
44
public class DefaultMessageListenerContainerTests {
43
45
@@ -58,6 +60,8 @@ public void applyBackOff() {
58
60
assertThat (container .isRunning ()).isFalse ();
59
61
verify (backOff ).start ();
60
62
verify (execution ).nextBackOff ();
63
+
64
+ container .destroy ();
61
65
}
62
66
63
67
@ Test
@@ -75,6 +79,8 @@ public void applyBackOffRetry() {
75
79
assertThat (container .isRunning ()).isFalse ();
76
80
verify (backOff ).start ();
77
81
verify (execution , times (2 )).nextBackOff ();
82
+
83
+ container .destroy ();
78
84
}
79
85
80
86
@ Test
@@ -92,27 +98,65 @@ public void recoverResetBackOff() {
92
98
assertThat (container .isRunning ()).isTrue ();
93
99
verify (backOff ).start ();
94
100
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 ();
95
112
}
96
113
97
114
@ 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 {
99
128
DefaultMessageListenerContainer container = createRunningContainer ();
100
129
container .stop ();
101
130
102
131
// 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 ();
106
137
}
107
138
108
139
109
140
private DefaultMessageListenerContainer createRunningContainer () {
110
141
DefaultMessageListenerContainer container = createContainer (createSuccessfulConnectionFactory ());
142
+ container .setCacheLevel (DefaultMessageListenerContainer .CACHE_CONNECTION );
143
+ container .setBackOff (new FixedBackOff (100 , 1 ));
111
144
container .afterPropertiesSet ();
112
145
container .start ();
113
146
return container ;
114
147
}
115
148
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
+
116
160
private DefaultMessageListenerContainer createContainer (ConnectionFactory connectionFactory ) {
117
161
Destination destination = new Destination () {};
118
162
@@ -159,17 +203,6 @@ public Object answer(InvocationOnMock invocation) throws Throwable {
159
203
}
160
204
}
161
205
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
-
173
206
174
207
private static class TestRunnable implements Runnable {
175
208
@@ -181,7 +214,7 @@ public void run() {
181
214
}
182
215
183
216
public void waitForCompletion () throws InterruptedException {
184
- this .countDownLatch .await (2 , TimeUnit .SECONDS );
217
+ this .countDownLatch .await (1 , TimeUnit .SECONDS );
185
218
assertThat (this .countDownLatch .getCount ()).as ("callback was not invoked" ).isEqualTo (0 );
186
219
}
187
220
}
0 commit comments