test/jdk/java/net/Socket/LingerTest.java
author chegar
Sun, 05 Nov 2017 17:32:13 +0000
branchhttp-client-branch
changeset 55763 634d8e14c172
parent 47216 71c04702a3d5
child 49431 5812849b5027
permissions -rw-r--r--
http-client-branch: intial load from jdk10/sandbox
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 21818
diff changeset
     2
 * Copyright (c) 2003, 2013, 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 4796166
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Linger interval delays usage of released file descriptor
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
public class LingerTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    static class Sender implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
        Socket s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
        public Sender(Socket s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
            this.s = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
            System.out.println ("Sender starts");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
                s.getOutputStream().write(new byte[128*1024]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
            catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
            System.out.println ("Sender ends");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    static class Closer implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        Socket s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        public Closer(Socket s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            this.s = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            System.out.println ("Closer starts");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                s.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            System.out.println ("Closer ends");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
21818
1f6c379e48f6 8028581: [TESTBUG] java/net/Socket/LingerTest.java failing
michaelm
parents: 7668
diff changeset
    71
    static class Other implements Runnable {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        int port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        long delay;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        boolean connected = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
21818
1f6c379e48f6 8028581: [TESTBUG] java/net/Socket/LingerTest.java failing
michaelm
parents: 7668
diff changeset
    76
        public Other(int port, long delay) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            this.port = port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            this.delay = delay;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        public void run() {
21818
1f6c379e48f6 8028581: [TESTBUG] java/net/Socket/LingerTest.java failing
michaelm
parents: 7668
diff changeset
    82
            System.out.println ("Other starts: sleep " + delay);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            try {
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    84
                Thread.sleep(delay);
21818
1f6c379e48f6 8028581: [TESTBUG] java/net/Socket/LingerTest.java failing
michaelm
parents: 7668
diff changeset
    85
                System.out.println ("Other opening socket");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                Socket s = new Socket("localhost", port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                    connected = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                s.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            catch (Exception ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                ioe.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            }
21818
1f6c379e48f6 8028581: [TESTBUG] java/net/Socket/LingerTest.java failing
michaelm
parents: 7668
diff changeset
    95
            System.out.println ("Other ends");
2
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 synchronized boolean connected() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            return connected;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    public static void main(String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        ServerSocket ss = new ServerSocket(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        Socket s1 = new Socket("localhost", ss.getLocalPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        Socket s2 = ss.accept();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        // setup conditions for untransmitted data and lengthy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            // linger interval
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            s1.setSendBufferSize(128*1024);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        s1.setSoLinger(true, 30);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        s2.setReceiveBufferSize(1*1024);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        // start sender
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            Thread thr = new Thread(new Sender(s1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        thr.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
21818
1f6c379e48f6 8028581: [TESTBUG] java/net/Socket/LingerTest.java failing
michaelm
parents: 7668
diff changeset
   119
        // other thread that will connect after 5 seconds.
1f6c379e48f6 8028581: [TESTBUG] java/net/Socket/LingerTest.java failing
michaelm
parents: 7668
diff changeset
   120
            Other other = new Other(ss.getLocalPort(), 5000);
1f6c379e48f6 8028581: [TESTBUG] java/net/Socket/LingerTest.java failing
michaelm
parents: 7668
diff changeset
   121
        thr = new Thread(other);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        thr.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        // give sender time to queue the data
21818
1f6c379e48f6 8028581: [TESTBUG] java/net/Socket/LingerTest.java failing
michaelm
parents: 7668
diff changeset
   125
            System.out.println ("Main sleep 1000");
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   126
            Thread.sleep(1000);
21818
1f6c379e48f6 8028581: [TESTBUG] java/net/Socket/LingerTest.java failing
michaelm
parents: 7668
diff changeset
   127
            System.out.println ("Main continue");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        // close the socket asynchronously
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            (new Thread(new Closer(s1))).start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
21818
1f6c379e48f6 8028581: [TESTBUG] java/net/Socket/LingerTest.java failing
michaelm
parents: 7668
diff changeset
   132
            System.out.println ("Main sleep 15000");
1f6c379e48f6 8028581: [TESTBUG] java/net/Socket/LingerTest.java failing
michaelm
parents: 7668
diff changeset
   133
        // give other time to run
1f6c379e48f6 8028581: [TESTBUG] java/net/Socket/LingerTest.java failing
michaelm
parents: 7668
diff changeset
   134
            Thread.sleep(15000);
1f6c379e48f6 8028581: [TESTBUG] java/net/Socket/LingerTest.java failing
michaelm
parents: 7668
diff changeset
   135
            System.out.println ("Main closing serversocket");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   137
        ss.close();
21818
1f6c379e48f6 8028581: [TESTBUG] java/net/Socket/LingerTest.java failing
michaelm
parents: 7668
diff changeset
   138
        // check that other is done
1f6c379e48f6 8028581: [TESTBUG] java/net/Socket/LingerTest.java failing
michaelm
parents: 7668
diff changeset
   139
            if (!other.connected()) {
1f6c379e48f6 8028581: [TESTBUG] java/net/Socket/LingerTest.java failing
michaelm
parents: 7668
diff changeset
   140
            throw new RuntimeException("Other thread is blocked");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        System.out.println ("Main ends");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
}