@@ -43,13 +43,10 @@ public class TestShutdownHookManager {
43
43
LoggerFactory .getLogger (TestShutdownHookManager .class .getName ());
44
44
45
45
/**
46
- * remove all the shutdown hooks so that they never get invoked later
47
- * on in this test process.
46
+ * A new instance of ShutdownHookManager to ensure parallel tests
47
+ * don't have shared context
48
48
*/
49
- @ After
50
- public void clearShutdownHooks () {
51
- ShutdownHookManager .get ().clearShutdownHooks ();
52
- }
49
+ private final ShutdownHookManager mgr = new ShutdownHookManager ();
53
50
54
51
/**
55
52
* Verify hook registration, then execute the hook callback stage
@@ -58,7 +55,6 @@ public void clearShutdownHooks() {
58
55
*/
59
56
@ Test
60
57
public void shutdownHookManager () {
61
- ShutdownHookManager mgr = ShutdownHookManager .get ();
62
58
assertNotNull ("No ShutdownHookManager" , mgr );
63
59
assertEquals (0 , mgr .getShutdownHooksInOrder ().size ());
64
60
Hook hook1 = new Hook ("hook1" , 0 , false );
@@ -193,7 +189,6 @@ public void testShutdownTimeoutBadConfiguration() throws Throwable {
193
189
*/
194
190
@ Test
195
191
public void testDuplicateRegistration () throws Throwable {
196
- ShutdownHookManager mgr = ShutdownHookManager .get ();
197
192
Hook hook = new Hook ("hook1" , 0 , false );
198
193
199
194
// add the hook
@@ -222,6 +217,20 @@ public void testDuplicateRegistration() throws Throwable {
222
217
223
218
}
224
219
220
+ @ Test
221
+ public void testShutdownRemove () throws Throwable {
222
+ assertNotNull ("No ShutdownHookManager" , mgr );
223
+ assertEquals (0 , mgr .getShutdownHooksInOrder ().size ());
224
+ Hook hook1 = new Hook ("hook1" , 0 , false );
225
+ Hook hook2 = new Hook ("hook2" , 0 , false );
226
+ mgr .addShutdownHook (hook1 , 9 ); // create Hook1 with priority 9
227
+ assertTrue (mgr .hasShutdownHook (hook1 )); // hook1 lookup works
228
+ assertEquals (1 , mgr .getShutdownHooksInOrder ().size ()); // 1 hook
229
+ assertFalse (mgr .removeShutdownHook (hook2 )); // can't delete hook2
230
+ assertTrue (mgr .removeShutdownHook (hook1 )); // can delete hook1
231
+ assertEquals (0 , mgr .getShutdownHooksInOrder ().size ()); // no more hooks
232
+ }
233
+
225
234
private static final AtomicInteger INVOCATION_COUNT = new AtomicInteger ();
226
235
227
236
/**
0 commit comments