Path: blob/master/test/jdk/java/nio/channels/Selector/UpdateReadyOps.java
41153 views
/*1* Copyright (c) 2018, 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* @run testng UpdateReadyOps25* @summary Test that the ready set from a selection operation is bitwise-disjoined26* into a key's ready set when the key is already in the selected-key set27*/2829import java.io.Closeable;30import java.io.IOException;31import java.net.InetAddress;32import java.net.InetSocketAddress;33import java.nio.ByteBuffer;34import java.nio.channels.SelectionKey;35import java.nio.channels.Selector;36import java.nio.channels.ServerSocketChannel;37import java.nio.channels.SocketChannel;3839import org.testng.annotations.Test;40import static org.testng.Assert.*;4142@Test43public class UpdateReadyOps {4445/**46* Test that OP_WRITE is preserved when updating the ready set of a key in47* the selected-key set to add OP_READ.48*/49public void testOpWritePreserved() throws Exception {50try (ConnectionPair pair = new ConnectionPair();51Selector sel = Selector.open()) {5253SocketChannel sc1 = pair.channel1();54SocketChannel sc2 = pair.channel2();5556sc1.configureBlocking(false);57SelectionKey key = sc1.register(sel, SelectionKey.OP_WRITE);5859int updated = sel.select();60assertTrue(updated == 1);61assertTrue(sel.selectedKeys().contains(key));62assertFalse(key.isReadable());63assertTrue(key.isWritable());6465// select again, should be no updates66updated = sel.select();67assertTrue(updated == 0);68assertTrue(sel.selectedKeys().contains(key));69assertFalse(key.isReadable());70assertTrue(key.isWritable());7172// write some bytes73sc2.write(helloMessage());7475// change interest ops to OP_READ, do a selection operation, and76// check that the ready set becomes OP_READ|OP_WRITE.7778key.interestOps(SelectionKey.OP_READ);79updated = sel.select();80assertTrue(updated == 1);81assertTrue(sel.selectedKeys().size() == 1);82assertTrue(key.isReadable());83assertTrue(key.isWritable());84assertTrue(key.readyOps() == (SelectionKey.OP_READ|SelectionKey.OP_WRITE));8586// select again, should be no updates87updated = sel.select();88assertTrue(updated == 0);89assertTrue(sel.selectedKeys().size() == 1);90assertTrue(key.isReadable());91assertTrue(key.isWritable());92}93}9495/**96* Test that OP_READ is preserved when updating the ready set of a key in97* the selected-key set to add OP_WRITE.98*/99public void testOpReadPreserved() throws Exception {100try (ConnectionPair pair = new ConnectionPair();101Selector sel = Selector.open()) {102103SocketChannel sc1 = pair.channel1();104SocketChannel sc2 = pair.channel2();105106sc1.configureBlocking(false);107SelectionKey key = sc1.register(sel, SelectionKey.OP_READ);108109// write some bytes110sc2.write(helloMessage());111112int updated = sel.select();113assertTrue(updated == 1);114assertTrue(sel.selectedKeys().size() == 1);115assertTrue(sel.selectedKeys().contains(key));116assertTrue(key.isReadable());117assertFalse(key.isWritable());118119// select again, should be no updates120updated = sel.select();121assertTrue(updated == 0);122assertTrue(sel.selectedKeys().contains(key));123assertTrue(key.isReadable());124assertFalse(key.isWritable());125126key.interestOps(SelectionKey.OP_WRITE);127updated = sel.select();128assertTrue(updated == 1);129assertTrue(sel.selectedKeys().size() == 1);130assertTrue(sel.selectedKeys().contains(key));131assertTrue(key.isReadable());132assertTrue(key.isWritable());133assertTrue(key.readyOps() == (SelectionKey.OP_READ|SelectionKey.OP_WRITE));134135// select again, should be no updates136updated = sel.select();137assertTrue(updated == 0);138assertTrue(sel.selectedKeys().size() == 1);139assertTrue(key.isReadable());140assertTrue(key.isWritable());141}142}143144static class ConnectionPair implements Closeable {145146private final SocketChannel sc1;147private final SocketChannel sc2;148149ConnectionPair() throws IOException {150InetAddress lb = InetAddress.getLoopbackAddress();151try (ServerSocketChannel ssc = ServerSocketChannel.open()) {152ssc.bind(new InetSocketAddress(lb, 0));153this.sc1 = SocketChannel.open(ssc.getLocalAddress());154this.sc2 = ssc.accept();155}156}157158SocketChannel channel1() {159return sc1;160}161162SocketChannel channel2() {163return sc2;164}165166public void close() throws IOException {167sc1.close();168sc2.close();169}170}171172static ByteBuffer helloMessage() throws Exception {173return ByteBuffer.wrap("hello".getBytes("UTF-8"));174}175}176177178