1 /* 2 * Copyright (c) 2017, 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 import java.net.ServerSocket; 25 import java.net.URI; 26 import jdk.incubator.http.HttpClient; 27 import jdk.incubator.http.HttpRequest; 28 import static java.lang.System.out; 29 import static jdk.incubator.http.HttpResponse.BodyHandler.discard; 30 31 /** 32 * @test 33 * @summary Basic test for interrupted blocking send 34 */ 35 36 public class InterruptedBlockingSend { 37 38 static volatile Throwable throwable; 39 40 public static void main(String[] args) throws Exception { 41 HttpClient client = HttpClient.newHttpClient(); 42 try (ServerSocket ss = new ServerSocket(0, 20)) { 43 int port = ss.getLocalPort(); 44 URI uri = new URI("http://127.0.0.1:" + port + "/"); 45 46 HttpRequest request = HttpRequest.newBuilder(uri).build(); 47 48 Thread t = new Thread(() -> { 49 try { 50 client.send(request, discard(null)); 51 } catch (InterruptedException e) { 52 throwable = e; 53 } catch (Throwable th) { 54 throwable = th; 55 } 56 }); 57 t.start(); 58 Thread.sleep(5000); 59 t.interrupt(); 60 t.join(); 61 62 if (!(throwable instanceof InterruptedException)) { 63 throw new RuntimeException("Expected InterruptedException, got " + throwable); 64 } else { 65 out.println("Caught expected InterruptedException: " + throwable); 66 } 67 } 68 } 69 }