author | chegar |
Sat, 03 Mar 2018 09:54:31 +0000 | |
branch | http-client-branch |
changeset 56233 | 1753108d07b9 |
parent 56167 | 96fa4f49a9ff |
child 56265 | ec34ae013fbe |
permissions | -rw-r--r-- |
48083 | 1 |
/* |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56089
diff
changeset
|
2 |
* Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved. |
48083 | 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 |
||
56233
1753108d07b9
http-client-branch: setReuseAddress(false) for all test ServerSockets
chegar
parents:
56167
diff
changeset
|
24 |
import java.net.InetSocketAddress; |
48083 | 25 |
import java.net.ServerSocket; |
26 |
import java.net.URI; |
|
56089
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56082
diff
changeset
|
27 |
import java.net.http.HttpClient; |
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56082
diff
changeset
|
28 |
import java.net.http.HttpRequest; |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56089
diff
changeset
|
29 |
import java.net.http.HttpResponse.BodyHandlers; |
48083 | 30 |
import static java.lang.System.out; |
31 |
||
32 |
/** |
|
33 |
* @test |
|
34 |
* @summary Basic test for interrupted blocking send |
|
56089
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56082
diff
changeset
|
35 |
* @run main/othervm InterruptedBlockingSend |
48083 | 36 |
*/ |
37 |
||
38 |
public class InterruptedBlockingSend { |
|
39 |
||
40 |
static volatile Throwable throwable; |
|
41 |
||
42 |
public static void main(String[] args) throws Exception { |
|
43 |
HttpClient client = HttpClient.newHttpClient(); |
|
56233
1753108d07b9
http-client-branch: setReuseAddress(false) for all test ServerSockets
chegar
parents:
56167
diff
changeset
|
44 |
try (ServerSocket ss = new ServerSocket()) { |
1753108d07b9
http-client-branch: setReuseAddress(false) for all test ServerSockets
chegar
parents:
56167
diff
changeset
|
45 |
ss.setReuseAddress(false); |
1753108d07b9
http-client-branch: setReuseAddress(false) for all test ServerSockets
chegar
parents:
56167
diff
changeset
|
46 |
ss.bind(new InetSocketAddress(0)); |
48083 | 47 |
int port = ss.getLocalPort(); |
48 |
URI uri = new URI("http://127.0.0.1:" + port + "/"); |
|
49 |
||
50 |
HttpRequest request = HttpRequest.newBuilder(uri).build(); |
|
51 |
||
52 |
Thread t = new Thread(() -> { |
|
53 |
try { |
|
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56089
diff
changeset
|
54 |
client.send(request, BodyHandlers.discarding()); |
48083 | 55 |
} catch (InterruptedException e) { |
56 |
throwable = e; |
|
57 |
} catch (Throwable th) { |
|
58 |
throwable = th; |
|
59 |
} |
|
60 |
}); |
|
61 |
t.start(); |
|
62 |
Thread.sleep(5000); |
|
63 |
t.interrupt(); |
|
64 |
t.join(); |
|
65 |
||
66 |
if (!(throwable instanceof InterruptedException)) { |
|
67 |
throw new RuntimeException("Expected InterruptedException, got " + throwable); |
|
68 |
} else { |
|
69 |
out.println("Caught expected InterruptedException: " + throwable); |
|
70 |
} |
|
71 |
} |
|
72 |
} |
|
73 |
} |