Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/nio/channels/AsynchronousServerSocketChannel/Basic.java
38828 views
/*1* Copyright (c) 2008, 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 4607272 684268725* @summary Unit test for AsynchronousServerSocketChannel26* @run main/timeout=180 Basic27*/2829import java.nio.channels.*;30import java.net.*;31import static java.net.StandardSocketOptions.*;32import java.io.IOException;33import java.util.Arrays;34import java.util.List;35import java.util.Set;36import java.util.concurrent.ExecutionException;37import java.util.concurrent.Future;38import java.util.concurrent.atomic.AtomicReference;39import static jdk.net.ExtendedSocketOptions.TCP_KEEPCOUNT;40import static jdk.net.ExtendedSocketOptions.TCP_KEEPIDLE;41import static jdk.net.ExtendedSocketOptions.TCP_KEEPINTERVAL;4243public class Basic {4445public static void main(String[] args) throws Exception {46testBind();47testAccept();48testSocketOptions();49}5051static void testBind() throws Exception {52System.out.println("-- bind --");5354AsynchronousServerSocketChannel ch = AsynchronousServerSocketChannel.open();55if (ch.getLocalAddress() != null)56throw new RuntimeException("Local address should be 'null'");57ch.bind(new InetSocketAddress(0), 20);5859// check local address after binding60InetSocketAddress local = (InetSocketAddress)ch.getLocalAddress();61if (local.getPort() == 0)62throw new RuntimeException("Unexpected port");63if (!local.getAddress().isAnyLocalAddress())64throw new RuntimeException("Not bound to a wildcard address");6566// try to re-bind67try {68ch.bind(new InetSocketAddress(0));69throw new RuntimeException("AlreadyBoundException expected");70} catch (AlreadyBoundException x) {71}72ch.close();7374// check ClosedChannelException75ch = AsynchronousServerSocketChannel.open();76ch.close();77try {78ch.bind(new InetSocketAddress(0));79throw new RuntimeException("ClosedChannelException expected");80} catch (ClosedChannelException x) {81}82}8384static void testAccept() throws Exception {85System.out.println("-- accept --");8687final AsynchronousServerSocketChannel listener =88AsynchronousServerSocketChannel.open().bind(new InetSocketAddress(0));8990InetAddress lh = InetAddress.getLocalHost();91int port = ((InetSocketAddress)(listener.getLocalAddress())).getPort();92final InetSocketAddress isa = new InetSocketAddress(lh, port);9394// establish a few loopback connections95for (int i=0; i<100; i++) {96SocketChannel sc = SocketChannel.open(isa);97AsynchronousSocketChannel ch = listener.accept().get();98sc.close();99ch.close();100}101102final AtomicReference<Throwable> exception = new AtomicReference<Throwable>();103104// start accepting105listener.accept((Void)null, new CompletionHandler<AsynchronousSocketChannel,Void>() {106public void completed(AsynchronousSocketChannel ch, Void att) {107try {108ch.close();109} catch (IOException ignore) { }110}111public void failed(Throwable exc, Void att) {112exception.set(exc);113}114});115116// check AcceptPendingException117try {118listener.accept();119throw new RuntimeException("AcceptPendingException expected");120} catch (AcceptPendingException x) {121}122123// asynchronous close124listener.close();125while (exception.get() == null)126Thread.sleep(100);127if (!(exception.get() instanceof AsynchronousCloseException))128throw new RuntimeException("AsynchronousCloseException expected");129130// once closed when a further attemt should throw ClosedChannelException131try {132listener.accept().get();133throw new RuntimeException("ExecutionException expected");134} catch (ExecutionException x) {135if (!(x.getCause() instanceof ClosedChannelException))136throw new RuntimeException("Cause of ClosedChannelException expected");137} catch (InterruptedException x) {138}139140}141142static void testSocketOptions() throws Exception {143System.out.println("-- socket options --");144AsynchronousServerSocketChannel ch = AsynchronousServerSocketChannel.open();145try {146// check supported options147Set<SocketOption<?>> options = ch.supportedOptions();148if (!options.contains(SO_REUSEADDR))149throw new RuntimeException("SO_REUSEADDR should be supported");150if (!options.contains(SO_RCVBUF))151throw new RuntimeException("SO_RCVBUF should be supported");152153// allowed to change when not bound154ch.setOption(SO_RCVBUF, 256*1024); // can't check155int before = ch.getOption(SO_RCVBUF);156int after = ch.setOption(SO_RCVBUF, Integer.MAX_VALUE).getOption(SO_RCVBUF);157if (after < before)158throw new RuntimeException("setOption caused SO_RCVBUF to decrease");159ch.setOption(SO_REUSEADDR, true);160checkOption(ch, SO_REUSEADDR, true);161ch.setOption(SO_REUSEADDR, false);162checkOption(ch, SO_REUSEADDR, false);163List<SocketOption<?>> extOptions = Arrays.asList(TCP_KEEPCOUNT,164TCP_KEEPIDLE, TCP_KEEPINTERVAL);165if (options.containsAll(extOptions)) {166ch.setOption(TCP_KEEPIDLE, 1234);167checkOption(ch, TCP_KEEPIDLE, 1234);168ch.setOption(TCP_KEEPINTERVAL, 123);169checkOption(ch, TCP_KEEPINTERVAL, 123);170ch.setOption(TCP_KEEPCOUNT, 7);171checkOption(ch, TCP_KEEPCOUNT, 7);172}173} finally {174ch.close();175}176}177178static void checkOption(AsynchronousServerSocketChannel ch,179SocketOption name, Object expectedValue)180throws IOException181{182Object value = ch.getOption(name);183if (!value.equals(expectedValue))184throw new RuntimeException("value not as expected");185}186}187188189