|
@@ -21,7 +21,6 @@ import java.util.List;
|
|
import java.util.concurrent.TimeUnit;
|
|
import java.util.concurrent.TimeUnit;
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
|
|
|
|
|
-import org.junit.After;
|
|
|
|
import org.junit.Test;
|
|
import org.junit.Test;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.slf4j.LoggerFactory;
|
|
@@ -43,13 +42,10 @@ public class TestShutdownHookManager {
|
|
LoggerFactory.getLogger(TestShutdownHookManager.class.getName());
|
|
LoggerFactory.getLogger(TestShutdownHookManager.class.getName());
|
|
|
|
|
|
/**
|
|
/**
|
|
- * remove all the shutdown hooks so that they never get invoked later
|
|
|
|
- * on in this test process.
|
|
|
|
|
|
+ * A new instance of ShutdownHookManager to ensure parallel tests
|
|
|
|
+ * don't have shared context.
|
|
*/
|
|
*/
|
|
- @After
|
|
|
|
- public void clearShutdownHooks() {
|
|
|
|
- ShutdownHookManager.get().clearShutdownHooks();
|
|
|
|
- }
|
|
|
|
|
|
+ private final ShutdownHookManager mgr = new ShutdownHookManager();
|
|
|
|
|
|
/**
|
|
/**
|
|
* Verify hook registration, then execute the hook callback stage
|
|
* Verify hook registration, then execute the hook callback stage
|
|
@@ -58,7 +54,6 @@ public class TestShutdownHookManager {
|
|
*/
|
|
*/
|
|
@Test
|
|
@Test
|
|
public void shutdownHookManager() {
|
|
public void shutdownHookManager() {
|
|
- ShutdownHookManager mgr = ShutdownHookManager.get();
|
|
|
|
assertNotNull("No ShutdownHookManager", mgr);
|
|
assertNotNull("No ShutdownHookManager", mgr);
|
|
assertEquals(0, mgr.getShutdownHooksInOrder().size());
|
|
assertEquals(0, mgr.getShutdownHooksInOrder().size());
|
|
Hook hook1 = new Hook("hook1", 0, false);
|
|
Hook hook1 = new Hook("hook1", 0, false);
|
|
@@ -119,7 +114,7 @@ public class TestShutdownHookManager {
|
|
// now execute the hook shutdown sequence
|
|
// now execute the hook shutdown sequence
|
|
INVOCATION_COUNT.set(0);
|
|
INVOCATION_COUNT.set(0);
|
|
LOG.info("invoking executeShutdown()");
|
|
LOG.info("invoking executeShutdown()");
|
|
- int timeouts = ShutdownHookManager.executeShutdown();
|
|
|
|
|
|
+ int timeouts = mgr.executeShutdown();
|
|
LOG.info("Shutdown completed");
|
|
LOG.info("Shutdown completed");
|
|
assertEquals("Number of timed out hooks", 1, timeouts);
|
|
assertEquals("Number of timed out hooks", 1, timeouts);
|
|
|
|
|
|
@@ -193,7 +188,6 @@ public class TestShutdownHookManager {
|
|
*/
|
|
*/
|
|
@Test
|
|
@Test
|
|
public void testDuplicateRegistration() throws Throwable {
|
|
public void testDuplicateRegistration() throws Throwable {
|
|
- ShutdownHookManager mgr = ShutdownHookManager.get();
|
|
|
|
Hook hook = new Hook("hook1", 0, false);
|
|
Hook hook = new Hook("hook1", 0, false);
|
|
|
|
|
|
// add the hook
|
|
// add the hook
|
|
@@ -222,6 +216,21 @@ public class TestShutdownHookManager {
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ @Test
|
|
|
|
+ public void testShutdownRemove() throws Throwable {
|
|
|
|
+ assertNotNull("No ShutdownHookManager", mgr);
|
|
|
|
+ assertEquals(0, mgr.getShutdownHooksInOrder().size());
|
|
|
|
+ Hook hook1 = new Hook("hook1", 0, false);
|
|
|
|
+ Hook hook2 = new Hook("hook2", 0, false);
|
|
|
|
+ mgr.addShutdownHook(hook1, 9); // create Hook1 with priority 9
|
|
|
|
+ assertTrue("No hook1", mgr.hasShutdownHook(hook1)); // hook1 lookup works
|
|
|
|
+ assertEquals(1, mgr.getShutdownHooksInOrder().size()); // 1 hook
|
|
|
|
+ assertFalse("Delete hook2 should not be allowed",
|
|
|
|
+ mgr.removeShutdownHook(hook2));
|
|
|
|
+ assertTrue("Can't delete hook1", mgr.removeShutdownHook(hook1));
|
|
|
|
+ assertEquals(0, mgr.getShutdownHooksInOrder().size());
|
|
|
|
+ }
|
|
|
|
+
|
|
private static final AtomicInteger INVOCATION_COUNT = new AtomicInteger();
|
|
private static final AtomicInteger INVOCATION_COUNT = new AtomicInteger();
|
|
|
|
|
|
/**
|
|
/**
|