jdk/test/sun/net/www/http/ChunkedOutputStream/checkError.java
changeset 38892 ac9b9cc41b80
parent 38891 57d7bbc02fa6
child 38893 9ed71a236594
equal deleted inserted replaced
38891:57d7bbc02fa6 38892:ac9b9cc41b80
     1 /*
       
     2  * Copyright (c) 2004, 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 /**
       
    25  * @test
       
    26  * @bug 5054016
       
    27  * @key intermittent
       
    28  * @run main/othervm/timeout=300 checkError
       
    29  * @summary get the failure immediately when writing individual chunks over socket fail
       
    30  */
       
    31 
       
    32 import java.io.*;
       
    33 import java.net.*;
       
    34 import java.util.StringTokenizer;
       
    35 
       
    36 
       
    37 public class checkError {
       
    38     static final int TEST_PASSED = 95;
       
    39     static final int TEST_FAILED = 97;
       
    40 
       
    41     static int testStatus = TEST_PASSED;
       
    42 
       
    43     static String serverName = "localhost";
       
    44     static int bufferSize = 8192; // 8k
       
    45     static int totalBytes = 1048576; // 1M
       
    46 
       
    47     static int j = 0;
       
    48 
       
    49     static public Object threadStarting = new Object();
       
    50     static public Object threadWaiting = new Object();
       
    51 
       
    52 
       
    53     public static void main(String[] args) throws Exception {
       
    54         HttpURLConnection conn = null;
       
    55         OutputStream toServer = null;
       
    56         byte[] buffer = null;
       
    57         HTTPServer server = null;
       
    58         synchronized(threadWaiting) {
       
    59             System.out.println("HTTP-client>Starting default Http-server");
       
    60             synchronized(threadStarting) {
       
    61                 server = new HTTPServer();
       
    62                 server.start();
       
    63                 try {
       
    64                     System.out.println("waiting server to be start");
       
    65                     threadStarting.wait();
       
    66                 } catch (InterruptedException e) {
       
    67                 }
       
    68             }
       
    69             int port = server.getPort();
       
    70             URL url = new URL("http://" + serverName + ":" + port);
       
    71             conn = (HttpURLConnection )url.openConnection();
       
    72             conn.setRequestMethod("POST");
       
    73             conn.setDoOutput(true);
       
    74 
       
    75             System.out.println("assigning 1024 to the chunk length");
       
    76             conn.setChunkedStreamingMode(1024);
       
    77             conn.connect();
       
    78 
       
    79             toServer = conn.getOutputStream();
       
    80             buffer = getThickBuffer(bufferSize);
       
    81             System.out.println("sending " + totalBytes + " bytes");
       
    82         }
       
    83 
       
    84         int byteAtOnce = 0;
       
    85         int sendingBytes = totalBytes;
       
    86         try {
       
    87             while (sendingBytes > 0) {
       
    88                 if (sendingBytes > bufferSize) {
       
    89                     byteAtOnce = bufferSize;
       
    90                 } else {
       
    91                     byteAtOnce = sendingBytes;
       
    92                 }
       
    93                 toServer.write(buffer, 0, byteAtOnce);
       
    94                 sendingBytes -= byteAtOnce;
       
    95                 // System.out.println((totalBytes - sendingBytes) + " was sent");
       
    96                 toServer.flush();
       
    97             }
       
    98         } catch (OutOfMemoryError e) {
       
    99             e.printStackTrace();
       
   100             System.out.println("***ERR***> UNEXPECTED error: " + e);
       
   101             testStatus = TEST_FAILED;
       
   102             testExit();
       
   103         } catch (IOException e) {
       
   104             // e.printStackTrace();
       
   105             // this is the expected IOException
       
   106             // due to server.close()
       
   107             testStatus = TEST_PASSED;
       
   108             testExit();
       
   109         } finally {
       
   110             toServer.close();
       
   111         }
       
   112 
       
   113         // we have not received the expected IOException
       
   114         // test fail
       
   115         testStatus = TEST_FAILED;
       
   116         testExit();
       
   117 
       
   118     }
       
   119 
       
   120     static void testExit() {
       
   121         if (testStatus == TEST_FAILED) {
       
   122             throw new RuntimeException("Test Failed: haven't received the expected IOException");
       
   123         } else {
       
   124             System.out.println("TEST PASSED");
       
   125         }
       
   126         System.exit(testStatus);
       
   127     }
       
   128 
       
   129     static byte[] getThickBuffer(int size) {
       
   130 
       
   131         byte[] buffer = new byte[size];
       
   132 
       
   133         for (int i = 0; i < size; i++) {
       
   134             if (j > 9)
       
   135                 j = 0;
       
   136             String s = Integer.toString(j);
       
   137             buffer[i] = (byte )s.charAt(0);
       
   138             j++;
       
   139         }
       
   140 
       
   141         return buffer;
       
   142     }
       
   143 }
       
   144 
       
   145 
       
   146 class HTTPServer extends Thread {
       
   147 
       
   148     static volatile boolean isCompleted;
       
   149 
       
   150     Socket client;
       
   151     ServerSocket serverSocket;
       
   152 
       
   153     int getPort() {
       
   154         return serverSocket.getLocalPort();
       
   155     }
       
   156 
       
   157     public void run() {
       
   158 
       
   159         synchronized(checkError.threadStarting) {
       
   160 
       
   161             try {
       
   162                 serverSocket = new ServerSocket(0, 100);
       
   163             } catch (Exception e) {
       
   164                 e.printStackTrace();
       
   165                 checkError.testStatus = checkError.TEST_FAILED;
       
   166                 return;
       
   167             }
       
   168             checkError.threadStarting.notify();
       
   169         }
       
   170 
       
   171         try {
       
   172             client = serverSocket.accept();
       
   173         } catch (Exception e) {
       
   174             e.printStackTrace();
       
   175             checkError.testStatus = checkError.TEST_FAILED;
       
   176             return;
       
   177         }
       
   178 
       
   179         System.out.println("Server started");
       
   180 
       
   181         BufferedReader in = null;
       
   182         PrintStream out = null;
       
   183         InputStreamReader reader = null;
       
   184         String version = null;
       
   185         String line;
       
   186         String method;
       
   187 
       
   188         synchronized(checkError.threadWaiting) {
       
   189             try {
       
   190                 reader = new InputStreamReader(client.getInputStream());
       
   191                 in = new BufferedReader(reader);
       
   192                 line = in.readLine();
       
   193 
       
   194             } catch (Exception e) {
       
   195                 e.printStackTrace();
       
   196                 checkError.testStatus = checkError.TEST_FAILED;
       
   197                 return;
       
   198             }
       
   199             StringTokenizer st = new StringTokenizer(line);
       
   200             method = st.nextToken();
       
   201             String fileName = st.nextToken();
       
   202 
       
   203             // save version for replies
       
   204             if (st.hasMoreTokens()) version = st.nextToken();
       
   205 
       
   206             System.out.println("HTTP version: " + version);
       
   207 
       
   208         }
       
   209 
       
   210         try {
       
   211 
       
   212             while (line != null && line.length() > 0) {
       
   213                 line = in.readLine();
       
   214                 System.out.println(line);
       
   215             }
       
   216         } catch (IOException e) {
       
   217             e.printStackTrace();
       
   218             checkError.testStatus = checkError.TEST_FAILED;
       
   219             return;
       
   220         }
       
   221 
       
   222         if (method.equals("POST")) {
       
   223             System.out.println("receiving data");
       
   224             byte[] buf = new byte[1024];
       
   225             try {
       
   226                 //reading bytes until chunk whose size is zero,
       
   227                 // see 19.4.6 Introduction of Transfer-Encoding in RFC2616
       
   228                 int count = 0;
       
   229                 while (count <=5) {
       
   230                     count++;
       
   231                     in.readLine();
       
   232                 }
       
   233 
       
   234                 System.out.println("Server socket is closed");
       
   235                 in.close();
       
   236                 client.close();
       
   237                 serverSocket.close();
       
   238 
       
   239             } catch (IOException e) {
       
   240                 e.printStackTrace();
       
   241                 checkError.testStatus = checkError.TEST_FAILED;
       
   242                 return;
       
   243             } catch (OutOfMemoryError e) {
       
   244                 e.printStackTrace();
       
   245                 checkError.testStatus = checkError.TEST_FAILED;
       
   246                 return;
       
   247             }
       
   248 
       
   249         }
       
   250     }
       
   251 
       
   252 }