Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/nio/channels/Selector/ChangingInterests.java
38828 views
/*1* Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 720074225* @summary Test that Selector doesn't spin when changing interest ops26*/2728import java.net.*;29import java.nio.ByteBuffer;30import java.nio.channels.*;31import static java.nio.channels.SelectionKey.*;32import java.io.IOException;3334public class ChangingInterests {3536static int OPS[] = { 0, OP_WRITE, OP_READ, (OP_WRITE|OP_READ) };3738static String toOpsString(int ops) {39String s = "";40if ((ops & OP_READ) > 0)41s += "POLLIN";42if ((ops & OP_WRITE) > 0) {43if (s.length() > 0)44s += "|";45s += "POLLOUT";46}47if (s.length() == 0)48s = "0";49return "(" + s + ")";50}5152static void write1(SocketChannel peer) throws IOException {53peer.write(ByteBuffer.wrap(new byte[1]));54// give time for other end to be readable55try {56Thread.sleep(50);57} catch (InterruptedException ignore) { }58}5960static void drain(SocketChannel sc) throws IOException {61ByteBuffer buf = ByteBuffer.allocate(100);62int n;63while ((n = sc.read(buf)) > 0) {64buf.rewind();65}66}6768/**69* Changes the given key's interest set from one set to another and then70* checks the selected key set and the key's channel.71*/72static void testChange(SelectionKey key, int from, int to) throws IOException {73Selector sel = key.selector();74assertTrue(sel.keys().size() == 1, "Only one channel should be registered");7576// ensure that channel is registered with the "from" interest set77key.interestOps(from);78sel.selectNow();79sel.selectedKeys().clear();8081// change to the "to" interest set82key.interestOps(to);83System.out.println("select...");84int selected = sel.selectNow();85System.out.println("" + selected + " channel(s) selected");8687int expected = (to == 0) ? 0 : 1;88assertTrue(selected == expected, "Expected " + expected);8990// check selected keys91for (SelectionKey k: sel.selectedKeys()) {92assertTrue(k == key, "Unexpected key selected");9394boolean readable = k.isReadable();95boolean writable = k.isWritable();9697System.out.println("key readable: " + readable);98System.out.println("key writable: " + writable);99100if ((to & OP_READ) == 0) {101assertTrue(!readable, "Not expected to be readable");102} else {103assertTrue(readable, "Expected to be readable");104}105106if ((to & OP_WRITE) == 0) {107assertTrue(!writable, "Not expected to be writable");108} else {109assertTrue(writable, "Expected to be writable");110}111112sel.selectedKeys().clear();113}114}115116/**117* Tests that given Selector's select method blocks.118*/119static void testForSpin(Selector sel) throws IOException {120System.out.println("Test for spin...");121long start = System.currentTimeMillis();122int count = 3;123while (count-- > 0) {124int selected = sel.select(1000);125System.out.println("" + selected + " channel(s) selected");126assertTrue(selected == 0, "Channel should not be selected");127}128long dur = System.currentTimeMillis() - start;129assertTrue(dur > 1000, "select was too short");130}131132public static void main(String[] args) throws IOException {133InetAddress lh = InetAddress.getLocalHost();134135// create loopback connection136ServerSocketChannel ssc =137ServerSocketChannel.open().bind(new InetSocketAddress(0));138139final SocketChannel sc = SocketChannel.open();140sc.setOption(StandardSocketOptions.TCP_NODELAY, true);141sc.connect(new InetSocketAddress(lh, ssc.socket().getLocalPort()));142SocketChannel peer = ssc.accept();143peer.setOption(StandardSocketOptions.TCP_NODELAY, true);144145sc.configureBlocking(false);146147// ensure that channel "sc" is readable148write1(peer);149150try (Selector sel = Selector.open()) {151SelectionKey key = sc.register(sel, 0);152sel.selectNow();153154// test all transitions155for (int from: OPS) {156for (int to: OPS) {157158System.out.println(toOpsString(from) + " -> " + toOpsString(to));159160testChange(key, from, to);161162// if the interst ops is now 0 then Selector should not spin163if (to == 0)164testForSpin(sel);165166// if interest ops is now OP_READ then make non-readable167// and test that Selector does not spin.168if (to == OP_READ) {169System.out.println("Drain channel...");170drain(sc);171testForSpin(sel);172System.out.println("Make channel readable again");173write1(peer);174}175176System.out.println();177}178}179180} finally {181sc.close();182peer.close();183ssc.close();184}185}186187static void assertTrue(boolean v, String msg) {188if (!v) throw new RuntimeException(msg);189}190191}192193194