1 /* 2 * Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * 26 * 27 * This unit test checks that closing the channel returned by 28 * System.inheritedChannel closes the underlying network socket. 29 * 30 * The test launches the "echo service" with arguments to instruct the 31 * service to close the channel after it receives a small message. The 32 * service then delays/lingers for 15 seconds before shuting down. To 33 * prove that the close works we check that we see EOF (meaning the 34 * peer has closed the connection) in less than 15 seconds. 35 */ 36 import java.nio.ByteBuffer; 37 import java.nio.channels.SelectionKey; 38 import java.nio.channels.Selector; 39 import java.nio.channels.SocketChannel; 40 41 import jdk.test.lib.Utils; 42 43 public class CloseTest { 44 45 public static void main(String args[]) throws Exception { 46 String msg = "HELLO"; 47 48 // Launch the service with arguments to tell it to close 49 // the connection after reading 5 bytes ("HELLO"). After 50 // closing the connection the service should hang around 51 // for 15 seconds. 52 53 String service_args[] = new String[2]; 54 service_args[0] = String.valueOf(msg.length()); 55 service_args[1] = String.valueOf( Utils.adjustTimeout(15*1000) ); 56 57 58 SocketChannel sc = Launcher.launchWithSocketChannel("EchoService", service_args); 59 60 // send message - service will echo the message and close the connection. 61 62 sc.write(ByteBuffer.wrap(msg.getBytes("UTF-8"))); 63 64 // read the reply (with timeout) 65 ByteBuffer bb = ByteBuffer.allocateDirect(50); 66 sc.configureBlocking(false); 67 Selector sel = sc.provider().openSelector(); 68 SelectionKey sk = sc.register(sel, SelectionKey.OP_READ); 69 70 long to = Utils.adjustTimeout(12*1000); 71 for (;;) { 72 long st = System.currentTimeMillis(); 73 sel.select(to); 74 if (sk.isReadable()) { 75 int n = sc.read(bb); 76 77 // EOF 78 if (n < 0) { 79 break; 80 } 81 } 82 sel.selectedKeys().remove(sk); 83 to -= System.currentTimeMillis() - st; 84 if (to <= 0) { 85 throw new RuntimeException("Timed out waiting for connection to close"); 86 } 87 } 88 sel.close(); 89 sc.close(); 90 91 // finally check that the reply length is okay 92 bb.flip(); 93 if (bb.remaining() < msg.length()) { 94 throw new RuntimeException("Premature EOF from echo service"); 95 } 96 97 System.out.println("Test passed - service closed connection."); 98 } 99 }