2121import java .util .concurrent .TimeUnit ;
2222import java .util .concurrent .atomic .AtomicInteger ;
2323
24- import org .junit .After ;
2524import org .junit .Test ;
2625import org .slf4j .Logger ;
2726import org .slf4j .LoggerFactory ;
@@ -43,13 +42,10 @@ public class TestShutdownHookManager {
4342 LoggerFactory .getLogger (TestShutdownHookManager .class .getName ());
4443
4544 /**
46- * remove all the shutdown hooks so that they never get invoked later
47- * on in this test process .
45+ * A new instance of ShutdownHookManager to ensure parallel tests
46+ * don't have shared context .
4847 */
49- @ After
50- public void clearShutdownHooks () {
51- ShutdownHookManager .get ().clearShutdownHooks ();
52- }
48+ private final ShutdownHookManager mgr = new ShutdownHookManager ();
5349
5450 /**
5551 * Verify hook registration, then execute the hook callback stage
@@ -58,7 +54,6 @@ public void clearShutdownHooks() {
5854 */
5955 @ Test
6056 public void shutdownHookManager () {
61- ShutdownHookManager mgr = ShutdownHookManager .get ();
6257 assertNotNull ("No ShutdownHookManager" , mgr );
6358 assertEquals (0 , mgr .getShutdownHooksInOrder ().size ());
6459 Hook hook1 = new Hook ("hook1" , 0 , false );
@@ -119,7 +114,7 @@ public void shutdownHookManager() {
119114 // now execute the hook shutdown sequence
120115 INVOCATION_COUNT .set (0 );
121116 LOG .info ("invoking executeShutdown()" );
122- int timeouts = ShutdownHookManager .executeShutdown ();
117+ int timeouts = mgr .executeShutdown ();
123118 LOG .info ("Shutdown completed" );
124119 assertEquals ("Number of timed out hooks" , 1 , timeouts );
125120
@@ -193,7 +188,6 @@ public void testShutdownTimeoutBadConfiguration() throws Throwable {
193188 */
194189 @ Test
195190 public void testDuplicateRegistration () throws Throwable {
196- ShutdownHookManager mgr = ShutdownHookManager .get ();
197191 Hook hook = new Hook ("hook1" , 0 , false );
198192
199193 // add the hook
@@ -222,6 +216,21 @@ public void testDuplicateRegistration() throws Throwable {
222216
223217 }
224218
219+ @ Test
220+ public void testShutdownRemove () throws Throwable {
221+ assertNotNull ("No ShutdownHookManager" , mgr );
222+ assertEquals (0 , mgr .getShutdownHooksInOrder ().size ());
223+ Hook hook1 = new Hook ("hook1" , 0 , false );
224+ Hook hook2 = new Hook ("hook2" , 0 , false );
225+ mgr .addShutdownHook (hook1 , 9 ); // create Hook1 with priority 9
226+ assertTrue ("No hook1" , mgr .hasShutdownHook (hook1 )); // hook1 lookup works
227+ assertEquals (1 , mgr .getShutdownHooksInOrder ().size ()); // 1 hook
228+ assertFalse ("Delete hook2 should not be allowed" ,
229+ mgr .removeShutdownHook (hook2 ));
230+ assertTrue ("Can't delete hook1" , mgr .removeShutdownHook (hook1 ));
231+ assertEquals (0 , mgr .getShutdownHooksInOrder ().size ());
232+ }
233+
225234 private static final AtomicInteger INVOCATION_COUNT = new AtomicInteger ();
226235
227236 /**
0 commit comments