Path: blob/master/test/jdk/java/nio/channels/SelectionKey/AtomicUpdates.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* @bug 635005525* @run testng AtomicUpdates26* @summary Unit test for SelectionKey interestOpsOr and interestOpsAnd27*/2829import java.io.Closeable;30import java.io.IOException;31import java.net.InetAddress;32import java.net.InetSocketAddress;33import java.nio.channels.CancelledKeyException;34import java.nio.channels.SelectableChannel;35import java.nio.channels.SelectionKey;36import java.nio.channels.Selector;37import java.nio.channels.ServerSocketChannel;38import java.nio.channels.SocketChannel;39import org.testng.annotations.Test;4041import static java.nio.channels.SelectionKey.OP_READ;42import static java.nio.channels.SelectionKey.OP_WRITE;43import static java.nio.channels.SelectionKey.OP_CONNECT;44import static java.nio.channels.SelectionKey.OP_ACCEPT;45import static org.testng.Assert.*;4647@Test48public class AtomicUpdates {4950private SelectionKey keyFor(SocketChannel sc) {51return new SelectionKey() {52private int ops;53private boolean invalid;54private void ensureValid() {55if (!isValid())56throw new CancelledKeyException();57}58@Override59public SelectableChannel channel() {60return sc;61}62@Override63public Selector selector() {64throw new RuntimeException();65}66@Override67public boolean isValid() {68return !invalid;69}70@Override71public void cancel() {72invalid = true;73}74@Override75public int interestOps() {76ensureValid();77return ops;78}79@Override80public SelectionKey interestOps(int ops) {81ensureValid();82if ((ops & ~channel().validOps()) != 0)83throw new IllegalArgumentException();84this.ops = ops;85return this;86}87@Override88public int readyOps() {89ensureValid();90return 0;91}92};93}9495private void test(SelectionKey key) {96assertTrue(key.channel() instanceof SocketChannel);97key.interestOps(0);9899// 0 -> 0100int previous = key.interestOpsOr(0);101assertTrue(previous == 0);102assertTrue(key.interestOps() == 0);103104// 0 -> OP_CONNECT105previous = key.interestOpsOr(OP_CONNECT);106assertTrue(previous == 0);107assertTrue(key.interestOps() == OP_CONNECT);108109// OP_CONNECT -> OP_CONNECT110previous = key.interestOpsOr(0);111assertTrue(previous == OP_CONNECT);112assertTrue(key.interestOps() == OP_CONNECT);113114// OP_CONNECT -> OP_CONNECT | OP_READ | OP_WRITE115previous = key.interestOpsOr(OP_READ | OP_WRITE);116assertTrue(previous == OP_CONNECT);117assertTrue(key.interestOps() == (OP_CONNECT | OP_READ | OP_WRITE));118119// OP_CONNECT | OP_READ | OP_WRITE -> OP_CONNECT120previous = key.interestOpsAnd(~(OP_READ | OP_WRITE));121assertTrue(previous == (OP_CONNECT | OP_READ | OP_WRITE));122assertTrue(key.interestOps() == OP_CONNECT);123124// OP_CONNECT -> 0125previous = key.interestOpsAnd(~OP_CONNECT);126assertTrue(previous == OP_CONNECT);127assertTrue(key.interestOps() == 0);128129// OP_READ | OP_WRITE -> OP_READ | OP_WRITE130key.interestOps(OP_READ | OP_WRITE);131previous = key.interestOpsAnd(~OP_ACCEPT);132assertTrue(previous == (OP_READ | OP_WRITE));133assertTrue(key.interestOps() == (OP_READ | OP_WRITE));134135// OP_READ | OP_WRITE -> 0136previous = key.interestOpsAnd(0);137assertTrue(previous == (OP_READ | OP_WRITE));138assertTrue(key.interestOps() == 0);139140// 0 -> 0141previous = key.interestOpsAnd(0);142assertTrue(previous == 0);143assertTrue(key.interestOps() == 0);144145try {146key.interestOpsOr(OP_ACCEPT);147fail("IllegalArgumentException expected");148} catch (IllegalArgumentException expected) { }149150key.cancel();151try {152key.interestOpsOr(OP_READ);153fail("CancelledKeyException expected");154} catch (CancelledKeyException expected) { }155try {156key.interestOpsAnd(~OP_READ);157fail("CancelledKeyException expected");158} catch (CancelledKeyException expected) { }159}160161/**162* Test default implementation of interestOpsOr/interestOpsAnd163*/164public void testDefaultImplementation() throws Exception {165try (SocketChannel sc = SocketChannel.open()) {166SelectionKey key = keyFor(sc);167test(key);168}169}170171/**172* Test the default provider implementation of SelectionKey.173*/174public void testNioImplementation() throws Exception {175try (SocketChannel sc = SocketChannel.open();176Selector sel = Selector.open()) {177sc.configureBlocking(false);178SelectionKey key = sc.register(sel, 0);179test(key);180}181}182}183184185186