Path: blob/master/test/jdk/java/nio/channels/Selector/SelectorLimit.java
41153 views
/*1* Copyright (c) 2002, 2013, 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 4777504 802488325* @summary Ensure that a Selector can return at least 100 selected keys26* @author Mark Reinhold27* @library ..28* @build SelectorLimit29* @run main/othervm SelectorLimit30* @run main/othervm -Dsun.nio.ch.maxUpdateArraySize=128 SelectorLimit31*/3233import java.io.*;34import java.net.*;35import java.nio.*;36import java.nio.channels.*;37import java.util.*;383940public class SelectorLimit {4142static PrintStream log = System.err;4344static class Listener45extends TestThread46{47volatile int count = 0;48private ServerSocketChannel ssc;4950Listener(ServerSocketChannel ssc) {51super("Listener");52this.ssc = ssc;53}5455void go() throws IOException {56for (;;) {57ssc.accept();58count++;59}60}6162}6364static final int N_KEYS = 500;65static final int MIN_KEYS = 100;6667public static void main(String[] args) throws Exception {68ServerSocketChannel ssc = ServerSocketChannel.open();69TestUtil.bind(ssc);70Listener lth = new Listener(ssc);71lth.start();7273Selector sel = Selector.open();74SocketChannel[] sca = new SocketChannel[N_KEYS];75for (int i = 0; i < N_KEYS; i++) {76SocketChannel sc = SocketChannel.open();77sc.configureBlocking(false);78sc.register(sel, SelectionKey.OP_CONNECT | SelectionKey.OP_WRITE);79sc.connect(ssc.socket().getLocalSocketAddress());80}8182for (int i = 0; i < 10; i++) {83if (lth.count >= MIN_KEYS)84break;85Thread.sleep(1000);86}87log.println(lth.count + " connections accepted");88Thread.sleep(500);8990int n = sel.select();91log.println(n + " keys selected");92if (n < MIN_KEYS)93throw new Exception("Only selected " + n + " keys");9495}9697}9899100