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