jdk/test/java/rmi/server/Unreferenced/finiteGCLatency/FiniteGCLatency.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 309 bda219d843f6
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/* 
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1998 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/* @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @bug 4164696
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @summary The local garbage collector needs to inspect a client VM's heap
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * often enough (even if the VM is idle) to detect unreachable live remote
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * references, so that their server VMs can be informed that the client VM
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * is no longer holding a reference; this facilitates the server VM invoking
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * the remote object's unreferenced() method (if present), garbage collecting
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * the remote object, and allowing the server VM to exit.  This test focuses
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * on the unreferenced() method being invoked: it tests that the callback
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * method will be invoked within a reasonable time after the remote object is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * no longer remotely reachable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * @author Peter Jones
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * @library ../../../testlibrary
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * @build FiniteGCLatency
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * @build FiniteGCLatency_Stub
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @run main/othervm/timeout=120 FiniteGCLatency
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.rmi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.rmi.registry.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.rmi.server.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
public class FiniteGCLatency implements Remote, Unreferenced {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private final static String BINDING = "FiniteGCLatency";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private final static long GC_INTERVAL = 6000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private final static long TIMEOUT = 50000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private Object lock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private boolean unreferencedInvoked = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    public void unreferenced() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
	System.err.println("unreferenced() method invoked");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
	synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
	    unreferencedInvoked = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
	    lock.notify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
	}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    public static void main(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
	System.err.println("\nRegression test for bug 4164696\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
	/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
	 * Set the interval that RMI will request for GC latency (before RMI
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
	 * gets initialized and this property is read) to an unrealistically
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
	 * small value, so that this test shouldn't have to wait too long.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
	 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
	System.setProperty("sun.rmi.dgc.client.gcInterval",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
	    String.valueOf(GC_INTERVAL));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
	FiniteGCLatency obj = new FiniteGCLatency();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
	try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
	    UnicastRemoteObject.exportObject(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
	    System.err.println("exported remote object");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
	    LocateRegistry.createRegistry(TestLibrary.REGISTRY_PORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
	    System.err.println("created registry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
	    Registry registry = LocateRegistry.getRegistry("", TestLibrary.REGISTRY_PORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
	    registry.bind(BINDING, obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
	    System.err.println("bound remote object in registry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
	    synchronized (obj.lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
		registry.unbind(BINDING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
		System.err.println("unbound remote object from registry; " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
		    "waiting for unreferenced() callback...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
		obj.lock.wait(TIMEOUT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
		if (obj.unreferencedInvoked) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
		    System.err.println("TEST PASSED: unreferenced() invoked");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
		} else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
		    throw new RuntimeException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
			"TEST FAILED: unrefereced() not invoked after " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
			((double) TIMEOUT / 1000.0) + " seconds");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
		}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
	    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
	} catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
	    if (e instanceof RuntimeException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
		throw (RuntimeException) e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
	    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
		throw new RuntimeException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
		    "TEST FAILED: unexpected exception: " + e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
	    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
	} finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
	    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
	     * When all is said and done, try to unexport the remote object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
	     * so that the VM has a chance to exit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
	     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
	    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
		UnicastRemoteObject.unexportObject(obj, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
	    } catch (RemoteException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
	    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
	}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
}