Path: blob/master/test/jdk/java/rmi/transport/acceptLoop/CloseServerSocketOnTermination.java
41153 views
/*1* Copyright (c) 2005, 2012, 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 492457725* @summary When the current RMIFailureHandler (if any) is invoked26* because of a server socket accept failure, if it returns false,27* then (in addition to the accept loop terminating) the associated28* server socket should be closed. The server socket should also be29* closed if the accept loop terminates because of an unexpected30* exception for which it doesn't even consult the RMIFailureHandler.31* @author Peter Jones32*33* @run main/othervm CloseServerSocketOnTermination34*/3536import java.io.IOException;37import java.net.ServerSocket;38import java.net.Socket;39import java.rmi.Remote;40import java.rmi.server.RMIFailureHandler;41import java.rmi.server.RMIServerSocketFactory;42import java.rmi.server.RMISocketFactory;43import java.rmi.server.UnicastRemoteObject;44import java.util.concurrent.CountDownLatch;45import java.util.concurrent.TimeUnit;4647public class CloseServerSocketOnTermination {4849private static long TIMEOUT = 5000;5051public static void main(String[] args) throws Exception {52System.err.println("\nRegression test for bug 4924577\n");5354RMISocketFactory.setFailureHandler(new RMIFailureHandler() {55public boolean failure(Exception e) { return false; }56});5758tryWith(new IOException());59tryWith(new NullPointerException());60tryWith(new OutOfMemoryError());61tryWith(new NoClassDefFoundError());62tryWith(new InternalError());63tryWith(new Throwable());6465System.err.println("TEST PASSED");66}6768private static void tryWith(Throwable t) throws Exception {69Remote impl = new Remote() { };70try {71CountDownLatch latch = new CountDownLatch(1);72UnicastRemoteObject.exportObject(impl, 0, null, new SSF(t, latch));73if (!latch.await(TIMEOUT, TimeUnit.MILLISECONDS)) {74throw new Error("server socket not closed");75}76} finally {77UnicastRemoteObject.unexportObject(impl, true);78}79}8081private static class SSF implements RMIServerSocketFactory {82private final Throwable acceptFailure;83private final CountDownLatch closedLatch;84SSF(Throwable acceptFailure, CountDownLatch closedLatch) {85this.acceptFailure = acceptFailure;86this.closedLatch = closedLatch;87}88public ServerSocket createServerSocket(int port) throws IOException {89return new ServerSocket(port) {90private int acceptInvocations = 0;91public synchronized Socket accept() throws IOException {92if (acceptInvocations++ == 0) {93throwException(acceptFailure);94}95return super.accept();96}97public void close() throws IOException {98closedLatch.countDown();99super.close();100}101};102}103104// hack to throw an arbitrary (possibly checked) Throwable105private static void throwException(Throwable t) {106try {107toThrow.set(t);108Thrower.class.newInstance();109} catch (IllegalAccessException e) {110throw new AssertionError();111} catch (InstantiationException e) {112throw new AssertionError();113} finally {114toThrow.remove();115}116}117private static ThreadLocal<Throwable> toThrow =118new ThreadLocal<Throwable>();119private static class Thrower {120Thrower() throws Throwable { throw toThrow.get(); }121}122}123}124125126