File: CloseTimeoutChannel.java

package info (click to toggle)
openjdk-11 11.0.4%2B11-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 757,028 kB
  • sloc: java: 5,016,041; xml: 1,191,974; cpp: 934,731; ansic: 555,697; sh: 24,299; objc: 12,703; python: 3,602; asm: 3,415; makefile: 2,772; awk: 351; sed: 172; perl: 114; jsp: 24; csh: 3
file content (133 lines) | stat: -rw-r--r-- 4,719 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/* @test
   @bug 4726957 4724030 6232954
   @summary Test if the SocketChannel with timeout set can be closed immediately
   @run main/timeout=20 CloseTimeoutChannel
 */

import java.io.*;
import java.nio.*;
import java.nio.channels.*;
import java.net.*;

public class CloseTimeoutChannel {
    public static void main(String args[]) throws Exception {
        int port = -1;
        try {
            ServerSocketChannel listener=ServerSocketChannel.open();
            listener.socket().bind(new InetSocketAddress(0));
            port = listener.socket().getLocalPort();
            AcceptorThread thread=new AcceptorThread(listener);
            thread.start();
        } catch (IOException e) {
            System.out.println("Mysterious IO problem");
            e.printStackTrace();
            System.exit(1);
        }

        //Establish connection.  Bug only happens if we open with channel.
        try {
            System.out.println("Establishing connection");
            Socket socket=SocketChannel.open(
                new InetSocketAddress("127.0.0.1", port)).socket();
            OutputStream out=socket.getOutputStream();
            InputStream in=socket.getInputStream();

            System.out.println("1. Writing byte 1");
            out.write((byte)1);

            int n=read(socket, in);
            System.out.println("Read byte "+n+"\n");

            System.out.println("3. Writing byte 3");
            out.write((byte)3);

            System.out.println("Closing");
            socket.close();
        } catch (IOException e) {
            System.out.println("Mysterious IO problem");
            e.printStackTrace();
            System.exit(1);
        }
    }

    /** Reads one byte from in, which must be s.getInputStream.  */
    private static int read(Socket s, InputStream in) throws IOException {
        try {
            s.setSoTimeout(8000);     //causes a bug!
            return in.read();
        } finally {
            s.setSoTimeout(0);
        }

    }

    /** Server thread */
    static class AcceptorThread extends Thread {
        final String INDENT="\t\t\t\t";
        ServerSocketChannel _listener;
        /** @param listener MUST be bound to a port */
        AcceptorThread(ServerSocketChannel listener) {
            _listener=listener;
        }

        public void run() {
            try {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) { }

                System.out.println(INDENT+"Listening on port "+
                    _listener.socket().getLocalPort());
                ByteBuffer buf=ByteBuffer.allocate(5);
                Socket client=_listener.accept().socket();;
                System.out.println(INDENT+"Accepted client");

                OutputStream out=client.getOutputStream();
                InputStream in=client.getInputStream();

                int n=in.read();
                System.out.println(INDENT+"Read byte "+n+"\n");

                System.out.println(INDENT+"2. Writing byte 2");
                out.write((byte)2);

                n=in.read();
                System.out.println(INDENT+"Read byte "+n+"\n");

                n=in.read();
                System.out.println(INDENT+"Read byte "
                                   +(n<0 ? "EOF" : Integer.toString(n)));

                System.out.println(INDENT+"Closing");
                client.close();
            } catch (IOException e) {
                System.out.println(INDENT+"Error accepting!");
            } finally {
                try { _listener.close(); } catch (IOException ignore) { }
            }
        }
    }
}