jdk/test/java/nio/charset/coders/StreamTimeout.java
author iklam
Tue, 12 Aug 2014 17:29:01 -0700
changeset 26212 7585e2404b2a
parent 12441 dc1628ee8f30
permissions -rw-r--r--
8046070: Class Data Sharing clean up and refactoring Summary: Cleaned up CDS to be more configurable, maintainable and extensible Reviewed-by: dholmes, coleenp, acorn, mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4817
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
     1
/*
12441
dc1628ee8f30 7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents: 5506
diff changeset
     2
 * Copyright (c) 2010, 2012, Oracle and/or its affiliates. All rights reserved.
4817
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
     4
 *
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
     7
 * published by the Free Software Foundation.
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
     8
 *
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    13
 * accompanied this code).
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    14
 *
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4817
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4817
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4817
diff changeset
    21
 * questions.
4817
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    22
 */
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    23
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    24
/* @test
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    25
 * @bug 4521942
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    26
 * @summary Ensure that InputStreamReaders work properly
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    27
 *          when the underlying byte stream times out
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    28
 */
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    29
12441
dc1628ee8f30 7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents: 5506
diff changeset
    30
import java.io.Closeable;
dc1628ee8f30 7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents: 5506
diff changeset
    31
import java.io.IOException;
dc1628ee8f30 7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents: 5506
diff changeset
    32
import java.io.InputStream;
dc1628ee8f30 7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents: 5506
diff changeset
    33
import java.io.InputStreamReader;
dc1628ee8f30 7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents: 5506
diff changeset
    34
import java.io.InterruptedIOException;
dc1628ee8f30 7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents: 5506
diff changeset
    35
import java.io.OutputStreamWriter;
dc1628ee8f30 7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents: 5506
diff changeset
    36
import java.io.PrintStream;
dc1628ee8f30 7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents: 5506
diff changeset
    37
import java.io.Reader;
dc1628ee8f30 7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents: 5506
diff changeset
    38
import java.io.Writer;
dc1628ee8f30 7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents: 5506
diff changeset
    39
import java.net.ServerSocket;
dc1628ee8f30 7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents: 5506
diff changeset
    40
