test/jdk/sun/net/www/http/HttpClient/MultiThreadTest.java
author dfuchs
Wed, 15 May 2019 19:09:54 +0100
changeset 54885 e58e454c1409
parent 54787 7748aa47b4e2
permissions -rw-r--r--
8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic Reviewed-by: chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
54787
7748aa47b4e2 8223573: Replace wildcard address with loopback or local host in tests - part 4
dfuchs
parents: 51947
diff changeset
     2
 * Copyright (c) 2002, 2019, 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 4636628
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary HttpURLConnection duplicates HTTP GET requests when used with multiple threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * This tests keep-alive behavior using chunkedinputstreams
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * It checks that keep-alive connections are used and also
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * that requests are not being repeated (due to errors)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * It also checks that the keepalive connections are closed eventually
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * because the test will not terminate if the connections
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * are not closed by the keep-alive timer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.io.*;
51947
2d980757fd07 8211092: test/jdk/sun/net/www/http/HttpClient/MultiThreadTest.java fails intermittently when cleaning up
chegar
parents: 47216
diff changeset
    42
import java.time.Duration;
2d980757fd07 8211092: test/jdk/sun/net/www/http/HttpClient/MultiThreadTest.java fails intermittently when cleaning up
chegar
parents: 47216
diff changeset
    43
import java.util.Queue;
2d980757fd07 8211092: test/jdk/sun/net/www/http/HttpClient/MultiThreadTest.java fails intermittently when cleaning up
chegar
parents: 47216
diff changeset
    44
import java.util.concurrent.ConcurrentLinkedQueue;
54885
e58e454c1409 8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents: 54787
diff changeset
    45
