Path: blob/master/test/jdk/java/nio/channels/AsynchronousServerSocketChannel/Basic.java
41154 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* @modules jdk.net27* @run main/timeout=180 Basic28*/2930import java.nio.channels.*;31import java.net.*;32import static java.net.StandardSocketOptions.*;33import java.io.IOException;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();148boolean reuseport = options.contains(SO_REUSEPORT);149if (!options.contains(SO_REUSEADDR))150throw new RuntimeException("SO_REUSEADDR should be supported");151if (!options.contains(SO_REUSEPORT) && reuseport)152throw new RuntimeException("SO_REUSEPORT should be supported");153if (!options.contains(SO_RCVBUF))154throw new RuntimeException("SO_RCVBUF should be supported");155156// allowed to change when not bound157ch.setOption(SO_RCVBUF, 256*1024); // can't check158int before = ch.getOption(SO_RCVBUF);159int after = ch.setOption(SO_RCVBUF, Integer.MAX_VALUE).getOption(SO_RCVBUF);160if (after < before)161throw new RuntimeException("setOption caused SO_RCVBUF to decrease");162ch.setOption(SO_REUSEADDR, true);163checkOption(ch, SO_REUSEADDR, true);164ch.setOption(SO_REUSEADDR, false);165checkOption(ch, SO_REUSEADDR, false);166167if (reuseport) {168ch.setOption(SO_REUSEPORT, true);169checkOption(ch, SO_REUSEPORT, true);170ch.setOption(SO_REUSEPORT, false);171checkOption(ch, SO_REUSEPORT, false);172}173List<? extends SocketOption> extOptions = List.of(TCP_KEEPCOUNT,174TCP_KEEPIDLE, TCP_KEEPINTERVAL);175if (options.containsAll(extOptions)) {176ch.setOption(TCP_KEEPIDLE, 1234);177checkOption(ch, TCP_KEEPIDLE, 1234);178ch.setOption(TCP_KEEPINTERVAL, 123);179checkOption(ch, TCP_KEEPINTERVAL, 123);180ch.setOption(TCP_KEEPCOUNT, 7);181checkOption(ch, TCP_KEEPCOUNT, 7);182}183} finally {184ch.close();185}186}187188static void checkOption(AsynchronousServerSocketChannel ch,189SocketOption name, Object expectedValue)190throws IOException191{192Object value = ch.getOption(name);193if (!value.equals(expectedValue))194throw new RuntimeException("value not as expected");195}196}197198199