jdk/test/java/rmi/transport/dgcDeadLock/DGCDeadLock.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-1999 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 4118056
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary synopsis: Distributed Garbage Collection Deadlock
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author Laird Dornin
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @library ../../testlibrary
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @build DGCDeadLock
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * @build Test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * @build TestImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * @build TestImpl_Stub
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * @run main/othervm/policy=security.policy/timeout=360 DGCDeadLock
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/* This test attempts to cause a deadlock between the rmi leaseChecker
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * thread and a thread that is servicing a dgc clean call. Before the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * fix for this bug was implemented, deadlock could occur when the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * leaseChecker held the lock on the lease table and the clean thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * held the lock on a target x. The clean thread would attempt to get
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * the lock on the leaseTable to do DGCImpl.unregisterTarget and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * leaseChecker would attempt to get the lock on x to do
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * Target.vmidDead.  Each thread held a resource that the other thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * was attempting to lock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * This test causes the above conditions to occur and waits to see if
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * a given set of remote calls finishes "quickly enough."  
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import java.rmi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
public class DGCDeadLock implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    final static public int HOLD_TARGET_TIME = 25000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    public static int TEST_FAIL_TIME = HOLD_TARGET_TIME + 30000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    public static boolean finished = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    static DGCDeadLock test = new DGCDeadLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
	System.setProperty("sun.rmi.transport.cleanInterval", "50");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    static public void main(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
	JavaVM testImplVM = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
	System.err.println("\nregression test for 4118056\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
	TestLibrary.suggestSecurityManager("java.rmi.RMISecurityManager");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
	    String options = " -Djava.security.policy=" + 
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
		TestParams.defaultPolicy +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
		" -Djava.rmi.dgc.leaseValue=500000" + 
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
		"  -Dsun.rmi.dgc.checkInterval=" + 
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
		(HOLD_TARGET_TIME - 5000) + "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
	    testImplVM = new JavaVM("TestImpl", options, "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
	    testImplVM.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
	    synchronized (test) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
		Thread t = new Thread(test);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
		t.setDaemon(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
		t.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
		// wait for the remote calls to take place
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
		test.wait(TEST_FAIL_TIME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
	    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
	    if (!finished) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
		TestLibrary.bomb("Test failed, had exception or exercise" + 
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
					   " routines took too long to " + 
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
					   "execute");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
	    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
	    System.err.println("Test passed, exercises " + 
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
			       "finished in time.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
	    testImplVM = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
	    TestLibrary.bomb("test failed", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
	try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
	    String echo = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
	    // give the test remote object time to initialize.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
	    Thread.currentThread().sleep(8000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
	    // create a test client
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
	    Test foo = (Test) Naming.lookup("rmi://:" + 
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
					    TestLibrary.REGISTRY_PORT + 
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
					    "/Foo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
	    echo = foo.echo("Hello world");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
	    System.err.println("Test object created.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
	    /* give TestImpl time to lock the target in the 
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
	     * object table and any dirtys finish.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
	     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
	    Thread.currentThread().sleep(5000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
	    //unreference "Foo"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
	    foo = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
	    //garbage collect and finalize foo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
	    Runtime.getRuntime().gc();  
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
	    Runtime.getRuntime().runFinalization();  
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
	    
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
	    //import "Bar"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
	    Test bar = (Test) Naming.lookup("rmi://:" + 
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
					    TestLibrary.REGISTRY_PORT + 
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
					    "/Bar");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
	    
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
	    /* infinite loop to show the liveness of Client,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
	     * if we have deadlock remote call will not return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
	     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
	    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
		for (int i = 0; i < 500; i++) {  
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
		    echo = bar.echo("Remote call" + i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
		    Thread.sleep(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
		}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
		// flag exercises finished
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
		finished = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
	    } catch (RemoteException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
	    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
	    
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
	} catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
	    TestLibrary.bomb("test failed", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
	} finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
	}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
}