Skip to content

Commit de20242

Browse files
committed
8299325: java/net/httpclient/CancelRequestTest.java fails "test CancelRequestTest.testGetSendAsync("https://localhost:46509/https1/x/same/interrupt", true, true)"
Backport-of: a74ebd048ae569296619c112c23169c46b571863
1 parent 2b5b97b commit de20242

File tree

1 file changed

+35
-16
lines changed

1 file changed

+35
-16
lines changed

test/jdk/java/net/httpclient/CancelRequestTest.java

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686

8787
import static java.lang.System.arraycopy;
8888
import static java.lang.System.out;
89+
import static java.lang.System.err;
8990
import static java.net.http.HttpClient.Version.HTTP_1_1;
9091
import static java.net.http.HttpClient.Version.HTTP_2;
9192
import static java.nio.charset.StandardCharsets.UTF_8;
@@ -356,14 +357,22 @@ public void testGetSendAsync(String uri, boolean sameClient, boolean mayInterrup
356357
} catch (ExecutionException x) {
357358
assertEquals(response.isDone(), true);
358359
Throwable wrapped = x.getCause();
359-
assertTrue(CancellationException.class.isAssignableFrom(wrapped.getClass()));
360-
Throwable cause = wrapped.getCause();
361-
out.println("CancellationException cause: " + x);
362-
assertTrue(IOException.class.isAssignableFrom(cause.getClass()));
363-
if (cause instanceof HttpConnectTimeoutException) {
360+
Throwable cause = wrapped;
361+
if (mayInterruptIfRunning) {
362+
assertTrue(CancellationException.class.isAssignableFrom(wrapped.getClass()),
363+
"Unexpected exception: " + wrapped);
364+
cause = wrapped.getCause();
365+
out.println("CancellationException cause: " + x);
366+
if (cause instanceof HttpConnectTimeoutException) {
367+
cause.printStackTrace(out);
368+
throw new RuntimeException("Unexpected timeout exception", cause);
369+
}
370+
}
371+
if (!IOException.class.isInstance(cause)) {
372+
out.println("Unexpected cause: " + cause.getClass());
364373
cause.printStackTrace(out);
365-
throw new RuntimeException("Unexpected timeout exception", cause);
366374
}
375+
assertTrue(IOException.class.isAssignableFrom(cause.getClass()));
367376
if (mayInterruptIfRunning) {
368377
out.println("Got expected exception: " + wrapped);
369378
out.println("\tcause: " + cause);
@@ -381,7 +390,7 @@ public void testGetSendAsync(String uri, boolean sameClient, boolean mayInterrup
381390
assertEquals(cf2.isCancelled(), false);
382391
assertEquals(latch.getCount(), 0);
383392

384-
var error = TRACKER.check(tracker, 200,
393+
var error = TRACKER.check(tracker, 1000,
385394
(t) -> t.getOutstandingOperations() > 0 || t.getOutstandingSubscribers() > 0,
386395
"subscribers for testGetSendAsync(%s)\n\t step [%s]".formatted(req.uri(), i),
387396
false);
@@ -493,7 +502,7 @@ public Iterator<byte[]> iterator() {
493502
assertEquals(cf2.isCancelled(), false);
494503
assertEquals(latch.getCount(), 0);
495504

496-
var error = TRACKER.check(tracker, 200,
505+
var error = TRACKER.check(tracker, 1000,
497506
(t) -> t.getOutstandingOperations() > 0 || t.getOutstandingSubscribers() > 0,
498507
"subscribers for testPostSendAsync(%s)\n\t step [%s]".formatted(req.uri(), i),
499508
false);
@@ -515,9 +524,11 @@ public void testPostInterrupt(String uri, boolean sameClient)
515524

516525
Thread main = Thread.currentThread();
517526
CompletableFuture<Thread> interruptingThread = new CompletableFuture<>();
527+
var uriStr = uri + "/post/req=" + i;
518528
Runnable interrupt = () -> {
519529
Thread current = Thread.currentThread();
520-
out.printf("%s Interrupting main from: %s (%s)", now(), current, uri);
530+
out.printf("%s Interrupting main from: %s (%s)%n", now(), current, uriStr);
531+
err.printf("%s Interrupting main from: %s (%s)%n", now(), current, uriStr);
521532
interruptingThread.complete(current);
522533
main.interrupt();
523534
};
@@ -528,36 +539,44 @@ public void testPostInterrupt(String uri, boolean sameClient)
528539
return List.of(BODY.getBytes(UTF_8)).iterator();
529540
};
530541

531-
HttpRequest req = HttpRequest.newBuilder(URI.create(uri))
542+
HttpRequest req = HttpRequest.newBuilder(URI.create(uriStr))
532543
.POST(HttpRequest.BodyPublishers.ofByteArrays(iterable))
533544
.build();
534545
String body = null;
535546
Exception failed = null;
536547
try {
548+
out.println("Sending: " + uriStr);
537549
body = client.send(req, BodyHandlers.ofString()).body();
538550
} catch (Exception x) {
539551
failed = x;
540552
}
541-
553+
out.println(uriStr + ": got result or exception");
542554
if (failed instanceof InterruptedException) {
543-
out.println("Got expected exception: " + failed);
555+
out.println(uriStr + ": Got expected exception: " + failed);
544556
} else if (failed instanceof IOException) {
557+
out.println(uriStr + ": got IOException: " + failed);
545558
// that could be OK if the main thread was interrupted
546559
// from the main thread: the interrupt status could have
547560
// been caught by writing to the socket from the main
548561
// thread.
549-
if (interruptingThread.get() == main) {
550-
out.println("Accepting IOException: " + failed);
562+
if (interruptingThread.isDone() && interruptingThread.get() == main) {
563+
out.println(uriStr + ": Accepting IOException: " + failed);
551564
failed.printStackTrace(out);
552565
} else {
566+
out.println(uriStr + ": unexpected exception: " + failed);
553567
throw failed;
554568
}
555569
} else if (failed != null) {
556-
assertEquals(body, Stream.of(BODY.split("\\|")).collect(Collectors.joining()));
570+
out.println(uriStr + ": unexpected exception: " + failed);
557571
throw failed;
572+
} else {
573+
assert failed == null;
574+
out.println(uriStr + ": got body: " + body);
575+
assertEquals(body, Stream.of(BODY.split("\\|")).collect(Collectors.joining()));
558576
}
577+
out.println("next iteration");
559578

560-
var error = TRACKER.check(tracker, 200,
579+
var error = TRACKER.check(tracker, 1000,
561580
(t) -> t.getOutstandingOperations() > 0 || t.getOutstandingSubscribers() > 0,
562581
"subscribers for testPostInterrupt(%s)\n\t step [%s]".formatted(req.uri(), i),
563582
false);

0 commit comments

Comments
 (0)