Path: blob/master/test/jdk/java/nio/channels/SocketChannel/LingerOnClose.java
41154 views
/*1* Copyright (c) 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 820305925* @summary Test SocketChannel.close with SO_LINGER enabled26*/2728import java.io.IOException;29import java.net.InetAddress;30import java.net.InetSocketAddress;31import java.net.StandardSocketOptions;32import java.nio.ByteBuffer;33import java.nio.channels.SelectionKey;34import java.nio.channels.Selector;35import java.nio.channels.ServerSocketChannel;36import java.nio.channels.SocketChannel;3738public class LingerOnClose {3940private enum TestMode {41BLOCKING,42NON_BLOCKING,43NON_BLOCKING_AND_REGISTERED;44}4546public static void main(String[] args) throws IOException {47// blocking mode48test(TestMode.BLOCKING, -1);49test(TestMode.BLOCKING, 0);50test(TestMode.BLOCKING, 1);5152// non-blocking mode53test(TestMode.NON_BLOCKING, -1);54test(TestMode.NON_BLOCKING, 0);55test(TestMode.NON_BLOCKING, 1);5657// non-blocking mode, close while registered with Selector58test(TestMode.NON_BLOCKING_AND_REGISTERED, -1);59test(TestMode.NON_BLOCKING_AND_REGISTERED, 0);60test(TestMode.NON_BLOCKING_AND_REGISTERED, 1);61}6263/**64* Test closing a SocketChannel with SO_LINGER set to the given linger65* interval. If the linger interval is 0, it checks that the peer observes66* a connection reset (TCP RST).67*/68static void test(TestMode mode, int interval) throws IOException {69SocketChannel sc = null;70SocketChannel peer = null;71Selector sel = null;7273try (ServerSocketChannel ssc = ServerSocketChannel.open()) {74ssc.bind(new InetSocketAddress(InetAddress.getLocalHost(), 0));7576// establish loopback connection77sc = SocketChannel.open(ssc.getLocalAddress());78peer = ssc.accept();7980// configured blocking mode and register with Selector if needed81if (mode != TestMode.BLOCKING)82sc.configureBlocking(false);83if (mode == TestMode.NON_BLOCKING_AND_REGISTERED) {84sel = Selector.open();85sc.register(sel, SelectionKey.OP_READ);86sel.selectNow();87}8889// enable or disable SO_LINGER90sc.setOption(StandardSocketOptions.SO_LINGER, interval);9192// close channel and flush Selector if needed93sc.close();94if (mode == TestMode.NON_BLOCKING_AND_REGISTERED)95sel.selectNow();9697// read other end of connection, expect EOF or RST98ByteBuffer bb = ByteBuffer.allocate(100);99try {100int n = peer.read(bb);101if (interval == 0) {102throw new RuntimeException("RST expected");103} else if (n != -1) {104throw new RuntimeException("EOF expected");105}106} catch (IOException ioe) {107if (interval != 0) {108// exception not expected109throw ioe;110}111}112} finally {113if (sc != null) sc.close();114if (peer != null) peer.close();115if (sel != null) sel.close();116}117}118}119120121