< prev index next >

test/java/nio/channels/Selector/SelectAfterRead.java

Print this page


   1 /*
   2  * Copyright (c) 2002, 2010, 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 /* @test
  25  * @bug 4639943
  26  * @summary  Checks that Windows behavior matches Solaris for
  27  *           various read/select combinations.
  28  * @author kladko
  29  */
  30 

  31 import java.nio.ByteBuffer;
  32 import java.nio.channels.Selector;
  33 import java.nio.channels.SelectionKey;
  34 import java.nio.channels.SocketChannel;
  35 
  36 public class SelectAfterRead {
  37 
  38     private static final int TIMEOUT = 1000;

  39 
  40     public static void main(String[] argv) throws Exception {
  41 

  42         // server: accept connection and write one byte
  43         try (ByteServer server = new ByteServer();
  44              SocketChannel sc = SocketChannel.open(server.address())) {
  45 


  46             server.acceptConnection();




  47             server.write(1);
  48 
  49             try (Selector sel = Selector.open()) {
  50                 sc.read(ByteBuffer.allocate(1));
  51                 sc.configureBlocking(false);
  52                 sc.register(sel, SelectionKey.OP_READ);
  53                 // previously on Windows select would select channel here, although there was
  54                 // nothing to read
  55                 if (sel.selectNow() != 0)
  56                     throw new Exception("Select returned nonzero value");
  57             }
  58         }



  59 

  60         // Now we will test a two reads combination
  61         // server: accept connection and write two bytes
  62         try (ByteServer server = new ByteServer();
  63              SocketChannel sc = SocketChannel.open(server.address())) {
  64 


  65             server.acceptConnection();




  66             server.write(2);
  67 
  68             try (Selector sel = Selector.open()) {
  69                 sc.configureBlocking(false);
  70                 sc.register(sel, SelectionKey.OP_READ);
  71                 if (sel.select(TIMEOUT) != 1)
  72                     throw new Exception("One selected key expected");
  73                 sel.selectedKeys().clear();
  74                 // previously on Windows a channel would get selected only once
  75                 if (sel.selectNow() != 1)
  76                     throw new Exception("One selected key expected");
  77                 // Previously on Windows two consequent reads would cause select()
  78                 // to select a channel, although there was nothing remaining to
  79                 // read in the channel
  80                 if (sc.read(ByteBuffer.allocate(1)) != 1)
  81                     throw new Exception("One byte expected");
  82                 if (sc.read(ByteBuffer.allocate(1)) != 1)
  83                     throw new Exception("One byte expected");
  84                 if (sel.selectNow() != 0)
  85                     throw new Exception("Select returned nonzero value");
  86             }
  87         }



  88     }
  89 }
   1 /*
   2  * Copyright (c) 2002, 2016, 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 /* @test
  25  * @bug 4639943 8085792
  26  * @summary  Checks that Windows behavior matches Solaris for
  27  *           various read/select combinations.
  28  * @author kladko
  29  */
  30 
  31 import java.net.SocketTimeoutException;
  32 import java.nio.ByteBuffer;
  33 import java.nio.channels.Selector;
  34 import java.nio.channels.SelectionKey;
  35 import java.nio.channels.SocketChannel;
  36 
  37 public class SelectAfterRead {
  38 
  39     private static final int TIMEOUT = 1000;
  40     private static final int SERVER_ACCEPT_TIMEOUT = 100;
  41 
  42     public static void main(String[] argv) throws Exception {
  43 
  44         while (true) {
  45             // server: accept connection and write one byte
  46             try (ByteServer server = new ByteServer();
  47                  SocketChannel sc = SocketChannel.open(server.address())) {
  48 
  49                 server.setTimeout(SERVER_ACCEPT_TIMEOUT);
  50                 try {
  51                     server.acceptConnection();
  52                 } catch (SocketTimeoutException ex) {
  53                     System.err.println("accept connection(write one byte) timeout...");
  54                     continue;
  55                 }
  56                 server.write(1);
  57 
  58                 try (Selector sel = Selector.open()) {
  59                     sc.read(ByteBuffer.allocate(1));
  60                     sc.configureBlocking(false);
  61                     sc.register(sel, SelectionKey.OP_READ);
  62                     // previously on Windows select would select channel here, although there was
  63                     // nothing to read
  64                     if (sel.selectNow() != 0)
  65                         throw new Exception("Select returned nonzero value");
  66                 }
  67             }
  68             System.out.println("Test(write one byte) successes.");
  69             break;
  70         }
  71 
  72         while (true) {
  73             // Now we will test a two reads combination
  74             // server: accept connection and write two bytes
  75             try (ByteServer server = new ByteServer();
  76                  SocketChannel sc = SocketChannel.open(server.address())) {
  77 
  78                 server.setTimeout(SERVER_ACCEPT_TIMEOUT);
  79                 try {
  80                     server.acceptConnection();
  81                 } catch (SocketTimeoutException ex) {
  82                     System.err.println("accept connection(write two bytes) timeout...");
  83                     continue;
  84                 }
  85                 server.write(2);
  86 
  87                 try (Selector sel = Selector.open()) {
  88                     sc.configureBlocking(false);
  89                     sc.register(sel, SelectionKey.OP_READ);
  90                     if (sel.select(TIMEOUT) != 1)
  91                         throw new Exception("One selected key expected");
  92                     sel.selectedKeys().clear();
  93                     // previously on Windows a channel would get selected only once
  94                     if (sel.selectNow() != 1)
  95                         throw new Exception("One selected key expected");
  96                     // Previously on Windows two consequent reads would cause select()
  97                     // to select a channel, although there was nothing remaining to
  98                     // read in the channel
  99                     if (sc.read(ByteBuffer.allocate(1)) != 1)
 100                         throw new Exception("One byte expected");
 101                     if (sc.read(ByteBuffer.allocate(1)) != 1)
 102                         throw new Exception("One byte expected");
 103                     if (sel.selectNow() != 0)
 104                         throw new Exception("Select returned nonzero value");
 105                 }
 106             }
 107             System.out.println("Test(write two bytes) successes.");
 108             break;
 109         }
 110     }
 111 }
< prev index next >