Path: blob/master/test/jdk/java/nio/channels/ServerSocketChannel/NonBlockingAccept.java
41154 views
/*1* Copyright (c) 2003, 2016, 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 4801882 5046333 814159525* @summary test ServerSocketAdaptor.accept on nonblocking channel26* @library ..27* @build TestUtil28* @run main NonBlockingAccept29* @key randomness30*/3132import java.io.*;33import java.net.*;34import java.nio.*;35import java.nio.channels.*;363738public class NonBlockingAccept {3940public static void main(String[] args) throws Exception {4142ServerSocketChannel ssc = ServerSocketChannel.open();43InetSocketAddress isa = TestUtil.bindToRandomPort(ssc);44ssc.configureBlocking(false);45ServerSocket ss = ssc.socket();4647// Exception should be thrown when no connection is waiting48try {49ss.accept();50throw new RuntimeException("Expected exception not thrown");51} catch (IllegalBlockingModeException ibme) {52// Correct result53}5455// No exception should be thrown when a connection is waiting (5046333)56SocketChannel sc = SocketChannel.open();57sc.configureBlocking(false);58sc.connect(isa);5960// loop until accepted61while (true) {62try {63ss.accept();64break;65} catch (IllegalBlockingModeException ex) {66System.out.println(ex + ", sleeping ...");67Thread.sleep(100);68}69}7071}7273}747576