test/jdk/java/nio/channels/Selector/RegisterDuringSelect.java
author mli
Fri, 31 Aug 2018 10:00:22 +0800
changeset 51602 dbb0e798deeb
parent 51077 9baa91bc7567
permissions -rw-r--r--
8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set" 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
51602
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents: 51077
diff changeset
    26
 * @build SelectorUtils
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents: 51077
diff changeset
    27
 * @run main RegisterDuringSelect
49802
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    28
 * @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
    29
 *          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
    30
 */
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    31
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    32
import java.io.IOException;
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    33
import java.nio.channels.Pipe;
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    34
import java.nio.channels.SelectionKey;
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    35
import java.nio.channels.Selector;
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    36
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    37
public class RegisterDuringSelect {
51602
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents: 51077
diff changeset
    38
    interface TestOperation {
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents: 51077
diff changeset
    39
        void accept(Thread t, Selector sel, Pipe.SourceChannel sc) throws Exception;
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents: 51077
diff changeset
    40
    }
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents: 51077
diff changeset
    41
    static class Test {
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents: 51077
diff changeset
    42
        final Selector sel;
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents: 51077
diff changeset
    43
        final Pipe p;
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents: 51077
diff changeset
    44
        final Pipe.SourceChannel sc;
49802
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    45
51602
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents: 51077
diff changeset
    46
        Test() throws Exception {
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents: 51077
diff changeset
    47
            sel = Selector.open();
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents: 51077
diff changeset
    48
            p = Pipe.open();
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents: 51077
diff changeset
    49
            sc = p.source();
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents: 51077
diff changeset
    50
            sc.configureBlocking(false);
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents: 51077
diff changeset
    51
        }
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents: 51077
diff changeset
    52
        void test(TestOperation op) throws Exception {
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents: 51077
diff changeset
    53
            try {
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents: 51077
diff changeset
    54
                Thread t = new Thread(() -> {
51077
9baa91bc7567 8202481: RegisterDuringSelect.java fails with java.util.concurrent.ExecutionException: java.nio.channels.ClosedSelectorException
xiaofeya
parents: 49802
diff changeset
    55
                    try {
9baa91bc7567 8202481: RegisterDuringSelect.java fails with java.util.concurrent.ExecutionException: java.nio.channels.ClosedSelectorException
xiaofeya
parents: 49802
diff changeset
    56
                        sel.select();
51602
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents: 51077
diff changeset
    57
                    } catch (IOException ex) {
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents: 51077
diff changeset
    58
                        throw new RuntimeException(ex);
51077
9baa91bc7567 8202481: RegisterDuringSelect.java fails with java.util.concurrent.ExecutionException: java.nio.channels.ClosedSelectorException
xiaofeya
parents: 49802
diff changeset
    59
                    }
51602
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents: 51077
diff changeset
    60
                });
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents: 51077
diff changeset
    61
                t.start();
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents: 51077
diff changeset
    62
                op.accept(t, sel, sc);
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents: 51077
diff changeset
    63
            } finally {
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents: 51077
diff changeset
    64
                sel.close();
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents: 51077
diff changeset
    65
                p.source().close();
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents: 51077
diff changeset
    66
                p.sink().close();
49802
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    67
            }
51602
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents: 51077
diff changeset
    68
        }
49802
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    69
    }
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
     * 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
    72
     * doing a selection operation
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    73
     */
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    74
    public static void main(String args[]) throws Exception {
51602
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents: 51077
diff changeset
    75
        new Test().test((t, sel, sc) -> {
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents: 51077
diff changeset
    76
            System.out.println("register ...");
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents: 51077
diff changeset
    77
            // spin until make sure select is invoked
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents: 51077
diff changeset
    78
            SelectorUtils.spinUntilLocked(t, sel);
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents: 51077
diff changeset
    79
            SelectionKey key = sc.register(sel, SelectionKey.OP_READ);
49802
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    80
            try {
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    81
                if (!sel.keys().contains(key))
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    82
                    throw new RuntimeException("key not in key set");
51602
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents: 51077
diff changeset
    83
            } finally {
49802
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    84
                sel.wakeup();
51602
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents: 51077
diff changeset
    85
                t.join();
49802
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents:
diff changeset
    86
            }
51602
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents: 51077
diff changeset
    87
        });
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents: 51077
diff changeset
    88
        new Test().test((t, sel, sc) -> {
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents: 51077
diff changeset
    89
            System.out.println("interestOps ...");
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents: 51077
diff changeset
    90
            SelectionKey key = sc.register(sel, SelectionKey.OP_READ);
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents: 51077
diff changeset
    91
            // spin until make sure select is invoked
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents: 51077
diff changeset
    92
            SelectorUtils.spinUntilLocked(t, sel);
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents: 51077
diff changeset
    93
            key.interestOps(0);
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents: 51077
diff changeset
    94
            try {
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents: 51077
diff changeset
    95
                if (key.interestOps() != 0)
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents: 51077
diff changeset
    96
                    throw new RuntimeException("interested ops not cleared");
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents: 51077
diff changeset
    97
            } finally {
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents: 51077
diff changeset
    98
                sel.wakeup();
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents: 51077
diff changeset
    99
                t.join();
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents: 51077
diff changeset
   100
            }
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents: 51077
diff changeset
   101
        });
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents: 51077
diff changeset
   102
        new Test().test((t, sel, sc) -> {
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents: 51077
diff changeset
   103
            System.out.println("cancel ...");
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents: 51077
diff changeset
   104
            SelectionKey key = sc.register(sel, SelectionKey.OP_READ);
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents: 51077
diff changeset
   105
            // spin until make sure select is invoked
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents: 51077
diff changeset
   106
            SelectorUtils.spinUntilLocked(t, sel);
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents: 51077
diff changeset
   107
            key.cancel();
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents: 51077
diff changeset
   108
            sel.wakeup();
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents: 51077
diff changeset
   109
            t.join();
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents: 51077
diff changeset
   110
            if (sel.keys().contains(key))
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents: 51077
diff changeset
   111
                throw new RuntimeException("key not removed from key set");
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents: 51077
diff changeset
   112
        });
49802
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
}