J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 1999-2004 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 4120329 |
| 26 | * @summary RMI registry creation is impossible if first attempt fails. |
| 27 | * @library ../../testlibrary |
| 28 | * @build StreamPipe TestParams TestLibrary JavaVM |
| 29 | * @build RegistryRunner RegistryRunner_Stub |
| 30 | * @build Reexport |
| 31 | * @run main/othervm Reexport |
| 32 | */ |
| 33 | |
| 34 | /* |
| 35 | * If a VM could not create an RMI registry because another registry |
| 36 | * usually in another process, was using the registry port, the next |
| 37 | * time the VM tried to create a registry (after the other registry |
| 38 | * was brought down) the attempt would fail. The second try to create |
| 39 | * a registry would fail because the registry ObjID would still be in |
| 40 | * use when it should never have been allocated. |
| 41 | * |
| 42 | * The test creates this conflict using Runtime.exec and ensures that |
| 43 | * a registry can still be created after the conflict is resolved. |
| 44 | */ |
| 45 | |
| 46 | import java.io.*; |
| 47 | import java.rmi.*; |
| 48 | import java.rmi.registry.*; |
| 49 | import java.rmi.server.*; |
| 50 | |
| 51 | public class Reexport { |
| 52 | static public final int regport = TestLibrary.REGISTRY_PORT; |
| 53 | |
| 54 | static public void main(String[] argv) { |
| 55 | |
| 56 | Registry reg = null; |
| 57 | |
| 58 | try { |
| 59 | System.err.println("\nregression test for 4120329\n"); |
| 60 | |
| 61 | // establish the registry (we hope) |
| 62 | System.err.println("Starting registry on port " + regport); |
| 63 | Reexport.makeRegistry(regport); |
| 64 | |
| 65 | // Get a handle to the registry |
| 66 | System.err.println("Creating duplicate registry, this should fail..."); |
| 67 | reg = createReg(true); |
| 68 | |
| 69 | if (reg != null) { |
| 70 | TestLibrary.bomb("failed was able to duplicate the registry?!?"); |
| 71 | } |
| 72 | |
| 73 | // Kill the first registry. |
| 74 | System.err.println("Bringing down the first registry"); |
| 75 | try { |
| 76 | Reexport.killRegistry(); |
| 77 | } catch (Exception foo) { |
| 78 | } |
| 79 | |
| 80 | // start another registry now that the first is gone; this should work |
| 81 | System.err.println("Trying again to start our own " + |
| 82 | "registry... this should work"); |
| 83 | |
| 84 | reg = createReg(false); |
| 85 | |
| 86 | if (reg == null) { |
| 87 | TestLibrary.bomb("Could not create registry on second try"); |
| 88 | } |
| 89 | |
| 90 | System.err.println("Test passed"); |
| 91 | |
| 92 | } catch (Exception e) { |
| 93 | TestLibrary.bomb(e); |
| 94 | } finally { |
| 95 | // dont leave the registry around to affect other tests. |
| 96 | killRegistry(); |
| 97 | |
| 98 | reg = null; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | static Registry createReg(boolean remoteOk) { |
| 103 | Registry reg = null; |
| 104 | |
| 105 | try { |
| 106 | reg = LocateRegistry.createRegistry(regport); |
| 107 | } catch (Throwable e) { |
| 108 | if (remoteOk) { |
| 109 | System.err.println("EXPECTING PORT IN USE EXCEPTION:"); |
| 110 | System.err.println(e.getMessage()); |
| 111 | e.printStackTrace(); |
| 112 | } else { |
| 113 | TestLibrary.bomb((Exception) e); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | return reg; |
| 118 | } |
| 119 | |
| 120 | public static void makeRegistry(int p) { |
| 121 | // sadly, we can't kill a registry if we have too-close control |
| 122 | // over it. We must make it in a subprocess, and then kill the |
| 123 | // subprocess when it has served our needs. |
| 124 | |
| 125 | try { |
| 126 | JavaVM jvm = new JavaVM("RegistryRunner", "", Integer.toString(p)); |
| 127 | jvm.start(); |
| 128 | Reexport.subreg = jvm.getVM(); |
| 129 | |
| 130 | } catch (IOException e) { |
| 131 | // one of these is summarily dropped, can't remember which one |
| 132 | System.out.println ("Test setup failed - cannot run rmiregistry"); |
| 133 | TestLibrary.bomb("Test setup failed - cannot run test", e); |
| 134 | } |
| 135 | // Slop - wait for registry to come up. This is stupid. |
| 136 | try { |
| 137 | Thread.sleep (5000); |
| 138 | } catch (Exception whatever) { |
| 139 | } |
| 140 | } |
| 141 | private static Process subreg = null; |
| 142 | |
| 143 | public static void killRegistry() { |
| 144 | if (Reexport.subreg != null) { |
| 145 | |
| 146 | RegistryRunner.requestExit(); |
| 147 | |
| 148 | try { |
| 149 | Reexport.subreg.waitFor(); |
| 150 | } catch (InterruptedException ie) { |
| 151 | } |
| 152 | } |
| 153 | Reexport.subreg = null; |
| 154 | } |
| 155 | } |