import java.net.Socket;
4817
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    41
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    42
public class StreamTimeout {
12441
dc1628ee8f30 7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents: 5506
diff changeset
    43
    static final PrintStream log = System.err;
dc1628ee8f30 7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents: 5506
diff changeset
    44
    static String charset = "US-ASCII";
4817
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    45
12441
dc1628ee8f30 7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents: 5506
diff changeset
    46
    private static class Client extends Thread implements Closeable {
dc1628ee8f30 7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents: 5506
diff changeset
    47
        private final Socket so;
4817
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    48
12441
dc1628ee8f30 7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents: 5506
diff changeset
    49
        Client(int port) throws IOException {
dc1628ee8f30 7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents: 5506
diff changeset
    50
            so = new Socket("127.0.0.1", port);
4817
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    51
        }
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    52
12441
dc1628ee8f30 7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents: 5506
diff changeset
    53
        @Override
4817
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    54
        public void run() {
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    55
            try {
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    56
                Writer wr = new OutputStreamWriter(so.getOutputStream(),
12441
dc1628ee8f30 7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents: 5506
diff changeset
    57
                        charset);
4817
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    58
                wr.write("ab");
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    59
                wr.flush();
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    60
            } catch (IOException x) {
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    61
                log.print("Unexpected exception in writer: ");
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    62
                x.printStackTrace();
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    63
            }
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    64
        }
12441
dc1628ee8f30 7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents: 5506
diff changeset
    65
dc1628ee8f30 7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents: 5506
diff changeset
    66
        @Override
dc1628ee8f30 7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents: 5506
diff changeset
    67
        public void close() throws IOException {
dc1628ee8f30 7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents: 5506
diff changeset
    68
            so.close();
dc1628ee8f30 7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents: 5506
diff changeset
    69
        }
4817
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    70
    }
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    71
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    72
    private static void gobble(InputStream is, Reader rd,
12441
dc1628ee8f30 7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents: 5506
diff changeset
    73
            int ec, boolean force)
dc1628ee8f30 7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents: 5506
diff changeset
    74
                    throws Exception
dc1628ee8f30 7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents: 5506
diff changeset
    75
                    {
4817
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    76
        int a = is.available();
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    77
        boolean r = rd.ready();
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    78
        log.print("" + a + " bytes available, "
12441
dc1628ee8f30 7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents: 5506
diff changeset
    79
                + "reader " + (r ? "" : "not ") + "ready");
4817
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    80
        if (!r && !force) {
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    81
            log.println();
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    82
            return;
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    83
        }
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    84
        int c;
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    85
        try {
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    86
            c = rd.read();
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    87
        } catch (InterruptedIOException x) {
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    88
            log.println();
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    89
            throw x;
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    90
        }
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    91
        log.println(", read() ==> "
12441
dc1628ee8f30 7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents: 5506
diff changeset
    92
                + (c >= 0 ? ("'" + (char)c + "'" ): "EOF"));
4817
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    93
        if (c != ec)
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    94
            throw new Exception("Incorrect value read: Expected "
12441
dc1628ee8f30 7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents: 5506
diff changeset
    95
                    + ec + ", read " + (char)c);
4817
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    96
    }
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    97
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    98
    public static void main(String[] args) throws Exception {
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    99
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   100
        if (args.length > 0)
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   101
            charset = args[0];
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   102
12441
dc1628ee8f30 7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents: 5506
diff changeset
   103
        try(ServerSocket ss = new ServerSocket(0);
dc1628ee8f30 7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents: 5506
diff changeset
   104
            Client cl = new Client(ss.getLocalPort())) {
dc1628ee8f30 7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents: 5506
diff changeset
   105
dc1628ee8f30 7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents: 5506
diff changeset
   106
            cl.start();
dc1628ee8f30 7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents: 5506
diff changeset
   107
dc1628ee8f30 7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents: 5506
diff changeset
   108
            try(Socket s = ss.accept()) {
dc1628ee8f30 7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents: 5506
diff changeset
   109
                s.setSoTimeout(150);
4817
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   110
12441
dc1628ee8f30 7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents: 5506
diff changeset
   111
                try(InputStream is = s.getInputStream();
dc1628ee8f30 7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents: 5506
diff changeset
   112
                    Reader rd = new InputStreamReader(is, charset)) {
4817
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   113
12441
dc1628ee8f30 7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents: 5506
diff changeset
   114
                    while (is.available() <= 0)
dc1628ee8f30 7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents: 5506
diff changeset
   115
                        Thread.yield();
dc1628ee8f30 7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents: 5506
diff changeset
   116
dc1628ee8f30 7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents: 5506
diff changeset
   117
                    gobble(is, rd, 'a', false);
dc1628ee8f30 7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents: 5506
diff changeset
   118
                    gobble(is, rd, 'b', false);
dc1628ee8f30 7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents: 5506
diff changeset
   119
                    gobble(is, rd, -1, false);
4817
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   120
12441
dc1628ee8f30 7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents: 5506
diff changeset
   121
                    boolean caught = false;
dc1628ee8f30 7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents: 5506
diff changeset
   122
                    try {
dc1628ee8f30 7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents: 5506
diff changeset
   123
                        gobble(is, rd, -1, true);
dc1628ee8f30 7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents: 5506
diff changeset
   124
                    } catch (InterruptedIOException e) {
dc1628ee8f30 7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents: 5506
diff changeset
   125
                        log.println("Read timed out, as expected");
dc1628ee8f30 7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents: 5506
diff changeset
   126
                        caught = true;
dc1628ee8f30 7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents: 5506
diff changeset
   127
                    }
dc1628ee8f30 7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents: 5506
diff changeset
   128
                    if (!caught) {
dc1628ee8f30 7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents: 5506
diff changeset
   129
                        log.println("Read did not time out, test inapplicable");
dc1628ee8f30 7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents: 5506
diff changeset
   130
                        return;
dc1628ee8f30 7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents: 5506
diff changeset
   131
                    }
4817
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   132
12441
dc1628ee8f30 7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents: 5506
diff changeset
   133
                    caught = false;
dc1628ee8f30 7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents: 5506
diff changeset
   134
                    try {
dc1628ee8f30 7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents: 5506
diff changeset
   135
                        gobble(is, rd, -1, true);
dc1628ee8f30 7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents: 5506
diff changeset
   136
                    } catch (InterruptedIOException x) {
dc1628ee8f30 7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents: 5506
diff changeset
   137
                        log.println("Second read timed out, as expected");
dc1628ee8f30 7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents: 5506
diff changeset
   138
                        caught = true;
dc1628ee8f30 7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents: 5506
diff changeset
   139
                    }
dc1628ee8f30 7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents: 5506
diff changeset
   140
                    if (!caught)
dc1628ee8f30 7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents: 5506
diff changeset
   141
                        throw new Exception("Second read completed");
dc1628ee8f30 7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents: 5506
diff changeset
   142
                }
dc1628ee8f30 7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents: 5506
diff changeset
   143
            }
dc1628ee8f30 7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents: 5506
diff changeset
   144
dc1628ee8f30 7116200: (cs) test/java/nio/charset/coders/StreamTimeout.java fails with "Address already in use"
alanb
parents: 5506
diff changeset
   145
            cl.join();
4817
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   146
        }
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   147
    }
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   148
}