Path: blob/master/test/jdk/java/rmi/transport/dgcDeadLock/DGCDeadLock.java
41153 views
/*1* Copyright (c) 1998, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 411805625* @key intermittent26*27* @summary synopsis: Distributed Garbage Collection Deadlock28* @author Laird Dornin29*30* @library ../../testlibrary31* @modules java.rmi/sun.rmi.registry32* java.rmi/sun.rmi.server33* java.rmi/sun.rmi.transport34* java.rmi/sun.rmi.transport.tcp35* @build TestLibrary Test TestImpl RegistryVM RegistryRunner36* @run main/othervm/policy=security.policy/timeout=360 DGCDeadLock37*/3839/* This test attempts to cause a deadlock between the rmi leaseChecker40* thread and a thread that is servicing a dgc clean call. Before the41* fix for this bug was implemented, deadlock could occur when the42* leaseChecker held the lock on the lease table and the clean thread43* held the lock on a target x. The clean thread would attempt to get44* the lock on the leaseTable to do DGCImpl.unregisterTarget and the45* leaseChecker would attempt to get the lock on x to do46* Target.vmidDead. Each thread held a resource that the other thread47* was attempting to lock.48*49* This test causes the above conditions to occur and waits to see if50* a given set of remote calls finishes "quickly enough."51*/5253import java.rmi.*;54import java.io.*;5556public class DGCDeadLock implements Runnable {57final static public int HOLD_TARGET_TIME = 25000;58public static final double TEST_FAIL_TIME =59(HOLD_TARGET_TIME + 30000) * TestLibrary.getTimeoutFactor();60public static volatile boolean finished = false;61static final DGCDeadLock test = new DGCDeadLock();62static volatile int registryPort = -1;6364static {65System.setProperty("sun.rmi.transport.cleanInterval", "50");66}6768static public void main(String[] args) {6970RegistryVM testImplVM = null;7172System.err.println("\nregression test for 4118056\n");73TestLibrary.suggestSecurityManager("java.rmi.RMISecurityManager");7475try {76String options = " --add-opens java.rmi/sun.rmi.transport=ALL-UNNAMED" +77" -Djava.rmi.dgc.leaseValue=500000" +78" -Dsun.rmi.dgc.checkInterval=" +79(HOLD_TARGET_TIME - 5000);8081testImplVM = RegistryVM.createRegistryVMWithRunner("TestImpl", options);82testImplVM.start();83registryPort = testImplVM.getPort();8485synchronized (test) {86Thread t = new Thread(test);87t.setDaemon(true);88t.start();8990// wait for the remote calls to take place91test.wait((long)TEST_FAIL_TIME);92}9394if (!finished) {95TestLibrary.bomb("Test failed, had exception or exercise" +96" routines took too long to " +97"execute");98}99System.err.println("Test passed, exercises " +100"finished in time.");101102} catch (Exception e) {103TestLibrary.bomb("test failed in main()", e);104} finally {105if (testImplVM != null) {106testImplVM.cleanup();107testImplVM = null;108}109}110}111112public void run() {113try {114String echo = null;115116// create a test client117Test foo = (Test) Naming.lookup("rmi://:" +118registryPort +119"/Foo");120echo = foo.echo("Hello world");121System.err.println("Test object created.");122123/* give TestImpl time to lock the target in the124* object table and any dirtys finish.125*/126Thread.currentThread().sleep(5000);127128//unreference "Foo"129foo = null;130131//garbage collect and finalize foo132Runtime.getRuntime().gc();133Runtime.getRuntime().runFinalization();134135//import "Bar"136Test bar = (Test) Naming.lookup("rmi://:" +137registryPort +138"/Bar");139140/* infinite loop to show the liveness of Client,141* if we have deadlock remote call will not return142*/143try {144for (int i = 0; i < 500; i++) {145echo = bar.echo("Remote call" + i);146Thread.sleep(10);147}148149// flag exercises finished150finished = true;151152} catch (RemoteException e) {153System.err.println("catch RemoteException");154e.printStackTrace();155}156157} catch (Exception e) {158TestLibrary.bomb("test failed in run()", e);159} finally {160synchronized(this) {161notify();162}163}164}165}166167168