test/jdk/java/net/httpclient/SmallTimeout.java
branchhttp-client-branch
changeset 56474 fe2bf7b369b8
parent 56451 9585061fdb04
child 56507 2294c51eae30
equal deleted inserted replaced
56463:b583caf69b39 56474:fe2bf7b369b8
    83         Throwable failed = null;
    83         Throwable failed = null;
    84         try (ServerSocket ss = new ServerSocket()) {
    84         try (ServerSocket ss = new ServerSocket()) {
    85             ss.setReuseAddress(false);
    85             ss.setReuseAddress(false);
    86             ss.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
    86             ss.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
    87             int port = ss.getLocalPort();
    87             int port = ss.getLocalPort();
    88             URI uri = new URI("http://localhost:" + port + "/");
    88             URI u = new URI("http://localhost:" + port + "/");
    89 
    89 
    90             HttpRequest[] requests = new HttpRequest[TIMEOUTS.length];
    90             HttpRequest[] requests = new HttpRequest[TIMEOUTS.length];
    91 
    91 
    92             out.println("--- TESTING Async");
    92             out.println("--- TESTING Async");
    93             for (int i = 0; i < TIMEOUTS.length; i++) {
    93             for (int i = 0; i < TIMEOUTS.length; i++) {
       
    94                 final int n = i;
       
    95                 URI uri = new URI(u.toString() + "/r" + n);
    94                 requests[i] = HttpRequest.newBuilder(uri)
    96                 requests[i] = HttpRequest.newBuilder(uri)
    95                                          .timeout(Duration.ofMillis(TIMEOUTS[i]))
    97                                          .timeout(Duration.ofMillis(TIMEOUTS[i]))
    96                                          .GET()
    98                                          .GET()
    97                                          .build();
    99                                          .build();
    98 
   100 
   100                 CompletableFuture<HttpResponse<Object>> response = client
   102                 CompletableFuture<HttpResponse<Object>> response = client
   101                     .sendAsync(req, BodyHandlers.replacing(null))
   103                     .sendAsync(req, BodyHandlers.replacing(null))
   102                     .whenComplete((HttpResponse<Object> r, Throwable t) -> {
   104                     .whenComplete((HttpResponse<Object> r, Throwable t) -> {
   103                         Throwable cause = null;
   105                         Throwable cause = null;
   104                         if (r != null) {
   106                         if (r != null) {
   105                             out.println("Unexpected response: " + r);
   107                             out.println("Unexpected response for r" + n + ": " + r);
   106                             cause = new RuntimeException("Unexpected response");
   108                             cause = new RuntimeException("Unexpected response for r" + n);
   107                             error = true;
   109                             error = true;
   108                         }
   110                         }
   109                         if (t != null) {
   111                         if (t != null) {
   110                             if (!(t.getCause() instanceof HttpTimeoutException)) {
   112                             if (!(t.getCause() instanceof HttpTimeoutException)) {
   111                                 out.println("Wrong exception type:" + t.toString());
   113                                 out.println("Wrong exception type for r" + n + ":" + t.toString());
   112                                 Throwable c = t.getCause() == null ? t : t.getCause();
   114                                 Throwable c = t.getCause() == null ? t : t.getCause();
   113                                 c.printStackTrace();
   115                                 c.printStackTrace();
   114                                 cause = c;
   116                                 cause = c;
   115                                 error = true;
   117                                 error = true;
   116                             } else {
   118                             } else {
   117                                 out.println("Caught expected timeout: " + t.getCause());
   119                                 out.println("Caught expected timeout for r" + n +": " + t.getCause());
   118                             }
   120                             }
   119                         }
   121                         }
   120                         if (t == null && r == null) {
   122                         if (t == null && r == null) {
   121                             out.println("Both response and throwable are null!");
   123                             out.println("Both response and throwable are null for r" + n + "!");
   122                             cause = new RuntimeException("Both response and throwable are null!");
   124                             cause = new RuntimeException("Both response and throwable are null for r"
       
   125                                     + n + "!");
   123                             error = true;
   126                             error = true;
   124                         }
   127                         }
   125                         queue.add(HttpResult.of(req,cause));
   128                         queue.add(HttpResult.of(req,cause));
   126                     });
   129                     });
   127             }
   130             }
   132             if (error)
   135             if (error)
   133                 throw new RuntimeException("Failed. Check output");
   136                 throw new RuntimeException("Failed. Check output");
   134 
   137 
   135             // Repeat blocking in separate threads. Use queue to wait.
   138             // Repeat blocking in separate threads. Use queue to wait.
   136             out.println("--- TESTING Sync");
   139             out.println("--- TESTING Sync");
       
   140             System.err.println("================= TESTING Sync =====================");
   137 
   141 
   138             // For running blocking response tasks
   142             // For running blocking response tasks
   139             ExecutorService executor = Executors.newCachedThreadPool();
   143             ExecutorService executor = Executors.newCachedThreadPool();
   140 
   144 
   141             for (int i = 0; i < TIMEOUTS.length; i++) {
   145             for (int i = 0; i < TIMEOUTS.length; i++) {
       
   146                 final int n = i;
       
   147                 URI uri = new URI(u.toString()+"/sync/r" + n);
   142                 requests[i] = HttpRequest.newBuilder(uri)
   148                 requests[i] = HttpRequest.newBuilder(uri)
   143                                          .timeout(Duration.ofMillis(TIMEOUTS[i]))
   149                                          .timeout(Duration.ofMillis(TIMEOUTS[i]))
   144                                          .GET()
   150                                          .GET()
   145                                          .build();
   151                                          .build();
   146 
   152 
   147                 final HttpRequest req = requests[i];
   153                 final HttpRequest req = requests[i];
   148                 executor.execute(() -> {
   154                 executor.execute(() -> {
   149                     Throwable cause = null;
   155                     Throwable cause = null;
   150                     try {
   156                     try {
   151                         client.send(req, BodyHandlers.replacing(null));
   157                         HttpResponse<?> r = client.send(req, BodyHandlers.replacing(null));
       
   158                         out.println("Unexpected success for r" + n +": " + r);
   152                     } catch (HttpTimeoutException e) {
   159                     } catch (HttpTimeoutException e) {
   153                         out.println("Caught expected timeout: " + e);
   160                         out.println("Caught expected timeout for r" + n +": " + e);
   154                     } catch (Throwable ee) {
   161                     } catch (Throwable ee) {
   155                         Throwable c = ee.getCause() == null ? ee : ee.getCause();
   162                         Throwable c = ee.getCause() == null ? ee : ee.getCause();
       
   163                         out.println("Unexpected exception for r" + n + ": " + c);
   156                         c.printStackTrace();
   164                         c.printStackTrace();
   157                         cause = c;
   165                         cause = c;
   158                         error = true;
   166                         error = true;
   159                     } finally {
   167                     } finally {
   160                         queue.offer(HttpResult.of(req, cause));
   168                         queue.offer(HttpResult.of(req, cause));