test/java/rmi/testlibrary/RMID.java

Print this page
rev 15773 : 8085192: java/rmi/activation/Activatable tests fail intermittently due to "Port already in use"
Reviewed-by:


  32  * file of name <code>TestParams.defaultPolicy</code>.
  33  *
  34  * Activation groups should run with the same security manager as the
  35  * test.
  36  */
  37 public class RMID extends JavaVM {
  38 
  39     // TODO: adjust these based on the timeout factor
  40     // such as jcov.sleep.multiplier; see start(long) method.
  41     // Also consider the test.timeout.factor property (a float).
  42     private static final long TIMEOUT_SHUTDOWN_MS = 60_000L;
  43     private static final long TIMEOUT_DESTROY_MS  = 10_000L;
  44     private static final long STARTTIME_MS        = 15_000L;
  45     private static final long POLLTIME_MS         = 100L;
  46 
  47     private static final String SYSTEM_NAME = ActivationSystem.class.getName();
  48         // "java.rmi.activation.ActivationSystem"
  49 
  50     public static String MANAGER_OPTION="-Djava.security.manager=";
  51 
  52     /** Test port for rmid */
  53     private final int port;







  54 
  55     /** Initial log name */
  56     protected static String log = "log";
  57     /** rmid's logfile directory; currently must be "." */
  58     protected static String LOGDIR = ".";
  59 




  60     private static void mesg(Object mesg) {
  61         System.err.println("RMID: " + mesg.toString());
  62     }
  63 
  64     /** make test options and arguments */
  65     private static String makeOptions(boolean debugExec) {
  66 
  67         String options = " -Dsun.rmi.server.activation.debugExec=" +
  68             debugExec;
  69         // +
  70         //" -Djava.compiler= ";
  71 
  72         // if test params set, want to propagate them
  73         if (!TestParams.testSrc.equals("")) {
  74             options += " -Dtest.src=" + TestParams.testSrc + " ";
  75         }
  76         //if (!TestParams.testClasses.equals("")) {
  77         //    options += " -Dtest.classes=" + TestParams.testClasses + " ";
  78         //}
  79         options += " -Dtest.classes=" + TestParams.testClasses //;
  80          +
  81          " -Djava.rmi.server.logLevel=v ";
  82 
  83         // +
  84         // " -Djava.security.debug=all ";
  85 
  86         // Set execTimeout to 60 sec (default is 30 sec)
  87         // to avoid spurious timeouts on slow machines.
  88         options += " -Dsun.rmi.activation.execTimeout=60000";
  89 











  90         return options;
  91     }
  92 
  93     private static String makeArgs(boolean includePortArg, int port) {
  94         String propagateManager = null;
  95 
  96         // rmid will run with a security manager set, but no policy
  97         // file - it should not need one.
  98         if (System.getSecurityManager() == null) {
  99             propagateManager = MANAGER_OPTION +
 100                 TestParams.defaultSecurityManager;
 101         } else {
 102             propagateManager = MANAGER_OPTION +
 103                 System.getSecurityManager().getClass().getName();
 104         }
 105 
 106         // getAbsolutePath requires permission to read user.dir
 107         String args =
 108             " -log " + (new File(LOGDIR, log)).getAbsolutePath();
 109 
 110         if (includePortArg) {

 111             args += " -port " + port;
 112         }
 113 
 114         // +
 115         //      " -C-Djava.compiler= ";
 116 
 117         // if test params set, want to propagate them
 118         if (!TestParams.testSrc.equals("")) {
 119             args += " -C-Dtest.src=" + TestParams.testSrc;
 120         }
 121         if (!TestParams.testClasses.equals("")) {
 122             args += " -C-Dtest.classes=" + TestParams.testClasses;
 123         }
 124 
 125         if (!TestParams.testJavaOpts.equals("")) {
 126             for (String a : TestParams.testJavaOpts.split(" +")) {
 127                 args += " -C" + a;
 128             }
 129         }
 130 


 143     /**
 144      * Routine that creates an rmid that will run with or without a
 145      * policy file.
 146      */
 147     public static RMID createRMID() {
 148         return createRMID(System.out, System.err, true, true,
 149                           TestLibrary.getUnusedRandomPort());
 150     }
 151 
 152     public static RMID createRMID(OutputStream out, OutputStream err,
 153                                   boolean debugExec)
 154     {
 155         return createRMID(out, err, debugExec, true,
 156                           TestLibrary.getUnusedRandomPort());
 157     }
 158 
 159     public static RMID createRMID(OutputStream out, OutputStream err,
 160                                   boolean debugExec, boolean includePortArg,
 161                                   int port)
 162     {
 163         String options = makeOptions(debugExec);
 164         String args = makeArgs(includePortArg, port);
 165         RMID rmid = new RMID("sun.rmi.server.Activation", options, args,
 166                              out, err, port);
 167         rmid.setPolicyFile(TestParams.defaultRmidPolicy);
 168 
 169         return rmid;
 170     }
 171 











 172 
 173     /**
 174      * Private constructor. RMID instances should be created
 175      * using the static factory methods.
 176      */
 177     private RMID(String classname, String options, String args,
 178                    OutputStream out, OutputStream err, int port)
 179     {
 180         super(classname, options, args, out, err);
 181         this.port = port;
 182     }
 183 
 184     /**
 185      * Removes rmid's log file directory.
 186      */
 187     public static void removeLog() {
 188         File f = new File(LOGDIR, log);
 189 
 190         if (f.exists()) {
 191             mesg("Removing rmid's old log file.");


 230     }
 231 
 232     /**
 233      * Starts rmid and waits up to the default timeout period
 234      * to confirm that it's running.
 235      */
 236     public void start() throws IOException {
 237         start(STARTTIME_MS);
 238     }
 239 
 240     /**
 241      * Starts rmid and waits up to the given timeout period
 242      * to confirm that it's running.
 243      */
 244     public void start(long waitTime) throws IOException {
 245 
 246         // if rmid is already running, then the test will fail with
 247         // a well recognized exception (port already in use...).
 248 
 249         mesg("Starting rmid on port " + port + ".");
 250         super.start();



 251 
 252         // int slopFactor = 1;
 253         // try {
 254         //     slopFactor = Integer.valueOf(
 255         //         TestLibrary.getExtraProperty("jcov.sleep.multiplier","1"));
 256         // } catch (NumberFormatException ignore) {}
 257         // waitTime = waitTime * slopFactor;
 258 
 259         long startTime = System.currentTimeMillis();
 260         long deadline = TestLibrary.computeDeadline(startTime, waitTime);
 261 
 262         while (true) {
 263             try {
 264                 Thread.sleep(POLLTIME_MS);
 265             } catch (InterruptedException ie) {
 266                 Thread.currentThread().interrupt();
 267                 mesg("Starting rmid interrupted, giving up at " +
 268                     (System.currentTimeMillis() - startTime) + "ms.");
 269                 return;
 270             }
 271 
 272             try {
 273                 int status = vm.exitValue();

 274                 TestLibrary.bomb("Rmid process exited with status " + status + " after " +
 275                     (System.currentTimeMillis() - startTime) + "ms.");


 276             } catch (IllegalThreadStateException ignore) { }
 277 
 278             // The rmid process is alive; check to see whether
 279             // it responds to a remote call.
 280 
 281             if (lookupSystem(port) != null) {
 282                 /*
 283                  * We need to set the java.rmi.activation.port value as the
 284                  * activation system will use the property to determine the
 285                  * port #.  The activation system will use this value if set.
 286                  * If it isn't set, the activation system will set it to an
 287                  * incorrect value.
 288                  */
 289                 System.setProperty("java.rmi.activation.port", Integer.toString(port));
 290                 mesg("Started successfully after " +
 291                     (System.currentTimeMillis() - startTime) + "ms.");
 292                 return;
 293             }
 294 
 295             if (System.currentTimeMillis() > deadline) {
 296                 TestLibrary.bomb("Failed to start rmid, giving up after " +
 297                     (System.currentTimeMillis() - startTime) + "ms.", null);
 298             }
 299         }
 300     }
 301 
 302     /**
 303      * Destroys rmid and restarts it. Note that this does NOT clean up
 304      * the log file, because it stores information about restartable
 305      * and activatable objects that must be carried over to the new
 306      * rmid instance.
 307      */
 308     public void restart() throws IOException {
 309         destroy();


 310         start();
 311     }
 312 
 313     /**
 314      * Ask rmid to shutdown gracefully using a remote method call.
 315      * catch any errors that might occur from rmid not being present
 316      * at time of shutdown invocation. If the remote call is
 317      * successful, wait for the process to terminate. Return true
 318      * if the process terminated, otherwise return false.
 319      */
 320     private boolean shutdown() throws InterruptedException {
 321         mesg("shutdown()");
 322         long startTime = System.currentTimeMillis();
 323         ActivationSystem system = lookupSystem(port);
 324         if (system == null) {
 325             mesg("lookupSystem() returned null after " +
 326                 (System.currentTimeMillis() - startTime) + "ms.");
 327             return false;
 328         }
 329 




  32  * file of name <code>TestParams.defaultPolicy</code>.
  33  *
  34  * Activation groups should run with the same security manager as the
  35  * test.
  36  */
  37 public class RMID extends JavaVM {
  38 
  39     // TODO: adjust these based on the timeout factor
  40     // such as jcov.sleep.multiplier; see start(long) method.
  41     // Also consider the test.timeout.factor property (a float).
  42     private static final long TIMEOUT_SHUTDOWN_MS = 60_000L;
  43     private static final long TIMEOUT_DESTROY_MS  = 10_000L;
  44     private static final long STARTTIME_MS        = 15_000L;
  45     private static final long POLLTIME_MS         = 100L;
  46 
  47     private static final String SYSTEM_NAME = ActivationSystem.class.getName();
  48         // "java.rmi.activation.ActivationSystem"
  49 
  50     public static String MANAGER_OPTION="-Djava.security.manager=";
  51 
  52     /**
  53      * Test port for rmid.
  54      *
  55      * May initially be 0, which means that the child rmid process will choose
  56      * an ephemeral port and report it back to the parent process. This field
  57      * will then be set to the child rmid's ephemeral port value.
  58      */
  59     private volatile int port;
  60     //private final boolean ephemeralPort
  61 
  62     /** Initial log name */
  63     protected static String log = "log";
  64     /** rmid's logfile directory; currently must be "." */
  65     protected static String LOGDIR = ".";
  66 
  67     /** The output message from the child rmid process that directly precedes
  68      * the ephemeral port number.*/
  69     public static final String EPHEMERAL_MSG = "RmidSelectorProvider-listening-On:";
  70 
  71     private static void mesg(Object mesg) {
  72         System.err.println("RMID: " + mesg.toString());
  73     }
  74 
  75     /** make test options and arguments */
  76     private static String makeOptions(int port, boolean debugExec) {
  77 
  78         String options = " -Dsun.rmi.server.activation.debugExec=" +
  79             debugExec;
  80         // +
  81         //" -Djava.compiler= ";
  82 
  83         // if test params set, want to propagate them
  84         if (!TestParams.testSrc.equals("")) {
  85             options += " -Dtest.src=" + TestParams.testSrc + " ";
  86         }
  87         //if (!TestParams.testClasses.equals("")) {
  88         //    options += " -Dtest.classes=" + TestParams.testClasses + " ";
  89         //}
  90         options += " -Dtest.classes=" + TestParams.testClasses //;
  91          +
  92          " -Djava.rmi.server.logLevel=v ";
  93 
  94         // +
  95         // " -Djava.security.debug=all ";
  96 
  97         // Set execTimeout to 60 sec (default is 30 sec)
  98         // to avoid spurious timeouts on slow machines.
  99         options += " -Dsun.rmi.activation.execTimeout=60000";
 100 
 101         if (port == 0) {
 102             // Ephemeral port, so have the rmid child process create the
 103             // server socket channel and report its port number, over stdin.
 104             options += " -classpath " + TestParams.testClassPath;
 105             options += " --add-exports=java.base/sun.nio.ch=ALL-UNNAMED";
 106             options += " -Djava.nio.channels.spi.SelectorProvider=RMIDSelectorProvider";
 107 
 108             // Disable redirection of System.err to /tmp
 109             options += " -Dsun.rmi.server.activation.disableErrRedirect";
 110         }
 111 
 112         return options;
 113     }
 114 
 115     private static String makeArgs(boolean includePortArg, int port) {
 116         String propagateManager = null;
 117 
 118         // rmid will run with a security manager set, but no policy
 119         // file - it should not need one.
 120         if (System.getSecurityManager() == null) {
 121             propagateManager = MANAGER_OPTION +
 122                 TestParams.defaultSecurityManager;
 123         } else {
 124             propagateManager = MANAGER_OPTION +
 125                 System.getSecurityManager().getClass().getName();
 126         }
 127 
 128         // getAbsolutePath requires permission to read user.dir
 129         String args =
 130             " -log " + (new File(LOGDIR, log)).getAbsolutePath();
 131 
 132         // 0 = ephemeral port, do not include an explicit port number
 133         if (includePortArg && port != 0) {
 134             args += " -port " + port;
 135         }
 136 
 137         // +
 138         //      " -C-Djava.compiler= ";
 139 
 140         // if test params set, want to propagate them
 141         if (!TestParams.testSrc.equals("")) {
 142             args += " -C-Dtest.src=" + TestParams.testSrc;
 143         }
 144         if (!TestParams.testClasses.equals("")) {
 145             args += " -C-Dtest.classes=" + TestParams.testClasses;
 146         }
 147 
 148         if (!TestParams.testJavaOpts.equals("")) {
 149             for (String a : TestParams.testJavaOpts.split(" +")) {
 150                 args += " -C" + a;
 151             }
 152         }
 153 


 166     /**
 167      * Routine that creates an rmid that will run with or without a
 168      * policy file.
 169      */
 170     public static RMID createRMID() {
 171         return createRMID(System.out, System.err, true, true,
 172                           TestLibrary.getUnusedRandomPort());
 173     }
 174 
 175     public static RMID createRMID(OutputStream out, OutputStream err,
 176                                   boolean debugExec)
 177     {
 178         return createRMID(out, err, debugExec, true,
 179                           TestLibrary.getUnusedRandomPort());
 180     }
 181 
 182     public static RMID createRMID(OutputStream out, OutputStream err,
 183                                   boolean debugExec, boolean includePortArg,
 184                                   int port)
 185     {
 186         String options = makeOptions(port, debugExec);
 187         String args = makeArgs(includePortArg, port);
 188         RMID rmid = new RMID("sun.rmi.server.Activation", options, args,
 189                              out, err, port);
 190         rmid.setPolicyFile(TestParams.defaultRmidPolicy);
 191 
 192         return rmid;
 193     }
 194 
 195     public static RMID createRMIDOnEphemeralPort() {
 196         return createRMID(System.out, System.err, true, true, 0);
 197     }
 198 
 199     public static RMID createRMIDOnEphemeralPort(OutputStream out,
 200                                                  OutputStream err,
 201                                                  boolean debugExec)
 202     {
 203         return createRMID(out, err, debugExec, true, 0);
 204     }
 205 
 206 
 207     /**
 208      * Private constructor. RMID instances should be created
 209      * using the static factory methods.
 210      */
 211     private RMID(String classname, String options, String args,
 212                    OutputStream out, OutputStream err, int port)
 213     {
 214         super(classname, options, args, out, err);
 215         this.port = port;
 216     }
 217 
 218     /**
 219      * Removes rmid's log file directory.
 220      */
 221     public static void removeLog() {
 222         File f = new File(LOGDIR, log);
 223 
 224         if (f.exists()) {
 225             mesg("Removing rmid's old log file.");


 264     }
 265 
 266     /**
 267      * Starts rmid and waits up to the default timeout period
 268      * to confirm that it's running.
 269      */
 270     public void start() throws IOException {
 271         start(STARTTIME_MS);
 272     }
 273 
 274     /**
 275      * Starts rmid and waits up to the given timeout period
 276      * to confirm that it's running.
 277      */
 278     public void start(long waitTime) throws IOException {
 279 
 280         // if rmid is already running, then the test will fail with
 281         // a well recognized exception (port already in use...).
 282 
 283         mesg("Starting rmid on port " + port + ".");
 284         int p = super.startAndGetPort();
 285         if (p != -1)
 286             port = p;
 287         mesg("Started rmid on port " + port + ".");
 288 
 289         // int slopFactor = 1;
 290         // try {
 291         //     slopFactor = Integer.valueOf(
 292         //         TestLibrary.getExtraProperty("jcov.sleep.multiplier","1"));
 293         // } catch (NumberFormatException ignore) {}
 294         // waitTime = waitTime * slopFactor;
 295 
 296         long startTime = System.currentTimeMillis();
 297         long deadline = TestLibrary.computeDeadline(startTime, waitTime);
 298 
 299         while (true) {
 300             try {
 301                 Thread.sleep(POLLTIME_MS);
 302             } catch (InterruptedException ie) {
 303                 Thread.currentThread().interrupt();
 304                 mesg("Starting rmid interrupted, giving up at " +
 305                     (System.currentTimeMillis() - startTime) + "ms.");
 306                 return;
 307             }
 308 
 309             try {
 310                 int status = vm.exitValue();
 311                 waitFor(TIMEOUT_SHUTDOWN_MS);
 312                 TestLibrary.bomb("Rmid process exited with status " + status + " after " +
 313                     (System.currentTimeMillis() - startTime) + "ms.");
 314             } catch (InterruptedException | TimeoutException e) {
 315                 mesg(e);
 316             } catch (IllegalThreadStateException ignore) { }
 317 
 318             // The rmid process is alive; check to see whether
 319             // it responds to a remote call.
 320 
 321             if (lookupSystem(port) != null) {
 322                 /*
 323                  * We need to set the java.rmi.activation.port value as the
 324                  * activation system will use the property to determine the
 325                  * port #.  The activation system will use this value if set.
 326                  * If it isn't set, the activation system will set it to an
 327                  * incorrect value.
 328                  */
 329                 System.setProperty("java.rmi.activation.port", Integer.toString(port));
 330                 mesg("Started successfully after " +
 331                     (System.currentTimeMillis() - startTime) + "ms.");
 332                 return;
 333             }
 334 
 335             if (System.currentTimeMillis() > deadline) {
 336                 TestLibrary.bomb("Failed to start rmid, giving up after " +
 337                     (System.currentTimeMillis() - startTime) + "ms.", null);
 338             }
 339         }
 340     }
 341 
 342     /**
 343      * Destroys rmid and restarts it. Note that this does NOT clean up
 344      * the log file, because it stores information about restartable
 345      * and activatable objects that must be carried over to the new
 346      * rmid instance.
 347      */
 348     public void restart() throws IOException {
 349         destroy();
 350         options = makeOptions(port, true);
 351         args = makeArgs(true, port);
 352         start();
 353     }
 354 
 355     /**
 356      * Ask rmid to shutdown gracefully using a remote method call.
 357      * catch any errors that might occur from rmid not being present
 358      * at time of shutdown invocation. If the remote call is
 359      * successful, wait for the process to terminate. Return true
 360      * if the process terminated, otherwise return false.
 361      */
 362     private boolean shutdown() throws InterruptedException {
 363         mesg("shutdown()");
 364         long startTime = System.currentTimeMillis();
 365         ActivationSystem system = lookupSystem(port);
 366         if (system == null) {
 367             mesg("lookupSystem() returned null after " +
 368                 (System.currentTimeMillis() - startTime) + "ms.");
 369             return false;
 370         }
 371