17
17
package org .springframework .integration .support .leader ;
18
18
19
19
import static org .hamcrest .CoreMatchers .is ;
20
+ import static org .junit .Assert .assertFalse ;
20
21
import static org .junit .Assert .assertNull ;
21
22
import static org .junit .Assert .assertThat ;
22
23
import static org .junit .Assert .assertTrue ;
30
31
import static org .mockito .Mockito .spy ;
31
32
32
33
import java .util .concurrent .CountDownLatch ;
34
+ import java .util .concurrent .ExecutorService ;
35
+ import java .util .concurrent .Executors ;
33
36
import java .util .concurrent .TimeUnit ;
34
37
import java .util .concurrent .atomic .AtomicBoolean ;
35
38
import java .util .concurrent .atomic .AtomicReference ;
48
51
import org .springframework .integration .leader .event .LeaderEventPublisher ;
49
52
import org .springframework .integration .support .locks .DefaultLockRegistry ;
50
53
import org .springframework .integration .support .locks .LockRegistry ;
54
+ import org .springframework .integration .test .util .TestUtils ;
51
55
52
56
/**
53
57
* @author Dave Syer
54
58
* @author Artem Bilan
55
59
* @author Vedran Pavic
56
60
* @author Glenn Renfro
61
+ * @author Kiel Boatman
57
62
*
58
63
* @since 4.3.1
59
64
*/
60
65
public class LockRegistryLeaderInitiatorTests {
61
66
62
- private CountDownLatch granted ;
67
+ private CountDownLatch granted = new CountDownLatch ( 1 ) ;
63
68
64
- private CountDownLatch revoked ;
69
+ private CountDownLatch revoked = new CountDownLatch ( 1 ) ;
65
70
66
71
private final LockRegistry registry = new DefaultLockRegistry ();
67
72
@@ -70,8 +75,6 @@ public class LockRegistryLeaderInitiatorTests {
70
75
71
76
@ Before
72
77
public void init () {
73
- this .granted = new CountDownLatch (1 );
74
- this .revoked = new CountDownLatch (1 );
75
78
this .initiator .setLeaderEventPublisher (new CountingPublisher (this .granted , this .revoked ));
76
79
}
77
80
@@ -105,6 +108,7 @@ public void yield() throws Exception {
105
108
public void competing () throws Exception {
106
109
LockRegistryLeaderInitiator another =
107
110
new LockRegistryLeaderInitiator (this .registry , new DefaultCandidate ());
111
+
108
112
CountDownLatch other = new CountDownLatch (1 );
109
113
another .setLeaderEventPublisher (new CountingPublisher (other ));
110
114
this .initiator .start ();
@@ -139,9 +143,7 @@ public void competingWithErrorPublish() throws Exception {
139
143
public void testExceptionFromEvent () throws Exception {
140
144
CountDownLatch onGranted = new CountDownLatch (1 );
141
145
142
- LockRegistryLeaderInitiator initiator = new LockRegistryLeaderInitiator (this .registry , new DefaultCandidate ());
143
-
144
- initiator .setLeaderEventPublisher (new DefaultLeaderEventPublisher () {
146
+ this .initiator .setLeaderEventPublisher (new DefaultLeaderEventPublisher () {
145
147
146
148
@ Override
147
149
public void publishOnGranted (Object source , Context context , String role ) {
@@ -155,12 +157,12 @@ public void publishOnGranted(Object source, Context context, String role) {
155
157
156
158
});
157
159
158
- initiator .start ();
160
+ this . initiator .start ();
159
161
160
162
assertTrue (onGranted .await (10 , TimeUnit .SECONDS ));
161
163
assertTrue (initiator .getContext ().isLeader ());
162
164
163
- initiator .stop ();
165
+ this . initiator .stop ();
164
166
}
165
167
166
168
@ Test
@@ -177,6 +179,7 @@ public void competingWithLock() throws Exception {
177
179
// set up first initiator instance using first LockRegistry
178
180
LockRegistryLeaderInitiator first =
179
181
new LockRegistryLeaderInitiator (firstRegistry , new DefaultCandidate ());
182
+
180
183
CountDownLatch firstGranted = new CountDownLatch (1 );
181
184
CountDownLatch firstRevoked = new CountDownLatch (1 );
182
185
CountDownLatch firstAquireLockFailed = new CountDownLatch (1 );
@@ -193,6 +196,7 @@ public void competingWithLock() throws Exception {
193
196
// set up second initiator instance using second LockRegistry
194
197
LockRegistryLeaderInitiator second =
195
198
new LockRegistryLeaderInitiator (secondRegistry , new DefaultCandidate ());
199
+
196
200
CountDownLatch secondGranted = new CountDownLatch (1 );
197
201
CountDownLatch secondRevoked = new CountDownLatch (1 );
198
202
CountDownLatch secondAquireLockFailed = new CountDownLatch (1 );
@@ -246,20 +250,20 @@ public void testGracefulLeaderSelectorExit() throws Exception {
246
250
given (registry .obtain (anyString ()))
247
251
.willReturn (lock );
248
252
249
- LockRegistryLeaderInitiator initiator = new LockRegistryLeaderInitiator (registry );
253
+ LockRegistryLeaderInitiator another = new LockRegistryLeaderInitiator (registry );
250
254
251
255
willAnswer (invocation -> {
252
- initiator .stop ();
256
+ another .stop ();
253
257
return false ;
254
258
})
255
259
.given (lock )
256
260
.tryLock (anyLong (), eq (TimeUnit .MILLISECONDS ));
257
261
258
- new DirectFieldAccessor (initiator ).setPropertyValue ("executorService" ,
262
+ new DirectFieldAccessor (another ).setPropertyValue ("executorService" ,
259
263
new ExecutorServiceAdapter (
260
264
new SyncTaskExecutor ()));
261
265
262
- initiator .start ();
266
+ another .start ();
263
267
264
268
Throwable throwable = throwableAtomicReference .get ();
265
269
assertNull (throwable );
@@ -284,17 +288,40 @@ public void testExceptionFromLock() throws Exception {
284
288
285
289
CountDownLatch onGranted = new CountDownLatch (1 );
286
290
287
- LockRegistryLeaderInitiator initiator = new LockRegistryLeaderInitiator (registry );
291
+ LockRegistryLeaderInitiator another = new LockRegistryLeaderInitiator (registry );
288
292
289
- initiator .setLeaderEventPublisher (new CountingPublisher (onGranted ));
293
+ another .setLeaderEventPublisher (new CountingPublisher (onGranted ));
290
294
291
- initiator .start ();
295
+ another .start ();
292
296
293
297
assertTrue (onGranted .await (10 , TimeUnit .SECONDS ));
294
- assertTrue (initiator .getContext ().isLeader ());
298
+ assertTrue (another .getContext ().isLeader ());
295
299
assertTrue (exceptionThrown .get ());
296
300
297
- initiator .stop ();
301
+ another .stop ();
302
+ }
303
+
304
+ @ Test
305
+ public void shouldShutdownInternalExecutorService () {
306
+ this .initiator .start ();
307
+ this .initiator .destroy ();
308
+
309
+ ExecutorService executorService =
310
+ TestUtils .getPropertyValue (this .initiator , "executorService" , ExecutorService .class );
311
+
312
+ assertTrue (executorService .isShutdown ());
313
+ }
314
+
315
+ @ Test
316
+ public void doNotShutdownProvidedExecutorService () {
317
+ LockRegistryLeaderInitiator another = new LockRegistryLeaderInitiator (this .registry );
318
+ ExecutorService executorService = Executors .newSingleThreadExecutor ();
319
+ another .setExecutorService (executorService );
320
+
321
+ another .start ();
322
+ another .destroy ();
323
+
324
+ assertFalse (executorService .isShutdown ());
298
325
}
299
326
300
327
private static class CountingPublisher implements LeaderEventPublisher {
0 commit comments