1 /*
   2  * Copyright (c) 2003, 2017, 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 public class CloseTest {
  42 
  43     public static void main(String args[]) throws Exception {
  44         String msg = "HELLO";
  45 
  46         // Launch the service with arguments to tell it to close
  47         // the connection after reading 5 bytes ("HELLO"). After
  48         // closing the connection the service should hang around
  49         // for 15 seconds.
  50 
  51         String service_args[] = new String[2];
  52         service_args[0] = String.valueOf(msg.length());
  53         service_args[1] = String.valueOf( 15*1000 );
  54 
  55 
  56         SocketChannel sc = Launcher.launchWithSocketChannel("EchoService", service_args);
  57 
  58         // send message - service will echo the message and close the connection.
  59 
  60         sc.write(ByteBuffer.wrap(msg.getBytes("UTF-8")));
  61 
  62         // read the reply (with timeout)
  63         ByteBuffer bb = ByteBuffer.allocateDirect(50);
  64         sc.configureBlocking(false);
  65         Selector sel = sc.provider().openSelector();
  66         SelectionKey sk = sc.register(sel, SelectionKey.OP_READ);
  67 
  68         long to = 12 * 1000;
  69         for (;;) {
  70             long st = System.currentTimeMillis();
  71             sel.select(to);
  72             if (sk.isReadable()) {
  73                 int n = sc.read(bb);
  74 
  75                 // EOF
  76                 if (n < 0) {
  77                     break;
  78                 }
  79             }
  80             sel.selectedKeys().remove(sk);
  81             to -= System.currentTimeMillis() - st;
  82             if (to <= 0) {
  83                 throw new RuntimeException("Timed out waiting for connection to close");
  84             }
  85         }
  86         sel.close();
  87         sc.close();
  88 
  89         // finally check that the reply length is okay
  90         bb.flip();
  91         if (bb.remaining() < msg.length()) {
  92             throw new RuntimeException("Premature EOF from echo service");
  93         }
  94 
  95         System.out.println("Test passed - service closed connection.");
  96     }
  97 }