test/jdk/java/nio/channels/SocketChannel/AdaptorStreams.java
author alanb
Thu, 25 Apr 2019 10:41:49 +0100
changeset 54620 13b67c1420b8
child 54902 d1717e05e51c
permissions -rw-r--r--
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks Reviewed-by: dfuchs, bpb, martin
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
     1
/*
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
     4
 *
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
     8
 *
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    13
 * accompanied this code).
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    14
 *
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    18
 *
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    21
 * questions.
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    22
 */
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    23
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    24
/* @test
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    25
 * @bug 8222774 4430139
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    26
 * @run testng AdaptorStreams
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    27
 * @summary Exercise socket adaptor input/output streams
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    28
 */
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    29
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    30
import java.io.Closeable;
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    31
import java.io.IOException;
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    32
import java.io.InputStream;
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    33
import java.io.OutputStream;
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    34
import java.net.ServerSocket;
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    35
import java.net.Socket;
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    36
import java.net.SocketTimeoutException;
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    37
import java.nio.channels.IllegalBlockingModeException;
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    38
import java.nio.channels.SocketChannel;
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    39
import java.util.concurrent.ExecutorService;
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    40
import java.util.concurrent.Executors;
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    41
import java.util.concurrent.Future;
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    42
import java.util.concurrent.ScheduledExecutorService;
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    43
import java.util.concurrent.TimeUnit;
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    44
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    45
import org.testng.annotations.Test;
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    46
import static org.testng.Assert.*;
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    47
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    48
@Test
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    49
public class AdaptorStreams {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    50
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    51
    /**
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    52
     * Test read when bytes are available
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    53
     */
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    54
    public void testRead1() throws Exception {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    55
        withConnection((sc, peer) -> {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    56
            peer.getOutputStream().write(99);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    57
            int n = sc.socket().getInputStream().read();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    58
            assertTrue(n == 99);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    59
        });
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    60
    }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    61
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    62
    /**
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    63
     * Test read blocking before bytes are available
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    64
     */
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    65
    public void testRead2() throws Exception {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    66
        withConnection((sc, peer) -> {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    67
            scheduleWrite(peer.getOutputStream(), 99, 1000);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    68
            int n = sc.socket().getInputStream().read();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    69
            assertTrue(n == 99);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    70
        });
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    71
    }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    72
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    73
    /**
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    74
     * Test read when peer has closed connection
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    75
     */
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    76
    public void testRead3() throws Exception {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    77
        withConnection((sc, peer) -> {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    78
            peer.close();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    79
            int n = sc.socket().getInputStream().read();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    80
            assertTrue(n == -1);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    81
        });
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    82
    }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    83
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    84
    /**
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    85
     * Test read blocking before peer closes connection
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    86
     */
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    87
    public void testRead4() throws Exception {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    88
        withConnection((sc, peer) -> {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    89
            scheduleClose(peer, 1000);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    90
            int n = sc.socket().getInputStream().read();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    91
            assertTrue(n == -1);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    92
        });
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    93
    }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    94
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    95
    /**
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    96
     * Test async close of socket when thread blocked in read
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    97
     */
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    98
    public void testRead5() throws Exception {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
    99
        withConnection((sc, peer) -> {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   100
            scheduleClose(sc, 2000);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   101
            InputStream in = sc.socket().getInputStream();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   102
            expectThrows(IOException.class, () -> in.read());
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   103
        });
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   104
    }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   105
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   106
    /**
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   107
     * Test interrupt status set before read
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   108
     */
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   109
    public void testRead6() throws Exception {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   110
        withConnection((sc, peer) -> {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   111
            Socket s = sc.socket();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   112
            Thread.currentThread().interrupt();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   113
            try {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   114
                InputStream in = s.getInputStream();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   115
                expectThrows(IOException.class, () -> in.read());
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   116
            } finally {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   117
                Thread.interrupted();  // clear interrupt
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   118
            }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   119
            assertTrue(s.isClosed());
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   120
        });
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   121
    }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   122
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   123
    /**
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   124
     * Test interrupt of thread blocked in read
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   125
     */
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   126
    public void testRead7() throws Exception {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   127
        withConnection((sc, peer) -> {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   128
            Future<?> interrupter = scheduleInterrupt(Thread.currentThread(), 2000);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   129
            Socket s = sc.socket();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   130
            try {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   131
                InputStream in = s.getInputStream();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   132
                expectThrows(IOException.class, () -> in.read());
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   133
            } finally {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   134
                interrupter.cancel(true);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   135
                Thread.interrupted();  // clear interrupt
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   136
            }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   137
            assertTrue(s.isClosed());
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   138
        });
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   139
    }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   140
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   141
    /**
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   142
     * Test read when channel is configured non-blocking
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   143
     */
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   144
    public void testRead8() throws Exception {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   145
        withConnection((sc, peer) -> {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   146
            sc.configureBlocking(false);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   147
            InputStream in = sc.socket().getInputStream();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   148
            expectThrows(IllegalBlockingModeException.class, () -> in.read());
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   149
        });
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   150
    }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   151
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   152
    /**
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   153
     * Test timed read when bytes are available
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   154
     */
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   155
    public void testTimedRead1() throws Exception {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   156
        withConnection((sc, peer) -> {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   157
            peer.getOutputStream().write(99);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   158
            Socket s = sc.socket();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   159
            s.setSoTimeout(1000);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   160
            int n = s.getInputStream().read();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   161
            assertTrue(n == 99);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   162
        });
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   163
    }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   164
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   165
    /**
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   166
     * Test timed read blocking before bytes are available
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   167
     */
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   168
    public void testTimedRead2() throws Exception {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   169
        withConnection((sc, peer) -> {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   170
            scheduleWrite(peer.getOutputStream(), 99, 1000);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   171
            Socket s = sc.socket();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   172
            s.setSoTimeout(5000);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   173
            int n = s.getInputStream().read();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   174
            assertTrue(n == 99);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   175
        });
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   176
    }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   177
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   178
    /**
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   179
     * Test timed read when the read times out
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   180
     */
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   181
    public void testTimedRead3() throws Exception {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   182
        withConnection((sc, peer) -> {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   183
            Socket s = sc.socket();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   184
            s.setSoTimeout(1000);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   185
            InputStream in = s.getInputStream();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   186
            expectThrows(SocketTimeoutException.class, () -> in.read());
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   187
        });
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   188
    }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   189
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   190
    /**
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   191
     * Test async close of socket when thread blocked in timed read
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   192
     */
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   193
    public void testTimedRead4() throws Exception {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   194
        withConnection((sc, peer) -> {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   195
            scheduleClose(sc, 2000);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   196
            Socket s = sc.socket();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   197
            s.setSoTimeout(60*1000);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   198
            InputStream in = s.getInputStream();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   199
            expectThrows(IOException.class, () -> in.read());
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   200
        });
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   201
    }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   202
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   203
    /**
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   204
     * Test interrupt status set before timed read
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   205
     */
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   206
    public void testTimedRead5() throws Exception {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   207
        withConnection((sc, peer) -> {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   208
            Socket s = sc.socket();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   209
            Thread.currentThread().interrupt();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   210
            try {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   211
                s.setSoTimeout(60*1000);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   212
                InputStream in = s.getInputStream();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   213
                expectThrows(IOException.class, () -> in.read());
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   214
            } finally {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   215
                Thread.interrupted();  // clear interrupt
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   216
            }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   217
            assertTrue(s.isClosed());
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   218
        });
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   219
    }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   220
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   221
    /**
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   222
     * Test interrupt of thread blocked in timed read
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   223
     */
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   224
    public void testTimedRead6() throws Exception {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   225
        withConnection((sc, peer) -> {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   226
            Future<?> interrupter = scheduleInterrupt(Thread.currentThread(), 2000);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   227
            Socket s = sc.socket();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   228
            try {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   229
                s.setSoTimeout(60*1000);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   230
                InputStream in = s.getInputStream();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   231
                expectThrows(IOException.class, () -> in.read());
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   232
                assertTrue(s.isClosed());
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   233
            } finally {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   234
                interrupter.cancel(true);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   235
                Thread.interrupted();  // clear interrupt
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   236
            }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   237
            assertTrue(s.isClosed());
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   238
        });
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   239
    }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   240
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   241
    /**
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   242
     * Test async close of socket when thread blocked in write
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   243
     */
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   244
    public void testWrite1() throws Exception {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   245
        withConnection((sc, peer) -> {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   246
            scheduleClose(sc, 2000);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   247
            expectThrows(IOException.class, () -> {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   248
                OutputStream out = sc.socket().getOutputStream();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   249
                byte[] data = new byte[64*1000];
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   250
                while (true) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   251
                    out.write(data);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   252
                }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   253
            });
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   254
        });
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   255
    }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   256
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   257
    /**
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   258
     * Test interrupt status set before write
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   259
     */
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   260
    public void testWrite2() throws Exception {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   261
        withConnection((sc, peer) -> {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   262
            Socket s = sc.socket();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   263
            Thread.currentThread().interrupt();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   264
            try {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   265
                OutputStream out = s.getOutputStream();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   266
                expectThrows(IOException.class, () -> out.write(99));
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   267
            } finally {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   268
                Thread.interrupted();  // clear interrupt
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   269
            }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   270
            assertTrue(s.isClosed());
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   271
        });
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   272
    }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   273
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   274
    /**
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   275
     * Test interrupt of thread blocked in write
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   276
     */
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   277
    public void testWrite3() throws Exception {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   278
        withConnection((sc, peer) -> {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   279
            Future<?> interrupter = scheduleInterrupt(Thread.currentThread(), 2000);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   280
            Socket s = sc.socket();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   281
            try {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   282
                expectThrows(IOException.class, () -> {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   283
                    OutputStream out = sc.socket().getOutputStream();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   284
                    byte[] data = new byte[64*1000];
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   285
                    while (true) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   286
                        out.write(data);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   287
                    }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   288
                });
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   289
            } finally {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   290
                interrupter.cancel(true);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   291
                Thread.interrupted();  // clear interrupt
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   292
            }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   293
            assertTrue(s.isClosed());
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   294
        });
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   295
    }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   296
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   297
    /**
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   298
     * Test write when channel is configured non-blocking
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   299
     */
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   300
    public void testWrite4() throws Exception {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   301
        withConnection((sc, peer) -> {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   302
            sc.configureBlocking(false);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   303
            OutputStream out = sc.socket().getOutputStream();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   304
            expectThrows(IllegalBlockingModeException.class, () -> out.write(99));
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   305
        });
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   306
    }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   307
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   308
    /**
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   309
     * Test read when there are bytes available and another thread is blocked
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   310
     * in write
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   311
     */
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   312
    public void testConcurrentReadWrite1() throws Exception {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   313
        withConnection((sc, peer) -> {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   314
            Socket s = sc.socket();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   315
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   316
            // block thread in write
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   317
            execute(() -> {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   318
                var data = new byte[64*1024];
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   319
                OutputStream out = s.getOutputStream();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   320
                for (;;) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   321
                    out.write(data);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   322
                }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   323
            });
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   324
            Thread.sleep(1000); // give writer time to block
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   325
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   326
            // test read when bytes are available
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   327
            peer.getOutputStream().write(99);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   328
            int n = s.getInputStream().read();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   329
            assertTrue(n == 99);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   330
        });
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   331
    }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   332
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   333
    /**
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   334
     * Test read blocking when another thread is blocked in write
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   335
     */
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   336
    public void testConcurrentReadWrite2() throws Exception {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   337
        withConnection((sc, peer) -> {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   338
            Socket s = sc.socket();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   339
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   340
            // block thread in write
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   341
            execute(() -> {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   342
                var data = new byte[64*1024];
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   343
                OutputStream out = s.getOutputStream();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   344
                for (;;) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   345
                    out.write(data);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   346
                }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   347
            });
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   348
            Thread.sleep(1000); // give writer time to block
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   349
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   350
            // test read blocking until bytes are available
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   351
            scheduleWrite(peer.getOutputStream(), 99, 500);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   352
            int n = s.getInputStream().read();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   353
            assertTrue(n == 99);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   354
        });
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   355
    }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   356
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   357
    /**
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   358
     * Test writing when another thread is blocked in read
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   359
     */
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   360
    public void testConcurrentReadWrite3() throws Exception {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   361
        withConnection((sc, peer) -> {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   362
            Socket s = sc.socket();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   363
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   364
            // block thread in read
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   365
            execute(() -> {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   366
                s.getInputStream().read();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   367
            });
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   368
            Thread.sleep(100); // give reader time to block
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   369
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   370
            // test write
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   371
            s.getOutputStream().write(99);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   372
            int n = peer.getInputStream().read();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   373
            assertTrue(n == 99);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   374
        });
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   375
    }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   376
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   377
    /**
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   378
     * Test timed read when there are bytes available and another thread is
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   379
     * blocked in write
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   380
     */
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   381
    public void testConcurrentTimedReadWrite1() throws Exception {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   382
        withConnection((sc, peer) -> {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   383
            Socket s = sc.socket();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   384
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   385
            // block thread in write
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   386
            execute(() -> {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   387
                var data = new byte[64*1024];
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   388
                OutputStream out = s.getOutputStream();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   389
                for (;;) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   390
                    out.write(data);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   391
                }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   392
            });
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   393
            Thread.sleep(1000); // give writer time to block
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   394
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   395
            // test read when bytes are available
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   396
            peer.getOutputStream().write(99);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   397
            s.setSoTimeout(60*1000);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   398
            int n = s.getInputStream().read();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   399
            assertTrue(n == 99);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   400
        });
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   401
    }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   402
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   403
    /**
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   404
     * Test timed read blocking when another thread is blocked in write
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   405
     */
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   406
    public void testConcurrentTimedReadWrite2() throws Exception {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   407
        withConnection((sc, peer) -> {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   408
            Socket s = sc.socket();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   409
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   410
            // block thread in write
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   411
            execute(() -> {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   412
                var data = new byte[64*1024];
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   413
                OutputStream out = s.getOutputStream();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   414
                for (;;) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   415
                    out.write(data);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   416
                }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   417
            });
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   418
            Thread.sleep(1000); // give writer time to block
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   419
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   420
            // test read blocking until bytes are available
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   421
            scheduleWrite(peer.getOutputStream(), 99, 500);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   422
            s.setSoTimeout(60*1000);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   423
            int n = s.getInputStream().read();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   424
            assertTrue(n == 99);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   425
        });
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   426
    }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   427
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   428
    /**
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   429
     * Test writing when another thread is blocked in read
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   430
     */
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   431
    public void testConcurrentTimedReadWrite3() throws Exception {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   432
        withConnection((sc, peer) -> {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   433
            Socket s = sc.socket();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   434
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   435
            // block thread in read
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   436
            execute(() -> {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   437
                s.setSoTimeout(60*1000);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   438
                s.getInputStream().read();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   439
            });
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   440
            Thread.sleep(100); // give reader time to block
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   441
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   442
            // test write
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   443
            s.getOutputStream().write(99);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   444
            int n = peer.getInputStream().read();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   445
            assertTrue(n == 99);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   446
        });
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   447
    }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   448
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   449
    // -- test infrastructure --
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   450
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   451
    interface ThrowingTask {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   452
        void run() throws Exception;
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   453
    }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   454
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   455
    interface ThrowingBiConsumer<T, U> {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   456
        void accept(T t, U u) throws Exception;
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   457
    }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   458
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   459
    /**
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   460
     * Invokes the consumer with a connected pair of socket channel and socket
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   461
     */
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   462
    static void withConnection(ThrowingBiConsumer<SocketChannel, Socket> consumer)
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   463
        throws Exception
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   464
    {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   465
        try (ServerSocket ss = new ServerSocket(0);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   466
             SocketChannel sc = SocketChannel.open(ss.getLocalSocketAddress());
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   467
             Socket peer = ss.accept()) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   468
            consumer.accept(sc, peer);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   469
        }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   470
    }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   471
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   472
    static Future<?> scheduleWrite(OutputStream out, byte[] data, long delay) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   473
        return schedule(() -> {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   474
            try {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   475
                out.write(data);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   476
            } catch (IOException ioe) { }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   477
        }, delay);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   478
    }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   479
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   480
    static Future<?> scheduleWrite(OutputStream out, int b, long delay) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   481
        return scheduleWrite(out, new byte[] { (byte)b }, delay);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   482
    }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   483
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   484
    static Future<?> scheduleClose(Closeable c, long delay) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   485
        return schedule(() -> {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   486
            try {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   487
                c.close();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   488
            } catch (IOException ioe) { }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   489
        }, delay);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   490
    }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   491
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   492
    static Future<?> scheduleInterrupt(Thread t, long delay) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   493
        return schedule(() -> t.interrupt(), delay);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   494
    }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   495
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   496
    static Future<?> schedule(Runnable task, long delay) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   497
        ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   498
        try {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   499
            return executor.schedule(task, delay, TimeUnit.MILLISECONDS);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   500
        } finally {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   501
            executor.shutdown();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   502
        }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   503
    }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   504
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   505
    static Future<?> execute(ThrowingTask task) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   506
        ExecutorService pool = Executors.newFixedThreadPool(1);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   507
        try {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   508
            return pool.submit(() -> {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   509
                task.run();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   510
                return null;
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   511
            });
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   512
        } finally {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   513
            pool.shutdown();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   514
        }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   515
    }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
diff changeset
   516
}