1 /*
  2  * Copyright (c) 2020, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 /**
 25  * @test
 26  * @summary Test that HTTP connections can be interrupted
 27  * @compile --enable-preview -source ${jdk.version} InterruptHttp.java
 28  * @run testng/othervm --enable-preview InterruptHttp
 29  */
 30 
 31 import java.io.Closeable;
 32 import java.io.IOException;
 33 import java.io.InputStream;
 34 import java.net.InetAddress;
 35 import java.net.ServerSocket;
 36 import java.net.Socket;
 37 import java.net.SocketException;
 38 import java.net.URL;
 39 import java.util.List;
 40 import java.util.concurrent.CopyOnWriteArrayList;
 41 import java.util.concurrent.atomic.AtomicReference;
 42 
 43 public class InterruptHttp {
 44 
 45     public static void main(String[] args) throws Exception {
 46         try (Server server = new Server()) {
 47             AtomicReference<Exception> exception = new AtomicReference<>();
 48 
 49             // start thread to connect to server
 50             Thread thread = Thread.ofVirtual().start(() -> {
 51                 try {
 52                     URL url = new URL(server.address() + "/hello");
 53                     try (InputStream in = url.openStream()) { }
 54                 } catch (Exception e) {
 55                     exception.set(e);
 56                 }
 57             });
 58 
 59             // give time for thread to block waiting for HTTP server
 60             while (server.connectionCount() == 0) {
 61                 Thread.sleep(100);
 62             }
 63             thread.interrupt();
 64             thread.join();
 65 
 66             // HTTP request should fail with a SocketException
 67             Exception e = exception.get();
 68             if (!(e instanceof SocketException))
 69                 throw new RuntimeException("Expected SocketException, got: " + e);
 70 
 71             // server should have accepted one connection
 72             if (server.connectionCount() != 1)
 73                 throw new RuntimeException("Expected 1 HTTP connection");
 74         }
 75     }
 76 
 77     /**
 78      * Simple server that accepts connections but does not reply.
 79      */
 80     static class Server implements Closeable {
 81         private final ServerSocket listener;
 82         private final List<Socket> connections = new CopyOnWriteArrayList<>();
 83         private final String address;
 84 
 85         Server() throws IOException {
 86             InetAddress lb = InetAddress.getLoopbackAddress();
 87             listener = new ServerSocket(0, -1, lb);
 88             address = "http://" + lb.getHostAddress() + ":" + listener.getLocalPort();
 89             Thread.ofVirtual().start(this::run);
 90         }
 91 
 92         private void run() {
 93             try {
 94                 while (true) {
 95                     Socket s = listener.accept();
 96                     connections.add(s);
 97                 }
 98             } catch (Exception e) {
 99                 if (!listener.isClosed()) {
100                     e.printStackTrace();
101                 }
102             }
103         }
104 
105         int connectionCount() {
106             return connections.size();
107         }
108 
109         String address() {
110             return address;
111         }
112 
113         @Override
114         public void close() throws IOException {
115             connections.forEach(s -> {
116                 try { s.close(); } catch (Exception ignore) { }
117             });
118             listener.close();
119         }
120     }
121 }