test/java/rmi/testlibrary/RMID.java

Print this page
rev 11003 : 8035000: clean up ActivationLibrary.DestroyThread

*** 19,46 **** * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ - /** - * - */ - import java.io.*; import java.rmi.*; import java.rmi.activation.*; import java.rmi.registry.*; /** * Utility class that creates an instance of rmid with a policy * file of name <code>TestParams.defaultPolicy</code>. * * Activation groups should run with the same security manager as the * test. */ public class RMID extends JavaVM { private static final String SYSTEM_NAME = ActivationSystem.class.getName(); // "java.rmi.activation.ActivationSystem" public static String MANAGER_OPTION="-Djava.security.manager="; --- 19,51 ---- * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ import java.io.*; import java.rmi.*; import java.rmi.activation.*; import java.rmi.registry.*; + import java.util.concurrent.TimeoutException; /** * Utility class that creates an instance of rmid with a policy * file of name <code>TestParams.defaultPolicy</code>. * * Activation groups should run with the same security manager as the * test. */ public class RMID extends JavaVM { + // TODO: adjust these based on the timeout factor + // such as jcov.sleep.multiplier; see start(long) method. + // Also consider the test.timeout.factor property (a float). + private static final long TIMEOUT_SHUTDOWN_MS = 60_000L; + private static final long TIMEOUT_DESTROY_MS = 10_000L; + private static final long STARTTIME_MS = 15_000L; + private static final long POLLTIME_MS = 100L; + private static final String SYSTEM_NAME = ActivationSystem.class.getName(); // "java.rmi.activation.ActivationSystem" public static String MANAGER_OPTION="-Djava.security.manager=";
*** 138,156 **** /** * Routine that creates an rmid that will run with or without a * policy file. */ public static RMID createRMID() { ! return createRMID(System.out, System.err, true); ! } ! ! public static RMID createRMID(boolean debugExec) { ! return createRMID(System.out, System.err, debugExec); ! } ! ! public static RMID createRMID(OutputStream out, OutputStream err) { ! return createRMID(out, err, true); } public static RMID createRMID(OutputStream out, OutputStream err, boolean debugExec) { --- 143,154 ---- /** * Routine that creates an rmid that will run with or without a * policy file. */ public static RMID createRMID() { ! return createRMID(System.out, System.err, true, true, ! TestLibrary.getUnusedRandomPort()); } public static RMID createRMID(OutputStream out, OutputStream err, boolean debugExec) {
*** 171,208 **** return rmid; } /** ! * Test RMID should be created with the createRMID method. */ ! protected RMID(String classname, String options, String args, OutputStream out, OutputStream err, int port) { super(classname, options, args, out, err); this.port = port; } ! public static void removeLog() { ! /* ! * Remove previous log file directory before ! * starting up rmid. */ File f = new File(LOGDIR, log); if (f.exists()) { ! mesg("removing rmid's old log file..."); String[] files = f.list(); if (files != null) { for (int i=0; i<files.length; i++) { (new File(f, files[i])).delete(); } } ! if (f.delete() != true) { ! mesg("\t" + " unable to delete old log file."); } } } /** --- 169,206 ---- return rmid; } /** ! * Private constructor. RMID instances should be created ! * using the static factory methods. */ ! private RMID(String classname, String options, String args, OutputStream out, OutputStream err, int port) { super(classname, options, args, out, err); this.port = port; } ! /** ! * Removes rmid's log file directory. */ + public static void removeLog() { File f = new File(LOGDIR, log); if (f.exists()) { ! mesg("Removing rmid's old log file."); String[] files = f.list(); if (files != null) { for (int i=0; i<files.length; i++) { (new File(f, files[i])).delete(); } } ! if (! f.delete()) { ! mesg("Warning: unable to delete old log file."); } } } /**
*** 213,230 **** */ protected static String getCodeCoverageArgs() { return TestLibrary.getExtraProperty("rmid.jcov.args",""); } - public void start() throws IOException { - start(10000); - } - - public void slowStart() throws IOException { - start(60000); - } - /** * Looks up the activation system in the registry on the given port, * returning its stub, or null if it's not present. This method differs from * ActivationGroup.getSystem() because this method looks on a specific port * instead of using the java.rmi.activation.port property like --- 211,220 ----
*** 237,275 **** } catch (RemoteException | NotBoundException ex) { return null; } } public void start(long waitTime) throws IOException { // if rmid is already running, then the test will fail with // a well recognized exception (port already in use...). ! mesg("starting rmid on port #" + port + "..."); super.start(); int slopFactor = 1; try { slopFactor = Integer.valueOf( TestLibrary.getExtraProperty("jcov.sleep.multiplier","1")); } catch (NumberFormatException ignore) {} waitTime = waitTime * slopFactor; ! // We check several times (as many as provides passed waitTime) to ! // see if Rmid is currently running. Waiting steps last 100 msecs. ! final long rmidStartSleepTime = 100; do { - // Sleeping for another rmidStartSleepTime time slice. try { ! Thread.sleep(Math.min(waitTime, rmidStartSleepTime)); } catch (InterruptedException ie) { Thread.currentThread().interrupt(); ! mesg("Thread interrupted while checking for start of Activation System. Giving up check."); ! mesg("Activation System state unknown"); return; } ! waitTime -= rmidStartSleepTime; // Checking if rmid is present if (lookupSystem(port) != null) { /* * We need to set the java.rmi.activation.port value as the --- 227,274 ---- } catch (RemoteException | NotBoundException ex) { return null; } } + /** + * Starts rmid and waits up to the default timeout period + * to confirm that it's running. + */ + public void start() throws IOException { + start(STARTTIME_MS); + } + + /** + * Starts rmid and waits up to the given timeout period + * to confirm that it's running. + */ public void start(long waitTime) throws IOException { // if rmid is already running, then the test will fail with // a well recognized exception (port already in use...). ! mesg("Starting rmid on port " + port + "."); super.start(); int slopFactor = 1; try { slopFactor = Integer.valueOf( TestLibrary.getExtraProperty("jcov.sleep.multiplier","1")); } catch (NumberFormatException ignore) {} waitTime = waitTime * slopFactor; ! // We check several times, for a maximum of waitTime, until we have ! // verified that rmid is running. do { try { ! Thread.sleep(Math.min(waitTime, POLLTIME_MS)); } catch (InterruptedException ie) { Thread.currentThread().interrupt(); ! mesg("Interrupted while starting activation system, giving up."); return; } ! waitTime -= POLLTIME_MS; // Checking if rmid is present if (lookupSystem(port) != null) { /* * We need to set the java.rmi.activation.port value as the
*** 277,389 **** * port #. The activation system will use this value if set. * If it isn't set, the activation system will set it to an * incorrect value. */ System.setProperty("java.rmi.activation.port", Integer.toString(port)); ! mesg("finished starting rmid."); return; } else { if (waitTime > 0) { mesg("rmid not started, will retry for " + waitTime + "ms"); } } } while (waitTime > 0); ! TestLibrary.bomb("start rmid failed... giving up", null); } public void restart() throws IOException { destroy(); start(); } /** * Ask rmid to shutdown gracefully using a remote method call. * catch any errors that might occur from rmid not being present ! * at time of shutdown invocation. ! * ! * Shutdown does not nullify possible references to the rmid ! * process object (destroy does though). */ ! public static void shutdown(int port) { ! ! try { ActivationSystem system = lookupSystem(port); - if (system == null) { ! TestLibrary.bomb("reference to the activation system was null"); } system.shutdown(); - } catch (RemoteException re) { - mesg("shutting down the activation daemon failed"); } catch (Exception e) { ! mesg("caught exception trying to shutdown rmid"); ! mesg(e.getMessage()); e.printStackTrace(); } ! mesg("testlibrary finished shutting down rmid"); } /** * Ask rmid to shutdown gracefully but then destroy the rmid * process if it does not exit by itself. This method only works * if rmid is a child process of the current VM. */ public void destroy() { ! // attempt graceful shutdown of the activation system ! shutdown(port); ! if (vm != null) { try { ! /* Waiting for distant RMID process to shutdown. ! * Waiting is bounded at a hardcoded max of 60 secs (1 min). ! * Waiting by steps of 200 msecs, thus at most 300 such attempts ! * for termination of distant RMID process. If process is not ! * known to be terminated properly after that time, ! * we give up for a gracefull termination, and thus go for ! * forcibly destroying the process. ! */ ! boolean vmEnded = false; ! int waitingTrials = 0; ! final int maxTrials = 300; ! final long vmProcessEndWaitInterval = 200; ! int vmExitValue; ! do { try { ! Thread.sleep(vmProcessEndWaitInterval); ! waitingTrials++; ! vmExitValue = vm.exitValue(); ! mesg("rmid exited on shutdown request"); ! vmEnded = true; ! } catch (IllegalThreadStateException illegal) { ! mesg("RMID's process still not terminated after more than " + ! (waitingTrials * vmProcessEndWaitInterval) + " milliseconds"); ! } ! } ! while (!vmEnded && ! (waitingTrials < maxTrials)); ! ! if (waitingTrials >= maxTrials) { ! mesg("RMID's process still not terminated after more than " + ! (waitingTrials * vmProcessEndWaitInterval) + " milliseconds." + ! "Givinp up gracefull termination..."); ! mesg("destroying RMID's process using Process.destroy()"); ! super.destroy(); } - } catch (InterruptedException ie) { Thread.currentThread().interrupt(); ! mesg("Thread interrupted while checking for termination of distant rmid vm. Giving up check."); ! } catch (Exception e) { ! mesg("caught unexpected exception trying to destroy rmid: " + ! e.getMessage()); ! e.printStackTrace(); } - // rmid will not restart if its process is not null vm = null; } } ! public int getPort() {return port;} } --- 276,385 ---- * port #. The activation system will use this value if set. * If it isn't set, the activation system will set it to an * incorrect value. */ System.setProperty("java.rmi.activation.port", Integer.toString(port)); ! mesg("Started successfully."); return; } else { if (waitTime > 0) { mesg("rmid not started, will retry for " + waitTime + "ms"); } } } while (waitTime > 0); ! TestLibrary.bomb("Failed to start rmid, giving up.", null); } + /** + * Destroys rmid and restarts it. Note that this does NOT clean up + * the log file, because it stores information about restartable + * and activatable objects that must be carried over to the new + * rmid instance. + */ public void restart() throws IOException { destroy(); start(); } /** * Ask rmid to shutdown gracefully using a remote method call. * catch any errors that might occur from rmid not being present ! * at time of shutdown invocation. If the remote call is ! * successful, wait for the process to terminate. Return true ! * if the process terminated, otherwise return false. */ ! private boolean shutdown() throws InterruptedException { ! mesg("shutdown()"); ActivationSystem system = lookupSystem(port); if (system == null) { ! mesg("lookupSystem() returned null"); ! return false; } + try { + mesg("ActivationSystem.shutdown()"); system.shutdown(); } catch (Exception e) { ! mesg("Caught exception from ActivationSystem.shutdown():"); e.printStackTrace(); } ! try { ! waitFor(TIMEOUT_SHUTDOWN_MS); ! mesg("Shutdown successful."); ! return true; ! } catch (TimeoutException ex) { ! mesg("Shutdown timed out:"); ! ex.printStackTrace(); ! return false; ! } } /** * Ask rmid to shutdown gracefully but then destroy the rmid * process if it does not exit by itself. This method only works * if rmid is a child process of the current VM. */ public void destroy() { ! if (vm == null) { ! throw new IllegalStateException("can't wait for RMID that isn't running"); ! } ! // First, attempt graceful shutdown of the activation system. try { ! if (! shutdown()) { ! // Graceful shutdown failed, use Process.destroy(). ! mesg("Destroying RMID process."); ! vm.destroy(); try { ! waitFor(TIMEOUT_DESTROY_MS); ! mesg("Destroy successful."); ! } catch (TimeoutException ex) { ! mesg("Destroy timed out, giving up."); ! ex.printStackTrace(); ! } } } catch (InterruptedException ie) { + mesg("Shutdown/destroy interrupted, giving up."); + ie.printStackTrace(); Thread.currentThread().interrupt(); ! return; } vm = null; } + + /** + * Shuts down rmid and then removes its log file. + */ + public void cleanup() { + destroy(); + RMID.removeLog(); } ! /** ! * Gets the port on which this rmid is listening. ! */ ! public int getPort() { ! return port; ! } }