test/jdk/java/nio/channels/Selector/RegisterDuringSelect.java
author xiaofeya
Fri, 13 Jul 2018 11:21:55 +0800
changeset 51077 9baa91bc7567
parent 49802 8ac08fa69f00
child 51602 dbb0e798deeb
permissions -rw-r--r--
8202481: RegisterDuringSelect.java fails with java.util.concurrent.ExecutionException: java.nio.channels.ClosedSelectorException 8207023: Add trace info to java/nio/channels/Selector/RegisterDuringSelect.java Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
49802
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
     1
/*
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
     4
 *
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
     8
 *
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    13
 * accompanied this code).
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    14
 *
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    18
 *
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    21
 * questions.
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    22
 */
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    23
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    24
/* @test
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    25
 * @bug 8201315
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    26
 * @summary Test that channels can be registered, interest ops can changed,
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    27
 *          and keys cancelled while a selection operation is in progress.
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    28
 */
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    29
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    30
import java.io.IOException;
51077
9baa91bc7567 8202481: RegisterDuringSelect.java fails with java.util.concurrent.ExecutionException: java.nio.channels.ClosedSelectorException
xiaofeya
parents: 49802
diff changeset
    31
import java.nio.channels.ClosedSelectorException;
49802
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    32
import java.nio.channels.Pipe;
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    33
import java.nio.channels.SelectionKey;
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    34
import java.nio.channels.Selector;
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    35
import java.util.concurrent.Callable;
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    36
import java.util.concurrent.ExecutorService;
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    37
import java.util.concurrent.Executors;
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    38
import java.util.concurrent.Future;
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    39
import java.util.concurrent.Phaser;
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    40
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    41
public class RegisterDuringSelect {
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    42
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    43
    static Callable<Void> selectLoop(Selector sel, Phaser barrier) {
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    44
        return new Callable<Void>() {
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    45
            @Override
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    46
            public Void call() throws IOException {
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    47
                for (;;) {
51077
9baa91bc7567 8202481: RegisterDuringSelect.java fails with java.util.concurrent.ExecutionException: java.nio.channels.ClosedSelectorException
xiaofeya
parents: 49802
diff changeset
    48
                    try {
9baa91bc7567 8202481: RegisterDuringSelect.java fails with java.util.concurrent.ExecutionException: java.nio.channels.ClosedSelectorException
xiaofeya
parents: 49802
diff changeset
    49
                        sel.select();
9baa91bc7567 8202481: RegisterDuringSelect.java fails with java.util.concurrent.ExecutionException: java.nio.channels.ClosedSelectorException
xiaofeya
parents: 49802
diff changeset
    50
                    } catch (ClosedSelectorException ignore) {
9baa91bc7567 8202481: RegisterDuringSelect.java fails with java.util.concurrent.ExecutionException: java.nio.channels.ClosedSelectorException
xiaofeya
parents: 49802
diff changeset
    51
                        return null;
9baa91bc7567 8202481: RegisterDuringSelect.java fails with java.util.concurrent.ExecutionException: java.nio.channels.ClosedSelectorException
xiaofeya
parents: 49802
diff changeset
    52
                    }
49802
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    53
                    if (sel.isOpen()) {
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    54
                        barrier.arriveAndAwaitAdvance();
51077
9baa91bc7567 8202481: RegisterDuringSelect.java fails with java.util.concurrent.ExecutionException: java.nio.channels.ClosedSelectorException
xiaofeya
parents: 49802
diff changeset
    55
                        System.out.println("selectLoop advanced ...");
49802
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    56
                    } else {
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    57
                        // closed
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    58
                        return null;
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    59
                    }
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    60
                }
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    61
            }
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    62
        };
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    63
    }
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    64
    /**
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    65
     * Invoke register, interestOps, and cancel concurrently with a thread
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    66
     * doing a selection operation
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    67
     */
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    68
    public static void main(String args[]) throws Exception {
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    69
        Future<Void> result;
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    70
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    71
        ExecutorService pool = Executors.newFixedThreadPool(1);
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    72
        try (Selector sel = Selector.open()) {
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    73
            Phaser barrier = new Phaser(2);
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    74
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    75
            // submit task to do the select loop
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    76
            result = pool.submit(selectLoop(sel, barrier));
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    77
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    78
            Pipe p = Pipe.open();
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    79
            try {
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    80
                Pipe.SourceChannel sc = p.source();
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    81
                sc.configureBlocking(false);
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    82
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    83
                System.out.println("register ...");
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    84
                SelectionKey key = sc.register(sel, SelectionKey.OP_READ);
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    85
                if (!sel.keys().contains(key))
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    86
                    throw new RuntimeException("key not in key set");
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    87
                sel.wakeup();
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    88
                barrier.arriveAndAwaitAdvance();
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    89
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    90
                System.out.println("interestOps ...");
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    91
                key.interestOps(0);
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    92
                sel.wakeup();
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    93
                barrier.arriveAndAwaitAdvance();
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    94
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    95
                System.out.println("cancel ...");
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    96
                key.cancel();
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    97
                sel.wakeup();
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    98
                barrier.arriveAndAwaitAdvance();
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    99
                if (sel.keys().contains(key))
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
   100
                    throw new RuntimeException("key not removed from key set");
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
   101
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
   102
            } finally {
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
   103
                p.source().close();
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
   104
                p.sink().close();
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
   105
            }
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
   106
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
   107
        } finally {
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
   108
            pool.shutdown();
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
   109
        }
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
   110
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
   111
        // ensure selectLoop completes without exception
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
   112
        result.get();
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
   113
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
   114
    }
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
   115
}
51077
9baa91bc7567 8202481: RegisterDuringSelect.java fails with java.util.concurrent.ExecutionException: java.nio.channels.ClosedSelectorException
xiaofeya
parents: 49802
diff changeset
   116