test/jdk/java/net/httpclient/HttpResponseInputStreamTest.java
branchhttp-client-branch
changeset 55776 9950bc2ee874
child 55858 cd5eeec735fb
equal deleted inserted replaced
55775:5aad325e0407 55776:9950bc2ee874
       
     1 /*
       
     2  * Copyright (c) 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 jdk.incubator.http.HttpResponse;
       
    25 import jdk.incubator.http.HttpResponse.BodySubscriber;
       
    26 
       
    27 import java.io.IOException;
       
    28 import java.io.InputStream;
       
    29 import java.util.List;
       
    30 import java.util.concurrent.CompletionStage;
       
    31 import java.util.concurrent.CountDownLatch;
       
    32 import java.util.concurrent.ExecutionException;
       
    33 import java.util.concurrent.Flow;
       
    34 import org.testng.annotations.Test;
       
    35 
       
    36 /*
       
    37  * @test
       
    38  * @summary Simple smoke test for BodySubscriber.asInputStream();
       
    39  * @run testng/othervm HttpResponseInputStreamTest
       
    40  * @author daniel fuchs
       
    41  */
       
    42 public class HttpResponseInputStreamTest {
       
    43 
       
    44     static class TestException extends IOException {}
       
    45 
       
    46     public static void main(String[] args) throws InterruptedException, ExecutionException {
       
    47         testOnError();
       
    48     }
       
    49 
       
    50     /**
       
    51      * Tests that a client will be unblocked and will
       
    52      * @throws InterruptedException
       
    53      * @throws ExecutionException
       
    54      */
       
    55     @Test
       
    56     public static void testOnError() throws InterruptedException, ExecutionException {
       
    57         CountDownLatch latch = new CountDownLatch(1);
       
    58         BodySubscriber<InputStream> isb = BodySubscriber.asInputStream();
       
    59         ErrorTestSubscription s = new ErrorTestSubscription(isb);
       
    60         CompletionStage<Throwable> cs =
       
    61                 isb.getBody().thenApplyAsync((is) -> s.accept(latch, is));
       
    62         latch.await();
       
    63         isb.onSubscribe(s);
       
    64         s.t.join();
       
    65         Throwable result = cs.toCompletableFuture().get();
       
    66         Throwable t = result;
       
    67         if (!(t instanceof IOException)) {
       
    68             throw new RuntimeException("Failed to receive excpected IOException", result);
       
    69         } else {
       
    70             System.out.println("Got expected exception: " + t);
       
    71         }
       
    72         while (t != null) {
       
    73             if (t instanceof TestException) break;
       
    74             t = t.getCause();
       
    75         }
       
    76         if (t instanceof TestException) {
       
    77             System.out.println("Got expected cause: " + t);
       
    78         } else {
       
    79             throw new RuntimeException("Failed to receive excpected TestException", result);
       
    80         }
       
    81     }
       
    82 
       
    83     static class ErrorTestSubscription implements Flow.Subscription {
       
    84         final BodySubscriber<InputStream> isb;
       
    85         final Thread t = new Thread() {
       
    86             @Override
       
    87             public void run() {
       
    88                 try {
       
    89                     // Give time to
       
    90                     System.out.println("waiting...");
       
    91                     Thread.sleep(1000);
       
    92                 } catch (InterruptedException e) {
       
    93 
       
    94                 }
       
    95                 System.out.println("Calling onError...");
       
    96                 isb.onError(new TestException());
       
    97             }
       
    98         };
       
    99 
       
   100         ErrorTestSubscription(BodySubscriber<InputStream> isb) {
       
   101             this.isb = isb;
       
   102         }
       
   103 
       
   104         int requested = 0;
       
   105 
       
   106         @Override
       
   107         public void request(long n) {
       
   108             System.out.println("Got request: " + n);
       
   109             if (requested == 0 && n > 0) {
       
   110                 //isb.onNext(List.of(java.nio.ByteBuffer.wrap(new byte[] {0x01})));
       
   111                 requested += n;
       
   112                 t.start();
       
   113             }
       
   114         }
       
   115 
       
   116         @Override
       
   117         public void cancel() {
       
   118         }
       
   119 
       
   120         public Throwable accept(CountDownLatch latch, InputStream is) {
       
   121             System.out.println("got " + is);
       
   122             try {
       
   123                 latch.countDown();
       
   124                 System.out.println("reading all bytes");
       
   125                 is.readAllBytes();
       
   126                 System.out.println("all bytes read");
       
   127             } catch (IOException e) {
       
   128                 return e;
       
   129             } finally {
       
   130                 try {
       
   131                     is.close();
       
   132                 } catch (IOException e) {
       
   133                     return e;
       
   134                 }
       
   135             }
       
   136             return is == null ? new NullPointerException() : null;
       
   137         }
       
   138     }
       
   139 }