|
1 /* |
|
2 * Copyright (c) 2016, 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.io.File; |
|
25 import java.io.IOException; |
|
26 import java.net.URI; |
|
27 import java.net.http.HttpClient; |
|
28 import java.net.http.HttpRequest; |
|
29 import java.net.http.HttpResponse; |
|
30 import java.net.http.HttpTimeoutException; |
|
31 import java.util.concurrent.TimeUnit; |
|
32 import javax.net.ssl.SSLServerSocket; |
|
33 import javax.net.ssl.SSLServerSocketFactory; |
|
34 import javax.net.ssl.SSLSocket; |
|
35 |
|
36 /* |
|
37 * @test |
|
38 * @bug 8156710 |
|
39 * @summary Check if HttpTimeoutException is thrown if a server doesn't reply |
|
40 * @run main/othervm Timeout |
|
41 */ |
|
42 public class Timeout { |
|
43 |
|
44 private static final int RANDOM_PORT = 0; |
|
45 private static final int TIMEOUT = 3 * 1000; // in millis |
|
46 private static final String KEYSTORE = System.getProperty("test.src") |
|
47 + File.separator + "keystore.p12"; |
|
48 private static final String PASSWORD = "password"; |
|
49 |
|
50 // indicates if server is ready to accept connections |
|
51 private static volatile boolean ready = false; |
|
52 |
|
53 public static void main(String[] args) throws Exception { |
|
54 System.setProperty("javax.net.ssl.keyStore", KEYSTORE); |
|
55 System.setProperty("javax.net.ssl.keyStorePassword", PASSWORD); |
|
56 System.setProperty("javax.net.ssl.trustStore", KEYSTORE); |
|
57 System.setProperty("javax.net.ssl.trustStorePassword", PASSWORD); |
|
58 |
|
59 SSLServerSocketFactory factory = |
|
60 (SSLServerSocketFactory) SSLServerSocketFactory.getDefault(); |
|
61 |
|
62 try (SSLServerSocket ssocket = |
|
63 (SSLServerSocket) factory.createServerSocket(RANDOM_PORT)) { |
|
64 |
|
65 // start server |
|
66 Thread server = new Thread(() -> { |
|
67 while (true) { |
|
68 System.out.println("server: ready"); |
|
69 ready = true; |
|
70 try (SSLSocket socket = (SSLSocket) ssocket.accept()) { |
|
71 |
|
72 // just read forever |
|
73 System.out.println("server: accepted"); |
|
74 while (true) { |
|
75 socket.getInputStream().read(); |
|
76 } |
|
77 } catch (IOException e) { |
|
78 // ignore exceptions on server side |
|
79 System.out.println("server: exception: " + e); |
|
80 } |
|
81 } |
|
82 }); |
|
83 server.setDaemon(true); |
|
84 server.start(); |
|
85 |
|
86 // wait for server is ready |
|
87 do { |
|
88 Thread.sleep(1000); |
|
89 } while (!ready); |
|
90 |
|
91 String uri = "https://localhost:" + ssocket.getLocalPort(); |
|
92 connect(uri); |
|
93 } |
|
94 } |
|
95 |
|
96 private static void connect(String server) throws Exception { |
|
97 try { |
|
98 HttpClient.create() |
|
99 .version(HttpClient.Version.HTTP_2) |
|
100 .build() |
|
101 .request(new URI(server)) |
|
102 .timeout(TimeUnit.MILLISECONDS, TIMEOUT) |
|
103 .body(HttpRequest.fromString("body")) |
|
104 .GET() |
|
105 .response() |
|
106 .body(HttpResponse.asString()); |
|
107 |
|
108 throw new RuntimeException("unexpected successful connection"); |
|
109 } catch (HttpTimeoutException e) { |
|
110 System.out.println("expected exception: " + e); |
|
111 } |
|
112 } |
|
113 |
|
114 } |