< prev index next >

test/jdk/java/net/httpclient/TimeoutBasic.java

Print this page


   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 }
   1 /*
   2  * Copyright (c) 2015, 2017, 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 jdk.testlibrary.SimpleSSLContext;
  32 
  33 import javax.net.ServerSocketFactory;
  34 import javax.net.ssl.SSLContext;
  35 import javax.net.ssl.SSLServerSocketFactory;
  36 import java.time.Duration;
  37 import java.util.Arrays;
  38 import java.util.List;
  39 import java.util.concurrent.CompletionException;
  40 import java.util.function.Function;
  41 
  42 import static java.lang.System.out;
  43 import static jdk.incubator.http.HttpResponse.BodyHandler.discard;
  44 
  45 /**
  46  * @test
  47  * @library /lib/testlibrary
  48  * @build jdk.testlibrary.SimpleSSLContext
  49  * @summary Basic tests for response timeouts
  50  * @run main/othervm TimeoutBasic

  51  */
  52 
  53 public class TimeoutBasic {
  54 
  55     static List<Duration> TIMEOUTS = List.of(Duration.ofSeconds(1),
  56                                              Duration.ofMillis(100),
  57                                              Duration.ofNanos(99),
  58                                              Duration.ofNanos(1));
  59 
  60     static final List<Function<HttpRequest.Builder, HttpRequest.Builder>> METHODS =
  61             Arrays.asList(HttpRequest.Builder::GET,
  62                           TimeoutBasic::DELETE,
  63                           TimeoutBasic::PUT,
  64                           TimeoutBasic::POST,
  65                           null);
  66 
  67     static final List<HttpClient.Version> VERSIONS =
  68             Arrays.asList(HttpClient.Version.HTTP_2, HttpClient.Version.HTTP_1_1, null);
  69 
  70     static final List<String> SCHEMES = List.of("https", "http");
  71 
  72     static {
  73         try {
  74             SSLContext.setDefault(new SimpleSSLContext().get());
  75         } catch (IOException x) {
  76             throw new ExceptionInInitializerError(x);
  77         }
  78     }
  79 
  80     public static void main(String[] args) throws Exception {
  81         for (Function<HttpRequest.Builder, HttpRequest.Builder> m : METHODS) {
  82             for (HttpClient.Version version : List.of(HttpClient.Version.HTTP_1_1)) {
  83                 for (HttpClient.Version reqVersion : VERSIONS) {
  84                     for (String scheme : SCHEMES) {
  85                         ServerSocketFactory ssf;
  86                         if (scheme.equalsIgnoreCase("https")) {
  87                             ssf = SSLServerSocketFactory.getDefault();
  88                         } else {
  89                             ssf = ServerSocketFactory.getDefault();
  90                         }
  91                         test(version, reqVersion, scheme, m, ssf);
  92                     }
  93                 }
  94             }
  95         }
  96     }
  97 
  98     static HttpRequest.Builder DELETE(HttpRequest.Builder builder) {
  99         HttpRequest.BodyPublisher noBody = HttpRequest.BodyPublisher.noBody();
 100         return builder.DELETE(noBody);
 101     }
 102 
 103     static HttpRequest.Builder PUT(HttpRequest.Builder builder) {
 104         HttpRequest.BodyPublisher noBody = HttpRequest.BodyPublisher.noBody();
 105         return builder.PUT(noBody);
 106     }
 107 
 108     static HttpRequest.Builder POST(HttpRequest.Builder builder) {
 109         HttpRequest.BodyPublisher noBody = HttpRequest.BodyPublisher.noBody();
 110         return builder.POST(noBody);
 111     }
 112 
 113     static HttpRequest newRequest(URI uri,
 114                                   Duration duration,
 115                                   HttpClient.Version reqVersion,
 116                                   Function<HttpRequest.Builder, HttpRequest.Builder> method) {
 117         HttpRequest.Builder reqBuilder = HttpRequest.newBuilder(uri)
 118                 .timeout(duration);
 119         if (method != null) reqBuilder = method.apply(reqBuilder);
 120         if (reqVersion != null) reqBuilder = reqBuilder.version(reqVersion);
 121         HttpRequest request = reqBuilder.build();
 122         if (duration.compareTo(Duration.ofSeconds(1)) >= 0) {
 123             if (method == null || !request.method().equalsIgnoreCase("get")) {
 124                 out.println("Skipping " + duration + " for " + request.method());
 125                 return null;
 126             }
 127         }
 128         return request;
 129     }
 130 
 131     public static void test(HttpClient.Version version,
 132                             HttpClient.Version reqVersion,
 133                             String scheme,
 134                             Function<HttpRequest.Builder, HttpRequest.Builder> method,
 135                             ServerSocketFactory ssf)
 136             throws Exception
 137     {
 138         HttpClient.Builder builder = HttpClient.newBuilder()
 139                 .proxy(HttpClient.Builder.NO_PROXY);
 140         if (version != null) builder.version(version);
 141         HttpClient client = builder.build();
 142         out.printf("%ntest(version=%s, reqVersion=%s, scheme=%s)%n", version, reqVersion, scheme);
 143         try (ServerSocket ss = ssf.createServerSocket(0, 20)) {
 144             int port = ss.getLocalPort();
 145             URI uri = new URI(scheme +"://127.0.0.1:" + port + "/");
 146 
 147             out.println("--- TESTING Async");
 148             int count = 0;
 149             for (Duration duration : TIMEOUTS) {
 150                 out.println("  with duration of " + duration);
 151                 HttpRequest request = newRequest(uri, duration, reqVersion, method);
 152                 if (request == null) continue;
 153                 count++;
 154                 try {
 155                     HttpResponse<?> resp = client.sendAsync(request, discard(null)).join();
 156                     throw new RuntimeException("Unexpected response: " + resp.statusCode());
 157                 } catch (CompletionException e) {
 158                     if (!(e.getCause() instanceof HttpTimeoutException)) {
 159                         throw new RuntimeException("Unexpected exception: " + e.getCause());
 160                     } else {
 161                         out.println("Caught expected timeout: " + e.getCause());
 162                     }
 163                 }
 164             }
 165             assert count >= TIMEOUTS.size() -1;
 166 
 167             out.println("--- TESTING Sync");
 168             count = 0;
 169             for (Duration duration : TIMEOUTS) {
 170                 out.println("  with duration of " + duration);
 171                 HttpRequest request = newRequest(uri, duration, reqVersion, method);
 172                 if (request == null) continue;
 173                 count++;

 174                 try {
 175                     client.send(request, discard(null));
 176                 } catch (HttpTimeoutException e) {
 177                     out.println("Caught expected timeout: " + e);
 178                 }
 179             }
 180             assert count >= TIMEOUTS.size() -1;
 181 
 182         }
 183     }
 184 }
< prev index next >