/* * Copyright (c) 2020, 2022, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
finalstatic String BODY = "Some string | that ? can | be split ? several | ways.";
// should accept SSLHandshakeException because of the connectionAborter // with http/2 and should accept Stream 5 cancelled. // => also examine in what measure we should always // rewrap in "Request Cancelled" when the multi exchange was aborted... privatestaticboolean isCancelled(Throwable t) { while (t instanceof ExecutionException) t = t.getCause(); if (t instanceof CancellationException) returntrue; if (t instanceof IOException) return String.valueOf(t).contains("Request cancelled");
out.println("Not a cancellation exception: " + t);
t.printStackTrace(out); returnfalse;
}
// Cancelling the request may cause an IOException instead... boolean hasCancellationException = false; try {
cf1.get();
} catch (CancellationException | ExecutionException x) {
out.println("Got expected exception: " + x);
assertTrue(isCancelled(x));
hasCancellationException = x instanceof CancellationException;
}
// because it's cf1 that was cancelled then response might not have // completed yet - so wait for it here... try {
String body = response.get().body();
assertEquals(body, Stream.of(BODY.split("\\|")).collect(Collectors.joining())); if (mayInterruptIfRunning) { // well actually - this could happen... In which case we'll need to // increase the latency in the server handler... thrownew AssertionError("Expected Exception not received");
}
} catch (ExecutionException x) {
assertEquals(response.isDone(), true);
Throwable wrapped = x.getCause();
assertTrue(CancellationException.class.isAssignableFrom(wrapped.getClass()));
Throwable cause = wrapped.getCause();
out.println("CancellationException cause: " + x);
assertTrue(IOException.class.isAssignableFrom(cause.getClass())); if (cause instanceof HttpConnectTimeoutException) {
cause.printStackTrace(out); thrownew RuntimeException("Unexpected timeout exception", cause);
} if (mayInterruptIfRunning) {
out.println("Got expected exception: " + wrapped);
out.println("\tcause: " + cause);
} else {
out.println("Unexpected exception: " + wrapped);
wrapped.printStackTrace(out); throw x;
}
}
// Cancelling the request may cause an IOException instead... boolean hasCancellationException = false; try {
cf1.get();
} catch (CancellationException | ExecutionException x) {
out.println("Got expected exception: " + x);
assertTrue(isCancelled(x));
hasCancellationException = x instanceof CancellationException;
}
// because it's cf1 that was cancelled then response might not have // completed yet - so wait for it here... try {
String body = response.get().body();
assertEquals(body, Stream.of(BODY.split("\\|")).collect(Collectors.joining())); if (mayInterruptIfRunning) { // well actually - this could happen... In which case we'll need to // increase the latency in the server handler... thrownew AssertionError("Expected Exception not received");
}
} catch (ExecutionException x) {
assertEquals(response.isDone(), true);
Throwable wrapped = x.getCause();
assertTrue(CancellationException.class.isAssignableFrom(wrapped.getClass()));
Throwable cause = wrapped.getCause();
assertTrue(IOException.class.isAssignableFrom(cause.getClass())); if (cause instanceof HttpConnectTimeoutException) {
cause.printStackTrace(out); thrownew RuntimeException("Unexpected timeout exception", cause);
} if (mayInterruptIfRunning) {
out.println("Got expected exception: " + wrapped);
out.println("\tcause: " + cause);
} else {
out.println("Unexpected exception: " + wrapped);
wrapped.printStackTrace(out); throw x;
}
}
if (failed instanceof InterruptedException) {
out.println("Got expected exception: " + failed);
} elseif (failed instanceof IOException) { // that could be OK if the main thread was interrupted // from the main thread: the interrupt status could have // been caught by writing to the socket from the main // thread. if (interruptingThread.get() == main) {
out.println("Accepting IOException: " + failed);
failed.printStackTrace(out);
} else { throw failed;
}
} elseif (failed != null) {
assertEquals(body, Stream.of(BODY.split("\\|")).collect(Collectors.joining())); throw failed;
}
var error = TRACKER.check(tracker, 200,
(t) -> t.getOutstandingOperations() > 0 || t.getOutstandingSubscribers() > 0, "subscribers for testPostInterrupt(%s)\n\t step [%s]".formatted(req.uri(), i), false);
Reference.reachabilityFence(client); if (error != null) throw error;
}
}
@BeforeTest publicvoid setup() throws Exception {
sslContext = new SimpleSSLContext().get(); if (sslContext == null) thrownew AssertionError("Unexpected null sslContext");
// HTTP/1.1
HttpTestHandler h1_chunkHandler = new HTTPSlowHandler();
InetSocketAddress sa = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
httpTestServer = HttpTestServer.of(HttpServer.create(sa, 0));
httpTestServer.addHandler(h1_chunkHandler, "/http1/x/");
httpURI = "http://" + httpTestServer.serverAuthority() + "/http1/x/";
/** * A handler that slowly sends back a body to give time for the * the request to get cancelled before the body is fully received.
*/ staticclass HTTPSlowHandler implements HttpTestHandler {
@Override publicvoid handle(HttpTestExchange t) throws IOException { try {
out.println("HTTPSlowHandler received request to " + t.getRequestURI());
System.err.println("HTTPSlowHandler received request to " + t.getRequestURI());
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.