0N/A/*
2362N/A * Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/**
0N/A * @test
0N/A * @bug 5054016
0N/A * @run main/othervm/timeout=300 checkError
0N/A * @summary get the failure immediately when writing individual chunks over socket fail
0N/A */
0N/A
0N/Aimport java.io.*;
0N/Aimport java.net.*;
0N/Aimport java.util.StringTokenizer;
0N/A
0N/A
0N/Apublic class checkError {
0N/A static final int TEST_PASSED = 95;
0N/A static final int TEST_FAILED = 97;
0N/A
0N/A static int testStatus = TEST_PASSED;
0N/A
0N/A static String serverName = "localhost";
0N/A static int bufferSize = 8192; // 8k
0N/A static int totalBytes = 1048576; // 1M
0N/A
0N/A static int j = 0;
0N/A
0N/A static public Object threadStarting = new Object();
0N/A static public Object threadWaiting = new Object();
0N/A
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A HttpURLConnection conn = null;
0N/A OutputStream toServer = null;
0N/A byte[] buffer = null;
0N/A HTTPServer server = null;
0N/A synchronized(threadWaiting) {
0N/A System.out.println("HTTP-client>Starting default Http-server");
0N/A synchronized(threadStarting) {
0N/A server = new HTTPServer();
0N/A server.start();
0N/A try {
0N/A System.out.println("waiting server to be start");
0N/A threadStarting.wait();
0N/A } catch (InterruptedException e) {
0N/A }
0N/A }
0N/A int port = server.getPort();
0N/A URL url = new URL("http://" + serverName + ":" + port);
0N/A conn = (HttpURLConnection )url.openConnection();
0N/A conn.setRequestMethod("POST");
0N/A conn.setDoOutput(true);
0N/A
0N/A System.out.println("assigning 1024 to the chunk length");
0N/A conn.setChunkedStreamingMode(1024);
0N/A conn.connect();
0N/A
0N/A toServer = conn.getOutputStream();
0N/A buffer = getThickBuffer(bufferSize);
0N/A System.out.println("sending " + totalBytes + " bytes");
0N/A }
0N/A
0N/A int byteAtOnce = 0;
0N/A int sendingBytes = totalBytes;
0N/A try {
0N/A while (sendingBytes > 0) {
0N/A if (sendingBytes > bufferSize) {
0N/A byteAtOnce = bufferSize;
0N/A } else {
0N/A byteAtOnce = sendingBytes;
0N/A }
0N/A toServer.write(buffer, 0, byteAtOnce);
0N/A sendingBytes -= byteAtOnce;
0N/A // System.out.println((totalBytes - sendingBytes) + " was sent");
0N/A toServer.flush();
0N/A }
0N/A } catch (OutOfMemoryError e) {
0N/A e.printStackTrace();
0N/A System.out.println("***ERR***> UNEXPECTED error: " + e);
0N/A testStatus = TEST_FAILED;
0N/A testExit();
0N/A } catch (IOException e) {
0N/A // e.printStackTrace();
0N/A // this is the expected IOException
0N/A // due to server.close()
0N/A testStatus = TEST_PASSED;
0N/A testExit();
0N/A } finally {
0N/A toServer.close();
0N/A }
0N/A
0N/A // we have not received the expected IOException
0N/A // test fail
0N/A testStatus = TEST_FAILED;
0N/A testExit();
0N/A
0N/A }
0N/A
0N/A static void testExit() {
0N/A if (testStatus == TEST_FAILED) {
0N/A throw new RuntimeException("Test Failed: haven't received the expected IOException");
0N/A } else {
0N/A System.out.println("TEST PASSED");
0N/A }
0N/A System.exit(testStatus);
0N/A }
0N/A
0N/A static byte[] getThickBuffer(int size) {
0N/A
0N/A byte[] buffer = new byte[size];
0N/A
0N/A for (int i = 0; i < size; i++) {
0N/A if (j > 9)
0N/A j = 0;
0N/A String s = Integer.toString(j);
0N/A buffer[i] = (byte )s.charAt(0);
0N/A j++;
0N/A }
0N/A
0N/A return buffer;
0N/A }
0N/A}
0N/A
0N/A
0N/Aclass HTTPServer extends Thread {
0N/A
0N/A static volatile boolean isCompleted;
0N/A
0N/A Socket client;
0N/A ServerSocket serverSocket;
0N/A
0N/A int getPort() {
0N/A return serverSocket.getLocalPort();
0N/A }
0N/A
0N/A public void run() {
0N/A
0N/A synchronized(checkError.threadStarting) {
0N/A
0N/A try {
0N/A serverSocket = new ServerSocket(0, 100);
0N/A } catch (Exception e) {
0N/A e.printStackTrace();
0N/A checkError.testStatus = checkError.TEST_FAILED;
0N/A return;
0N/A }
0N/A checkError.threadStarting.notify();
0N/A }
0N/A
0N/A try {
0N/A client = serverSocket.accept();
0N/A } catch (Exception e) {
0N/A e.printStackTrace();
0N/A checkError.testStatus = checkError.TEST_FAILED;
0N/A return;
0N/A }
0N/A
0N/A System.out.println("Server started");
0N/A
0N/A BufferedReader in = null;
0N/A PrintStream out = null;
0N/A InputStreamReader reader = null;
0N/A String version = null;
0N/A String line;
0N/A String method;
0N/A
0N/A synchronized(checkError.threadWaiting) {
0N/A try {
0N/A reader = new InputStreamReader(client.getInputStream());
0N/A in = new BufferedReader(reader);
0N/A line = in.readLine();
0N/A
0N/A } catch (Exception e) {
0N/A e.printStackTrace();
0N/A checkError.testStatus = checkError.TEST_FAILED;
0N/A return;
0N/A }
0N/A StringTokenizer st = new StringTokenizer(line);
0N/A method = st.nextToken();
0N/A String fileName = st.nextToken();
0N/A
0N/A // save version for replies
0N/A if (st.hasMoreTokens()) version = st.nextToken();
0N/A
0N/A System.out.println("HTTP version: " + version);
0N/A
0N/A }
0N/A
0N/A try {
0N/A
0N/A while (line != null && line.length() > 0) {
0N/A line = in.readLine();
0N/A System.out.println(line);
0N/A }
0N/A } catch (IOException e) {
0N/A e.printStackTrace();
0N/A checkError.testStatus = checkError.TEST_FAILED;
0N/A return;
0N/A }
0N/A
0N/A if (method.equals("POST")) {
0N/A System.out.println("receiving data");
0N/A byte[] buf = new byte[1024];
0N/A try {
0N/A //reading bytes until chunk whose size is zero,
0N/A // see 19.4.6 Introduction of Transfer-Encoding in RFC2616
0N/A int count = 0;
0N/A while (count <=5) {
0N/A count++;
0N/A in.readLine();
0N/A }
0N/A
0N/A System.out.println("Server socket is closed");
0N/A in.close();
0N/A client.close();
0N/A serverSocket.close();
0N/A
0N/A } catch (IOException e) {
0N/A e.printStackTrace();
0N/A checkError.testStatus = checkError.TEST_FAILED;
0N/A return;
0N/A } catch (OutOfMemoryError e) {
0N/A e.printStackTrace();
0N/A checkError.testStatus = checkError.TEST_FAILED;
0N/A return;
0N/A }
0N/A
0N/A }
0N/A }
0N/A
0N/A}