test/java/rmi/activation/Activatable/shutdownGracefully/ShutdownGracefully.java

Print this page
rev 9281 : 8032050: Clean up for java/rmi/activation/Activatable/shutdownGracefully/ShutdownGracefully.java
Reviewed-by: smarks
   1 /*
   2  * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /* @test
  25  * @bug 4183169
  26  * @summary Minor problem with the way ReliableLog handles IOExceptions.
  27  *
  28  * @author Laird Dornin; code borrowed from Ann Wollrath
  29  *
  30  * @library ../../../testlibrary
  31  * @build TestLibrary RMID
  32  *     TestSecurityManager RegisteringActivatable ShutdownGracefully_Stub
  33  * @run main/othervm/policy=security.policy/timeout=700 ShutdownGracefully
  34  */
  35 
  36 import java.rmi.activation.*;
  37 import java.rmi.*;
  38 import java.util.Properties;

  39 
  40 /**
  41  * The test creates an rmid with a special security manager.  After
  42  * rmid makes two registrations (which is greater than rmid's
  43  * snapshotInterval) the security manager stops allowing rmid to write
  44  * to update and snapshot log files in rmid's log directory.  The Test
  45  * registers an Activatable object twice with different group ids.
  46  * The second registration will cause rmid to have to write to a
  47  * LogFile (it causes a snapshot) and the security manager will not
  48  * allow the file write to happen.  The test makes sure that rmid
  49  * shuts down in a graceful manner without any explicit request to do
  50  * so.  The test will not exit for 400 seconds if rmid does not exit
  51  * (after that time, the test will fail).
  52  */
  53 public class ShutdownGracefully
  54     extends Activatable implements Runnable, RegisteringActivatable
  55 {
  56     private static RegisteringActivatable registering = null;
  57 
  58     private final static long SHUTDOWN_TIMEOUT = 400 * 1000;
  59 
  60     public static void main(String args[]) {
  61 
  62         RMID rmid = null;


  63 


  64         System.err.println("\nRegression test for bug/rfe 4183169\n");
  65 
  66         try {
  67             TestLibrary.suggestSecurityManager(
  68                 "java.rmi.RMISecurityManager");
  69 
  70             // start an rmid.
  71             RMID.removeLog();
  72             rmid = RMID.createRMID();
  73 
  74             // rmid needs to run with a security manager that
  75             // simulates a log problem; rmid should also snapshot
  76             // quickly.
  77             rmid.addOptions(new String[] {
  78                 "-Djava.security.manager=TestSecurityManager",
  79                 "-Dsun.rmi.activation.snapshotInterval=1"});
  80 
  81             //      rmid.addArguments(new String[] {
  82             //          "-C-Djava.rmi.server.logCalls=true"});
  83 


 115              * have caused rmid to have thrown a runtime exception and
 116              * continue running in an unstable state.  With the fix
 117              * for 4183169, rmid should shutdown gracefully instead.
 118              */
 119 
 120             /*
 121              * register another activatable with a new group id; rmid
 122              * should not recover from this...  I use two
 123              * registrations to more closely simulate the environment
 124              * in which the bug was found.  In java versions with out
 125              * the appropriate bug fix, rmid would hide a
 126              * NullPointerException in this circumstance.
 127              */
 128             p.put("dummyname", "dummyvalue");
 129             groupDesc = new ActivationGroupDesc(p, null);
 130             ActivationGroupID secondGroupID =
 131                 system.registerGroup(groupDesc);
 132             desc = new ActivationDesc(secondGroupID,
 133                 "ShutdownGracefully", null, null);
 134 




 135             try {
 136                 registering = (RegisteringActivatable)
 137                     Activatable.register(desc);
 138 
 139                 System.err.println("second activate and deactivate " +
 140                                    "object via method call");
 141             } catch (ActivationException e) {
 142                 System.err.println("received exception from registration " +
 143                                    "call that should have failed...");
 144             }
 145 
 146             /*
 147              * no longer needed because the security manager
 148              * throws an exception during snapshot
 149              */
 150             /*
 151             try {
 152                 registering.shutdown();
 153 
 154                 System.err.println("received exception from remote " +
 155                                    "call that should have failed...");
 156             } catch (RemoteException e) {
 157             }
 158             */
 159 
 160         } catch (Exception e) {
 161             TestLibrary.bomb("\nfailure: unexpected exception ", e);
 162         } finally {
 163             try {
 164                 Thread.sleep(4000);
 165             } catch (InterruptedException e) {
 166             }
 167 
 168             registering = null;
 169 
 170             // Need to make sure that rmid goes away by itself
 171             JavaVM rmidProcess = rmid;
 172             if (rmidProcess != null) {
 173                 try {
 174                     Runnable waitThread =
 175                         new ShutdownDetectThread(rmidProcess);
 176 
 177                     synchronized (waitThread) {
 178                         (new Thread(waitThread)).start();
 179                         waitThread.wait(SHUTDOWN_TIMEOUT);
 180                         System.err.println("rmid has shutdown");
 181 
 182                         if (!rmidDone) {
 183                             // ensure that this rmid does not infect
 184                             // other tests.
 185                             rmidProcess.destroy();
 186                             TestLibrary.bomb("rmid did not shutdown " +
 187                                              "gracefully in time");
 188                         }
 189                     }
 190                 } catch (Exception e) {
 191                     TestLibrary.bomb("exception waiting for rmid " +
 192                                      "to shut down");
 193                 }
 194             }
 195             // else rmid should be down
 196         }
 197 
 198         System.err.println
 199             ("\nsuccess: ShutdownGracefully test passed ");
 200     }
 201 
 202     private static boolean rmidDone = false;
 203 
 204     /**
 205      * class that waits for rmid to exit
 206      */
 207     private static class ShutdownDetectThread implements Runnable {
 208         private JavaVM rmidProcess = null;
 209 
 210         ShutdownDetectThread(JavaVM rmidProcess) {
 211             this.rmidProcess = rmidProcess;
 212         }
 213         public void run() {
 214             System.err.println("waiting for rmid to shutdown");
 215 
 216             try {
 217                 rmidProcess.waitFor();
 218             } catch (InterruptedException e) {
 219                 // should not happen
 220             }
 221 
 222             synchronized (this) {
 223                 // notify parent thread when rmid has exited
 224                 this.notify();
 225                 rmidDone = true;
 226             }
 227 
 228             RMID.removeLog();
 229         }


 230     }
 231 
 232     /**
 233      * implementation of RegisteringActivatable
 234      */
 235     public ShutdownGracefully
 236         (ActivationID id, MarshalledObject mo) throws RemoteException
 237     {
 238         // register/export anonymously
 239         super(id, 0);
 240     }
 241 
 242     /**
 243      * Spawns a thread to deactivate the object.


 244      */
 245     public void shutdown() throws Exception {
 246         (new Thread(this, "ShutdownGracefully")).start();
 247     }
 248 
 249     /**
 250      * Thread to deactivate object. First attempts to make object
 251      * inactive (via the inactive method).  If that fails (the
 252      * object may still have pending/executing calls), then
 253      * unexport the object forcibly.
 254      */
 255     public void run() {
 256         try {
 257             Thread.sleep(50 * 1000);
 258         } catch (InterruptedException e) {
 259         }
 260         ActivationLibrary.deactivate(this, getID());
 261     }
 262 }
   1 /*
   2  * Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /* @test
  25  * @bug 4183169 8032050
  26  * @summary Minor problem with the way ReliableLog handles IOExceptions.
  27  *
  28  * @author Laird Dornin; code borrowed from Ann Wollrath
  29  *
  30  * @library ../../../testlibrary
  31  * @build TestLibrary RMID
  32  *     TestSecurityManager RegisteringActivatable ShutdownGracefully_Stub
  33  * @run main/othervm/policy=security.policy/timeout=700 ShutdownGracefully
  34  */
  35 
  36 import java.rmi.activation.*;
  37 import java.rmi.*;
  38 import java.util.Properties;
  39 import java.util.concurrent.TimeoutException;
  40 
  41 /**
  42  * The test creates an rmid with a special security manager.  After
  43  * rmid makes two registrations (which is greater than rmid's
  44  * snapshotInterval) the security manager stops allowing rmid to write
  45  * to update and snapshot log files in rmid's log directory.  The Test
  46  * registers an Activatable object twice with different group ids.
  47  * The second registration will cause rmid to have to write to a
  48  * LogFile (it causes a snapshot) and the security manager will not
  49  * allow the file write to happen.  The test makes sure that rmid
  50  * shuts down in a graceful manner without any explicit request to do
  51  * so.  The test will not exit for 400 seconds if rmid does not exit
  52  * (after that time, the test will fail).
  53  */
  54 public class ShutdownGracefully
  55     extends Activatable implements RegisteringActivatable
  56 {
  57     private static RegisteringActivatable registering = null;
  58 
  59     private final static long SHUTDOWN_TIMEOUT = 400 * 1000;
  60 
  61     public static void main(String args[]) {
  62 
  63         RMID rmid = null;
  64         // flag to show if test succeed
  65         boolean testSucceeded = false;
  66 
  67         // error message
  68         String failure = null;
  69         System.err.println("\nRegression test for bug/rfe 4183169\n");
  70 
  71         try {
  72             TestLibrary.suggestSecurityManager(
  73                 "java.rmi.RMISecurityManager");
  74 
  75             // start an rmid.
  76             RMID.removeLog();
  77             rmid = RMID.createRMID();
  78 
  79             // rmid needs to run with a security manager that
  80             // simulates a log problem; rmid should also snapshot
  81             // quickly.
  82             rmid.addOptions(new String[] {
  83                 "-Djava.security.manager=TestSecurityManager",
  84                 "-Dsun.rmi.activation.snapshotInterval=1"});
  85 
  86             //      rmid.addArguments(new String[] {
  87             //          "-C-Djava.rmi.server.logCalls=true"});
  88 


 120              * have caused rmid to have thrown a runtime exception and
 121              * continue running in an unstable state.  With the fix
 122              * for 4183169, rmid should shutdown gracefully instead.
 123              */
 124 
 125             /*
 126              * register another activatable with a new group id; rmid
 127              * should not recover from this...  I use two
 128              * registrations to more closely simulate the environment
 129              * in which the bug was found.  In java versions with out
 130              * the appropriate bug fix, rmid would hide a
 131              * NullPointerException in this circumstance.
 132              */
 133             p.put("dummyname", "dummyvalue");
 134             groupDesc = new ActivationGroupDesc(p, null);
 135             ActivationGroupID secondGroupID =
 136                 system.registerGroup(groupDesc);
 137             desc = new ActivationDesc(secondGroupID,
 138                 "ShutdownGracefully", null, null);
 139 
 140             /*
 141              * registration request is expected to be failed. succeeded case
 142              * should be recorded. And raise error after clean up  rmid.
 143              */
 144             try {
 145                 registering = (RegisteringActivatable)
 146                     Activatable.register(desc);
 147                 failure = "The registration request succeeded unexpectedly";


 148             } catch (ActivationException e) {
 149                 System.err.println("received exception from registration " +
 150                                    "call that should have failed...");
 151                 // Need wait rmid prcoess terminates.




























 152                 try {
 153                     int exitCode = rmid.waitFor(SHUTDOWN_TIMEOUT);
 154                     System.err.println("RMID has exited gracefully with exitcode:" + exitCode);
 155                     rmid = null;
 156                     testSucceeded = true;
 157                 } catch (TimeoutException te) {
 158                     failure = "RMID hasn't exited gracefully in expected time";








 159                 }
 160             }
 161         } catch (Exception e) {
 162             failure = "unexpected exception " + e;
 163         } finally {
 164             if (rmid != null)
 165                 rmid.destroy();


































 166         }
 167         if (!testSucceeded)
 168             TestLibrary.bomb("\nfailure: " + failure);
 169     }
 170 
 171     /**
 172      * implementation of RegisteringActivatable
 173      */
 174     public ShutdownGracefully
 175         (ActivationID id, MarshalledObject mo) throws RemoteException
 176     {
 177         // register/export anonymously
 178         super(id, 0);
 179     }
 180 
 181     /**
 182      * Deactivates the object. We need to unexport forcibly because this call
 183      * in-progress on this object, which is the same object that we are trying
 184      * to deactivate.
 185      */
 186     public void shutdown() throws Exception {
 187         Activatable.unexportObject(this, true);













 188         ActivationLibrary.deactivate(this, getID());
 189     }
 190 }