Path: blob/master/test/jdk/java/rmi/transport/closeServerSocket/CloseServerSocket.java
41155 views
/*1* Copyright (c) 2005, 2019, 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 445768325* @summary After all of the remote objects (including a registry, if26* applicable) that had been exported with a given27* RMIServerSocketFactory value (including null) have been unexported,28* the server socket created for the exports should be closed (so that29* the local port is released).30* @author Peter Jones31*32* @library ../../testlibrary33* @modules java.rmi/sun.rmi.registry34* java.rmi/sun.rmi.server35* java.rmi/sun.rmi.transport36* java.rmi/sun.rmi.transport.tcp37* @build TestLibrary38* @run main/othervm CloseServerSocket39* @key intermittent40*/4142import java.io.IOException;43import java.net.BindException;44import java.net.ServerSocket;45import java.rmi.Remote;46import java.rmi.registry.LocateRegistry;47import java.rmi.registry.Registry;48import java.rmi.server.RMIServerSocketFactory;49import java.rmi.server.UnicastRemoteObject;5051public class CloseServerSocket implements Remote {52private static final int PORT = TestLibrary.getUnusedRandomPort();5354private CloseServerSocket() { }5556public static void main(String[] args) throws Exception {57System.err.println("\nRegression test for bug 4457683\n");5859verifyPortFree(PORT);60Registry registry = LocateRegistry.createRegistry(PORT);61System.err.println("- exported registry: " + registry);62verifyPortInUse(PORT);63UnicastRemoteObject.unexportObject(registry, true);64System.err.println("- unexported registry");65int tries = (int)TestLibrary.getTimeoutFactor();66tries = Math.max(tries, 1);67while (tries-- > 0) {68Thread.sleep(1000);69try {70verifyPortFree(PORT);71break;72} catch (IOException ignore) { }73}74if (tries < 0) {75throw new RuntimeException("time out after tries: " + tries);76}777879/*80* The follow portion of this test is disabled temporarily81* because 4457683 was partially backed out because of82* 6269166; for now, only server sockets originally opened for83* exports on non-anonymous ports will be closed when all of84* the corresponding remote objects have been exported. A85* separate bug will be filed to represent the remainder of86* 4457683 for anonymous-port exports.87*/8889// SSF ssf = new SSF();90// Remote impl = new CloseServerSocket();91// Remote stub = UnicastRemoteObject.exportObject(impl, 0, null, ssf);92// System.err.println("- exported object: " + stub);93// UnicastRemoteObject.unexportObject(impl, true);94// System.err.println("- unexported object");95// synchronized (ssf) {96// if (!ssf.serverSocketClosed) {97// throw new RuntimeException("TEST FAILED: " +98// "server socket not closed");99// }100// }101102System.err.println("TEST PASSED");103}104105private static void verifyPortFree(int port) throws IOException {106ServerSocket ss = new ServerSocket(port);107ss.close();108System.err.println("- port " + port + " is free");109}110111private static void verifyPortInUse(int port) throws IOException {112try {113verifyPortFree(port);114throw new RuntimeException("port is not in use: " + port);115} catch (BindException e) {116System.err.println("- port " + port + " is in use");117return;118}119}120121private static class SSF implements RMIServerSocketFactory {122boolean serverSocketClosed = false;123SSF() { };124125public ServerSocket createServerSocket(int port) throws IOException {126return new SS(port);127}128129private class SS extends ServerSocket {130SS(int port) throws IOException {131super(port);132System.err.println("- created server socket: " + this);133};134135public void close() throws IOException {136synchronized (SSF.this) {137serverSocketClosed = true;138SSF.this.notifyAll();139}140System.err.println("- closing server socket: " + this);141super.close();142}143}144}145}146147148