Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/nio/channels/etc/AdaptorCloseAndInterrupt.java
38828 views
/*1* Copyright (c) 2012, 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 718493225* @summary Test asynchronous close and interrupt of timed socket adapter methods26* @key randomness27*/2829import java.io.*;30import java.nio.*;31import java.nio.channels.*;32import java.nio.channels.spi.AbstractSelectableChannel;33import java.net.*;34import java.util.concurrent.Callable;35import java.util.concurrent.Executors;36import java.util.concurrent.ScheduledExecutorService;37import java.util.concurrent.TimeUnit;38import java.util.Random;394041public class AdaptorCloseAndInterrupt {42private static final ScheduledExecutorService pool =43Executors.newScheduledThreadPool(1);44final ServerSocketChannel listener;45final DatagramChannel peer;46final int port;4748public AdaptorCloseAndInterrupt() {49listener = null;50peer = null;51port = -1;52}5354public AdaptorCloseAndInterrupt(ServerSocketChannel listener) {55this.listener = listener;56this.port = listener.socket().getLocalPort();57this.peer = null;58}5960public AdaptorCloseAndInterrupt(DatagramChannel listener) {61this.peer = listener;62this.port = peer.socket().getLocalPort();63this.listener = null;64}6566public static void main(String args[]) throws Exception {67try {68try (ServerSocketChannel listener = ServerSocketChannel.open()) {69listener.socket().bind(null);70new AdaptorCloseAndInterrupt(listener).scReadAsyncClose();71new AdaptorCloseAndInterrupt(listener).scReadAsyncInterrupt();72}7374try (DatagramChannel peer = DatagramChannel.open()) {75peer.socket().bind(null);76new AdaptorCloseAndInterrupt(peer).dcReceiveAsyncClose();77new AdaptorCloseAndInterrupt(peer).dcReceiveAsyncInterrupt();78}7980new AdaptorCloseAndInterrupt().ssAcceptAsyncClose();81new AdaptorCloseAndInterrupt().ssAcceptAsyncInterrupt();82} finally {83pool.shutdown();84}85System.out.println("Test Passed");86}8788void scReadAsyncClose() throws IOException {89try {90SocketChannel sc = SocketChannel.open(new InetSocketAddress(91"127.0.0.1", port));92sc.socket().setSoTimeout(30*1000);9394doAsyncClose(sc);9596try {97sc.socket().getInputStream().read(new byte[100]);98throw new RuntimeException("read should not have completed");99} catch (ClosedChannelException expected) {}100101if (!sc.socket().isClosed())102throw new RuntimeException("socket is not closed");103} finally {104// accept connection and close it.105listener.accept().close();106}107}108109void scReadAsyncInterrupt() throws IOException {110try {111final SocketChannel sc = SocketChannel.open(new InetSocketAddress(112"127.0.0.1", port));113sc.socket().setSoTimeout(30*1000);114115doAsyncInterrupt();116117try {118sc.socket().getInputStream().read(new byte[100]);119throw new RuntimeException("read should not have completed");120} catch (ClosedByInterruptException expected) {121Thread.currentThread().interrupted();122}123124if (!sc.socket().isClosed())125throw new RuntimeException("socket is not closed");126} finally {127// accept connection and close it.128listener.accept().close();129}130}131132void dcReceiveAsyncClose() throws IOException {133DatagramChannel dc = DatagramChannel.open();134dc.connect(new InetSocketAddress(135"127.0.0.1", port));136dc.socket().setSoTimeout(30*1000);137138doAsyncClose(dc);139140try {141dc.socket().receive(new DatagramPacket(new byte[100], 100));142throw new RuntimeException("receive should not have completed");143} catch (ClosedChannelException expected) {}144145if (!dc.socket().isClosed())146throw new RuntimeException("socket is not closed");147}148149void dcReceiveAsyncInterrupt() throws IOException {150DatagramChannel dc = DatagramChannel.open();151dc.connect(new InetSocketAddress(152"127.0.0.1", port));153dc.socket().setSoTimeout(30*1000);154155doAsyncInterrupt();156157try {158dc.socket().receive(new DatagramPacket(new byte[100], 100));159throw new RuntimeException("receive should not have completed");160} catch (ClosedByInterruptException expected) {161Thread.currentThread().interrupted();162}163164if (!dc.socket().isClosed())165throw new RuntimeException("socket is not closed");166}167168void ssAcceptAsyncClose() throws IOException {169ServerSocketChannel ssc = ServerSocketChannel.open();170ssc.socket().bind(null);171ssc.socket().setSoTimeout(30*1000);172173doAsyncClose(ssc);174175try {176ssc.socket().accept();177throw new RuntimeException("accept should not have completed");178} catch (ClosedChannelException expected) {}179180if (!ssc.socket().isClosed())181throw new RuntimeException("socket is not closed");182}183184void ssAcceptAsyncInterrupt() throws IOException {185ServerSocketChannel ssc = ServerSocketChannel.open();186ssc.socket().bind(null);187ssc.socket().setSoTimeout(30*1000);188189doAsyncInterrupt();190191try {192ssc.socket().accept();193throw new RuntimeException("accept should not have completed");194} catch (ClosedByInterruptException expected) {195Thread.currentThread().interrupted();196}197198if (!ssc.socket().isClosed())199throw new RuntimeException("socket is not closed");200}201202void doAsyncClose(final AbstractSelectableChannel sc) {203AdaptorCloseAndInterrupt.pool.schedule(new Callable<Void>() {204public Void call() throws Exception {205sc.close();206return null;207}208}, new Random().nextInt(1000), TimeUnit.MILLISECONDS);209}210211void doAsyncInterrupt() {212final Thread current = Thread.currentThread();213AdaptorCloseAndInterrupt.pool.schedule(new Callable<Void>() {214public Void call() throws Exception {215current.interrupt();216return null;217}218}, new Random().nextInt(1000), TimeUnit.MILLISECONDS);219}220221}222223224