test/jdk/java/net/httpclient/FlowAdapterSubscriberTest.java
branchhttp-client-branch
changeset 56166 56c52d6417d1
parent 56164 4db4bec0e5bb
child 56167 96fa4f49a9ff
equal deleted inserted replaced
56165:8a6065d830b9 56166:56c52d6417d1
    70  *          java.logging
    70  *          java.logging
    71  *          jdk.httpserver
    71  *          jdk.httpserver
    72  * @library /lib/testlibrary http2/server
    72  * @library /lib/testlibrary http2/server
    73  * @build Http2TestServer
    73  * @build Http2TestServer
    74  * @build jdk.testlibrary.SimpleSSLContext
    74  * @build jdk.testlibrary.SimpleSSLContext
    75  * @run testng/othervm FlowAdapterSubscriberTest
    75  * @run testng/othervm -Djdk.internal.httpclient.debug=true FlowAdapterSubscriberTest
    76  */
    76  */
    77 
    77 
    78 public class FlowAdapterSubscriberTest {
    78 public class FlowAdapterSubscriberTest {
    79 
    79 
    80     SSLContext sslContext;
    80     SSLContext sslContext;
    84     Http2TestServer https2TestServer;  // HTTP/2 ( h2  )
    84     Http2TestServer https2TestServer;  // HTTP/2 ( h2  )
    85     String httpURI;
    85     String httpURI;
    86     String httpsURI;
    86     String httpsURI;
    87     String http2URI;
    87     String http2URI;
    88     String https2URI;
    88     String https2URI;
       
    89     static final long start = System.nanoTime();
       
    90     public static String now() {
       
    91         long now = System.nanoTime() - start;
       
    92         long secs = now / 1000_000_000;
       
    93         long mill = (now % 1000_000_000) / 1000_000;
       
    94         long nan = now % 1000_000;
       
    95         return String.format("[%d s, %d ms, %d ns] ", secs, mill, nan);
       
    96     }
    89 
    97 
    90     @DataProvider(name = "uris")
    98     @DataProvider(name = "uris")
    91     public Object[][] variants() {
    99     public Object[][] variants() {
    92         return new Object[][]{
   100         return new Object[][]{
    93                 { httpURI   },
   101                 { httpURI   },
    99 
   107 
   100     static final Class<NullPointerException> NPE = NullPointerException.class;
   108     static final Class<NullPointerException> NPE = NullPointerException.class;
   101 
   109 
   102     @Test
   110     @Test
   103     public void testNull() {
   111     public void testNull() {
       
   112         System.out.printf(now() + "testNull() starting%n");
   104         assertThrows(NPE, () -> BodyHandler.fromSubscriber(null));
   113         assertThrows(NPE, () -> BodyHandler.fromSubscriber(null));
   105         assertThrows(NPE, () -> BodyHandler.fromSubscriber(null, Function.identity()));
   114         assertThrows(NPE, () -> BodyHandler.fromSubscriber(null, Function.identity()));
   106         assertThrows(NPE, () -> BodyHandler.fromSubscriber(new ListSubscriber(), null));
   115         assertThrows(NPE, () -> BodyHandler.fromSubscriber(new ListSubscriber(), null));
   107         assertThrows(NPE, () -> BodyHandler.fromSubscriber(null, null));
   116         assertThrows(NPE, () -> BodyHandler.fromSubscriber(null, null));
   108 
   117 
   119 
   128 
   120     // List<ByteBuffer>
   129     // List<ByteBuffer>
   121 
   130 
   122     @Test(dataProvider = "uris")
   131     @Test(dataProvider = "uris")
   123     void testListWithFinisher(String url) {
   132     void testListWithFinisher(String url) {
       
   133         System.out.printf(now() + "testListWithFinisher(%s) starting%n", url);
   124         HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
   134         HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
   125         HttpRequest request = HttpRequest.newBuilder(URI.create(url))
   135         HttpRequest request = HttpRequest.newBuilder(URI.create(url))
   126                 .POST(fromString("May the luck of the Irish be with you!")).build();
   136                 .POST(fromString("May the luck of the Irish be with you!")).build();
   127 
   137 
   128         ListSubscriber subscriber = new ListSubscriber();
   138         ListSubscriber subscriber = new ListSubscriber();
   134         assertEquals(text, "May the luck of the Irish be with you!");
   144         assertEquals(text, "May the luck of the Irish be with you!");
   135     }
   145     }
   136 
   146 
   137     @Test(dataProvider = "uris")
   147     @Test(dataProvider = "uris")
   138     void testListWithoutFinisher(String url) {
   148     void testListWithoutFinisher(String url) {
       
   149         System.out.printf(now() + "testListWithoutFinisher(%s) starting%n", url);
   139         HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
   150         HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
   140         HttpRequest request = HttpRequest.newBuilder(URI.create(url))
   151         HttpRequest request = HttpRequest.newBuilder(URI.create(url))
   141                 .POST(fromString("May the luck of the Irish be with you!")).build();
   152                 .POST(fromString("May the luck of the Irish be with you!")).build();
   142 
   153 
   143         ListSubscriber subscriber = new ListSubscriber();
   154         ListSubscriber subscriber = new ListSubscriber();
   149         assertEquals(text, "May the luck of the Irish be with you!");
   160         assertEquals(text, "May the luck of the Irish be with you!");
   150     }
   161     }
   151 
   162 
   152     @Test(dataProvider = "uris")
   163     @Test(dataProvider = "uris")
   153     void testListWithFinisherBlocking(String url) throws Exception {
   164     void testListWithFinisherBlocking(String url) throws Exception {
       
   165         System.out.printf(now() + "testListWithFinisherBlocking(%s) starting%n", url);
   154         HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
   166         HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
   155         HttpRequest request = HttpRequest.newBuilder(URI.create(url))
   167         HttpRequest request = HttpRequest.newBuilder(URI.create(url))
   156                 .POST(fromString("May the luck of the Irish be with you!")).build();
   168                 .POST(fromString("May the luck of the Irish be with you!")).build();
   157 
   169 
   158         ListSubscriber subscriber = new ListSubscriber();
   170         ListSubscriber subscriber = new ListSubscriber();
   164         assertEquals(text, "May the luck of the Irish be with you!");
   176         assertEquals(text, "May the luck of the Irish be with you!");
   165     }
   177     }
   166 
   178 
   167     @Test(dataProvider = "uris")
   179     @Test(dataProvider = "uris")
   168     void testListWithoutFinisherBlocking(String url) throws Exception {
   180     void testListWithoutFinisherBlocking(String url) throws Exception {
       
   181         System.out.printf(now() + "testListWithoutFinisherBlocking(%s) starting%n", url);
   169         HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
   182         HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
   170         HttpRequest request = HttpRequest.newBuilder(URI.create(url))
   183         HttpRequest request = HttpRequest.newBuilder(URI.create(url))
   171                 .POST(fromString("May the luck of the Irish be with you!")).build();
   184                 .POST(fromString("May the luck of the Irish be with you!")).build();
   172 
   185 
   173         ListSubscriber subscriber = new ListSubscriber();
   186         ListSubscriber subscriber = new ListSubscriber();
   181 
   194 
   182     // Collection<ByteBuffer>
   195     // Collection<ByteBuffer>
   183 
   196 
   184     @Test(dataProvider = "uris")
   197     @Test(dataProvider = "uris")
   185     void testCollectionWithFinisher(String url) {
   198     void testCollectionWithFinisher(String url) {
       
   199         System.out.printf(now() + "testCollectionWithFinisher(%s) starting%n", url);
   186         HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
   200         HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
   187         HttpRequest request = HttpRequest.newBuilder(URI.create(url))
   201         HttpRequest request = HttpRequest.newBuilder(URI.create(url))
   188                 .POST(fromString("What's the craic?")).build();
   202                 .POST(fromString("What's the craic?")).build();
   189 
   203 
   190         CollectionSubscriber subscriber = new CollectionSubscriber();
   204         CollectionSubscriber subscriber = new CollectionSubscriber();
   196         assertEquals(text, "What's the craic?");
   210         assertEquals(text, "What's the craic?");
   197     }
   211     }
   198 
   212 
   199     @Test(dataProvider = "uris")
   213     @Test(dataProvider = "uris")
   200     void testCollectionWithoutFinisher(String url) {
   214     void testCollectionWithoutFinisher(String url) {
       
   215         System.out.printf(now() + "testCollectionWithoutFinisher(%s) starting%n", url);
   201         HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
   216         HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
   202         HttpRequest request = HttpRequest.newBuilder(URI.create(url))
   217         HttpRequest request = HttpRequest.newBuilder(URI.create(url))
   203                 .POST(fromString("What's the craic?")).build();
   218                 .POST(fromString("What's the craic?")).build();
   204 
   219 
   205         CollectionSubscriber subscriber = new CollectionSubscriber();
   220         CollectionSubscriber subscriber = new CollectionSubscriber();
   211         assertEquals(text, "What's the craic?");
   226         assertEquals(text, "What's the craic?");
   212     }
   227     }
   213 
   228 
   214     @Test(dataProvider = "uris")
   229     @Test(dataProvider = "uris")
   215     void testCollectionWithFinisherBlocking(String url) throws Exception {
   230     void testCollectionWithFinisherBlocking(String url) throws Exception {
       
   231         System.out.printf(now() + "testCollectionWithFinisherBlocking(%s) starting%n", url);
   216         HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
   232         HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
   217         HttpRequest request = HttpRequest.newBuilder(URI.create(url))
   233         HttpRequest request = HttpRequest.newBuilder(URI.create(url))
   218                 .POST(fromString("What's the craic?")).build();
   234                 .POST(fromString("What's the craic?")).build();
   219 
   235 
   220         CollectionSubscriber subscriber = new CollectionSubscriber();
   236         CollectionSubscriber subscriber = new CollectionSubscriber();
   226         assertEquals(text, "What's the craic?");
   242         assertEquals(text, "What's the craic?");
   227     }
   243     }
   228 
   244 
   229     @Test(dataProvider = "uris")
   245     @Test(dataProvider = "uris")
   230     void testCollectionWithoutFinisheBlocking(String url) throws Exception {
   246     void testCollectionWithoutFinisheBlocking(String url) throws Exception {
       
   247         System.out.printf(now() + "testCollectionWithoutFinisheBlocking(%s) starting%n", url);
   231         HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
   248         HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
   232         HttpRequest request = HttpRequest.newBuilder(URI.create(url))
   249         HttpRequest request = HttpRequest.newBuilder(URI.create(url))
   233                 .POST(fromString("What's the craic?")).build();
   250                 .POST(fromString("What's the craic?")).build();
   234 
   251 
   235         CollectionSubscriber subscriber = new CollectionSubscriber();
   252         CollectionSubscriber subscriber = new CollectionSubscriber();
   243 
   260 
   244     // Iterable<ByteBuffer>
   261     // Iterable<ByteBuffer>
   245 
   262 
   246     @Test(dataProvider = "uris")
   263     @Test(dataProvider = "uris")
   247     void testIterableWithFinisher(String url) {
   264     void testIterableWithFinisher(String url) {
       
   265         System.out.printf(now() + "testIterableWithFinisher(%s) starting%n", url);
   248         HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
   266         HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
   249         HttpRequest request = HttpRequest.newBuilder(URI.create(url))
   267         HttpRequest request = HttpRequest.newBuilder(URI.create(url))
   250                 .POST(fromString("We're sucking diesel now!")).build();
   268                 .POST(fromString("We're sucking diesel now!")).build();
   251 
   269 
   252         IterableSubscriber subscriber = new IterableSubscriber();
   270         IterableSubscriber subscriber = new IterableSubscriber();
   258         assertEquals(text, "We're sucking diesel now!");
   276         assertEquals(text, "We're sucking diesel now!");
   259     }
   277     }
   260 
   278 
   261     @Test(dataProvider = "uris")
   279     @Test(dataProvider = "uris")
   262     void testIterableWithoutFinisher(String url) {
   280     void testIterableWithoutFinisher(String url) {
       
   281         System.out.printf(now() + "testIterableWithoutFinisher(%s) starting%n", url);
   263         HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
   282         HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
   264         HttpRequest request = HttpRequest.newBuilder(URI.create(url))
   283         HttpRequest request = HttpRequest.newBuilder(URI.create(url))
   265                 .POST(fromString("We're sucking diesel now!")).build();
   284                 .POST(fromString("We're sucking diesel now!")).build();
   266 
   285 
   267         IterableSubscriber subscriber = new IterableSubscriber();
   286         IterableSubscriber subscriber = new IterableSubscriber();
   273         assertEquals(text, "We're sucking diesel now!");
   292         assertEquals(text, "We're sucking diesel now!");
   274     }
   293     }
   275 
   294 
   276     @Test(dataProvider = "uris")
   295     @Test(dataProvider = "uris")
   277     void testIterableWithFinisherBlocking(String url) throws Exception {
   296     void testIterableWithFinisherBlocking(String url) throws Exception {
       
   297         System.out.printf(now() + "testIterableWithFinisherBlocking(%s) starting%n", url);
   278         HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
   298         HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
   279         HttpRequest request = HttpRequest.newBuilder(URI.create(url))
   299         HttpRequest request = HttpRequest.newBuilder(URI.create(url))
   280                 .POST(fromString("We're sucking diesel now!")).build();
   300                 .POST(fromString("We're sucking diesel now!")).build();
   281 
   301 
   282         IterableSubscriber subscriber = new IterableSubscriber();
   302         IterableSubscriber subscriber = new IterableSubscriber();
   288         assertEquals(text, "We're sucking diesel now!");
   308         assertEquals(text, "We're sucking diesel now!");
   289     }
   309     }
   290 
   310 
   291     @Test(dataProvider = "uris")
   311     @Test(dataProvider = "uris")
   292     void testIterableWithoutFinisherBlocking(String url) throws Exception {
   312     void testIterableWithoutFinisherBlocking(String url) throws Exception {
       
   313         System.out.printf(now() + "testIterableWithoutFinisherBlocking(%s) starting%n", url);
   293         HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
   314         HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
   294         HttpRequest request = HttpRequest.newBuilder(URI.create(url))
   315         HttpRequest request = HttpRequest.newBuilder(URI.create(url))
   295                 .POST(fromString("We're sucking diesel now!")).build();
   316                 .POST(fromString("We're sucking diesel now!")).build();
   296 
   317 
   297         IterableSubscriber subscriber = new IterableSubscriber();
   318         IterableSubscriber subscriber = new IterableSubscriber();
   305 
   326 
   306     // Subscriber<Object>
   327     // Subscriber<Object>
   307 
   328 
   308     @Test(dataProvider = "uris")
   329     @Test(dataProvider = "uris")
   309     void testObjectWithFinisher(String url) {
   330     void testObjectWithFinisher(String url) {
       
   331         System.out.printf(now() + "testObjectWithFinisher(%s) starting%n", url);
   310         HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
   332         HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
   311         HttpRequest request = HttpRequest.newBuilder(URI.create(url))
   333         HttpRequest request = HttpRequest.newBuilder(URI.create(url))
   312                 .POST(fromString("May the wind always be at your back.")).build();
   334                 .POST(fromString("May the wind always be at your back.")).build();
   313 
   335 
   314         ObjectSubscriber subscriber = new ObjectSubscriber();
   336         ObjectSubscriber subscriber = new ObjectSubscriber();
   320         assertTrue(text.length() != 0);  // what else can be asserted!
   342         assertTrue(text.length() != 0);  // what else can be asserted!
   321     }
   343     }
   322 
   344 
   323     @Test(dataProvider = "uris")
   345     @Test(dataProvider = "uris")
   324     void testObjectWithoutFinisher(String url) {
   346     void testObjectWithoutFinisher(String url) {
       
   347         System.out.printf(now() + "testObjectWithoutFinisher(%s) starting%n", url);
   325         HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
   348         HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
   326         HttpRequest request = HttpRequest.newBuilder(URI.create(url))
   349         HttpRequest request = HttpRequest.newBuilder(URI.create(url))
   327                 .POST(fromString("May the wind always be at your back.")).build();
   350                 .POST(fromString("May the wind always be at your back.")).build();
   328 
   351 
   329         ObjectSubscriber subscriber = new ObjectSubscriber();
   352         ObjectSubscriber subscriber = new ObjectSubscriber();
   335         assertTrue(text.length() != 0);  // what else can be asserted!
   358         assertTrue(text.length() != 0);  // what else can be asserted!
   336     }
   359     }
   337 
   360 
   338     @Test(dataProvider = "uris")
   361     @Test(dataProvider = "uris")
   339     void testObjectWithFinisherBlocking(String url) throws Exception {
   362     void testObjectWithFinisherBlocking(String url) throws Exception {
       
   363         System.out.printf(now() + "testObjectWithFinisherBlocking(%s) starting%n", url);
   340         HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
   364         HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
   341         HttpRequest request = HttpRequest.newBuilder(URI.create(url))
   365         HttpRequest request = HttpRequest.newBuilder(URI.create(url))
   342                 .POST(fromString("May the wind always be at your back.")).build();
   366                 .POST(fromString("May the wind always be at your back.")).build();
   343 
   367 
   344         ObjectSubscriber subscriber = new ObjectSubscriber();
   368         ObjectSubscriber subscriber = new ObjectSubscriber();
   350         assertTrue(text.length() != 0);  // what else can be asserted!
   374         assertTrue(text.length() != 0);  // what else can be asserted!
   351     }
   375     }
   352 
   376 
   353     @Test(dataProvider = "uris")
   377     @Test(dataProvider = "uris")
   354     void testObjectWithoutFinisherBlocking(String url) throws Exception {
   378     void testObjectWithoutFinisherBlocking(String url) throws Exception {
       
   379         System.out.printf(now() + "testObjectWithoutFinisherBlocking(%s) starting%n", url);
   355         HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
   380         HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
   356         HttpRequest request = HttpRequest.newBuilder(URI.create(url))
   381         HttpRequest request = HttpRequest.newBuilder(URI.create(url))
   357                 .POST(fromString("May the wind always be at your back.")).build();
   382                 .POST(fromString("May the wind always be at your back.")).build();
   358 
   383 
   359         ObjectSubscriber subscriber = new ObjectSubscriber();
   384         ObjectSubscriber subscriber = new ObjectSubscriber();
   368 
   393 
   369     // -- mapping using convenience handlers
   394     // -- mapping using convenience handlers
   370 
   395 
   371     @Test(dataProvider = "uris")
   396     @Test(dataProvider = "uris")
   372     void mappingFromByteArray(String url) throws Exception {
   397     void mappingFromByteArray(String url) throws Exception {
       
   398         System.out.printf(now() + "mappingFromByteArray(%s) starting%n", url);
   373         HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
   399         HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
   374         HttpRequest request = HttpRequest.newBuilder(URI.create(url))
   400         HttpRequest request = HttpRequest.newBuilder(URI.create(url))
   375                 .POST(fromString("We're sucking diesel now!")).build();
   401                 .POST(fromString("We're sucking diesel now!")).build();
   376 
   402 
   377         client.sendAsync(request, BodyHandler.fromSubscriber(asByteArray(),
   403         client.sendAsync(request, BodyHandler.fromSubscriber(asByteArray(),
   382                 .join();
   408                 .join();
   383     }
   409     }
   384 
   410 
   385     @Test(dataProvider = "uris")
   411     @Test(dataProvider = "uris")
   386     void mappingFromInputStream(String url) throws Exception {
   412     void mappingFromInputStream(String url) throws Exception {
       
   413         System.out.printf(now() + "mappingFromInputStream(%s) starting%n", url);
   387         HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
   414         HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
   388         HttpRequest request = HttpRequest.newBuilder(URI.create(url))
   415         HttpRequest request = HttpRequest.newBuilder(URI.create(url))
   389                 .POST(fromString("May the wind always be at your back.")).build();
   416                 .POST(fromString("May the wind always be at your back.")).build();
   390 
   417 
   391         client.sendAsync(request, BodyHandler.fromSubscriber(asInputStream(),
   418         client.sendAsync(request, BodyHandler.fromSubscriber(asInputStream(),