1 /*
   2  * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.io.IOException;
  25 import java.net.ServerSocket;
  26 import java.net.URI;
  27 import jdk.incubator.http.HttpClient;
  28 import jdk.incubator.http.HttpRequest;
  29 import jdk.incubator.http.HttpResponse;
  30 import jdk.incubator.http.HttpTimeoutException;
  31 import java.time.Duration;
  32 import java.util.List;
  33 import java.util.concurrent.*;
  34 
  35 import static java.lang.System.out;
  36 import static jdk.incubator.http.HttpResponse.BodyHandler.discard;
  37 
  38 /**
  39  * @test
  40  * @summary Basic tests for response timeouts
  41  * @run main/othervm TimeoutBasic
  42  * @ignore
  43  */
  44 
  45 public class TimeoutBasic {
  46 
  47     static List<Duration> TIMEOUTS = List.of(/*Duration.ofSeconds(1),
  48                                              Duration.ofMillis(100),*/
  49                                              Duration.ofNanos(99)
  50                                             /* Duration.ofNanos(1)*/);
  51 
  52     public static void main(String[] args) throws Exception {
  53         HttpClient client = HttpClient.newHttpClient();
  54         try (ServerSocket ss = new ServerSocket(0, 20)) {
  55             int port = ss.getLocalPort();
  56             URI uri = new URI("http://127.0.0.1:" + port + "/");
  57 
  58 //            out.println("--- TESTING Async");
  59 //            for (Duration duration : TIMEOUTS) {
  60 //                out.println("  with duration of " + duration);
  61 //                HttpRequest request = HttpRequest.newBuilder(uri)
  62 //                                                 .timeout(duration)
  63 //                                                 .GET().build();
  64 //                try {
  65 //                    HttpResponse<?> resp = client.sendAsync(request, discard(null)).join();
  66 //                    throw new RuntimeException("Unexpected response: " + resp.statusCode());
  67 //                } catch (CompletionException e) {
  68 //                    if (!(e.getCause() instanceof HttpTimeoutException)) {
  69 //                        throw new RuntimeException("Unexpected exception: " + e.getCause());
  70 //                    } else {
  71 //                        out.println("Caught expected timeout: " + e.getCause());
  72 //                    }
  73 //                }
  74 //            }
  75 
  76             out.println("--- TESTING Sync");
  77             for (Duration duration : TIMEOUTS) {
  78                 out.println("  with duration of " + duration);
  79                 HttpRequest request = HttpRequest.newBuilder(uri)
  80                                                  .timeout(duration)
  81                                                  .GET()
  82                                                  .build();
  83                 try {
  84                     client.send(request, discard(null));
  85                 } catch (HttpTimeoutException e) {
  86                     out.println("Caught expected timeout: " + e);
  87                 }
  88             }
  89         } finally {
  90             ((ExecutorService) client.executor()).shutdownNow();
  91         }
  92     }
  93 }