Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/rmi/transport/closeServerSocket/CloseServerSocket.java
38828 views
/*1* Copyright (c) 2005, 2013, 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* @build TestLibrary34* @run main/othervm CloseServerSocket35*/3637import java.io.IOException;38import java.net.BindException;39import java.net.ServerSocket;40import java.rmi.Remote;41import java.rmi.registry.LocateRegistry;42import java.rmi.registry.Registry;43import java.rmi.server.RMIServerSocketFactory;44import java.rmi.server.UnicastRemoteObject;4546public class CloseServerSocket implements Remote {47private static final int PORT = TestLibrary.getUnusedRandomPort();4849private CloseServerSocket() { }5051public static void main(String[] args) throws Exception {52System.err.println("\nRegression test for bug 4457683\n");5354verifyPortFree(PORT);55Registry registry = LocateRegistry.createRegistry(PORT);56System.err.println("- exported registry: " + registry);57verifyPortInUse(PORT);58UnicastRemoteObject.unexportObject(registry, true);59System.err.println("- unexported registry");60Thread.sleep(1000); // work around BindException (bug?)61verifyPortFree(PORT);6263/*64* The follow portion of this test is disabled temporarily65* because 4457683 was partially backed out because of66* 6269166; for now, only server sockets originally opened for67* exports on non-anonymous ports will be closed when all of68* the corresponding remote objects have been exported. A69* separate bug will be filed to represent the remainder of70* 4457683 for anonymous-port exports.71*/7273// SSF ssf = new SSF();74// Remote impl = new CloseServerSocket();75// Remote stub = UnicastRemoteObject.exportObject(impl, 0, null, ssf);76// System.err.println("- exported object: " + stub);77// UnicastRemoteObject.unexportObject(impl, true);78// System.err.println("- unexported object");79// synchronized (ssf) {80// if (!ssf.serverSocketClosed) {81// throw new RuntimeException("TEST FAILED: " +82// "server socket not closed");83// }84// }8586System.err.println("TEST PASSED");87}8889private static void verifyPortFree(int port) throws IOException {90ServerSocket ss = new ServerSocket(port);91ss.close();92System.err.println("- port " + port + " is free");93}9495private static void verifyPortInUse(int port) throws IOException {96try {97verifyPortFree(port);98} catch (BindException e) {99System.err.println("- port " + port + " is in use");100return;101}102}103104private static class SSF implements RMIServerSocketFactory {105boolean serverSocketClosed = false;106SSF() { };107108public ServerSocket createServerSocket(int port) throws IOException {109return new SS(port);110}111112private class SS extends ServerSocket {113SS(int port) throws IOException {114super(port);115System.err.println("- created server socket: " + this);116};117118public void close() throws IOException {119synchronized (SSF.this) {120serverSocketClosed = true;121SSF.this.notifyAll();122}123System.err.println("- closing server socket: " + this);124super.close();125}126}127}128}129130131