test/jdk/java/net/httpclient/TimeoutBasic.java
changeset 48083 b1c1b4ef4be2
parent 47216 71c04702a3d5
child 49765 ee6f7a61f3a5
child 55973 4d9b002587db
equal deleted inserted replaced
48081:89829dd3cc54 48083:b1c1b4ef4be2
     1 /*
     1 /*
     2  * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
     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.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    26 import java.net.URI;
    26 import java.net.URI;
    27 import jdk.incubator.http.HttpClient;
    27 import jdk.incubator.http.HttpClient;
    28 import jdk.incubator.http.HttpRequest;
    28 import jdk.incubator.http.HttpRequest;
    29 import jdk.incubator.http.HttpResponse;
    29 import jdk.incubator.http.HttpResponse;
    30 import jdk.incubator.http.HttpTimeoutException;
    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;
    31 import java.time.Duration;
    36 import java.time.Duration;
       
    37 import java.util.Arrays;
    32 import java.util.List;
    38 import java.util.List;
    33 import java.util.concurrent.*;
    39 import java.util.concurrent.CompletionException;
       
    40 import java.util.function.Function;
    34 
    41 
    35 import static java.lang.System.out;
    42 import static java.lang.System.out;
    36 import static jdk.incubator.http.HttpResponse.BodyHandler.discard;
    43 import static jdk.incubator.http.HttpResponse.BodyHandler.discard;
    37 
    44 
    38 /**
    45 /**
    39  * @test
    46  * @test
       
    47  * @library /lib/testlibrary
       
    48  * @build jdk.testlibrary.SimpleSSLContext
    40  * @summary Basic tests for response timeouts
    49  * @summary Basic tests for response timeouts
    41  * @run main/othervm TimeoutBasic
    50  * @run main/othervm TimeoutBasic
    42  * @ignore
       
    43  */
    51  */
    44 
    52 
    45 public class TimeoutBasic {
    53 public class TimeoutBasic {
    46 
    54 
    47     static List<Duration> TIMEOUTS = List.of(/*Duration.ofSeconds(1),
    55     static List<Duration> TIMEOUTS = List.of(Duration.ofSeconds(1),
    48                                              Duration.ofMillis(100),*/
    56                                              Duration.ofMillis(100),
    49                                              Duration.ofNanos(99)
    57                                              Duration.ofNanos(99),
    50                                             /* Duration.ofNanos(1)*/);
    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     }
    51 
    79 
    52     public static void main(String[] args) throws Exception {
    80     public static void main(String[] args) throws Exception {
    53         HttpClient client = HttpClient.newHttpClient();
    81         for (Function<HttpRequest.Builder, HttpRequest.Builder> m : METHODS) {
    54         try (ServerSocket ss = new ServerSocket(0, 20)) {
    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)) {
    55             int port = ss.getLocalPort();
   144             int port = ss.getLocalPort();
    56             URI uri = new URI("http://127.0.0.1:" + port + "/");
   145             URI uri = new URI(scheme +"://127.0.0.1:" + port + "/");
    57 
   146 
    58 //            out.println("--- TESTING Async");
   147             out.println("--- TESTING Async");
    59 //            for (Duration duration : TIMEOUTS) {
   148             int count = 0;
    60 //                out.println("  with duration of " + duration);
   149             for (Duration duration : TIMEOUTS) {
    61 //                HttpRequest request = HttpRequest.newBuilder(uri)
   150                 out.println("  with duration of " + duration);
    62 //                                                 .timeout(duration)
   151                 HttpRequest request = newRequest(uri, duration, reqVersion, method);
    63 //                                                 .GET().build();
   152                 if (request == null) continue;
    64 //                try {
   153                 count++;
    65 //                    HttpResponse<?> resp = client.sendAsync(request, discard(null)).join();
   154                 try {
    66 //                    throw new RuntimeException("Unexpected response: " + resp.statusCode());
   155                     HttpResponse<?> resp = client.sendAsync(request, discard(null)).join();
    67 //                } catch (CompletionException e) {
   156                     throw new RuntimeException("Unexpected response: " + resp.statusCode());
    68 //                    if (!(e.getCause() instanceof HttpTimeoutException)) {
   157                 } catch (CompletionException e) {
    69 //                        throw new RuntimeException("Unexpected exception: " + e.getCause());
   158                     if (!(e.getCause() instanceof HttpTimeoutException)) {
    70 //                    } else {
   159                         throw new RuntimeException("Unexpected exception: " + e.getCause());
    71 //                        out.println("Caught expected timeout: " + e.getCause());
   160                     } else {
    72 //                    }
   161                         out.println("Caught expected timeout: " + e.getCause());
    73 //                }
   162                     }
    74 //            }
   163                 }
       
   164             }
       
   165             assert count >= TIMEOUTS.size() -1;
    75 
   166 
    76             out.println("--- TESTING Sync");
   167             out.println("--- TESTING Sync");
       
   168             count = 0;
    77             for (Duration duration : TIMEOUTS) {
   169             for (Duration duration : TIMEOUTS) {
    78                 out.println("  with duration of " + duration);
   170                 out.println("  with duration of " + duration);
    79                 HttpRequest request = HttpRequest.newBuilder(uri)
   171                 HttpRequest request = newRequest(uri, duration, reqVersion, method);
    80                                                  .timeout(duration)
   172                 if (request == null) continue;
    81                                                  .GET()
   173                 count++;
    82                                                  .build();
       
    83                 try {
   174                 try {
    84                     client.send(request, discard(null));
   175                     client.send(request, discard(null));
    85                 } catch (HttpTimeoutException e) {
   176                 } catch (HttpTimeoutException e) {
    86                     out.println("Caught expected timeout: " + e);
   177                     out.println("Caught expected timeout: " + e);
    87                 }
   178                 }
    88             }
   179             }
    89         } finally {
   180             assert count >= TIMEOUTS.size() -1;
    90             ((ExecutorService) client.executor()).shutdownNow();
   181 
    91         }
   182         }
    92     }
   183     }
    93 }
   184 }