J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 1999 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /* @test |
| 25 | * @bug 4268258 |
| 26 | * @summary When a DGC dirty call fails, RMI's client-side DGC implementation |
| 27 | * should attempt to retry the same dirty call a few times, at least until the |
| 28 | * known lease for that endpoint has expired, instead of just giving up |
| 29 | * renewing that lease at all after the first failure. |
| 30 | * @author Peter Jones (inspired by Adrian Colley's test case in 4268258) |
| 31 | * |
| 32 | * @build RetryDirtyCalls |
| 33 | * @build RetryDirtyCalls_Stub |
| 34 | * @run main/othervm RetryDirtyCalls |
| 35 | */ |
| 36 | |
| 37 | import java.io.*; |
| 38 | import java.net.*; |
| 39 | import java.rmi.*; |
| 40 | import java.rmi.server.*; |
| 41 | |
| 42 | interface Self extends Remote { |
| 43 | Self getSelf() throws RemoteException; |
| 44 | } |
| 45 | |
| 46 | public class RetryDirtyCalls implements Self, Unreferenced { |
| 47 | |
| 48 | /** how long we wait before declaring that this test has passed */ |
| 49 | private final static long TIMEOUT = 20000; |
| 50 | |
| 51 | /** true if this object's unreferenced method has been called */ |
| 52 | private boolean unreferenced = false; |
| 53 | |
| 54 | /** |
| 55 | * Return this object. The need for this method is explained below. |
| 56 | */ |
| 57 | public Self getSelf() { |
| 58 | return this; |
| 59 | } |
| 60 | |
| 61 | public void unreferenced() { |
| 62 | synchronized (this) { |
| 63 | unreferenced = true; |
| 64 | notifyAll(); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | public static void main(String[] args) { |
| 69 | |
| 70 | System.err.println("\nRegression test for bug 4268258\n"); |
| 71 | |
| 72 | /* |
| 73 | * Set properties to tweak DGC behavior so that this test will execute |
| 74 | * quickly: set the granted lease duration to 10 seconds, the interval |
| 75 | * that leases are checked to 3 seconds. |
| 76 | */ |
| 77 | System.setProperty("java.rmi.dgc.leaseValue", "10000"); |
| 78 | System.setProperty("sun.rmi.dgc.checkInterval", "3000"); |
| 79 | |
| 80 | /* |
| 81 | * Make idle connections time out almost instantly (0.1 seconds) so |
| 82 | * that the DGC implementation will have to make a new connection for |
| 83 | * each dirty call, thus going through the socket factory, where we |
| 84 | * can easily cause the operation to fail. |
| 85 | */ |
| 86 | System.setProperty("sun.rmi.transport.connectionTimeout", "100"); |
| 87 | |
| 88 | RetryDirtyCalls impl = new RetryDirtyCalls(); |
| 89 | |
| 90 | try { |
| 91 | TestSF sf = new TestSF(); |
| 92 | RMISocketFactory.setSocketFactory(sf); |
| 93 | |
| 94 | /* |
| 95 | * The stub returned by UnicastRemoteObject.exportObject() does |
| 96 | * not participate in DGC, but it does allow us to invoke a method |
| 97 | * on the remote object through RMI. Therefore, we invoke the |
| 98 | * getSelf() method through RMI, which returns an equivalent stub |
| 99 | * that does participate in DGC. |
| 100 | */ |
| 101 | Self stub = (Self) UnicastRemoteObject.exportObject(impl); |
| 102 | Self dgcStub = stub.getSelf(); |
| 103 | stub = null; // in case 4114579 has been fixed |
| 104 | |
| 105 | /* |
| 106 | * Set the socket factory to cause 3 connections attempts in a row |
| 107 | * to fail before allowing a connection to succeed, expecting the |
| 108 | * client-side DGC implementation to make at least four attempts. |
| 109 | */ |
| 110 | final int FLAKE_FACTOR = 3; |
| 111 | sf.setFlakeFactor(FLAKE_FACTOR); |
| 112 | |
| 113 | long deadline = System.currentTimeMillis() + TIMEOUT; |
| 114 | boolean unreferenced; |
| 115 | |
| 116 | synchronized (impl) { |
| 117 | while (!(unreferenced = impl.unreferenced)) { |
| 118 | long timeToWait = deadline - System.currentTimeMillis(); |
| 119 | if (timeToWait > 0) { |
| 120 | impl.wait(timeToWait); |
| 121 | } else { |
| 122 | break; |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | if (unreferenced) { |
| 128 | throw new RuntimeException("remote object unreferenced"); |
| 129 | } |
| 130 | |
| 131 | int createCount = sf.getCreateCount(); |
| 132 | if (createCount == 0) { |
| 133 | throw new RuntimeException("test socket factory never used"); |
| 134 | } else if (createCount < (FLAKE_FACTOR + 3)) { |
| 135 | /* |
| 136 | * The unreferenced method was not invoked for some reason, |
| 137 | * but the dirty calls were clearly not retried well enough. |
| 138 | */ |
| 139 | throw new RuntimeException( |
| 140 | "test failed because dirty calls not retried enough, " + |
| 141 | "but remote object not unreferenced"); |
| 142 | } |
| 143 | |
| 144 | System.err.println( |
| 145 | "TEST PASSED: remote object not unreferenced"); |
| 146 | |
| 147 | } catch (Exception e) { |
| 148 | e.printStackTrace(); |
| 149 | throw new RuntimeException("TEST FAILED: " + e.toString()); |
| 150 | } finally { |
| 151 | /* |
| 152 | * When all is said and done, try to unexport the remote object |
| 153 | * so that the VM has a chance to exit. |
| 154 | */ |
| 155 | try { |
| 156 | UnicastRemoteObject.unexportObject(impl, true); |
| 157 | } catch (Exception e) { |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | class TestSF extends RMISocketFactory { |
| 164 | |
| 165 | private int flakeFactor = 0; |
| 166 | |
| 167 | private int flakeState = 0; |
| 168 | |
| 169 | private int createCount = 0; |
| 170 | |
| 171 | public synchronized void setFlakeFactor(int newFlakeFactor) { |
| 172 | flakeFactor = newFlakeFactor; |
| 173 | } |
| 174 | |
| 175 | public synchronized int getCreateCount() { |
| 176 | return createCount; |
| 177 | } |
| 178 | |
| 179 | public synchronized Socket createSocket(String host, int port) |
| 180 | throws IOException |
| 181 | { |
| 182 | createCount++; |
| 183 | |
| 184 | if (++flakeState > flakeFactor) { |
| 185 | flakeState = 0; |
| 186 | } |
| 187 | |
| 188 | if (flakeState == 0) { |
| 189 | return new Socket(host, port); |
| 190 | } else { |
| 191 | throw new IOException("random network failure"); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | public ServerSocket createServerSocket(int port) throws IOException { |
| 196 | return new ServerSocket(port); |
| 197 | } |
| 198 | } |