Path: blob/master/test/jdk/java/nio/channels/Selector/Alias.java
41153 views
/*1* Copyright (c) 2001, 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 451301125* @summary Registering and cancelling same fd many times26* @library .. /test/lib27* @build jdk.test.lib.Utils TestServers28* @run main Alias29*/3031import java.net.*;32import java.nio.*;33import java.nio.channels.*;34import java.nio.channels.spi.SelectorProvider;35import java.util.*;3637public class Alias {3839static int success = 0;40static int LIMIT = 20; // Hangs after just 1 if problem is present4142public static void main(String[] args) throws Exception {43try (TestServers.DayTimeServer daytimeServer44= TestServers.DayTimeServer.startNewServer(100)) {45test1(daytimeServer);46}47}4849static void test1(TestServers.DayTimeServer daytimeServer) throws Exception {50Selector selector = SelectorProvider.provider().openSelector();51InetAddress myAddress = daytimeServer.getAddress();52InetSocketAddress isa53= new InetSocketAddress(myAddress,54daytimeServer.getPort());5556for (int j=0; j<LIMIT; j++) {57SocketChannel sc = SocketChannel.open();58sc.configureBlocking(false);59boolean result = sc.connect(isa);6061// On some platforms - given that we're using a local server,62// we may not enter into the if () { } statement below...63if (!result) {64SelectionKey key = sc.register(selector,65SelectionKey.OP_CONNECT);66while (!result) {67int keysAdded = selector.select(100);68if (keysAdded > 0) {69Set readyKeys = selector.selectedKeys();70Iterator i = readyKeys.iterator();71while (i.hasNext()) {72SelectionKey sk = (SelectionKey)i.next();73SocketChannel nextReady =74(SocketChannel)sk.channel();75result = nextReady.finishConnect();76}77}78}79key.cancel();80}81read(sc);82}83selector.close();84}8586static void read(SocketChannel sc) throws Exception {87ByteBuffer bb = ByteBuffer.allocateDirect(100);88int n = 0;89while (n == 0) // Note this is not a rigorous check for done reading90n = sc.read(bb);91//bb.position(bb.position() - 2);92//bb.flip();93//CharBuffer cb = Charset.forName("US-ASCII").newDecoder().decode(bb);94//System.out.println("Received: \"" + cb + "\"");95sc.close();96success++;97}98}99100101