1 /*
   2  * Copyright (c) 2001, 2016, 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 4285878
  26  * @summary When the "java.rmi.dgc.leaseValue" system property is set to a
  27  * value much lower than its default (10 minutes), then the server-side
  28  * user-visible detection of DGC lease expiration-- in the form of
  29  * Unreferenced.unreferenced() invocations and possibly even local garbage
  30  * collection (including weak reference notification, finalization, etc.)--
  31  * may be delayed longer than expected.  While this is not a spec violation
  32  * (because there are no timeliness guarantees for any of these garbage
  33  * collection-related events), the user might expect that an unreferenced()
  34  * invocation for an object whose last client has terminated abnorally
  35  * should occur on relatively the same time order as the lease value
  36  * granted.
  37  * @author Peter Jones
  38  *
  39  * @library ../../../testlibrary
  40  * @modules java.rmi/sun.rmi.registry
  41  *          java.rmi/sun.rmi.server
  42  *          java.rmi/sun.rmi.transport
  43  *          java.rmi/sun.rmi.transport.tcp
  44  * @build TestLibrary JavaVM LeaseCheckInterval_Stub SelfTerminator
  45  * @run main/othervm LeaseCheckInterval
  46  * @key intermittent
  47  */
  48 
  49 import java.rmi.Remote;
  50 import java.rmi.RemoteException;
  51 import java.rmi.registry.LocateRegistry;
  52 import java.rmi.registry.Registry;
  53 import java.rmi.server.UnicastRemoteObject;
  54 import java.rmi.server.Unreferenced;
  55 
  56 public class LeaseCheckInterval implements Remote, Unreferenced {
  57 
  58     public static final String BINDING = "LeaseCheckInterval";
  59     private static final long LEASE_VALUE = 10000;
  60     private static final long TIMEOUT = 20000;
  61 
  62     private Object lock = new Object();
  63     private boolean unreferencedInvoked = false;
  64 
  65     public void unreferenced() {
  66         System.err.println("unreferenced() method invoked");
  67         synchronized (lock) {
  68             unreferencedInvoked = true;
  69             lock.notify();
  70         }
  71     }
  72 
  73     public static void main(String[] args) throws Exception {
  74 
  75         System.err.println("\nRegression test for bug 4285878\n");
  76 
  77         /*
  78          * Set the duration of leases granted to a very small value, so that
  79          * we can test if expirations are detected in a roughly comparable
  80          * time.
  81          */
  82         System.setProperty("java.rmi.dgc.leaseValue",
  83                            String.valueOf(LEASE_VALUE));
  84 
  85         LeaseCheckInterval obj = new LeaseCheckInterval();
  86 
  87         try {
  88             UnicastRemoteObject.exportObject(obj);
  89             System.err.println("exported remote object");
  90 
  91             int registryPort = TestLibrary.getUnusedRandomPort();
  92             Registry localRegistry =
  93                 LocateRegistry.createRegistry(registryPort);
  94             System.err.println("created local registry");
  95 
  96             localRegistry.bind(BINDING, obj);
  97             System.err.println("bound remote object in local registry");
  98 
  99             synchronized (obj.lock) {
 100                 System.err.println("starting remote client VM...");
 101                 (new JavaVM("SelfTerminator", "-Drmi.registry.port=" +
 102                             registryPort, "")).start();
 103 
 104                 System.err.println("waiting for unreferenced() callback...");
 105                 obj.lock.wait(TIMEOUT);
 106 
 107                 if (obj.unreferencedInvoked) {
 108                     System.err.println("TEST PASSED: " +
 109                         "unreferenced() invoked in timely fashion");
 110                 } else {
 111                     throw new RuntimeException(
 112                         "TEST FAILED: unreferenced() not invoked after " +
 113                         ((double) TIMEOUT / 1000.0) + " seconds");
 114                 }
 115             }
 116 
 117         } catch (Exception e) {
 118             if (e instanceof RuntimeException) {
 119                 throw (RuntimeException) e;
 120             } else {
 121                 throw new RuntimeException(
 122                     "TEST FAILED: unexpected exception: " + e.toString());
 123             }
 124         } finally {
 125             /*
 126              * When all is said and done, try to unexport the remote object
 127              * so that the VM has a chance to exit.
 128              */
 129             try {
 130                 UnicastRemoteObject.unexportObject(obj, true);
 131             } catch (RemoteException e) {
 132             }
 133         }
 134     }
 135 }