test/jdk/com/sun/net/httpserver/bugs/B6373555.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 37779 jdk/test/com/sun/net/httpserver/bugs/B6373555.java@7c84df693837
child 54314 46cf212cdcca
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 10075
diff changeset
     2
 * Copyright (c) 2006, 2011, 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 java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import com.sun.net.httpserver.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.concurrent.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
public class B6373555 {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    private static int s_received = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    private static int sent = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private static int received = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    private static int port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private static volatile boolean error = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    static HttpServer httpServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    static ExecutorService pool, execs;
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    47
    static int NUM = 1000;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            if (args.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
                NUM = Integer.parseInt (args[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            execs = Executors.newFixedThreadPool(5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            httpServer = createHttpServer(execs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            port = httpServer.getAddress().getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            pool = Executors.newFixedThreadPool(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            httpServer.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            for (int i=0; i < NUM; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                pool.execute(new Client());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                if (error) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                    throw new Exception ("error in test");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            System.out.println("Main thread waiting");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            pool.shutdown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            long latest = System.currentTimeMillis() + 200 * 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            while (System.currentTimeMillis() < latest) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                if (pool.awaitTermination(2000L, TimeUnit.MILLISECONDS)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                    System.out.println("Main thread done!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                if (error) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                    throw new Exception ("error in test");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            throw new Exception ("error in test: timed out");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            httpServer.stop(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            pool.shutdownNow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            execs.shutdownNow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    public static class Client implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        byte[] getBuf () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            byte[] buf = new byte [5200];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            for (int i=0; i< 5200; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                buf [i] = (byte)i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            return buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                Thread.sleep(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                byte[] buf = getBuf();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                URL url = new URL("http://127.0.0.1:"+port+"/test");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                HttpURLConnection con = (HttpURLConnection)url.openConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                con.setDoOutput(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                con.setDoInput(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                con.setRequestMethod("POST");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                con.setRequestProperty(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                    "Content-Type",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                    "Multipart/Related; type=\"application/xop+xml\"; boundary=\"----=_Part_0_6251267.1128549570165\"; start-info=\"text/xml\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                OutputStream out = con.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                out.write(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                out.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                InputStream in = con.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                byte[] newBuf = readFully(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                if (buf.length != newBuf.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                    System.out.println("Doesn't match");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                    error = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            catch(Exception e) {
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   119
                e.printStackTrace();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                System.out.print (".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                error = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    private static byte[] readFully(InputStream istream) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        byte[] buf = new byte[1024];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        int num = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        if (istream != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            while ((num = istream.read(buf)) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                bout.write(buf, 0, num);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        byte[] ret = bout.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    private static HttpServer createHttpServer(ExecutorService execs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        InetSocketAddress inetAddress = new InetSocketAddress(0);
10075
c8d9bac189cc 7058832: com/sun/net/httpserver/bugs/B6373555.java failing intermittently
michaelm
parents: 7668
diff changeset
   144
        HttpServer testServer = HttpServer.create(inetAddress, 15);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        testServer.setExecutor(execs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        HttpContext context = testServer.createContext("/test");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        context.setHandler(new HttpHandler() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            public void handle(HttpExchange msg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                    String method = msg.getRequestMethod();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                        if (method.equals("POST")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                        InputStream is = msg.getRequestBody();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                            byte[] buf = readFully(is);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                            is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                            writePostReply(msg, buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                        System.out.println("****** METHOD not handled ***** "+method);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                            System.out.println("Received="+s_received);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                catch(Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                    e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                    msg.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        return testServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    private static void writePostReply(HttpExchange msg, byte[] buf)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        msg.getResponseHeaders().add("Content-Type", "text/xml");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        msg.sendResponseHeaders(200, buf.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        OutputStream out = msg.getResponseBody();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        out.write(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        out.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
}