import java.util.concurrent.atomic.AtomicInteger;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
public class MultiThreadTest extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * Is debugging enabled - start with -d to enable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     */
51947
2d980757fd07 8211092: test/jdk/sun/net/www/http/HttpClient/MultiThreadTest.java fails intermittently when cleaning up
chegar
parents: 47216
diff changeset
    52
    static boolean debug = true; // disable debug once stability proven
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    static Object threadlock = new Object ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    static int threadCounter = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    static Object getLock() { return threadlock; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    static void debug(String msg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        if (debug)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            System.out.println(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
54885
e58e454c1409 8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents: 54787
diff changeset
    64
    static final AtomicInteger reqnum = new AtomicInteger();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    void doRequest(String uri) throws Exception {
54885
e58e454c1409 8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents: 54787
diff changeset
    67
        URL url = new URL(uri + "?foo="+reqnum.getAndIncrement());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        HttpURLConnection http = (HttpURLConnection)url.openConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        InputStream in = http.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        byte b[] = new byte[100];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        int total = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            n = in.read(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            if (n > 0) total += n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        } while (n > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        debug ("client: read " + total + " bytes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        http.disconnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    String uri;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    byte[] b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    int requests;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
54787
7748aa47b4e2 8223573: Replace wildcard address with loopback or local host in tests - part 4
dfuchs
parents: 51947
diff changeset
    86
    MultiThreadTest(String authority, int requests) throws Exception {
7748aa47b4e2 8223573: Replace wildcard address with loopback or local host in tests - part 4
dfuchs
parents: 51947
diff changeset
    87
        uri = "http://" + authority + "/foo.html";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        b = new byte [256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        this.requests = requests;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        synchronized (threadlock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            threadCounter ++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
51947
2d980757fd07 8211092: test/jdk/sun/net/www/http/HttpClient/MultiThreadTest.java fails intermittently when cleaning up
chegar
parents: 47216
diff changeset
    97
    public void run() {
2d980757fd07 8211092: test/jdk/sun/net/www/http/HttpClient/MultiThreadTest.java fails intermittently when cleaning up
chegar
parents: 47216
diff changeset
    98
        long start = System.nanoTime();
2d980757fd07 8211092: test/jdk/sun/net/www/http/HttpClient/MultiThreadTest.java fails intermittently when cleaning up
chegar
parents: 47216
diff changeset
    99
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            for (int i=0; i<requests; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                doRequest (uri);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            throw new RuntimeException (e.getMessage());
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   106
        } finally {
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   107
            synchronized (threadlock) {
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   108
                threadCounter --;
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   109
                if (threadCounter == 0) {
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   110
                    threadlock.notifyAll();
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   111
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
51947
2d980757fd07 8211092: test/jdk/sun/net/www/http/HttpClient/MultiThreadTest.java fails intermittently when cleaning up
chegar
parents: 47216
diff changeset
   114
        debug("client: end - " + Duration.ofNanos(System.nanoTime() - start));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    static int threads=5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    public static void main(String args[]) throws Exception {
51947
2d980757fd07 8211092: test/jdk/sun/net/www/http/HttpClient/MultiThreadTest.java fails intermittently when cleaning up
chegar
parents: 47216
diff changeset
   120
        long start = System.nanoTime();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        int x = 0, arg_len = args.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        int requests = 20;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        if (arg_len > 0 && args[0].equals("-d")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            debug = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            x = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            arg_len --;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        if (arg_len > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            threads = Integer.parseInt (args[x]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            requests = Integer.parseInt (args[x+1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        /* start the server */
54787
7748aa47b4e2 8223573: Replace wildcard address with loopback or local host in tests - part 4
dfuchs
parents: 51947
diff changeset
   136
        InetAddress loopback = InetAddress.getLoopbackAddress();
7748aa47b4e2 8223573: Replace wildcard address with loopback or local host in tests - part 4
dfuchs
parents: 51947
diff changeset
   137
        ServerSocket ss = new ServerSocket();
7748aa47b4e2 8223573: Replace wildcard address with loopback or local host in tests - part 4
dfuchs
parents: 51947
diff changeset
   138
        ss.bind(new InetSocketAddress(loopback, 0));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        Server svr = new Server(ss);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        svr.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        Object lock = MultiThreadTest.getLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            for (int i=0; i<threads; i++) {
54787
7748aa47b4e2 8223573: Replace wildcard address with loopback or local host in tests - part 4
dfuchs
parents: 51947
diff changeset
   145
                MultiThreadTest t = new MultiThreadTest(svr.getAuthority(), requests);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                t.start ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                lock.wait();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            } catch (InterruptedException e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        // shutdown server - we're done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        svr.shutdown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        int cnt = svr.connectionCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        MultiThreadTest.debug("Connections = " + cnt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        int reqs = Worker.getRequests ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        MultiThreadTest.debug("Requests = " + reqs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        System.out.println ("Connection count = " + cnt + " Request count = " + reqs);
54885
e58e454c1409 8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents: 54787
diff changeset
   161
e58e454c1409 8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents: 54787
diff changeset
   162
        // We may have received traffic from something else than
e58e454c1409 8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents: 54787
diff changeset
   163
        // our client. We should only count those workers for which
e58e454c1409 8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents: 54787
diff changeset
   164
        // the expected header has been found.
e58e454c1409 8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents: 54787
diff changeset
   165
        int validConnections = 0;
e58e454c1409 8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents: 54787
diff changeset
   166
        for (Worker w : svr.workers()) {
e58e454c1409 8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents: 54787
diff changeset
   167
            if (w.headerFound) validConnections++;
e58e454c1409 8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents: 54787
diff changeset
   168
        }
e58e454c1409 8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents: 54787
diff changeset
   169
e58e454c1409 8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents: 54787
diff changeset
   170
        if (validConnections > threads + 1 || validConnections == 0) { // could be less
e58e454c1409 8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents: 54787
diff changeset
   171
            throw new RuntimeException ("Expected " + threads + " connections: used " + validConnections);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        }
54885
e58e454c1409 8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents: 54787
diff changeset
   173
e58e454c1409 8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents: 54787
diff changeset
   174
        // Sometimes the client drops a connection after a while and
e58e454c1409 8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents: 54787
diff changeset
   175
        // spawns a new one. Why this is happening is not clear,
e58e454c1409 8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents: 54787
diff changeset
   176
        // and JDK-8223783 is logged to follow up on this. For the sake
e58e454c1409 8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents: 54787
diff changeset
   177
        // of test stabilization we don't fail on `threads + 1` connections
e58e454c1409 8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents: 54787
diff changeset
   178
        // but log a warning instead.
e58e454c1409 8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents: 54787
diff changeset
   179
        if (validConnections == threads + 1) {
e58e454c1409 8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents: 54787
diff changeset
   180
            debug("WARNING: " + validConnections
e58e454c1409 8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents: 54787
diff changeset
   181
                + " have been used, where only " + threads
e58e454c1409 8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents: 54787
diff changeset
   182
                + " were expected!");
e58e454c1409 8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents: 54787
diff changeset
   183
        }
e58e454c1409 8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents: 54787
diff changeset
   184
e58e454c1409 8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents: 54787
diff changeset
   185
        if (validConnections != cnt) {
e58e454c1409 8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents: 54787
diff changeset
   186
            debug("WARNING: got " + (cnt - validConnections) + " unexpected connections!");
e58e454c1409 8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents: 54787
diff changeset
   187
        }
e58e454c1409 8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents: 54787
diff changeset
   188
        if  (validConnections == cnt && reqs != threads*requests) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            throw new RuntimeException ("Expected "+ threads*requests+ " requests: got " +reqs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        }
54885
e58e454c1409 8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents: 54787
diff changeset
   191
51947
2d980757fd07 8211092: test/jdk/sun/net/www/http/HttpClient/MultiThreadTest.java fails intermittently when cleaning up
chegar
parents: 47216
diff changeset
   192
        for (Thread worker : svr.workers()) {
2d980757fd07 8211092: test/jdk/sun/net/www/http/HttpClient/MultiThreadTest.java fails intermittently when cleaning up
chegar
parents: 47216
diff changeset
   193
            worker.join(60_000);
2d980757fd07 8211092: test/jdk/sun/net/www/http/HttpClient/MultiThreadTest.java fails intermittently when cleaning up
chegar
parents: 47216
diff changeset
   194
        }
2d980757fd07 8211092: test/jdk/sun/net/www/http/HttpClient/MultiThreadTest.java fails intermittently when cleaning up
chegar
parents: 47216
diff changeset
   195
2d980757fd07 8211092: test/jdk/sun/net/www/http/HttpClient/MultiThreadTest.java fails intermittently when cleaning up
chegar
parents: 47216
diff changeset
   196
        debug("main thread end - " + Duration.ofNanos(System.nanoTime() - start));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * Server thread to accept connection and create worker threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * to service each connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    class Server extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        ServerSocket ss;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        int connectionCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        boolean shutdown = false;
54885
e58e454c1409 8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents: 54787
diff changeset
   208
        private final Queue<Worker> workers = new ConcurrentLinkedQueue<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        Server(ServerSocket ss) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            this.ss = ss;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
54787
7748aa47b4e2 8223573: Replace wildcard address with loopback or local host in tests - part 4
dfuchs
parents: 51947
diff changeset
   214
        public String getAuthority() {
7748aa47b4e2 8223573: Replace wildcard address with loopback or local host in tests - part 4
dfuchs
parents: 51947
diff changeset
   215
            InetAddress address = ss.getInetAddress();
7748aa47b4e2 8223573: Replace wildcard address with loopback or local host in tests - part 4
dfuchs
parents: 51947
diff changeset
   216
            String hostaddr = address.isAnyLocalAddress()
7748aa47b4e2 8223573: Replace wildcard address with loopback or local host in tests - part 4
dfuchs
parents: 51947
diff changeset
   217
                ? "localhost" : address.getHostAddress();
7748aa47b4e2 8223573: Replace wildcard address with loopback or local host in tests - part 4
dfuchs
parents: 51947
diff changeset
   218
            if (hostaddr.indexOf(':') > -1) {
7748aa47b4e2 8223573: Replace wildcard address with loopback or local host in tests - part 4
dfuchs
parents: 51947
diff changeset
   219
                hostaddr = "[" + hostaddr + "]";
7748aa47b4e2 8223573: Replace wildcard address with loopback or local host in tests - part 4
dfuchs
parents: 51947
diff changeset
   220
            }
7748aa47b4e2 8223573: Replace wildcard address with loopback or local host in tests - part 4
dfuchs
parents: 51947
diff changeset
   221
            return hostaddr + ":" + ss.getLocalPort();
7748aa47b4e2 8223573: Replace wildcard address with loopback or local host in tests - part 4
dfuchs
parents: 51947
diff changeset
   222
        }
7748aa47b4e2 8223573: Replace wildcard address with loopback or local host in tests - part 4
dfuchs
parents: 51947
diff changeset
   223
51947
2d980757fd07 8211092: test/jdk/sun/net/www/http/HttpClient/MultiThreadTest.java fails intermittently when cleaning up
chegar
parents: 47216
diff changeset
   224
        public Queue<Worker> workers() {
2d980757fd07 8211092: test/jdk/sun/net/www/http/HttpClient/MultiThreadTest.java fails intermittently when cleaning up
chegar
parents: 47216
diff changeset
   225
            return workers;
2d980757fd07 8211092: test/jdk/sun/net/www/http/HttpClient/MultiThreadTest.java fails intermittently when cleaning up
chegar
parents: 47216
diff changeset
   226
        }
2d980757fd07 8211092: test/jdk/sun/net/www/http/HttpClient/MultiThreadTest.java fails intermittently when cleaning up
chegar
parents: 47216
diff changeset
   227
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        public synchronized int connectionCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            return connectionCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        public synchronized void shutdown() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            shutdown = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                ss.setSoTimeout(2000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                    Socket s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                        MultiThreadTest.debug("server: calling accept.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                        s = ss.accept();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                        MultiThreadTest.debug("server: return accept.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                    } catch (SocketTimeoutException te) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                        MultiThreadTest.debug("server: STE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                            if (shutdown) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                                MultiThreadTest.debug("server: Shuting down.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                    int id;
51947
2d980757fd07 8211092: test/jdk/sun/net/www/http/HttpClient/MultiThreadTest.java fails intermittently when cleaning up
chegar
parents: 47216
diff changeset
   258
                    Worker w;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                    synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                        id = connectionCount++;
51947
2d980757fd07 8211092: test/jdk/sun/net/www/http/HttpClient/MultiThreadTest.java fails intermittently when cleaning up
chegar
parents: 47216
diff changeset
   261
                        w = new Worker(s, id);
2d980757fd07 8211092: test/jdk/sun/net/www/http/HttpClient/MultiThreadTest.java fails intermittently when cleaning up
chegar
parents: 47216
diff changeset
   262
                        workers.add(w);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                    w.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                    MultiThreadTest.debug("server: Started worker " + id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                    ss.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                } catch (Exception e) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * Worker thread to service single connection - can service
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * multiple http requests on same connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    class Worker extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        Socket s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        int id;
54885
e58e454c1409 8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents: 54787
diff changeset
   285
        volatile boolean headerFound;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        Worker(Socket s, int id) {
54885
e58e454c1409 8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents: 54787
diff changeset
   288
            super("Worker-" + id);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            this.s = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            this.id = id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        static int requests = 0;
51947
2d980757fd07 8211092: test/jdk/sun/net/www/http/HttpClient/MultiThreadTest.java fails intermittently when cleaning up
chegar
parents: 47216
diff changeset
   294
        static final Object rlock = new Object();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        public static int getRequests () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            synchronized (rlock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                return requests;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        }
54885
e58e454c1409 8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents: 54787
diff changeset
   301
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        public static void incRequests () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            synchronized (rlock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                requests++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
54885
e58e454c1409 8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents: 54787
diff changeset
   308
        int readUntil(InputStream in, StringBuilder headers, char[] seq) throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            int i=0, count=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                int c = in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                if (c == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                    return -1;
54885
e58e454c1409 8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents: 54787
diff changeset
   314
                headers.append((char)c);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                if (c == seq[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                    i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                    if (i == seq.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                        return count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                    i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        public void run() {
51947
2d980757fd07 8211092: test/jdk/sun/net/www/http/HttpClient/MultiThreadTest.java fails intermittently when cleaning up
chegar
parents: 47216
diff changeset
   328
            long start = System.nanoTime();
2d980757fd07 8211092: test/jdk/sun/net/www/http/HttpClient/MultiThreadTest.java fails intermittently when cleaning up
chegar
parents: 47216
diff changeset
   329
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                int max = 400;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                byte b[] = new byte[1000];
51947
2d980757fd07 8211092: test/jdk/sun/net/www/http/HttpClient/MultiThreadTest.java fails intermittently when cleaning up
chegar
parents: 47216
diff changeset
   333
                InputStream in = new BufferedInputStream(s.getInputStream());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                // response to client
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                PrintStream out = new PrintStream(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                                    new BufferedOutputStream(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                                                s.getOutputStream() ));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                    // read entire request from client
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                    int n=0;
54885
e58e454c1409 8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents: 54787
diff changeset
   343
                    StringBuilder headers = new StringBuilder();
e58e454c1409 8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents: 54787
diff changeset
   344
                    n = readUntil(in, headers, new char[] {'\r','\n', '\r','\n'});
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                    if (n <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                        MultiThreadTest.debug("worker: " + id + ": Shutdown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                        s.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                    }
54885
e58e454c1409 8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents: 54787
diff changeset
   350
                    if (headers.toString().contains("/foo.html?foo=")) {
e58e454c1409 8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents: 54787
diff changeset
   351
                        headerFound = true;
e58e454c1409 8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents: 54787
diff changeset
   352
                    } else {
e58e454c1409 8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents: 54787
diff changeset
   353
                        MultiThreadTest.debug("worker: " + id + ": Unexpected request received: " + headers);
e58e454c1409 8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents: 54787
diff changeset
   354
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                    MultiThreadTest.debug("worker " + id +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                        ": Read request from client " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                        "(" + n + " bytes).");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                    incRequests();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                    out.print("HTTP/1.1 200 OK\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                    out.print("Transfer-Encoding: chunked\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                    out.print("Content-Type: text/html\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                    out.print("Connection: Keep-Alive\r\n");
51947
2d980757fd07 8211092: test/jdk/sun/net/www/http/HttpClient/MultiThreadTest.java fails intermittently when cleaning up
chegar
parents: 47216
diff changeset
   365
                    out.print("Keep-Alive: timeout=15, max="+max+"\r\n");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                    out.print("\r\n");
51947
2d980757fd07 8211092: test/jdk/sun/net/www/http/HttpClient/MultiThreadTest.java fails intermittently when cleaning up
chegar
parents: 47216
diff changeset
   367
                    out.print("6\r\nHello \r\n");
2d980757fd07 8211092: test/jdk/sun/net/www/http/HttpClient/MultiThreadTest.java fails intermittently when cleaning up
chegar
parents: 47216
diff changeset
   368
                    out.print("5\r\nWorld\r\n");
2d980757fd07 8211092: test/jdk/sun/net/www/http/HttpClient/MultiThreadTest.java fails intermittently when cleaning up
chegar
parents: 47216
diff changeset
   369
                    out.print("0\r\n\r\n");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                    out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                    if (--max == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                        s.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                    s.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                } catch (Exception e) { }
51947
2d980757fd07 8211092: test/jdk/sun/net/www/http/HttpClient/MultiThreadTest.java fails intermittently when cleaning up
chegar
parents: 47216
diff changeset
   384
                MultiThreadTest.debug("worker: " + id  + " end - " +
2d980757fd07 8211092: test/jdk/sun/net/www/http/HttpClient/MultiThreadTest.java fails intermittently when cleaning up
chegar
parents: 47216
diff changeset
   385
                            Duration.ofNanos(System.nanoTime() - start));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    }