169N/A/*
2362N/A * Copyright (c) 1999, 2008, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/* @test
0N/A * @bug 4268258
0N/A * @summary When a DGC dirty call fails, RMI's client-side DGC implementation
0N/A * should attempt to retry the same dirty call a few times, at least until the
0N/A * known lease for that endpoint has expired, instead of just giving up
0N/A * renewing that lease at all after the first failure.
0N/A * @author Peter Jones (inspired by Adrian Colley's test case in 4268258)
0N/A *
5551N/A * @build RetryDirtyCalls RetryDirtyCalls_Stub
0N/A * @run main/othervm RetryDirtyCalls
0N/A */
0N/A
0N/Aimport java.io.*;
0N/Aimport java.net.*;
0N/Aimport java.rmi.*;
0N/Aimport java.rmi.server.*;
0N/A
0N/Ainterface Self extends Remote {
0N/A Self getSelf() throws RemoteException;
0N/A}
0N/A
0N/Apublic class RetryDirtyCalls implements Self, Unreferenced {
0N/A
0N/A /** how long we wait before declaring that this test has passed */
0N/A private final static long TIMEOUT = 20000;
0N/A
0N/A /** true if this object's unreferenced method has been called */
0N/A private boolean unreferenced = false;
0N/A
0N/A /**
0N/A * Return this object. The need for this method is explained below.
0N/A */
0N/A public Self getSelf() {
169N/A return this;
0N/A }
0N/A
0N/A public void unreferenced() {
169N/A synchronized (this) {
169N/A unreferenced = true;
169N/A notifyAll();
169N/A }
0N/A }
0N/A
0N/A public static void main(String[] args) {
0N/A
169N/A System.err.println("\nRegression test for bug 4268258\n");
0N/A
169N/A /*
169N/A * Set properties to tweak DGC behavior so that this test will execute
169N/A * quickly: set the granted lease duration to 10 seconds, the interval
169N/A * that leases are checked to 3 seconds.
169N/A */
169N/A System.setProperty("java.rmi.dgc.leaseValue", "10000");
169N/A System.setProperty("sun.rmi.dgc.checkInterval", "3000");
0N/A
169N/A /*
169N/A * Make idle connections time out almost instantly (0.1 seconds) so
169N/A * that the DGC implementation will have to make a new connection for
169N/A * each dirty call, thus going through the socket factory, where we
169N/A * can easily cause the operation to fail.
169N/A */
169N/A System.setProperty("sun.rmi.transport.connectionTimeout", "100");
0N/A
169N/A RetryDirtyCalls impl = new RetryDirtyCalls();
0N/A
169N/A try {
169N/A TestSF sf = new TestSF();
169N/A RMISocketFactory.setSocketFactory(sf);
0N/A
169N/A /*
169N/A * The stub returned by UnicastRemoteObject.exportObject() does
169N/A * not participate in DGC, but it does allow us to invoke a method
169N/A * on the remote object through RMI. Therefore, we invoke the
169N/A * getSelf() method through RMI, which returns an equivalent stub
169N/A * that does participate in DGC.
169N/A */
169N/A Self stub = (Self) UnicastRemoteObject.exportObject(impl);
169N/A Self dgcStub = stub.getSelf();
169N/A stub = null; // in case 4114579 has been fixed
0N/A
169N/A /*
169N/A * Set the socket factory to cause 3 connections attempts in a row
169N/A * to fail before allowing a connection to succeed, expecting the
169N/A * client-side DGC implementation to make at least four attempts.
169N/A */
169N/A final int FLAKE_FACTOR = 3;
169N/A sf.setFlakeFactor(FLAKE_FACTOR);
0N/A
169N/A long deadline = System.currentTimeMillis() + TIMEOUT;
169N/A boolean unreferenced;
0N/A
169N/A synchronized (impl) {
169N/A while (!(unreferenced = impl.unreferenced)) {
169N/A long timeToWait = deadline - System.currentTimeMillis();
169N/A if (timeToWait > 0) {
169N/A impl.wait(timeToWait);
169N/A } else {
169N/A break;
169N/A }
169N/A }
169N/A }
0N/A
169N/A if (unreferenced) {
169N/A throw new RuntimeException("remote object unreferenced");
169N/A }
0N/A
169N/A int createCount = sf.getCreateCount();
169N/A if (createCount == 0) {
169N/A throw new RuntimeException("test socket factory never used");
169N/A } else if (createCount < (FLAKE_FACTOR + 3)) {
169N/A /*
169N/A * The unreferenced method was not invoked for some reason,
169N/A * but the dirty calls were clearly not retried well enough.
169N/A */
169N/A throw new RuntimeException(
169N/A "test failed because dirty calls not retried enough, " +
169N/A "but remote object not unreferenced");
169N/A }
0N/A
169N/A System.err.println(
169N/A "TEST PASSED: remote object not unreferenced");
0N/A
169N/A } catch (Exception e) {
169N/A e.printStackTrace();
169N/A throw new RuntimeException("TEST FAILED: " + e.toString());
169N/A } finally {
169N/A /*
169N/A * When all is said and done, try to unexport the remote object
169N/A * so that the VM has a chance to exit.
169N/A */
169N/A try {
169N/A UnicastRemoteObject.unexportObject(impl, true);
169N/A } catch (Exception e) {
169N/A }
169N/A }
0N/A }
0N/A}
0N/A
0N/Aclass TestSF extends RMISocketFactory {
0N/A
0N/A private int flakeFactor = 0;
0N/A
0N/A private int flakeState = 0;
0N/A
0N/A private int createCount = 0;
0N/A
0N/A public synchronized void setFlakeFactor(int newFlakeFactor) {
169N/A flakeFactor = newFlakeFactor;
0N/A }
0N/A
0N/A public synchronized int getCreateCount() {
169N/A return createCount;
0N/A }
0N/A
0N/A public synchronized Socket createSocket(String host, int port)
169N/A throws IOException
0N/A {
169N/A createCount++;
0N/A
169N/A if (++flakeState > flakeFactor) {
169N/A flakeState = 0;
169N/A }
0N/A
169N/A if (flakeState == 0) {
169N/A return new Socket(host, port);
169N/A } else {
169N/A throw new IOException("random network failure");
169N/A }
0N/A }
0N/A
0N/A public ServerSocket createServerSocket(int port) throws IOException {
169N/A return new ServerSocket(port);
0N/A }
0N/A}