< prev index next >
test/jdk/java/net/httpclient/SmallTimeout.java
Print this page
*** 38,48 ****
/**
* @test
* @bug 8178147
* @summary Ensures that small timeouts do not cause hangs due to race conditions
! * @run main/othervm SmallTimeout
*/
// To enable logging use. Not enabled by default as it changes the dynamics
// of the test.
// @run main/othervm -Djdk.httpclient.HttpClient.log=all,frames:all SmallTimeout
--- 38,48 ----
/**
* @test
* @bug 8178147
* @summary Ensures that small timeouts do not cause hangs due to race conditions
! * @run main/othervm -Djdk.incubator.http.internal.common.DEBUG=true SmallTimeout
*/
// To enable logging use. Not enabled by default as it changes the dynamics
// of the test.
// @run main/othervm -Djdk.httpclient.HttpClient.log=all,frames:all SmallTimeout
*** 50,60 ****
public class SmallTimeout {
static int[] TIMEOUTS = {2, 1, 3, 2, 100, 1};
// A queue for placing timed out requests so that their order can be checked.
! static LinkedBlockingQueue<HttpRequest> queue = new LinkedBlockingQueue<>();
static volatile boolean error;
public static void main(String[] args) throws Exception {
HttpClient client = HttpClient.newHttpClient();
--- 50,78 ----
public class SmallTimeout {
static int[] TIMEOUTS = {2, 1, 3, 2, 100, 1};
// A queue for placing timed out requests so that their order can be checked.
! static LinkedBlockingQueue<HttpResult> queue = new LinkedBlockingQueue<>();
!
! static final class HttpResult {
! final HttpRequest request;
! final Throwable failed;
! HttpResult(HttpRequest request, Throwable failed) {
! this.request = request;
! this.failed = failed;
! }
!
! static HttpResult of(HttpRequest request) {
! return new HttpResult(request, null);
! }
!
! static HttpResult of(HttpRequest request, Throwable t) {
! return new HttpResult(request, t);
! }
!
! }
static volatile boolean error;
public static void main(String[] args) throws Exception {
HttpClient client = HttpClient.newHttpClient();
*** 74,102 ****
final HttpRequest req = requests[i];
CompletableFuture<HttpResponse<Object>> response = client
.sendAsync(req, discard(null))
.whenComplete((HttpResponse<Object> r, Throwable t) -> {
if (r != null) {
out.println("Unexpected response: " + r);
error = true;
}
if (t != null) {
if (!(t.getCause() instanceof HttpTimeoutException)) {
out.println("Wrong exception type:" + t.toString());
Throwable c = t.getCause() == null ? t : t.getCause();
c.printStackTrace();
error = true;
} else {
out.println("Caught expected timeout: " + t.getCause());
}
}
if (t == null && r == null) {
out.println("Both response and throwable are null!");
error = true;
}
! queue.add(req);
});
}
System.out.println("All requests submitted. Waiting ...");
checkReturn(requests);
--- 92,124 ----
final HttpRequest req = requests[i];
CompletableFuture<HttpResponse<Object>> response = client
.sendAsync(req, discard(null))
.whenComplete((HttpResponse<Object> r, Throwable t) -> {
+ Throwable cause = null;
if (r != null) {
out.println("Unexpected response: " + r);
+ cause = new RuntimeException("Unexpected response");
error = true;
}
if (t != null) {
if (!(t.getCause() instanceof HttpTimeoutException)) {
out.println("Wrong exception type:" + t.toString());
Throwable c = t.getCause() == null ? t : t.getCause();
c.printStackTrace();
+ cause = c;
error = true;
} else {
out.println("Caught expected timeout: " + t.getCause());
}
}
if (t == null && r == null) {
out.println("Both response and throwable are null!");
+ cause = new RuntimeException("Both response and throwable are null!");
error = true;
}
! queue.add(HttpResult.of(req,cause));
});
}
System.out.println("All requests submitted. Waiting ...");
checkReturn(requests);
*** 116,134 ****
.GET()
.build();
final HttpRequest req = requests[i];
executor.execute(() -> {
try {
client.send(req, discard(null));
} catch (HttpTimeoutException e) {
out.println("Caught expected timeout: " + e);
! queue.offer(req);
! } catch (IOException | InterruptedException ee) {
Throwable c = ee.getCause() == null ? ee : ee.getCause();
c.printStackTrace();
error = true;
}
});
}
System.out.println("All requests submitted. Waiting ...");
--- 138,159 ----
.GET()
.build();
final HttpRequest req = requests[i];
executor.execute(() -> {
+ Throwable cause = null;
try {
client.send(req, discard(null));
} catch (HttpTimeoutException e) {
out.println("Caught expected timeout: " + e);
! } catch (Throwable ee) {
Throwable c = ee.getCause() == null ? ee : ee.getCause();
c.printStackTrace();
+ cause = c;
error = true;
+ } finally {
+ queue.offer(HttpResult.of(req, cause));
}
});
}
System.out.println("All requests submitted. Waiting ...");
*** 137,158 ****
executor.shutdownNow();
if (error)
throw new RuntimeException("Failed. Check output");
- } finally {
- ((ExecutorService) client.executor()).shutdownNow();
}
}
static void checkReturn(HttpRequest[] requests) throws InterruptedException {
// wait for exceptions and check order
for (int j = 0; j < TIMEOUTS.length; j++) {
! HttpRequest req = queue.take();
! out.println("Got request from queue " + req + ", order: " + getRequest(req, requests));
}
! out.println("Return ok");
}
/** Returns the index of the request in the array. */
static String getRequest(HttpRequest req, HttpRequest[] requests) {
for (int i=0; i<requests.length; i++) {
--- 162,185 ----
executor.shutdownNow();
if (error)
throw new RuntimeException("Failed. Check output");
}
}
static void checkReturn(HttpRequest[] requests) throws InterruptedException {
// wait for exceptions and check order
+ boolean ok = true;
for (int j = 0; j < TIMEOUTS.length; j++) {
! HttpResult res = queue.take();
! HttpRequest req = res.request;
! out.println("Got request from queue " + req + ", order: " + getRequest(req, requests)
! + (res.failed == null ? "" : " failed: " + res.failed));
! ok = ok && res.failed == null;
}
! out.println("Return " + (ok ? "ok" : "nok"));
}
/** Returns the index of the request in the array. */
static String getRequest(HttpRequest req, HttpRequest[] requests) {
for (int i=0; i<requests.length; i++) {
< prev index next >