Path: blob/master/test/jdk/com/sun/nio/sctp/SctpMultiChannel/CloseDescriptors.java
66649 views
/*1* Copyright (c) 2021, 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 826948125* @summary Tests that file descriptors are closed26* @requires (os.family == "linux")27* @run main/othervm CloseDescriptors28*/2930import java.net.InetSocketAddress;31import java.net.ServerSocket;32import java.nio.ByteBuffer;33import java.nio.file.Files;34import java.nio.file.Path;35import java.util.List;36import java.util.Optional;37import com.sun.nio.sctp.MessageInfo;38import com.sun.nio.sctp.SctpMultiChannel;3940public class CloseDescriptors {4142private static final int NUM = 5;43private static final int SIZE = 1024;44private static final int MAX_DESC = 3;4546public static void main(String[] args) throws Exception {47if (!Util.isSCTPSupported()) {48System.out.println("SCTP protocol is not supported");49System.out.println("Test cannot be run");50return;51}5253List<String> lsofDirs = List.of("/usr/bin", "/usr/sbin");54Optional<Path> lsof = lsofDirs.stream()55.map(s -> Path.of(s, "lsof"))56.filter(f -> Files.isExecutable(f))57.findFirst();58if (!lsof.isPresent()) {59System.out.println("Cannot locate lsof in " + lsofDirs);60System.out.println("Test cannot be run");61return;62}6364try (ServerSocket ss = new ServerSocket(0)) {65int port = ss.getLocalPort();6667Thread server = new Server(port);68server.start();69Thread.sleep(100); // wait for server to be ready7071System.out.println("begin");72for (int i = 0; i < 5; ++i) {73System.out.println(i);74doIt(port);75Thread.sleep(100);76}77System.out.println("end");78server.join();79}8081long pid = ProcessHandle.current().pid();82ProcessBuilder pb = new ProcessBuilder(83lsof.get().toString(), "-U", "-a", "-p", Long.toString(pid));84Process p = pb.start();85Object[] lines = p.inputReader().lines().toArray();86p.waitFor();8788int nfds = lines.length - 1;89if (nfds > MAX_DESC) {90throw new RuntimeException("Number of open descriptors " +91nfds + " > " + MAX_DESC);92}93}9495static void doIt(int port) throws Exception {96InetSocketAddress sa = new InetSocketAddress("localhost", port);9798for (int i = 0; i < NUM; ++i) {99System.out.println(" " + i);100SctpMultiChannel channel = SctpMultiChannel.open();101channel.configureBlocking(true);102MessageInfo info = MessageInfo.createOutgoing(sa, 0);103ByteBuffer buffer = ByteBuffer.allocateDirect(SIZE);104channel.send(buffer, info);105channel.close();106107Thread.sleep(200);108}109}110111static class Server extends Thread {112int port;113114Server(int port) {115this.port = port;116}117118@Override119public void run() {120for (int i = 0; i < NUM; i++) {121try {122SctpMultiChannel sm = SctpMultiChannel.open();123InetSocketAddress sa =124new InetSocketAddress("localhost", port);125sm.bind(sa);126ByteBuffer buffer = ByteBuffer.allocateDirect(SIZE);127MessageInfo info = sm.receive(buffer, null, null);128sm.close();129} catch (Exception e) {130e.printStackTrace();131}132}133}134}135}136137138