jdk/test/com/sun/net/httpserver/bugs/B6373555.java
author chegar
Wed, 21 Jul 2010 13:29:26 +0100
changeset 6115 7c523cf2bc8a
parent 5506 202f599c92aa
child 7668 d4a77089c587
permissions -rw-r--r--
6969395: TEST_BUG: Tests in java/net sun/net problems Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 6373555
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary HTTP Server failing to answer client requests
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.xml.soap.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import com.sun.net.httpserver.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.concurrent.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
public class B6373555 {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    private static int s_received = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    private static int sent = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    private static int received = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private static int port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private static volatile boolean error = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private static Object lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    static HttpServer httpServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    static ExecutorService pool, execs;
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    49
    static int NUM = 1000;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            lock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            if (args.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
                NUM = Integer.parseInt (args[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            execs = Executors.newFixedThreadPool(5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            httpServer = createHttpServer(execs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            port = httpServer.getAddress().getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            pool = Executors.newFixedThreadPool(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            httpServer.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            for (int i=0; i < NUM; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                pool.execute(new Client());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                if (error) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                    throw new Exception ("error in test");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            System.out.println("Main thread waiting");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            pool.shutdown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            long latest = System.currentTimeMillis() + 200 * 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            while (System.currentTimeMillis() < latest) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                if (pool.awaitTermination(2000L, TimeUnit.MILLISECONDS)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                    System.out.println("Main thread done!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                if (error) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                    throw new Exception ("error in test");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            throw new Exception ("error in test: timed out");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            httpServer.stop(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            pool.shutdownNow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            execs.shutdownNow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    public static class Client implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        byte[] getBuf () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            byte[] buf = new byte [5200];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            for (int i=0; i< 5200; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                buf [i] = (byte)i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            return buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                Thread.sleep(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                byte[] buf = getBuf();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                URL url = new URL("http://127.0.0.1:"+port+"/test");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                HttpURLConnection con = (HttpURLConnection)url.openConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                con.setDoOutput(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                con.setDoInput(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                con.setRequestMethod("POST");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                con.setRequestProperty(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                    "Content-Type",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                    "Multipart/Related; type=\"application/xop+xml\"; boundary=\"----=_Part_0_6251267.1128549570165\"; start-info=\"text/xml\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                OutputStream out = con.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                out.write(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                out.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                InputStream in = con.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                byte[] newBuf = readFully(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                if (buf.length != newBuf.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                    System.out.println("Doesn't match");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                    error = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                synchronized(lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                    ++received;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                    if ((received % 1000) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                        System.out.println("Received="+received);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            catch(Exception e) {
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   128
                e.printStackTrace();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                System.out.print (".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                error = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    private static byte[] readFully(InputStream istream) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        byte[] buf = new byte[1024];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        int num = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        if (istream != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            while ((num = istream.read(buf)) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                bout.write(buf, 0, num);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        byte[] ret = bout.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    private static HttpServer createHttpServer(ExecutorService execs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        InetSocketAddress inetAddress = new InetSocketAddress(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        HttpServer testServer = HttpServer.create(inetAddress, 5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        testServer.setExecutor(execs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        HttpContext context = testServer.createContext("/test");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        context.setHandler(new HttpHandler() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            public void handle(HttpExchange msg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                    synchronized(lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                        ++s_received;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                            if ((s_received % 1000) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                            System.out.println("Received="+s_received);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                    String method = msg.getRequestMethod();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                        if (method.equals("POST")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                        InputStream is = msg.getRequestBody();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                            byte[] buf = readFully(is);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                            is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                            writePostReply(msg, buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                        System.out.println("****** METHOD not handled ***** "+method);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                            System.out.println("Received="+s_received);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                    synchronized(lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                        ++sent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                            if ((sent % 1000) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                            System.out.println("sent="+sent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                catch(Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                    e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                    msg.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        return testServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    private static void writePostReply(HttpExchange msg, byte[] buf)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        msg.getResponseHeaders().add("Content-Type", "text/xml");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        msg.sendResponseHeaders(200, buf.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        OutputStream out = msg.getResponseBody();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        out.write(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        out.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
}