Print this page


Split Close
Expand all
Collapse all
          --- old/test/sun/net/www/protocol/http/ChunkedErrorStream.java
          +++ new/test/sun/net/www/protocol/http/ChunkedErrorStream.java
↓ open down ↓ 15 lines elided ↑ open up ↑
  16   16   * 2 along with this work; if not, write to the Free Software Foundation,
  17   17   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18   18   *
  19   19   * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20   20   * or visit www.oracle.com if you need additional information or have any
  21   21   * questions.
  22   22   */
  23   23  
  24   24  /*
  25   25   * @test
  26      - * @bug 6488669 6595324
       26 + * @bug 6488669 6595324 6993490
  27   27   * @run main/othervm ChunkedErrorStream
  28   28   * @summary Chunked ErrorStream tests
  29   29   */
  30   30  
  31   31  import java.net.*;
  32   32  import java.io.*;
  33   33  import com.sun.net.httpserver.*;
  34   34  
  35   35  /**
  36   36   * Part 1: 6488669
↓ open down ↓ 4 lines elided ↑ open up ↑
  41   41   *    getErrorStream(). Some data must be able to be read
  42   42   *    from the errorStream.
  43   43   *
  44   44   * Part 2: 6595324
  45   45   * 1) Http server that responds with an error code (>=400)
  46   46   *    and a chunked response body greater than
  47   47   *    sun.net.http.errorstream.bufferSize, 4K + 10 bytes.
  48   48   * 2) Client sends request to server and tries to
  49   49   *    getErrorStream(). 4K + 10 bytes must be read from
  50   50   *    the errorStream.
       51 + *
       52 + * Part 3: 6993490
       53 + *    Reuse persistent connection from part 2, the error stream
       54 + *    buffering will have set a reduced timeout on the socket and
       55 + *    tried to reset it to the default, infinity. Client must not
       56 + *    throw a timeout exception. If it does, it indicates that the
       57 + *    default timeout was not reset correctly.
       58 + *    If no timeout exception is thrown, it does not guarantee that
       59 + *    the timeout was reset correctly, as there is a potential race
       60 + *    between the sleeping server and the client thread. Typically,
       61 + *    1000 millis has been enought to reliable reproduce this problem
       62 + *    since the error stream buffering sets the timeout to 60 millis.
  51   63   */
  52   64  
  53   65  public class ChunkedErrorStream
  54   66  {
  55   67      com.sun.net.httpserver.HttpServer httpServer;
  56   68  
  57   69      static {
  58   70          // Enable ErrorStream buffering
  59   71          System.getProperties().setProperty("sun.net.http.errorstream.enableBuffering", "true");
  60   72  
↓ open down ↓ 7 lines elided ↑ open up ↑
  68   80  
  69   81      public ChunkedErrorStream() {
  70   82          try {
  71   83              startHttpServer();
  72   84              doClient();
  73   85          } catch (IOException ioe) {
  74   86              ioe.printStackTrace();
  75   87          }  finally {
  76   88              httpServer.stop(1);
  77   89          }
  78      -
  79   90      }
  80   91  
  81   92      void doClient() {
  82      -        for (int times=0; times<2; times++) {
       93 +        for (int times=0; times<3; times++) {
  83   94              HttpURLConnection uc = null;
  84   95              try {
  85   96                  InetSocketAddress address = httpServer.getAddress();
  86   97                  String URLStr = "http://localhost:" + address.getPort() + "/test/";
  87   98                  if (times == 0) {
  88      -                    URLStr += 6488669;
       99 +                    URLStr += "first";
  89  100                  } else {
  90      -                    URLStr += 6595324;
      101 +                    URLStr += "second";
  91  102                  }
  92  103  
  93  104                  System.out.println("Trying " + URLStr);
  94  105                  URL url = new URL(URLStr);
  95  106                  uc = (HttpURLConnection)url.openConnection();
  96  107                  uc.getInputStream();
  97  108  
  98  109                  throw new RuntimeException("Failed: getInputStream should throw and IOException");
  99  110              }  catch (IOException e) {
      111 +                if (e instanceof SocketTimeoutException) {
      112 +                    e.printStackTrace();
      113 +                    throw new RuntimeException("Failed: SocketTimeoutException should not happen");
      114 +                }
      115 +
 100  116                  // This is what we expect to happen.
 101  117                  InputStream es = uc.getErrorStream();
 102  118                  byte[] ba = new byte[1024];
 103  119                  int count = 0, ret;
 104  120                  try {
 105  121                      while ((ret = es.read(ba)) != -1)
 106  122                          count += ret;
 107  123                      es.close();
 108  124                  } catch  (IOException ioe) {
 109  125                      ioe.printStackTrace();
 110  126                  }
 111  127  
 112  128                  if (count == 0)
 113  129                      throw new RuntimeException("Failed: ErrorStream returning 0 bytes");
 114  130  
 115      -                if (times == 1 && count != (4096+10))
      131 +                if (times >= 1 && count != (4096+10))
 116  132                      throw new RuntimeException("Failed: ErrorStream returning " + count +
 117  133                                                   " bytes. Expecting " + (4096+10));
 118  134  
 119  135                  System.out.println("Read " + count + " bytes from the errorStream");
 120  136              }
 121  137          }
 122  138      }
 123  139  
 124  140      /**
 125  141       * Http Server
 126  142       */
 127  143      void startHttpServer() throws IOException {
 128  144          httpServer = com.sun.net.httpserver.HttpServer.create(new InetSocketAddress(0), 0);
 129  145  
 130  146          // create HttpServer context
 131      -        HttpContext ctx1 = httpServer.createContext("/test/6488669", new Handler6488669());
 132      -        HttpContext ctx2 = httpServer.createContext("/test/6595324", new Handler6595324());
      147 +        httpServer.createContext("/test/first", new FirstHandler());
      148 +        httpServer.createContext("/test/second", new SecondHandler());
 133  149  
 134  150          httpServer.start();
 135  151      }
 136  152  
 137      -    class Handler6488669 implements HttpHandler {
      153 +    class FirstHandler implements HttpHandler {
 138  154          public void handle(HttpExchange t) throws IOException {
 139  155              InputStream is = t.getRequestBody();
 140  156              byte[] ba = new byte[1024];
 141  157              while (is.read(ba) != -1);
 142  158              is.close();
 143  159  
 144  160              Headers resHeaders = t.getResponseHeaders();
 145  161              resHeaders.add("Connection", "close");
 146  162              t.sendResponseHeaders(404, 0);
 147  163              OutputStream os = t.getResponseBody();
↓ open down ↓ 1 lines elided ↑ open up ↑
 149  165              // actual data doesn't matter. Just send 2K worth.
 150  166              byte b = 'a';
 151  167              for (int i=0; i<2048; i++)
 152  168                  os.write(b);
 153  169  
 154  170              os.close();
 155  171              t.close();
 156  172          }
 157  173      }
 158  174  
 159      -    class Handler6595324 implements HttpHandler {
      175 +    static class SecondHandler implements HttpHandler {
      176 +        /* count greater than 0, slow response */
      177 +        static int count = 0;
      178 +
 160  179          public void handle(HttpExchange t) throws IOException {
 161  180              InputStream is = t.getRequestBody();
 162  181              byte[] ba = new byte[1024];
 163  182              while (is.read(ba) != -1);
 164  183              is.close();
 165  184  
      185 +            if (count > 0) {
      186 +                System.out.println("server sleeping...");
      187 +                try { Thread.sleep(1000); } catch(InterruptedException e) {}
      188 +            }
      189 +            count++;
      190 + 
 166  191              t.sendResponseHeaders(404, 0);
 167  192              OutputStream os = t.getResponseBody();
 168  193  
 169  194              // actual data doesn't matter. Just send more than 4K worth
 170  195              byte b = 'a';
 171  196              for (int i=0; i<(4096+10); i++)
 172  197                  os.write(b);
 173  198  
 174  199              os.close();
 175  200              t.close();
 176  201          }
 177  202      }
 178  203  }
    
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX