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 * @summary get the failure immediately when writing individual chunks over socket fail 28 */ 29 30 import java.io.BufferedReader; 31 import java.io.IOException; 32 import java.io.InputStream; 33 import java.io.InputStreamReader; 34 import java.io.OutputStream; 35 import java.net.HttpURLConnection; 36 import java.net.ServerSocket; 37 import java.net.Socket; 38 import java.net.URL; 39 import static java.lang.System.out; 40 41 public class CheckError { 42 43 static int BUFFER_SIZE = 8192; // 8k 44 static int TOTAL_BYTES = 1 * 1024 * 1024; // 1M 45 46 public static void main(String[] args) throws Exception { 47 48 HTTPServer server = new HTTPServer(); 49 server.start(); 50 int port = server.getPort(); 51 out.println("Server listening on " + port); 52 53 54 URL url = new URL("http://localhost:" + port); 55 HttpURLConnection conn = (HttpURLConnection)url.openConnection(); 56 conn.setRequestMethod("POST"); 57 conn.setDoOutput(true); 58 conn.setChunkedStreamingMode(1024); 59 60 out.println("sending " + TOTAL_BYTES + " bytes"); 61 62 int byteAtOnce; 63 int sendingBytes = TOTAL_BYTES; 64 byte[] buffer = getBuffer(BUFFER_SIZE); 65 try (OutputStream toServer = conn.getOutputStream()) { 66 while (sendingBytes > 0) { 67 if (sendingBytes > BUFFER_SIZE) { 68 byteAtOnce = BUFFER_SIZE; 69 } else { 70 byteAtOnce = sendingBytes; 71 } 72 toServer.write(buffer, 0, byteAtOnce); 73 sendingBytes -= byteAtOnce; 74 out.print((TOTAL_BYTES - sendingBytes) + " was sent. "); 75 toServer.flush(); 76 // gives the server thread time to read, and eventually close; 77 Thread.sleep(500); 78 } 79 } catch (IOException expected) { 80 // Expected IOException due to server.close() 81 out.println("PASSED. Caught expected: " + expected); 82 return; 83 } 84 85 // Expected IOException not received. FAIL 86 throw new RuntimeException("Failed: Expected IOException not received"); 87 } 88 89 static byte[] getBuffer(int size) { 90 byte[] buffer = new byte[size]; 91 for (int i = 0; i < size; i++) 92 buffer[i] = (byte)i; 93 return buffer; 94 } 95 96 static class HTTPServer extends Thread { 97 98 final ServerSocket serverSocket; 99 100 HTTPServer() throws IOException { 101 serverSocket = new ServerSocket(0); 102 } 103 104 int getPort() { 105 return serverSocket.getLocalPort(); 106 } 107 108 public void run() { 109 try (Socket client = serverSocket.accept()) { 110 111 InputStream in = client.getInputStream(); 112 BufferedReader reader = new BufferedReader(new InputStreamReader(in)); 113 String line; 114 do { 115 line = reader.readLine(); 116 out.println("Server: " + line); 117 } while (line != null && line.length() > 0); 118 119 System.out.println("Server: receiving some data"); 120 // just read some data, then close the connecntion 121 in.read(new byte[1024]); 122 123 in.close(); 124 client.close(); 125 out.println("Server closed socket"); 126 } catch (IOException e) { 127 e.printStackTrace(); 128 } 129 } 130 } 131 }