Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/rmi/transport/readTimeout/ReadTimeoutTest.java
38828 views
/*1* Copyright (c) 1999, 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 420880425*26* @summary Incoming connections should be subject to timeout27* @author Adrian Colley28*29* @build TestIface TestImpl TestImpl_Stub30* @run main/othervm/policy=security.policy/timeout=6031* -Dsun.rmi.transport.tcp.readTimeout=5000 ReadTimeoutTest32*/3334/* This test sets a very short read timeout, exports an object, and then35* connects to the port and does nothing. The server should close the36* connection after the timeout. If that doesn't happen, the test fails.37*38* A background thread does the read. The foreground waits for DELAY*439* and then aborts. This should give sufficient margin for timing slop.40*/4142import java.rmi.*;43import java.rmi.server.RMISocketFactory;44import java.io.*;45import java.net.*;4647public class ReadTimeoutTest48{49private static final int DELAY = 5000; // milliseconds5051public static void main(String[] args)52throws Exception53{54// Make trouble for ourselves55if (System.getSecurityManager() == null)56System.setSecurityManager(new RMISecurityManager());5758// Flaky code alert - hope this is executed before TCPTransport.<clinit>59System.setProperty("sun.rmi.transport.tcp.readTimeout", Integer.toString(DELAY));6061// Set the socket factory.62System.err.println("(installing socket factory)");63SomeFactory fac = new SomeFactory();64RMISocketFactory.setSocketFactory(fac);6566// Create remote object67TestImpl impl = new TestImpl();6869// Export and get which port.70System.err.println("(exporting remote object)");71TestIface stub = impl.export();72Socket DoS = null;73try {74int port = fac.whichPort();7576// Sanity77if (port == 0)78throw new Error("TEST FAILED: export didn't reserve a port(?)");7980// Now, connect to that port81//Thread.sleep(2000);82System.err.println("(connecting to listening port on 127.0.0.1:" +83port + ")");84DoS = new Socket("127.0.0.1", port);85InputStream stream = DoS.getInputStream();8687// Read on the socket in the background88boolean[] successful = new boolean[] { false };89(new SomeReader(stream, successful)).start();9091// Wait for completion92int nretries = 4;93while (nretries-- > 0) {94if (successful[0])95break;96Thread.sleep(DELAY);97}9899if (successful[0]) {100System.err.println("TEST PASSED.");101} else {102throw new Error("TEST FAILED.");103}104105} finally {106try {107if (DoS != null)108DoS.close(); // aborts the reader if still blocked109impl.unexport();110} catch (Throwable unmatter) {111}112}113114// Should exit here115}116117private static class SomeFactory118extends RMISocketFactory119{120private int servport = 0;121122public Socket createSocket(String h, int p)123throws IOException124{125return (new Socket(h, p));126}127128/** Create a server socket and remember which port it's on.129* Aborts if createServerSocket(0) is called twice, because then130* it doesn't know whether to remember the first or second port.131*/132public ServerSocket createServerSocket(int p)133throws IOException134{135ServerSocket ss;136ss = new ServerSocket(p);137if (p == 0) {138if (servport != 0) {139System.err.println("TEST FAILED: " +140"Duplicate createServerSocket(0)");141throw new Error("Test aborted (createServerSocket)");142}143servport = ss.getLocalPort();144}145return (ss);146}147148/** Return which port was reserved by createServerSocket(0).149* If the return value was 0, createServerSocket(0) wasn't called.150*/151public int whichPort() {152return (servport);153}154} // end class SomeFactory155156protected static class SomeReader extends Thread {157private InputStream readon;158private boolean[] vec;159160public SomeReader(InputStream s, boolean[] successvec) {161super();162this.setDaemon(true);163this.readon = s;164this.vec = successvec;165}166167public void run() {168try {169int c = this.readon.read();170if (c != -1)171throw new Error ("Server returned " + c);172this.vec[0] = true;173174} catch (IOException e) {175e.printStackTrace();176}177}178} // end class SomeReader179}180181182