author | dfuchs |
Tue, 13 Mar 2018 14:24:15 +0000 | |
branch | http-client-branch |
changeset 56288 | 2de1aa88cf06 |
parent 56286 | 3d8333fa243b |
child 56399 | a0929d5dd63f |
permissions | -rw-r--r-- |
56133
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
1 |
/* |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
2 |
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
4 |
* |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
8 |
* |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
13 |
* accompanied this code). |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
14 |
* |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
18 |
* |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
21 |
* questions. |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
22 |
*/ |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
23 |
|
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
24 |
/* |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
25 |
* @test |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
26 |
* @summary Tests what happens when response body handlers and subscribers |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
27 |
* throw unexpected exceptions. |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
28 |
* @library /lib/testlibrary http2/server |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
29 |
* @build jdk.testlibrary.SimpleSSLContext HttpServerAdapters ThrowingSubscribers |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
30 |
* @modules java.base/sun.net.www.http |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
31 |
* java.net.http/jdk.internal.net.http.common |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
32 |
* java.net.http/jdk.internal.net.http.frame |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
33 |
* java.net.http/jdk.internal.net.http.hpack |
56165
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56134
diff
changeset
|
34 |
* @run testng/othervm -Djdk.internal.httpclient.debug=true ThrowingSubscribers |
56133
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
35 |
*/ |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
36 |
|
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
37 |
import com.sun.net.httpserver.HttpServer; |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
38 |
import com.sun.net.httpserver.HttpsConfigurator; |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
39 |
import com.sun.net.httpserver.HttpsServer; |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
40 |
import jdk.testlibrary.SimpleSSLContext; |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
41 |
import org.testng.annotations.AfterTest; |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
42 |
import org.testng.annotations.AfterClass; |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
43 |
import org.testng.annotations.BeforeTest; |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
44 |
import org.testng.annotations.DataProvider; |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
45 |
import org.testng.annotations.Test; |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
46 |
|
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
47 |
import javax.net.ssl.SSLContext; |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
48 |
import java.io.BufferedReader; |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
49 |
import java.io.IOException; |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
50 |
import java.io.InputStream; |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
51 |
import java.io.InputStreamReader; |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
52 |
import java.io.OutputStream; |
56134
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
53 |
import java.io.UncheckedIOException; |
56265
ec34ae013fbe
http-client-branch: tests should bind to the loopback only
chegar
parents:
56209
diff
changeset
|
54 |
import java.net.InetAddress; |
56133
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
55 |
import java.net.InetSocketAddress; |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
56 |
import java.net.URI; |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
57 |
import java.net.http.HttpClient; |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
58 |
import java.net.http.HttpHeaders; |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
59 |
import java.net.http.HttpRequest; |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
60 |
import java.net.http.HttpResponse; |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
61 |
import java.net.http.HttpResponse.BodyHandler; |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56165
diff
changeset
|
62 |
import java.net.http.HttpResponse.BodyHandlers; |
56133
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
63 |
import java.net.http.HttpResponse.BodySubscriber; |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
64 |
import java.nio.ByteBuffer; |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
65 |
import java.nio.charset.StandardCharsets; |
56288
2de1aa88cf06
http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents:
56286
diff
changeset
|
66 |
import java.util.EnumSet; |
56133
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
67 |
import java.util.List; |
56165
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56134
diff
changeset
|
68 |
import java.util.concurrent.CompletableFuture; |
56133
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
69 |
import java.util.concurrent.CompletionStage; |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
70 |
import java.util.concurrent.ConcurrentHashMap; |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
71 |
import java.util.concurrent.ConcurrentMap; |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
72 |
import java.util.concurrent.Executor; |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
73 |
import java.util.concurrent.Executors; |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
74 |
import java.util.concurrent.Flow; |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
75 |
import java.util.concurrent.atomic.AtomicLong; |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
76 |
import java.util.function.Consumer; |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
77 |
import java.util.function.Predicate; |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
78 |
import java.util.function.Supplier; |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
79 |
import java.util.stream.Collectors; |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
80 |
import java.util.stream.Stream; |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
81 |
|
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
82 |
import static java.lang.System.out; |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
83 |
import static java.lang.String.format; |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
84 |
import static java.nio.charset.StandardCharsets.UTF_8; |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
85 |
import static org.testng.Assert.assertEquals; |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
86 |
import static org.testng.Assert.assertTrue; |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
87 |
|
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
88 |
public class ThrowingSubscribers implements HttpServerAdapters { |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
89 |
|
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
90 |
SSLContext sslContext; |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
91 |
HttpTestServer httpTestServer; // HTTP/1.1 [ 4 servers ] |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
92 |
HttpTestServer httpsTestServer; // HTTPS/1.1 |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
93 |
HttpTestServer http2TestServer; // HTTP/2 ( h2c ) |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
94 |
HttpTestServer https2TestServer; // HTTP/2 ( h2 ) |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
95 |
String httpURI_fixed; |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
96 |
String httpURI_chunk; |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
97 |
String httpsURI_fixed; |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
98 |
String httpsURI_chunk; |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
99 |
String http2URI_fixed; |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
100 |
String http2URI_chunk; |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
101 |
String https2URI_fixed; |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
102 |
String https2URI_chunk; |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
103 |
|
56165
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56134
diff
changeset
|
104 |
static final int ITERATION_COUNT = 1; |
56133
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
105 |
// a shared executor helps reduce the amount of threads created by the test |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
106 |
static final Executor executor = new TestExecutor(Executors.newCachedThreadPool()); |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
107 |
static final ConcurrentMap<String, Throwable> FAILURES = new ConcurrentHashMap<>(); |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
108 |
static volatile boolean tasksFailed; |
56165
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56134
diff
changeset
|
109 |
static final AtomicLong serverCount = new AtomicLong(); |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56134
diff
changeset
|
110 |
static final AtomicLong clientCount = new AtomicLong(); |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56134
diff
changeset
|
111 |
static final long start = System.nanoTime(); |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56134
diff
changeset
|
112 |
public static String now() { |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56134
diff
changeset
|
113 |
long now = System.nanoTime() - start; |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56134
diff
changeset
|
114 |
long secs = now / 1000_000_000; |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56134
diff
changeset
|
115 |
long mill = (now % 1000_000_000) / 1000_000; |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56134
diff
changeset
|
116 |
long nan = now % 1000_000; |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56134
diff
changeset
|
117 |
return String.format("[%d s, %d ms, %d ns] ", secs, mill, nan); |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56134
diff
changeset
|
118 |
} |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56134
diff
changeset
|
119 |
|
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56134
diff
changeset
|
120 |
private volatile HttpClient sharedClient; |
56133
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
121 |
|
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
122 |
static class TestExecutor implements Executor { |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
123 |
final AtomicLong tasks = new AtomicLong(); |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
124 |
Executor executor; |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
125 |
TestExecutor(Executor executor) { |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
126 |
this.executor = executor; |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
127 |
} |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
128 |
|
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
129 |
@Override |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
130 |
public void execute(Runnable command) { |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
131 |
long id = tasks.incrementAndGet(); |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
132 |
executor.execute(() -> { |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
133 |
try { |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
134 |
command.run(); |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
135 |
} catch (Throwable t) { |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
136 |
tasksFailed = true; |
56165
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56134
diff
changeset
|
137 |
System.out.printf(now() + "Task %s failed: %s%n", id, t); |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56134
diff
changeset
|
138 |
System.err.printf(now() + "Task %s failed: %s%n", id, t); |
56133
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
139 |
FAILURES.putIfAbsent("Task " + id, t); |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
140 |
throw t; |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
141 |
} |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
142 |
}); |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
143 |
} |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
144 |
} |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
145 |
|
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
146 |
@AfterClass |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
147 |
static final void printFailedTests() { |
56165
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56134
diff
changeset
|
148 |
out.println("\n========================="); |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56134
diff
changeset
|
149 |
try { |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56134
diff
changeset
|
150 |
out.printf("%n%sCreated %d servers and %d clients%n", |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56134
diff
changeset
|
151 |
now(), serverCount.get(), clientCount.get()); |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56134
diff
changeset
|
152 |
if (FAILURES.isEmpty()) return; |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56134
diff
changeset
|
153 |
out.println("Failed tests: "); |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56134
diff
changeset
|
154 |
FAILURES.entrySet().forEach((e) -> { |
56133
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
155 |
out.printf("\t%s: %s%n", e.getKey(), e.getValue()); |
56209
43d5ad612710
http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents:
56167
diff
changeset
|
156 |
e.getValue().printStackTrace(out); |
56133
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
157 |
e.getValue().printStackTrace(); |
56165
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56134
diff
changeset
|
158 |
}); |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56134
diff
changeset
|
159 |
if (tasksFailed) { |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56134
diff
changeset
|
160 |
System.out.println("WARNING: Some tasks failed"); |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56134
diff
changeset
|
161 |
} |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56134
diff
changeset
|
162 |
} finally { |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56134
diff
changeset
|
163 |
out.println("\n=========================\n"); |
56133
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
164 |
} |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
165 |
} |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
166 |
|
56134
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
167 |
private String[] uris() { |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
168 |
return new String[] { |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
169 |
httpURI_fixed, |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
170 |
httpURI_chunk, |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
171 |
httpsURI_fixed, |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
172 |
httpsURI_chunk, |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
173 |
http2URI_fixed, |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
174 |
http2URI_chunk, |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
175 |
https2URI_fixed, |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
176 |
https2URI_chunk, |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
177 |
}; |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
178 |
} |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
179 |
|
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
180 |
@DataProvider(name = "noThrows") |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
181 |
public Object[][] noThrows() { |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
182 |
String[] uris = uris(); |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
183 |
Object[][] result = new Object[uris.length * 2][]; |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
184 |
int i = 0; |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
185 |
for (boolean sameClient : List.of(false, true)) { |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
186 |
for (String uri: uris()) { |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
187 |
result[i++] = new Object[] {uri, sameClient}; |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
188 |
} |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
189 |
} |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
190 |
assert i == uris.length * 2; |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
191 |
return result; |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
192 |
} |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
193 |
|
56133
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
194 |
@DataProvider(name = "variants") |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
195 |
public Object[][] variants() { |
56134
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
196 |
String[] uris = uris(); |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
197 |
Object[][] result = new Object[uris.length * 2 * 2][]; |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
198 |
int i = 0; |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
199 |
for (Thrower thrower : List.of( |
56165
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56134
diff
changeset
|
200 |
new UncheckedIOExceptionThrower(), |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56134
diff
changeset
|
201 |
new UncheckedCustomExceptionThrower())) { |
56134
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
202 |
for (boolean sameClient : List.of(false, true)) { |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
203 |
for (String uri : uris()) { |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
204 |
result[i++] = new Object[]{uri, sameClient, thrower}; |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
205 |
} |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
206 |
} |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
207 |
} |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
208 |
assert i == uris.length * 2 * 2; |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
209 |
return result; |
56133
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
210 |
} |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
211 |
|
56165
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56134
diff
changeset
|
212 |
private HttpClient makeNewClient() { |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56134
diff
changeset
|
213 |
clientCount.incrementAndGet(); |
56133
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
214 |
return HttpClient.newBuilder() |
56165
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56134
diff
changeset
|
215 |
.executor(executor) |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56134
diff
changeset
|
216 |
.sslContext(sslContext) |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56134
diff
changeset
|
217 |
.build(); |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56134
diff
changeset
|
218 |
} |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56134
diff
changeset
|
219 |
|
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56134
diff
changeset
|
220 |
HttpClient newHttpClient(boolean share) { |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56134
diff
changeset
|
221 |
if (!share) return makeNewClient(); |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56134
diff
changeset
|
222 |
HttpClient shared = sharedClient; |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56134
diff
changeset
|
223 |
if (shared != null) return shared; |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56134
diff
changeset
|
224 |
synchronized (this) { |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56134
diff
changeset
|
225 |
shared = sharedClient; |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56134
diff
changeset
|
226 |
if (shared == null) { |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56134
diff
changeset
|
227 |
shared = sharedClient = makeNewClient(); |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56134
diff
changeset
|
228 |
} |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56134
diff
changeset
|
229 |
return shared; |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56134
diff
changeset
|
230 |
} |
56133
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
231 |
} |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
232 |
|
56288
2de1aa88cf06
http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents:
56286
diff
changeset
|
233 |
enum SubscriberType { |
2de1aa88cf06
http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents:
56286
diff
changeset
|
234 |
INLINE, // In line subscribers complete their CF on ON_COMPLETE |
2de1aa88cf06
http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents:
56286
diff
changeset
|
235 |
// e.g. BodySubscribers::ofString |
2de1aa88cf06
http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents:
56286
diff
changeset
|
236 |
OFFLINE; // Off line subscribers complete their CF immediately |
2de1aa88cf06
http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents:
56286
diff
changeset
|
237 |
// but require the client to pull the data after the |
2de1aa88cf06
http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents:
56286
diff
changeset
|
238 |
// CF completes (e.g. BodySubscribers::ofInputStream) |
2de1aa88cf06
http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents:
56286
diff
changeset
|
239 |
} |
2de1aa88cf06
http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents:
56286
diff
changeset
|
240 |
|
2de1aa88cf06
http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents:
56286
diff
changeset
|
241 |
static EnumSet<Where> excludes(SubscriberType type) { |
2de1aa88cf06
http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents:
56286
diff
changeset
|
242 |
EnumSet<Where> set = EnumSet.noneOf(Where.class); |
2de1aa88cf06
http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents:
56286
diff
changeset
|
243 |
|
2de1aa88cf06
http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents:
56286
diff
changeset
|
244 |
if (type == SubscriberType.OFFLINE) { |
2de1aa88cf06
http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents:
56286
diff
changeset
|
245 |
// Throwing on onSubscribe needs some more work |
2de1aa88cf06
http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents:
56286
diff
changeset
|
246 |
// for the case of InputStream, where the body has already |
2de1aa88cf06
http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents:
56286
diff
changeset
|
247 |
// completed by the time the subscriber is subscribed. |
2de1aa88cf06
http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents:
56286
diff
changeset
|
248 |
// The only way we have at that point to relay the exception |
2de1aa88cf06
http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents:
56286
diff
changeset
|
249 |
// is to call onError on the subscriber, but should we if |
2de1aa88cf06
http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents:
56286
diff
changeset
|
250 |
// Subscriber::onSubscribed has thrown an exception and |
2de1aa88cf06
http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents:
56286
diff
changeset
|
251 |
// not completed normally? |
2de1aa88cf06
http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents:
56286
diff
changeset
|
252 |
set.add(Where.ON_SUBSCRIBE); |
2de1aa88cf06
http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents:
56286
diff
changeset
|
253 |
} |
2de1aa88cf06
http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents:
56286
diff
changeset
|
254 |
|
2de1aa88cf06
http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents:
56286
diff
changeset
|
255 |
// Don't know how to make the stack reliably cause onError |
2de1aa88cf06
http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents:
56286
diff
changeset
|
256 |
// to be called without closing the connection. |
2de1aa88cf06
http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents:
56286
diff
changeset
|
257 |
// And how do we get the exception if onError throws anyway? |
2de1aa88cf06
http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents:
56286
diff
changeset
|
258 |
set.add(Where.ON_ERROR); |
2de1aa88cf06
http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents:
56286
diff
changeset
|
259 |
|
2de1aa88cf06
http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents:
56286
diff
changeset
|
260 |
return set; |
2de1aa88cf06
http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents:
56286
diff
changeset
|
261 |
} |
2de1aa88cf06
http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents:
56286
diff
changeset
|
262 |
|
56134
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
263 |
@Test(dataProvider = "noThrows") |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
264 |
public void testNoThrows(String uri, boolean sameClient) |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
265 |
throws Exception { |
56133
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
266 |
HttpClient client = null; |
56165
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56134
diff
changeset
|
267 |
out.printf("%ntestNoThrows(%s, %b)%n", uri, sameClient); |
56133
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
268 |
for (int i=0; i< ITERATION_COUNT; i++) { |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
269 |
if (!sameClient || client == null) |
56165
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56134
diff
changeset
|
270 |
client = newHttpClient(sameClient); |
56133
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
271 |
|
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
272 |
HttpRequest req = HttpRequest.newBuilder(URI.create(uri)) |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
273 |
.build(); |
56134
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
274 |
BodyHandler<String> handler = |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
275 |
new ThrowingBodyHandler((w) -> {}, |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56165
diff
changeset
|
276 |
BodyHandlers.ofString()); |
56133
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
277 |
HttpResponse<String> response = client.send(req, handler); |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
278 |
String body = response.body(); |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
279 |
assertEquals(URI.create(body).getPath(), URI.create(uri).getPath()); |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
280 |
} |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
281 |
} |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
282 |
|
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
283 |
@Test(dataProvider = "variants") |
56134
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
284 |
public void testThrowingAsString(String uri, |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
285 |
boolean sameClient, |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
286 |
Thrower thrower) |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
287 |
throws Exception |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
288 |
{ |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
289 |
String test = format("testThrowingAsString(%s, %b, %s)", |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
290 |
uri, sameClient, thrower); |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56165
diff
changeset
|
291 |
testThrowing(test, uri, sameClient, BodyHandlers::ofString, |
56288
2de1aa88cf06
http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents:
56286
diff
changeset
|
292 |
this::shouldHaveThrown, thrower,false, |
2de1aa88cf06
http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents:
56286
diff
changeset
|
293 |
excludes(SubscriberType.INLINE)); |
56133
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
294 |
} |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
295 |
|
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
296 |
@Test(dataProvider = "variants") |
56134
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
297 |
public void testThrowingAsLines(String uri, |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
298 |
boolean sameClient, |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
299 |
Thrower thrower) |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
300 |
throws Exception |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
301 |
{ |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
302 |
String test = format("testThrowingAsLines(%s, %b, %s)", |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
303 |
uri, sameClient, thrower); |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56165
diff
changeset
|
304 |
testThrowing(test, uri, sameClient, BodyHandlers::ofLines, |
56288
2de1aa88cf06
http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents:
56286
diff
changeset
|
305 |
this::checkAsLines, thrower,false, |
2de1aa88cf06
http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents:
56286
diff
changeset
|
306 |
excludes(SubscriberType.OFFLINE)); |
56133
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
307 |
} |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
308 |
|
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
309 |
@Test(dataProvider = "variants") |
56134
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
310 |
public void testThrowingAsInputStream(String uri, |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
311 |
boolean sameClient, |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
312 |
Thrower thrower) |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
313 |
throws Exception |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
314 |
{ |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
315 |
String test = format("testThrowingAsInputStream(%s, %b, %s)", |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
316 |
uri, sameClient, thrower); |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56165
diff
changeset
|
317 |
testThrowing(test, uri, sameClient, BodyHandlers::ofInputStream, |
56288
2de1aa88cf06
http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents:
56286
diff
changeset
|
318 |
this::checkAsInputStream, thrower,false, |
2de1aa88cf06
http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents:
56286
diff
changeset
|
319 |
excludes(SubscriberType.OFFLINE)); |
56133
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
320 |
} |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
321 |
|
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
322 |
@Test(dataProvider = "variants") |
56134
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
323 |
public void testThrowingAsStringAsync(String uri, |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
324 |
boolean sameClient, |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
325 |
Thrower thrower) |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
326 |
throws Exception |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
327 |
{ |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
328 |
String test = format("testThrowingAsStringAsync(%s, %b, %s)", |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
329 |
uri, sameClient, thrower); |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56165
diff
changeset
|
330 |
testThrowing(test, uri, sameClient, BodyHandlers::ofString, |
56288
2de1aa88cf06
http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents:
56286
diff
changeset
|
331 |
this::shouldHaveThrown, thrower, true, |
2de1aa88cf06
http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents:
56286
diff
changeset
|
332 |
excludes(SubscriberType.INLINE)); |
56133
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
333 |
} |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
334 |
|
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
335 |
@Test(dataProvider = "variants") |
56134
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
336 |
public void testThrowingAsLinesAsync(String uri, |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
337 |
boolean sameClient, |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
338 |
Thrower thrower) |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
339 |
throws Exception |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
340 |
{ |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
341 |
String test = format("testThrowingAsLinesAsync(%s, %b, %s)", |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
342 |
uri, sameClient, thrower); |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56165
diff
changeset
|
343 |
testThrowing(test, uri, sameClient, BodyHandlers::ofLines, |
56288
2de1aa88cf06
http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents:
56286
diff
changeset
|
344 |
this::checkAsLines, thrower,true, |
2de1aa88cf06
http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents:
56286
diff
changeset
|
345 |
excludes(SubscriberType.OFFLINE)); |
56133
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
346 |
} |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
347 |
|
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
348 |
@Test(dataProvider = "variants") |
56134
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
349 |
public void testThrowingAsInputStreamAsync(String uri, |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
350 |
boolean sameClient, |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
351 |
Thrower thrower) |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
352 |
throws Exception |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
353 |
{ |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
354 |
String test = format("testThrowingAsInputStreamAsync(%s, %b, %s)", |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
355 |
uri, sameClient, thrower); |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56165
diff
changeset
|
356 |
testThrowing(test, uri, sameClient, BodyHandlers::ofInputStream, |
56288
2de1aa88cf06
http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents:
56286
diff
changeset
|
357 |
this::checkAsInputStream, thrower,true, |
2de1aa88cf06
http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents:
56286
diff
changeset
|
358 |
excludes(SubscriberType.OFFLINE)); |
56133
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
359 |
} |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
360 |
|
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
361 |
private <T,U> void testThrowing(String name, String uri, boolean sameClient, |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
362 |
Supplier<BodyHandler<T>> handlers, |
56288
2de1aa88cf06
http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents:
56286
diff
changeset
|
363 |
Finisher finisher, Thrower thrower, |
2de1aa88cf06
http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents:
56286
diff
changeset
|
364 |
boolean async, EnumSet<Where> excludes) |
56134
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
365 |
throws Exception |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
366 |
{ |
56165
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56134
diff
changeset
|
367 |
out.printf("%n%s%s%n", now(), name); |
56133
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
368 |
try { |
56288
2de1aa88cf06
http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents:
56286
diff
changeset
|
369 |
testThrowing(uri, sameClient, handlers, finisher, thrower, async, excludes); |
56133
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
370 |
} catch (Error | Exception x) { |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
371 |
FAILURES.putIfAbsent(name, x); |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
372 |
throw x; |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
373 |
} |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
374 |
} |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
375 |
|
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
376 |
private <T,U> void testThrowing(String uri, boolean sameClient, |
56134
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
377 |
Supplier<BodyHandler<T>> handlers, |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
378 |
Finisher finisher, Thrower thrower, |
56288
2de1aa88cf06
http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents:
56286
diff
changeset
|
379 |
boolean async, |
2de1aa88cf06
http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents:
56286
diff
changeset
|
380 |
EnumSet<Where> excludes) |
56134
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
381 |
throws Exception |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
382 |
{ |
56133
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
383 |
HttpClient client = null; |
56288
2de1aa88cf06
http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents:
56286
diff
changeset
|
384 |
for (Where where : EnumSet.complementOf(excludes)) { |
56286
3d8333fa243b
http-client-branch: added some comments to test/jdk/java/net/httpclient/ThrowingSubscribers.java
dfuchs
parents:
56273
diff
changeset
|
385 |
|
56133
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
386 |
if (!sameClient || client == null) |
56165
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56134
diff
changeset
|
387 |
client = newHttpClient(sameClient); |
56133
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
388 |
|
56134
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
389 |
HttpRequest req = HttpRequest. |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
390 |
newBuilder(URI.create(uri)) |
56133
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
391 |
.build(); |
56134
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
392 |
BodyHandler<T> handler = |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
393 |
new ThrowingBodyHandler(where.select(thrower), handlers.get()); |
56133
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
394 |
System.out.println("try throwing in " + where); |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
395 |
HttpResponse<T> response = null; |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
396 |
if (async) { |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
397 |
try { |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
398 |
response = client.sendAsync(req, handler).join(); |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
399 |
} catch (Error | Exception x) { |
56134
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
400 |
Throwable cause = findCause(x, thrower); |
56273
329e410c9a01
http-client-branch: improved failure report in ThrowingSubscribers
dfuchs
parents:
56265
diff
changeset
|
401 |
if (cause == null) throw causeNotFound(where, x); |
56165
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56134
diff
changeset
|
402 |
System.out.println(now() + "Got expected exception: " + cause); |
56133
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
403 |
} |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
404 |
} else { |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
405 |
try { |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
406 |
response = client.send(req, handler); |
56134
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
407 |
} catch (Error | Exception t) { |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
408 |
if (thrower.test(t)) { |
56165
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56134
diff
changeset
|
409 |
System.out.println(now() + "Got expected exception: " + t); |
56273
329e410c9a01
http-client-branch: improved failure report in ThrowingSubscribers
dfuchs
parents:
56265
diff
changeset
|
410 |
} else throw causeNotFound(where, t); |
56133
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
411 |
} |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
412 |
} |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
413 |
if (response != null) { |
56134
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
414 |
finisher.finish(where, response, thrower); |
56133
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
415 |
} |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
416 |
} |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
417 |
} |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
418 |
|
56165
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56134
diff
changeset
|
419 |
enum Where { |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56134
diff
changeset
|
420 |
BODY_HANDLER, ON_SUBSCRIBE, ON_NEXT, ON_COMPLETE, ON_ERROR, GET_BODY, BODY_CF; |
56133
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
421 |
public Consumer<Where> select(Consumer<Where> consumer) { |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
422 |
return new Consumer<Where>() { |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
423 |
@Override |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
424 |
public void accept(Where where) { |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
425 |
if (Where.this == where) { |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
426 |
consumer.accept(where); |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
427 |
} |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
428 |
} |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
429 |
}; |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
430 |
} |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
431 |
} |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
432 |
|
56273
329e410c9a01
http-client-branch: improved failure report in ThrowingSubscribers
dfuchs
parents:
56265
diff
changeset
|
433 |
static AssertionError causeNotFound(Where w, Throwable t) { |
329e410c9a01
http-client-branch: improved failure report in ThrowingSubscribers
dfuchs
parents:
56265
diff
changeset
|
434 |
return new AssertionError("Expected exception not found in " + w, t); |
329e410c9a01
http-client-branch: improved failure report in ThrowingSubscribers
dfuchs
parents:
56265
diff
changeset
|
435 |
} |
329e410c9a01
http-client-branch: improved failure report in ThrowingSubscribers
dfuchs
parents:
56265
diff
changeset
|
436 |
|
56134
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
437 |
interface Thrower extends Consumer<Where>, Predicate<Throwable> { |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
438 |
|
56133
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
439 |
} |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
440 |
|
56134
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
441 |
interface Finisher<T,U> { |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
442 |
U finish(Where w, HttpResponse<T> resp, Thrower thrower) throws IOException; |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
443 |
} |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
444 |
|
56165
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56134
diff
changeset
|
445 |
final <T,U> U shouldHaveThrown(Where w, HttpResponse<T> resp, Thrower thrower) { |
56273
329e410c9a01
http-client-branch: improved failure report in ThrowingSubscribers
dfuchs
parents:
56265
diff
changeset
|
446 |
String msg = "Expected exception not thrown in " + w |
329e410c9a01
http-client-branch: improved failure report in ThrowingSubscribers
dfuchs
parents:
56265
diff
changeset
|
447 |
+ "\n\tReceived: " + resp |
329e410c9a01
http-client-branch: improved failure report in ThrowingSubscribers
dfuchs
parents:
56265
diff
changeset
|
448 |
+ "\n\tWith body: " + resp.body(); |
329e410c9a01
http-client-branch: improved failure report in ThrowingSubscribers
dfuchs
parents:
56265
diff
changeset
|
449 |
System.out.println(msg); |
329e410c9a01
http-client-branch: improved failure report in ThrowingSubscribers
dfuchs
parents:
56265
diff
changeset
|
450 |
throw new RuntimeException(msg); |
56133
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
451 |
} |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
452 |
|
56165
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56134
diff
changeset
|
453 |
final List<String> checkAsLines(Where w, HttpResponse<Stream<String>> resp, Thrower thrower) { |
56133
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
454 |
switch(w) { |
56134
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
455 |
case BODY_HANDLER: return shouldHaveThrown(w, resp, thrower); |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
456 |
case ON_SUBSCRIBE: return shouldHaveThrown(w, resp, thrower); |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
457 |
case GET_BODY: return shouldHaveThrown(w, resp, thrower); |
56165
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56134
diff
changeset
|
458 |
case BODY_CF: return shouldHaveThrown(w, resp, thrower); |
56133
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
459 |
default: break; |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
460 |
} |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
461 |
List<String> result = null; |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
462 |
try { |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
463 |
result = resp.body().collect(Collectors.toList()); |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
464 |
} catch (Error | Exception x) { |
56134
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
465 |
Throwable cause = findCause(x, thrower); |
56133
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
466 |
if (cause != null) { |
56165
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56134
diff
changeset
|
467 |
out.println(now() + "Got expected exception in " + w + ": " + cause); |
56133
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
468 |
return result; |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
469 |
} |
56273
329e410c9a01
http-client-branch: improved failure report in ThrowingSubscribers
dfuchs
parents:
56265
diff
changeset
|
470 |
throw causeNotFound(w, x); |
56133
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
471 |
} |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
472 |
throw new RuntimeException("Expected exception not thrown in " + w); |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
473 |
} |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
474 |
|
56165
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56134
diff
changeset
|
475 |
final List<String> checkAsInputStream(Where w, HttpResponse<InputStream> resp, |
56134
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
476 |
Thrower thrower) |
56133
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
477 |
throws IOException |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
478 |
{ |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
479 |
switch(w) { |
56134
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
480 |
case BODY_HANDLER: return shouldHaveThrown(w, resp, thrower); |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
481 |
case ON_SUBSCRIBE: return shouldHaveThrown(w, resp, thrower); |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
482 |
case GET_BODY: return shouldHaveThrown(w, resp, thrower); |
56165
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56134
diff
changeset
|
483 |
case BODY_CF: return shouldHaveThrown(w, resp, thrower); |
56133
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
484 |
default: break; |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
485 |
} |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
486 |
List<String> result = null; |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
487 |
try (InputStreamReader r1 = new InputStreamReader(resp.body(), UTF_8); |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
488 |
BufferedReader r = new BufferedReader(r1)) { |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
489 |
try { |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
490 |
result = r.lines().collect(Collectors.toList()); |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
491 |
} catch (Error | Exception x) { |
56165
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56134
diff
changeset
|
492 |
Throwable cause = findCause(x, thrower); |
56133
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
493 |
if (cause != null) { |
56165
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56134
diff
changeset
|
494 |
out.println(now() + "Got expected exception in " + w + ": " + cause); |
56133
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
495 |
return result; |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
496 |
} |
56273
329e410c9a01
http-client-branch: improved failure report in ThrowingSubscribers
dfuchs
parents:
56265
diff
changeset
|
497 |
throw causeNotFound(w, x); |
56133
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
498 |
} |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
499 |
} |
56134
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
500 |
return shouldHaveThrown(w, resp, thrower); |
56133
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
501 |
} |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
502 |
|
56134
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
503 |
private static Throwable findCause(Throwable x, |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
504 |
Predicate<Throwable> filter) { |
56133
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
505 |
while (x != null && !filter.test(x)) x = x.getCause(); |
56134
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
506 |
return x; |
56133
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
507 |
} |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
508 |
|
56165
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56134
diff
changeset
|
509 |
static final class UncheckedCustomExceptionThrower implements Thrower { |
56133
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
510 |
@Override |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
511 |
public void accept(Where where) { |
56165
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56134
diff
changeset
|
512 |
out.println(now() + "Throwing in " + where); |
56133
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
513 |
throw new UncheckedCustomException(where.name()); |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
514 |
} |
56134
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
515 |
|
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
516 |
@Override |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
517 |
public boolean test(Throwable throwable) { |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
518 |
return UncheckedCustomException.class.isInstance(throwable); |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
519 |
} |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
520 |
|
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
521 |
@Override |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
522 |
public String toString() { |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
523 |
return "UncheckedCustomExceptionThrower"; |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
524 |
} |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
525 |
} |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
526 |
|
56165
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56134
diff
changeset
|
527 |
static final class UncheckedIOExceptionThrower implements Thrower { |
56134
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
528 |
@Override |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
529 |
public void accept(Where where) { |
56165
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56134
diff
changeset
|
530 |
out.println(now() + "Throwing in " + where); |
56134
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
531 |
throw new UncheckedIOException(new CustomIOException(where.name())); |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
532 |
} |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
533 |
|
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
534 |
@Override |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
535 |
public boolean test(Throwable throwable) { |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
536 |
return UncheckedIOException.class.isInstance(throwable) |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
537 |
&& CustomIOException.class.isInstance(throwable.getCause()); |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
538 |
} |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
539 |
|
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
540 |
@Override |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
541 |
public String toString() { |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
542 |
return "UncheckedIOExceptionThrower"; |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
543 |
} |
56133
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
544 |
} |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
545 |
|
56165
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56134
diff
changeset
|
546 |
static final class UncheckedCustomException extends RuntimeException { |
56133
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
547 |
UncheckedCustomException(String message) { |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
548 |
super(message); |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
549 |
} |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
550 |
UncheckedCustomException(String message, Throwable cause) { |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
551 |
super(message, cause); |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
552 |
} |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
553 |
} |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
554 |
|
56165
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56134
diff
changeset
|
555 |
static final class CustomIOException extends IOException { |
56134
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
556 |
CustomIOException(String message) { |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
557 |
super(message); |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
558 |
} |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
559 |
CustomIOException(String message, Throwable cause) { |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
560 |
super(message, cause); |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
561 |
} |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
562 |
} |
2fc9edf5628d
http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents:
56133
diff
changeset
|
563 |
|
56165
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56134
diff
changeset
|
564 |
static final class ThrowingBodyHandler<T> implements BodyHandler<T> { |
56133
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
565 |
final Consumer<Where> throwing; |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
566 |
final BodyHandler<T> bodyHandler; |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
567 |
ThrowingBodyHandler(Consumer<Where> throwing, BodyHandler<T> bodyHandler) { |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
568 |
this.throwing = throwing; |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
569 |
this.bodyHandler = bodyHandler; |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
570 |
} |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
571 |
@Override |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
572 |
public BodySubscriber<T> apply(int statusCode, HttpHeaders responseHeaders) { |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
573 |
throwing.accept(Where.BODY_HANDLER); |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
574 |
BodySubscriber<T> subscriber = bodyHandler.apply(statusCode, responseHeaders); |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
575 |
return new ThrowingBodySubscriber(throwing, subscriber); |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
576 |
} |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
577 |
} |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
578 |
|
56165
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56134
diff
changeset
|
579 |
static final class ThrowingBodySubscriber<T> implements BodySubscriber<T> { |
56133
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
580 |
private final BodySubscriber<T> subscriber; |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
581 |
volatile boolean onSubscribeCalled; |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
582 |
final Consumer<Where> throwing; |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
583 |
ThrowingBodySubscriber(Consumer<Where> throwing, BodySubscriber<T> subscriber) { |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
584 |
this.throwing = throwing; |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
585 |
this.subscriber = subscriber; |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
586 |
} |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
587 |
|
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
588 |
@Override |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
589 |
public void onSubscribe(Flow.Subscription subscription) { |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
590 |
//out.println("onSubscribe "); |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
591 |
onSubscribeCalled = true; |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
592 |
throwing.accept(Where.ON_SUBSCRIBE); |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
593 |
subscriber.onSubscribe(subscription); |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
594 |
} |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
595 |
|
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
596 |
@Override |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
597 |
public void onNext(List<ByteBuffer> item) { |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
598 |
// out.println("onNext " + item); |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
599 |
assertTrue(onSubscribeCalled); |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
600 |
throwing.accept(Where.ON_NEXT); |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
601 |
subscriber.onNext(item); |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
602 |
} |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
603 |
|
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
604 |
@Override |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
605 |
public void onError(Throwable throwable) { |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
606 |
//out.println("onError"); |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
607 |
assertTrue(onSubscribeCalled); |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
608 |
throwing.accept(Where.ON_ERROR); |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
609 |
subscriber.onError(throwable); |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
610 |
} |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
611 |
|
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
612 |
@Override |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
613 |
public void onComplete() { |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
614 |
//out.println("onComplete"); |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
615 |
assertTrue(onSubscribeCalled, "onComplete called before onSubscribe"); |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
616 |
throwing.accept(Where.ON_COMPLETE); |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
617 |
subscriber.onComplete(); |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
618 |
} |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
619 |
|
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
620 |
@Override |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
621 |
public CompletionStage<T> getBody() { |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
622 |
throwing.accept(Where.GET_BODY); |
56165
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56134
diff
changeset
|
623 |
try { |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56134
diff
changeset
|
624 |
throwing.accept(Where.BODY_CF); |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56134
diff
changeset
|
625 |
} catch (Throwable t) { |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56134
diff
changeset
|
626 |
return CompletableFuture.failedFuture(t); |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56134
diff
changeset
|
627 |
} |
56133
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
628 |
return subscriber.getBody(); |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
629 |
} |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
630 |
} |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
631 |
|
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
632 |
|
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
633 |
@BeforeTest |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
634 |
public void setup() throws Exception { |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
635 |
sslContext = new SimpleSSLContext().get(); |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
636 |
if (sslContext == null) |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
637 |
throw new AssertionError("Unexpected null sslContext"); |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
638 |
|
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
639 |
// HTTP/1.1 |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
640 |
HttpTestHandler h1_fixedLengthHandler = new HTTP_FixedLengthHandler(); |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
641 |
HttpTestHandler h1_chunkHandler = new HTTP_ChunkedHandler(); |
56265
ec34ae013fbe
http-client-branch: tests should bind to the loopback only
chegar
parents:
56209
diff
changeset
|
642 |
InetSocketAddress sa = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0); |
56133
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
643 |
httpTestServer = HttpTestServer.of(HttpServer.create(sa, 0)); |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
644 |
httpTestServer.addHandler(h1_fixedLengthHandler, "/http1/fixed"); |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
645 |
httpTestServer.addHandler(h1_chunkHandler, "/http1/chunk"); |
56265
ec34ae013fbe
http-client-branch: tests should bind to the loopback only
chegar
parents:
56209
diff
changeset
|
646 |
httpURI_fixed = "http://" + httpTestServer.serverAuthority() + "/http1/fixed/x"; |
ec34ae013fbe
http-client-branch: tests should bind to the loopback only
chegar
parents:
56209
diff
changeset
|
647 |
httpURI_chunk = "http://" + httpTestServer.serverAuthority() + "/http1/chunk/x"; |
56133
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
648 |
|
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
649 |
HttpsServer httpsServer = HttpsServer.create(sa, 0); |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
650 |
httpsServer.setHttpsConfigurator(new HttpsConfigurator(sslContext)); |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
651 |
httpsTestServer = HttpTestServer.of(httpsServer); |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
652 |
httpsTestServer.addHandler(h1_fixedLengthHandler, "/https1/fixed"); |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
653 |
httpsTestServer.addHandler(h1_chunkHandler, "/https1/chunk"); |
56265
ec34ae013fbe
http-client-branch: tests should bind to the loopback only
chegar
parents:
56209
diff
changeset
|
654 |
httpsURI_fixed = "https://" + httpsTestServer.serverAuthority() + "/https1/fixed/x"; |
ec34ae013fbe
http-client-branch: tests should bind to the loopback only
chegar
parents:
56209
diff
changeset
|
655 |
httpsURI_chunk = "https://" + httpsTestServer.serverAuthority() + "/https1/chunk/x"; |
56133
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
656 |
|
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
657 |
// HTTP/2 |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
658 |
HttpTestHandler h2_fixedLengthHandler = new HTTP_FixedLengthHandler(); |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
659 |
HttpTestHandler h2_chunkedHandler = new HTTP_ChunkedHandler(); |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
660 |
|
56265
ec34ae013fbe
http-client-branch: tests should bind to the loopback only
chegar
parents:
56209
diff
changeset
|
661 |
http2TestServer = HttpTestServer.of(new Http2TestServer("localhost", false, 0)); |
56133
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
662 |
http2TestServer.addHandler(h2_fixedLengthHandler, "/http2/fixed"); |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
663 |
http2TestServer.addHandler(h2_chunkedHandler, "/http2/chunk"); |
56265
ec34ae013fbe
http-client-branch: tests should bind to the loopback only
chegar
parents:
56209
diff
changeset
|
664 |
http2URI_fixed = "http://" + http2TestServer.serverAuthority() + "/http2/fixed/x"; |
ec34ae013fbe
http-client-branch: tests should bind to the loopback only
chegar
parents:
56209
diff
changeset
|
665 |
http2URI_chunk = "http://" + http2TestServer.serverAuthority() + "/http2/chunk/x"; |
56133
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
666 |
|
56265
ec34ae013fbe
http-client-branch: tests should bind to the loopback only
chegar
parents:
56209
diff
changeset
|
667 |
https2TestServer = HttpTestServer.of(new Http2TestServer("localhost", true, 0)); |
56133
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
668 |
https2TestServer.addHandler(h2_fixedLengthHandler, "/https2/fixed"); |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
669 |
https2TestServer.addHandler(h2_chunkedHandler, "/https2/chunk"); |
56265
ec34ae013fbe
http-client-branch: tests should bind to the loopback only
chegar
parents:
56209
diff
changeset
|
670 |
https2URI_fixed = "https://" + https2TestServer.serverAuthority() + "/https2/fixed/x"; |
ec34ae013fbe
http-client-branch: tests should bind to the loopback only
chegar
parents:
56209
diff
changeset
|
671 |
https2URI_chunk = "https://" + https2TestServer.serverAuthority() + "/https2/chunk/x"; |
56133
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
672 |
|
56165
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56134
diff
changeset
|
673 |
serverCount.addAndGet(4); |
56133
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
674 |
httpTestServer.start(); |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
675 |
httpsTestServer.start(); |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
676 |
http2TestServer.start(); |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
677 |
https2TestServer.start(); |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
678 |
} |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
679 |
|
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
680 |
@AfterTest |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
681 |
public void teardown() throws Exception { |
56165
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56134
diff
changeset
|
682 |
sharedClient = null; |
56133
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
683 |
httpTestServer.stop(); |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
684 |
httpsTestServer.stop(); |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
685 |
http2TestServer.stop(); |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
686 |
https2TestServer.stop(); |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
687 |
} |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
688 |
|
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
689 |
static class HTTP_FixedLengthHandler implements HttpTestHandler { |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
690 |
@Override |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
691 |
public void handle(HttpTestExchange t) throws IOException { |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
692 |
out.println("HTTP_FixedLengthHandler received request to " + t.getRequestURI()); |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
693 |
try (InputStream is = t.getRequestBody()) { |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
694 |
is.readAllBytes(); |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
695 |
} |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
696 |
byte[] resp = t.getRequestURI().toString().getBytes(StandardCharsets.UTF_8); |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
697 |
t.sendResponseHeaders(200, resp.length); //fixed content length |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
698 |
try (OutputStream os = t.getResponseBody()) { |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
699 |
os.write(resp); |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
700 |
} |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
701 |
} |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
702 |
} |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
703 |
|
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
704 |
static class HTTP_ChunkedHandler implements HttpTestHandler { |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
705 |
@Override |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
706 |
public void handle(HttpTestExchange t) throws IOException { |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
707 |
out.println("HTTP_ChunkedHandler received request to " + t.getRequestURI()); |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
708 |
byte[] resp = t.getRequestURI().toString().getBytes(StandardCharsets.UTF_8); |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
709 |
try (InputStream is = t.getRequestBody()) { |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
710 |
is.readAllBytes(); |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
711 |
} |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
712 |
t.sendResponseHeaders(200, -1); // chunked/variable |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
713 |
try (OutputStream os = t.getResponseBody()) { |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
714 |
os.write(resp); |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
715 |
} |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
716 |
} |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
717 |
} |
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
718 |
|
be9720a556c7
http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff
changeset
|
719 |
} |