jdk/test/sun/net/www/protocol/http/B6518816.java
author jjiang
Wed, 25 May 2016 03:06:24 +0100
changeset 38557 5c485e1ea6fa
parent 5506 202f599c92aa
permissions -rw-r--r--
8157635: Fix module dependencies for /sun/* tests Summary: Module declarations for the tests in /sun/* Reviewed-by: alanb Contributed-by: John Jiang <sha.jiang@oracle.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
38557
5c485e1ea6fa 8157635: Fix module dependencies for /sun/* tests
jjiang
parents: 5506
diff changeset
     2
 * Copyright (c) 2007, 2016, 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 6518816
38557
5c485e1ea6fa 8157635: Fix module dependencies for /sun/* tests
jjiang
parents: 5506
diff changeset
    27
 * @modules jdk.httpserver
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @run main/othervm B6518816
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.*;
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.Executors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.concurrent.ExecutorService;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
public class B6518816
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    com.sun.net.httpserver.HttpServer httpServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    ExecutorService executorService;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    public static void main(String[] args)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        new B6518816();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    public B6518816()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            startHttpServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            doClient();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            System.err.println(ioe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    final static int MAX_CONNS = 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    final static int TEN_MB = 10 * 1024 * 1024;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    static boolean stopped = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * we send approx 100MB and if more than 90MB is retained
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * then the bug is still there
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    void doClient() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            InetSocketAddress address = httpServer.getAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            List<HttpURLConnection> conns = new LinkedList<HttpURLConnection>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            long totalmem1=0, totalmem2=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            System.gc();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            Runtime runtime = Runtime.getRuntime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            totalmem1 = runtime.totalMemory() - runtime.freeMemory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            System.out.println ("Sending " + (TEN_MB*MAX_CONNS/1024) + " KB");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            System.out.println ("At start: " + totalmem1/1024 + " KB");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            byte [] buf = new byte [TEN_MB];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            for (int j=0; j<MAX_CONNS; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                URL url = new URL("http://localhost:"+address.getPort()+"/test/");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                HttpURLConnection uc = (HttpURLConnection)url.openConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                uc.setDoOutput (true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                OutputStream os = uc.getOutputStream ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                os.write (buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                InputStream is = uc.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                int resp = uc.getResponseCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                if (resp != 200) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                    throw new RuntimeException("Failed: Part 1, Response code is not 200");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                while  (is.read() != -1) ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                conns.add (uc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            httpServer.stop(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            httpServer = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            executorService.shutdown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            executorService = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            stopped = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            buf = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            System.runFinalization();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            try {Thread.sleep (1000); } catch (InterruptedException e){}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            System.gc();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            try {Thread.sleep (1000); } catch (InterruptedException e){}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            System.gc();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            totalmem2 = runtime.totalMemory() - runtime.freeMemory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            System.out.println ("At end: " + totalmem2/1024 + " KB");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            long diff = (totalmem2 - totalmem1) ;;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            System.out.println ("Diff " + diff/1024 + " kbytes ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            if (diff > (TEN_MB*MAX_CONNS*0.9)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                /* if >= 90 MB retained, then problem still there */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                throw new RuntimeException ("Excessive memory retained");
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 (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            throw new RuntimeException ("IOException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            if (!stopped) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                httpServer.stop(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                executorService.shutdown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * Http Server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    public void startHttpServer() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        httpServer = com.sun.net.httpserver.HttpServer.create(new InetSocketAddress(0), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        // create HttpServer context for Part 1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        HttpContext ctx = httpServer.createContext("/test/", new MyHandler());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        executorService = Executors.newCachedThreadPool();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        httpServer.setExecutor(executorService);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        httpServer.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    class MyHandler implements HttpHandler {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        public void handle(HttpExchange t) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            InputStream is = t.getRequestBody();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            Headers reqHeaders = t.getRequestHeaders();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            Headers resHeaders = t.getResponseHeaders();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            byte[] buf = new byte [16 * 1024];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            while (is.read (buf) != -1) ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            t.sendResponseHeaders(200, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            t.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
}