jdk/test/java/nio/channels/Selector/KeySets.java
author clanger
Fri, 04 Aug 2017 15:28:32 +0200
changeset 46094 0c23b05caf7d
parent 5506 202f599c92aa
permissions -rw-r--r--
8184330: Remove sun.nio.ch.Util.atBugLevel() either completely or at least get rid of volatile field bugLevel Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
46094
0c23b05caf7d 8184330: Remove sun.nio.ch.Util.atBugLevel() either completely or at least get rid of volatile field bugLevel
clanger
parents: 5506
diff changeset
     2
 * Copyright (c) 2003, 2017, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/* @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @bug 4776783 4778091 4778099
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @summary Check various properties of key and selected-key sets
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @run main KeySets
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.nio.channels.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
public class KeySets {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    static abstract class Catch {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
        abstract void go() throws Exception;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
        Catch(Class xc) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
                go();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
            } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
                if (xc.isInstance(x))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
                throw new Exception("Wrong exception", x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
            }
46094
0c23b05caf7d 8184330: Remove sun.nio.ch.Util.atBugLevel() either completely or at least get rid of volatile field bugLevel
clanger
parents: 5506
diff changeset
    47
            throw new Exception("Not thrown as expected: " + xc.getName());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    // 4776783: Closing a selector should make key sets inaccessible
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    static void testClose() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        final Selector sel = Selector.open();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        sel.keys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        sel.selectedKeys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        sel.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        new Catch(ClosedSelectorException.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                void go() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                    sel.keys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                }};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        new Catch(ClosedSelectorException.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                void go() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                    sel.selectedKeys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                }};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    static void testNoAddition(final Set s) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        new Catch(UnsupportedOperationException.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                void go() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                    s.add(new Object());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                }};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        new Catch(UnsupportedOperationException.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                void go() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                    ArrayList al = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                    al.add(new Object());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                    s.addAll(al);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                }};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    static interface Adder {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        void add() throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    static void testNoRemoval(final Set s, final Adder adder)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        new Catch(UnsupportedOperationException.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                void go() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                    adder.add();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                    s.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                }};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        new Catch(UnsupportedOperationException.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                void go() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                    adder.add();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                    Iterator i = s.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                    i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                    i.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                }};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        new Catch(UnsupportedOperationException.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                void go() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                    adder.add();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                    s.remove(s.iterator().next());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                }};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        new Catch(UnsupportedOperationException.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                void go() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                    adder.add();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                    HashSet hs = new HashSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                    hs.addAll(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                    s.removeAll(hs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                }};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        new Catch(UnsupportedOperationException.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                void go() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                    adder.add();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                    s.retainAll(Collections.EMPTY_SET);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                }};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    static SelectionKey reg(Selector sel) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        DatagramChannel dc = DatagramChannel.open();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        dc.configureBlocking(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        return dc.register(sel, SelectionKey.OP_WRITE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    static void testMutability() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        final Selector sel = Selector.open();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        // 4778091: Selector.keys() should be immutable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        testNoRemoval(sel.keys(), new Adder() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                public void add() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                    reg(sel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        testNoAddition(sel.keys());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        // 4778099: Selector.selectedKeys() should allow removal but not addition
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        sel.select();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        testNoAddition(sel.selectedKeys());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        SelectionKey sk = reg(sel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        sel.select();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        int n = sel.selectedKeys().size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        sel.selectedKeys().remove(sk);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        if (sel.selectedKeys().size() != n - 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            throw new Exception("remove failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        HashSet hs = new HashSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        hs.add(reg(sel));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        sel.select();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        sel.selectedKeys().retainAll(hs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        if (sel.selectedKeys().isEmpty())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            throw new Exception("retainAll failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        sel.selectedKeys().removeAll(hs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        if (!sel.selectedKeys().isEmpty())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            throw new Exception("removeAll failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        hs.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        hs.add(reg(sel));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        sel.select();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        sel.selectedKeys().clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        if (!sel.selectedKeys().isEmpty())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            throw new Exception("clear failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        testClose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        testMutability();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
}